Skip to content

Commit

Permalink
Make ChipMainLoopWork_WorkSchedule.cpp work on darwin (#30425)
Browse files Browse the repository at this point in the history
* It turns out that darwin returns ENOSYS on sem_init.

Switch implementation to mutex + cond-variable for python, which
should hopefully work everywhere.

* restyle

* Also fix java

* Remove empty constructor/destructor

---------

Co-authored-by: Andrei Litvin <andreilitvin@google.com>
  • Loading branch information
2 people authored and pull[bot] committed Dec 5, 2023
1 parent 2d6b3da commit 2154449
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
23 changes: 17 additions & 6 deletions src/app/server/java/ChipThreadWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

#include "ChipThreadWork.h"
#include <platform/CHIPDeviceLayer.h>
#include <semaphore.h>

#include <condition_variable>
#include <mutex>

namespace chip {
namespace ThreadWork {
Expand All @@ -26,12 +28,21 @@ namespace {
struct WorkData
{
WorkCallback callback;
sem_t done;
std::mutex mux;
std::condition_variable cond;
bool done = false;

WorkData() { sem_init(&done, 0 /* shared */, 0); }
~WorkData() { sem_destroy(&done); }
void Post() { sem_post(&done); }
void Wait() { sem_wait(&done); }
void Post()
{
std::unique_lock lock(mux);
done = true;
cond.notify_all();
}
void Wait()
{
std::unique_lock lock(mux);
cond.wait(lock, [&] { return done; });
}
};

void PerformWork(intptr_t arg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
*/
#include "ChipMainLoopWork.h"

#include <condition_variable>
#include <mutex>
#include <platform/CHIPDeviceLayer.h>
#include <semaphore.h>

namespace chip {
namespace MainLoopWork {
Expand All @@ -27,12 +28,21 @@ namespace {
struct WorkData
{
std::function<void()> callback;
sem_t done;
std::mutex mux;
std::condition_variable cond;
bool done = false;

WorkData() { sem_init(&done, 0 /* shared */, 0); }
~WorkData() { sem_destroy(&done); }
void Post() { sem_post(&done); }
void Wait() { sem_wait(&done); }
void Post()
{
std::unique_lock lock(mux);
done = true;
cond.notify_all();
}
void Wait()
{
std::unique_lock lock(mux);
cond.wait(lock, [&] { return done; });
}
};

void PerformWork(intptr_t arg)
Expand Down
2 changes: 1 addition & 1 deletion third_party/mbedtls/repo
Submodule repo updated from e44be6 to d6d43e
2 changes: 1 addition & 1 deletion third_party/pigweed/repo
Submodule repo updated 333 files

0 comments on commit 2154449

Please sign in to comment.