Skip to content

Commit

Permalink
Make System::Layer::mHandleSelectThread atomic.
Browse files Browse the repository at this point in the history
mHandleSelectThread is used for an optimization: when it's set to the
current thread id, WakeIOThread knows that:

1) We're in the middle of running Layer::HandleTimeout
2) We're doing that on the same thread where WakeIOThread was called

and hence WakeIOThread doesn't need to do anything, because we're
already on the IO thread and it's already awake.

The read of this member in WakeIOThread is not synchronized in any
way, but as long as we guarantee that it correctly reads a value that
was actually assigned to the member (which std::atomic does), things
will work correctly.  Either the value we read will be equal to the
current thread id, in which case we know we're on the one thread that
called Layer::HandleTimeout and are inside that function, or it will
not be equal to our thread id and then we need to do the actual wakeup
work, whatever that value is (including if it's null).

Fixes project-chip#7818
  • Loading branch information
bzbarsky-apple committed Jun 22, 2021
1 parent 82e6290 commit f8de43d
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/system/SystemLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING
#include <atomic>
#include <pthread.h>
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING

Expand Down Expand Up @@ -204,7 +205,7 @@ class DLL_EXPORT Layer
WatchableEventManager mWatchableEvents;
WakeEvent mWakeEvent;
#if CHIP_SYSTEM_CONFIG_POSIX_LOCKING
pthread_t mHandleSelectThread;
std::atomic<pthread_t> mHandleSelectThread;
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

Expand Down

0 comments on commit f8de43d

Please sign in to comment.