Skip to content

Commit

Permalink
Darwin - Start/Stop events on StartEventLoop/StopEventLoop (#7651)
Browse files Browse the repository at this point in the history
* Darwin - Start/Stop events on StartEventLoop/StopEventLoop

* Add missing member

* Fix Darwin build

Co-authored-by: Boris Zbarsky <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Jul 5, 2021
1 parent fd7c59c commit 2681058
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
2 changes: 0 additions & 2 deletions examples/chip-tool/commands/common/Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ int Commands::Run(NodeId localId, NodeId remoteId, int argc, char ** argv)
// since the CHIP thread and event queue have been stopped, preventing any thread
// races.
//
// TODO: This doesn't hold true on Darwin, issue #7557 tracks the problem.
//
mController.Shutdown();

return (err == CHIP_NO_ERROR) ? EXIT_SUCCESS : EXIT_FAILURE;
Expand Down
17 changes: 10 additions & 7 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,25 @@ - (BOOL)_isRunning

- (BOOL)shutdown
{
dispatch_sync(_chipWorkQueue, ^{
if (_cppCommissioner) {
dispatch_async(_chipWorkQueue, ^{
if (self->_cppCommissioner) {
CHIP_LOG_DEBUG("%@", kInfoStackShutdown);
_cppCommissioner->Shutdown();
delete _cppCommissioner;
_cppCommissioner = nullptr;
self->_cppCommissioner->Shutdown();
delete self->_cppCommissioner;
self->_cppCommissioner = nullptr;
}
});

// StopEventLoopTask will block until blocks are executed
chip::DeviceLayer::PlatformMgrImpl().StopEventLoopTask();

return YES;
}

- (BOOL)startup:(_Nullable id<CHIPPersistentStorageDelegate>)storageDelegate
{
chip::DeviceLayer::PlatformMgrImpl().StartEventLoopTask();

__block BOOL commissionerInitialized = NO;
dispatch_sync(_chipWorkQueue, ^{
if ([self _isRunning]) {
Expand Down Expand Up @@ -176,8 +181,6 @@ - (BOOL)startup:(_Nullable id<CHIPPersistentStorageDelegate>)storageDelegate
return;
}

// Start the IO pump
self.cppCommissioner->ServiceEvents();
commissionerInitialized = YES;
});

Expand Down
36 changes: 36 additions & 0 deletions src/platform/Darwin/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
// Include the non-inline definitions for the GenericPlatformManagerImpl<> template,
#include <platform/internal/GenericPlatformManagerImpl.cpp>

#include <CoreFoundation/CoreFoundation.h>

namespace chip {
namespace DeviceLayer {

Expand All @@ -53,6 +55,40 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()
return err;
}

CHIP_ERROR PlatformManagerImpl::_StartEventLoopTask()
{
if (mIsWorkQueueRunning == false)
{
mIsWorkQueueRunning = true;
dispatch_resume(mWorkQueue);
}

return CHIP_NO_ERROR;
};

CHIP_ERROR PlatformManagerImpl::_StopEventLoopTask()
{

if (mIsWorkQueueRunning == true)
{
mIsWorkQueueRunning = false;

// dispatch_sync is used in order to guarantee serialization of the caller with
// respect to any tasks that might already be on the queue, or running.
dispatch_sync(mWorkQueue, ^{
dispatch_suspend(mWorkQueue);
});
}

return CHIP_NO_ERROR;
};

void PlatformManagerImpl::_RunEventLoop()
{
_StartEventLoopTask();
CFRunLoopRun();
};

CHIP_ERROR PlatformManagerImpl::_Shutdown()
{
// Call up to the base class _Shutdown() to perform the bulk of the shutdown.
Expand Down
8 changes: 5 additions & 3 deletions src/platform/Darwin/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
if (mWorkQueue == nullptr)
{
mWorkQueue = dispatch_queue_create(CHIP_CONTROLLER_QUEUE, DISPATCH_QUEUE_SERIAL);
dispatch_suspend(mWorkQueue);
}
return mWorkQueue;
}
Expand All @@ -58,9 +59,9 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
CHIP_ERROR _Shutdown();

CHIP_ERROR _StartChipTimer(int64_t aMilliseconds) { return CHIP_ERROR_NOT_IMPLEMENTED; };
CHIP_ERROR _StartEventLoopTask() { return CHIP_NO_ERROR; };
CHIP_ERROR _StopEventLoopTask() { return CHIP_NO_ERROR; };
void _RunEventLoop(){};
CHIP_ERROR _StartEventLoopTask();
CHIP_ERROR _StopEventLoopTask();
void _RunEventLoop();
void _LockChipStack(){};
bool _TryLockChipStack() { return false; };
void _UnlockChipStack(){};
Expand All @@ -79,6 +80,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
static PlatformManagerImpl sInstance;

dispatch_queue_t mWorkQueue = nullptr;
bool mIsWorkQueueRunning = false;

inline ImplClass * Impl() { return static_cast<PlatformManagerImpl *>(this); }
};
Expand Down

0 comments on commit 2681058

Please sign in to comment.