Skip to content

Commit

Permalink
Fix data race in GenericPlatformManagerImpl_POSIX.ipp (project-chip#1588
Browse files Browse the repository at this point in the history
)

* Fix data race in GenericPlatformManagerImpl_POSIX.ipp

The mShouldRunEventLoop member variable is used for cross thread
communication without synchronization. This is a data race unless an
atomic variable is used. Make it so.

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and kedars committed Jul 19, 2020
1 parent b3a457a commit a531b3e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <sys/time.h>
#include <unistd.h>

#include <atomic>
#include <pthread.h>
#include <queue>

Expand Down Expand Up @@ -93,7 +94,7 @@ class GenericPlatformManagerImpl_POSIX : public GenericPlatformManagerImpl<ImplC

void ProcessDeviceEvents();

bool mShouldRunEventLoop;
std::atomic<bool> mShouldRunEventLoop;
static void * EventLoopTaskMain(void * arg);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ CHIP_ERROR GenericPlatformManagerImpl_POSIX<ImplClass>::_InitChipStack(void)
err = GenericPlatformManagerImpl<ImplClass>::_InitChipStack();
SuccessOrExit(err);

mShouldRunEventLoop = true;
mShouldRunEventLoop.store(true, std::memory_order_relaxed);

exit:
return err;
Expand Down Expand Up @@ -196,7 +196,7 @@ void GenericPlatformManagerImpl_POSIX<ImplClass>::_RunEventLoop(void)
{
SysUpdate();
SysProcess();
} while (mShouldRunEventLoop);
} while (mShouldRunEventLoop.load(std::memory_order_relaxed));

Impl()->UnlockChipStack();

Expand Down Expand Up @@ -232,7 +232,7 @@ template <class ImplClass>
CHIP_ERROR GenericPlatformManagerImpl_POSIX<ImplClass>::_Shutdown(void)
{
int err;
mShouldRunEventLoop = false;
mShouldRunEventLoop.store(false, std::memory_order_relaxed);;
err = pthread_join(mChipTask, NULL);
SuccessOrExit(err);

Expand Down

0 comments on commit a531b3e

Please sign in to comment.