-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update python logic for executing work in chip main loop (#28449)
* Add python main loop work method * Fix typo and restyle * Fix typo * Code review updates * Comment update * Restyle --------- Co-authored-by: Andrei Litvin <[email protected]>
- Loading branch information
Showing
5 changed files
with
210 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include <functional> | ||
|
||
namespace chip { | ||
namespace MainLoopWork { | ||
|
||
/** | ||
* Executes the given function in the CHIP main loop if one exists. | ||
* | ||
* Several implementations exist, however generally: | ||
* | ||
* - if already in the chip main loop (or main loop is not running), | ||
* `f` gets executed right away | ||
* - otherwise: | ||
* - if chip stack locking is available, `f` is executed within the lock | ||
* - if chip stack locking not available, this will schedule and WAIT | ||
* for `f` to execute | ||
*/ | ||
void ExecuteInMainLoop(std::function<void()> f); | ||
|
||
} // namespace MainLoopWork | ||
} // namespace chip |
44 changes: 44 additions & 0 deletions
44
src/controller/python/chip/native/ChipMainLoopWork_StackLock.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "ChipMainLoopWork.h" | ||
|
||
#include <platform/PlatformManager.h> | ||
|
||
namespace chip { | ||
namespace MainLoopWork { | ||
|
||
void ExecuteInMainLoop(std::function<void()> f) | ||
{ | ||
// NOTE: requires CHIP_STACK_LOCK_TRACKING_ENABLED to be available (which python builds | ||
// generally have) to ensure chip stack locks are not deadlocking, since these | ||
// functions do not know the actual state of the chip main loop. | ||
// | ||
// TODO: it may be a good assumption that python code asking for this will NOT run in | ||
// chip main loop, however we try to be generic | ||
if (chip::DeviceLayer::PlatformMgr().IsChipStackLockedByCurrentThread()) | ||
{ | ||
f(); | ||
return; | ||
} | ||
|
||
chip::DeviceLayer::StackLock lock; | ||
f(); | ||
} | ||
|
||
} // namespace MainLoopWork | ||
} // namespace chip |
75 changes: 75 additions & 0 deletions
75
src/controller/python/chip/native/ChipMainLoopWork_WorkSchedule.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#include "ChipMainLoopWork.h" | ||
|
||
#include <platform/CHIPDeviceLayer.h> | ||
#include <semaphore.h> | ||
|
||
namespace chip { | ||
namespace MainLoopWork { | ||
namespace { | ||
|
||
struct WorkData | ||
{ | ||
std::function<void()> callback; | ||
sem_t done; | ||
|
||
WorkData() { sem_init(&done, 0 /* shared */, 0); } | ||
~WorkData() { sem_destroy(&done); } | ||
void Post() { sem_post(&done); } | ||
void Wait() { sem_wait(&done); } | ||
}; | ||
|
||
void PerformWork(intptr_t arg) | ||
{ | ||
WorkData * work = reinterpret_cast<WorkData *>(arg); | ||
|
||
work->callback(); | ||
work->Post(); | ||
} | ||
|
||
} // namespace | ||
|
||
void ExecuteInMainLoop(std::function<void()> f) | ||
{ | ||
|
||
// NOTE: requires CHIP_STACK_LOCK_TRACKING_ENABLED to be available (which python builds | ||
// generally have) to ensure chip stack locks are not deadlocking, since these | ||
// functions do not know the actual state of the chip main loop. | ||
// | ||
// TODO: it may be a good assumption that python code asking for this will NOT run in | ||
// chip main loop, however we try to be generic | ||
if (chip::DeviceLayer::PlatformMgr().IsChipStackLockedByCurrentThread()) | ||
{ | ||
f(); | ||
return; | ||
} | ||
|
||
// NOTE: the code below assumes that chip main loop is running. | ||
// if it does not, this will deadlock. | ||
// | ||
// IsChipStackLockedByCurrentThread is expected to be aware of main loop | ||
// not running. | ||
WorkData workdata; | ||
workdata.callback = f; | ||
chip::DeviceLayer::PlatformMgr().ScheduleWork(PerformWork, reinterpret_cast<intptr_t>(&workdata)); | ||
workdata.Wait(); | ||
} | ||
|
||
} // namespace MainLoopWork | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters