Skip to content

Commit 5257202

Browse files
vivien-applepull[bot]
authored andcommitted
Dispatch IO thread work to the main dispatch_queue for darwin (#6412)
1 parent 5cb9172 commit 5257202

File tree

6 files changed

+72
-4
lines changed

6 files changed

+72
-4
lines changed

src/inet/IPEndPointBasis.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,11 @@ void IPEndPointBasis::HandlePendingIO(uint16_t aPort)
10821082
{
10831083
INET_ERROR lStatus = INET_NO_ERROR;
10841084
IPPacketInfo lPacketInfo;
1085+
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
1086+
__block System::PacketBufferHandle lBuffer;
1087+
#else
10851088
System::PacketBufferHandle lBuffer;
1089+
#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH
10861090

10871091
lPacketInfo.Clear();
10881092
lPacketInfo.DestPort = aPort;
@@ -1189,12 +1193,35 @@ void IPEndPointBasis::HandlePendingIO(uint16_t aPort)
11891193
if (lStatus == INET_NO_ERROR)
11901194
{
11911195
lBuffer.RightSize();
1192-
OnMessageReceived(this, std::move(lBuffer), &lPacketInfo);
1196+
1197+
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
1198+
dispatch_queue_t dispatchQueue = SystemLayer().GetDispatchQueue();
1199+
if (dispatchQueue != nullptr)
1200+
{
1201+
dispatch_sync(dispatchQueue, ^{
1202+
OnMessageReceived(this, std::move(lBuffer), &lPacketInfo);
1203+
});
1204+
}
1205+
else
1206+
#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH
1207+
OnMessageReceived(this, std::move(lBuffer), &lPacketInfo);
11931208
}
11941209
else
11951210
{
11961211
if (OnReceiveError != nullptr && lStatus != chip::System::MapErrorPOSIX(EAGAIN))
1197-
OnReceiveError(this, lStatus, nullptr);
1212+
{
1213+
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
1214+
dispatch_queue_t dispatchQueue = SystemLayer().GetDispatchQueue();
1215+
if (dispatchQueue != nullptr)
1216+
{
1217+
dispatch_sync(dispatchQueue, ^{
1218+
OnReceiveError(this, lStatus, nullptr);
1219+
});
1220+
}
1221+
else
1222+
#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH
1223+
OnReceiveError(this, lStatus, nullptr);
1224+
}
11981225
}
11991226
}
12001227
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

src/platform/Darwin/PlatformManagerImpl.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
4545
err = Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_InitChipStack();
4646
SuccessOrExit(err);
4747

48+
SystemLayer.SetDispatchQueue(GetWorkQueue());
49+
4850
exit:
4951
return err;
5052
}

src/system/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ buildconfig_header("system_buildconfig") {
5656
"CONFIG_DEVICE_LAYER=${config_device_layer}",
5757
"CHIP_SYSTEM_CONFIG_TEST=${chip_build_tests}",
5858
"CHIP_WITH_NLFAULTINJECTION=${chip_with_nlfaultinjection}",
59+
"CHIP_SYSTEM_CONFIG_USE_DISPATCH=${chip_system_config_use_dispatch}",
5960
"CHIP_SYSTEM_CONFIG_USE_LWIP=${chip_system_config_use_lwip}",
6061
"CHIP_SYSTEM_CONFIG_USE_SOCKETS=${chip_system_config_use_sockets}",
6162
"CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK=false",

src/system/SystemLayer.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,17 @@ void Layer::DispatchTimerCallbacks(const uint64_t kCurrentEpoch)
574574
// one-shot
575575
chip::Callback::Callback<> * cb = chip::Callback::Callback<>::FromCancelable(ready.mNext);
576576
cb->Cancel();
577-
cb->mCall(cb->mContext);
577+
578+
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
579+
if (mDispatchQueue != nullptr)
580+
{
581+
dispatch_sync(mDispatchQueue, ^{
582+
cb->mCall(cb->mContext);
583+
});
584+
}
585+
else
586+
#endif
587+
cb->mCall(cb->mContext);
578588
}
579589
}
580590

@@ -695,7 +705,16 @@ void Layer::HandleSelectResult(int aSetSize, fd_set * aReadSet, fd_set * aWriteS
695705

696706
if (lTimer != nullptr && !Timer::IsEarlierEpoch(kCurrentEpoch, lTimer->mAwakenEpoch))
697707
{
698-
lTimer->HandleComplete();
708+
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
709+
if (mDispatchQueue != nullptr)
710+
{
711+
dispatch_sync(mDispatchQueue, ^{
712+
lTimer->HandleComplete();
713+
});
714+
}
715+
else
716+
#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH
717+
lTimer->HandleComplete();
699718
}
700719
}
701720

src/system/SystemLayer.h

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#include <pthread.h>
4747
#endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING
4848

49+
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
50+
#include <dispatch/dispatch.h>
51+
#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH
52+
4953
namespace chip {
5054
namespace System {
5155

@@ -167,6 +171,11 @@ class DLL_EXPORT Layer
167171
Error HandlePlatformTimer(void);
168172
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
169173

174+
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
175+
void SetDispatchQueue(dispatch_queue_t dispatchQueue) { mDispatchQueue = dispatchQueue; };
176+
dispatch_queue_t GetDispatchQueue() { return mDispatchQueue; };
177+
#endif // CHIP_SYSTEM_CONFIG_USE_DISPATCH
178+
170179
static uint64_t GetClock_Monotonic();
171180
static uint64_t GetClock_MonotonicMS();
172181
static uint64_t GetClock_MonotonicHiRes();
@@ -207,6 +216,10 @@ class DLL_EXPORT Layer
207216
friend Error Platform::Layer::StartTimer(Layer & aLayer, void * aContext, uint32_t aMilliseconds);
208217
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
209218

219+
#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
220+
dispatch_queue_t mDispatchQueue;
221+
#endif
222+
210223
// Copy and assignment NOT DEFINED
211224
Layer(const Layer &) = delete;
212225
Layer & operator=(const Layer &) = delete;

src/system/system.gni

+6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ declare_args() {
3333
chip_system_config_provide_statistics = true
3434
}
3535

36+
declare_args() {
37+
# use the dispatch library
38+
chip_system_config_use_dispatch = chip_system_config_use_sockets &&
39+
(current_os == "mac" || current_os == "ios")
40+
}
41+
3642
if (chip_system_config_locking == "") {
3743
if (current_os != "freertos") {
3844
chip_system_config_locking = "posix"

0 commit comments

Comments
 (0)