-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generalize socket-based event loop #6561
Changes from all commits
e8488ef
4cfc73e
4463ddd
89a3ec9
e43bee0
c4468cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* Copyright (c) 2020-2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -24,7 +24,6 @@ | |
#ifndef GENERIC_PLATFORM_MANAGER_IMPL_POSIX_CPP | ||
#define GENERIC_PLATFORM_MANAGER_IMPL_POSIX_CPP | ||
|
||
#include "system/SystemError.h" | ||
#include <platform/PlatformManager.h> | ||
#include <platform/internal/CHIPDeviceLayerInternal.h> | ||
#include <platform/internal/GenericPlatformManagerImpl_POSIX.h> | ||
|
@@ -36,28 +35,18 @@ | |
#endif | ||
#include <platform/internal/GenericPlatformManagerImpl.cpp> | ||
|
||
#include <system/SystemError.h> | ||
#include <system/SystemLayer.h> | ||
|
||
#include <assert.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <poll.h> | ||
#include <sched.h> | ||
#include <sys/select.h> | ||
#include <unistd.h> | ||
|
||
#define DEFAULT_MIN_SLEEP_PERIOD (60 * 60 * 24 * 30) // Month [sec] | ||
|
||
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
namespace chip { | ||
namespace Mdns { | ||
void UpdateMdnsDataset(fd_set & readFdSet, fd_set & writeFdSet, fd_set & errorFdSet, int & maxFd, timeval & timeout); | ||
void ProcessMdns(fd_set & readFdSet, fd_set & writeFdSet, fd_set & errorFdSet); | ||
} // namespace Mdns | ||
} // namespace chip | ||
#endif | ||
|
||
namespace chip { | ||
namespace DeviceLayer { | ||
namespace Internal { | ||
|
||
|
@@ -140,6 +129,8 @@ bool GenericPlatformManagerImpl_POSIX<ImplClass>::_IsChipStackLockedByCurrentThr | |
template <class ImplClass> | ||
CHIP_ERROR GenericPlatformManagerImpl_POSIX<ImplClass>::_StartChipTimer(int64_t aMilliseconds) | ||
{ | ||
// TODO(#5556): Integrate timer platform details with WatchableEventManager. | ||
|
||
// Let SystemLayer.PrepareSelect() handle timers. | ||
return CHIP_NO_ERROR; | ||
} | ||
|
@@ -148,7 +139,10 @@ template <class ImplClass> | |
void GenericPlatformManagerImpl_POSIX<ImplClass>::_PostEvent(const ChipDeviceEvent * event) | ||
{ | ||
mChipEventQueue.Push(*event); | ||
SysOnEventSignal(this); // Trigger wake select on CHIP thread | ||
|
||
#if CHIP_SYSTEM_CONFIG_USE_IO_THREAD | ||
SystemLayer.WakeIOThread(); // Trigger wake select on CHIP thread | ||
#endif // CHIP_SYSTEM_CONFIG_USE_IO_THREAD | ||
} | ||
|
||
template <class ImplClass> | ||
|
@@ -161,77 +155,6 @@ void GenericPlatformManagerImpl_POSIX<ImplClass>::ProcessDeviceEvents() | |
} | ||
} | ||
|
||
template <class ImplClass> | ||
void GenericPlatformManagerImpl_POSIX<ImplClass>::SysOnEventSignal(void * arg) | ||
{ | ||
SystemLayer.WakeSelect(); | ||
} | ||
|
||
template <class ImplClass> | ||
void GenericPlatformManagerImpl_POSIX<ImplClass>::SysUpdate() | ||
{ | ||
FD_ZERO(&mReadSet); | ||
FD_ZERO(&mWriteSet); | ||
FD_ZERO(&mErrorSet); | ||
mMaxFd = 0; | ||
|
||
// Max out this duration and let CHIP set it appropriately. | ||
mNextTimeout.tv_sec = DEFAULT_MIN_SLEEP_PERIOD; | ||
mNextTimeout.tv_usec = 0; | ||
|
||
if (SystemLayer.State() == System::kLayerState_Initialized) | ||
{ | ||
SystemLayer.PrepareSelect(mMaxFd, &mReadSet, &mWriteSet, &mErrorSet, mNextTimeout); | ||
} | ||
|
||
#if !(CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK) | ||
if (InetLayer.State == InetLayer::kState_Initialized) | ||
{ | ||
InetLayer.PrepareSelect(mMaxFd, &mReadSet, &mWriteSet, &mErrorSet, mNextTimeout); | ||
} | ||
#endif // !(CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK) | ||
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
chip::Mdns::UpdateMdnsDataset(mReadSet, mWriteSet, mErrorSet, mMaxFd, mNextTimeout); | ||
#endif | ||
} | ||
|
||
template <class ImplClass> | ||
void GenericPlatformManagerImpl_POSIX<ImplClass>::SysProcess() | ||
{ | ||
int selectRes; | ||
int64_t nextTimeoutMs; | ||
|
||
nextTimeoutMs = mNextTimeout.tv_sec * 1000 + mNextTimeout.tv_usec / 1000; | ||
_StartChipTimer(nextTimeoutMs); | ||
|
||
Impl()->UnlockChipStack(); | ||
selectRes = select(mMaxFd + 1, &mReadSet, &mWriteSet, &mErrorSet, &mNextTimeout); | ||
Impl()->LockChipStack(); | ||
|
||
if (selectRes < 0) | ||
{ | ||
ChipLogError(DeviceLayer, "select failed: %s\n", ErrorStr(System::MapErrorPOSIX(errno))); | ||
return; | ||
} | ||
|
||
if (SystemLayer.State() == System::kLayerState_Initialized) | ||
{ | ||
SystemLayer.HandleSelectResult(mMaxFd, &mReadSet, &mWriteSet, &mErrorSet); | ||
} | ||
|
||
#if !(CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK) | ||
if (InetLayer.State == InetLayer::kState_Initialized) | ||
{ | ||
InetLayer.HandleSelectResult(mMaxFd, &mReadSet, &mWriteSet, &mErrorSet); | ||
} | ||
#endif // !(CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK) | ||
|
||
ProcessDeviceEvents(); | ||
#if CHIP_DEVICE_CONFIG_ENABLE_MDNS | ||
chip::Mdns::ProcessMdns(mReadSet, mWriteSet, mErrorSet); | ||
#endif | ||
} | ||
|
||
template <class ImplClass> | ||
void GenericPlatformManagerImpl_POSIX<ImplClass>::_RunEventLoop() | ||
{ | ||
|
@@ -254,11 +177,21 @@ void GenericPlatformManagerImpl_POSIX<ImplClass>::_RunEventLoop() | |
|
||
Impl()->LockChipStack(); | ||
|
||
System::WatchableEventManager & watchState = SystemLayer.WatchableEvents(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we going to be able to replace this code with the Android implementation https://developer.android.com/reference/android/os/Looper ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know any reason we shouldn't be able to, since the consuming-side interface (i.e. SystemSockets.h) is general. As long as there's some way to implement calling back on events of interest, it should be doable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok sounds good, I don't expect to solve it in this PR. I do think Android will want to move onto their Java based event loop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Filed #7626. |
||
watchState.EventLoopBegins(); | ||
do | ||
{ | ||
SysUpdate(); | ||
SysProcess(); | ||
watchState.PrepareEvents(); | ||
|
||
Impl()->UnlockChipStack(); | ||
watchState.WaitForEvents(); | ||
Impl()->LockChipStack(); | ||
|
||
watchState.HandleEvents(); | ||
|
||
ProcessDeviceEvents(); | ||
} while (mShouldRunEventLoop.load(std::memory_order_relaxed)); | ||
watchState.EventLoopEnds(); | ||
|
||
Impl()->UnlockChipStack(); | ||
|
||
|
@@ -340,7 +273,7 @@ CHIP_ERROR GenericPlatformManagerImpl_POSIX<ImplClass>::_StopEventLoopTask() | |
// SystemLayer. | ||
// | ||
Impl()->LockChipStack(); | ||
SystemLayer.WakeSelect(); | ||
SystemLayer.WakeIOThread(); | ||
Impl()->UnlockChipStack(); | ||
|
||
pthread_mutex_lock(&mStateLock); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no more SystemLayer.PrepareSelect() after this PR, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. I guess I considered fixing the comments to be part of the TODO.