-
Notifications
You must be signed in to change notification settings - Fork 118
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
Semaphore now able to be interrupted #380
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b5a87e1
semaphore now able to be interrupted, waitqs takes errno=-1 as intern…
Coldwings 0e76205
combine pred and no-pred wait method
Coldwings f4a2f7e
PRED able to return any result type, not only bool type
Coldwings 799437b
Semaphore wait keeps uninterrupted, while add wait_interruptible
Coldwings File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -212,9 +212,9 @@ namespace photon | |
protected: | ||
int wait(Timeout timeout = {}); | ||
int wait_defer(Timeout Timeout, void(*defer)(void*), void* arg); | ||
void resume(thread* th, int error_number = ECANCELED); // `th` must be waiting in this waitq! | ||
int resume_all(int error_number = ECANCELED); | ||
thread* resume_one(int error_number = ECANCELED); | ||
void resume(thread* th, int error_number = -1); // `th` must be waiting in this waitq! | ||
int resume_all(int error_number = -1); | ||
thread* resume_one(int error_number = -1); | ||
waitq() = default; | ||
waitq(const waitq& rhs) = delete; // not allowed to copy construct | ||
waitq(waitq&& rhs) = delete; | ||
|
@@ -362,17 +362,50 @@ namespace photon | |
{ | ||
return waitq::wait(timeout); | ||
} | ||
template <typename LOCK, typename PRED, | ||
typename = decltype(std::declval<PRED>()())> | ||
int wait(LOCK&& lock, PRED&& pred, Timeout timeout = {}) { | ||
return do_wait_pred( | ||
[&] { return wait(std::forward<LOCK>(lock), timeout); }, | ||
std::forward<PRED>(pred), timeout); | ||
} | ||
template <typename PRED, | ||
typename = decltype(std::declval<PRED>()())> | ||
int wait_no_lock(PRED&& pred, Timeout timeout = {}) { | ||
return do_wait_pred( | ||
[&] { return wait_no_lock(timeout); }, | ||
std::forward<PRED>(pred), timeout); | ||
} | ||
thread* signal() { return resume_one(); } | ||
thread* notify_one() { return resume_one(); } | ||
int notify_all() { return resume_all(); } | ||
int broadcast() { return resume_all(); } | ||
protected: | ||
template<typename DO_WAIT, typename PRED> | ||
int do_wait_pred(DO_WAIT&& do_wait, PRED&& pred, Timeout timeout) { | ||
int ret = 0; | ||
int err = ETIMEDOUT; | ||
while (!pred() && !timeout.expired()) { | ||
ret = do_wait(); | ||
err = errno; | ||
} | ||
errno = err; | ||
return ret; | ||
} | ||
}; | ||
|
||
class semaphore : protected waitq | ||
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. add a 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. done |
||
{ | ||
public: | ||
explicit semaphore(uint64_t count = 0) : m_count(count) { } | ||
int wait(uint64_t count, Timeout timeout = {}); | ||
int wait(uint64_t count, Timeout timeout = {}) { | ||
int ret = 0; | ||
do { | ||
ret = wait_interruptible(count, timeout); | ||
} while (ret < 0 && (errno != ESHUTDOWN && errno != ETIMEDOUT)); | ||
return ret; | ||
} | ||
int wait_interruptible(uint64_t count, Timeout timeout = {}); | ||
int signal(uint64_t count) | ||
{ | ||
if (count == 0) return 0; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is the default behavior of the default value -1 ? Documents should be adjusted in:
https://github.com/alibaba/PhotonLibOS/blob/main/doc/docs/api/thread.md#thread_interrupt
https://github.com/alibaba/PhotonLibOS/blob/main/doc/i18n/cn/docusaurus-plugin-content-docs/current/api/thread.md#thread_interrupt
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.
If it comes from the inside of the synchronization primitives due to technical reasons, we use -1 to distinguish from user generated ordinary error numbers (>0). And 0 has already been used for another scenario.
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.
Those documents are about
photon::thread_interrupt
. The default errno inphoton::thread_interrupt
is not changed, so it still EINTR, nothing has changed in document.Here is all about method
waitq::resume
, which is protected method, not a part of API.