-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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> Implement semaphore - WIP #420
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
43d7cf0
Implement the semaphore header
HugGa 47650e6
Use compare_exchange_strong instead of doing straight subtraction
HugGa 2bde7cb
Use more consistent _STL_ASSERT and _Ugly identifiers
HugGa c52bccd
Use scoped enums and untypedefed atomics
HugGa 3ea49f0
Clang-format semaphore
HugGa 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// semaphore standard header | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#pragma once | ||
|
||
#ifndef _SEMAPHORE_ | ||
#define _SEMAPHORE_ | ||
#include <yvals_core.h> | ||
#if _STL_COMPILER_PREPROCESSOR | ||
|
||
#if !_HAS_CXX20 | ||
#pragma message("The contents of <semaphore> are available only with C++20 or later.") | ||
#else // ^^^ !_HAS_CXX20 / _HAS_CXX20 vvv | ||
#include <atomic> | ||
#include <cassert> | ||
#include <chrono> | ||
#include <cstddef> | ||
|
||
#pragma pack(push, _CRT_PACKING) | ||
#pragma warning(push, _STL_WARNING_LEVEL) | ||
#pragma warning(disable : _STL_DISABLED_WARNINGS) | ||
_STL_DISABLE_CLANG_WARNINGS | ||
#pragma push_macro("new") | ||
#undef new | ||
|
||
_STD_BEGIN | ||
template <ptrdiff_t _LeastMaxValue = 256> | ||
class counting_semaphore { | ||
public: | ||
static constexpr ptrdiff_t max() noexcept { | ||
// _LeastMaxValue is used here so that things such as binary_semaphore are | ||
return _LeastMaxValue; | ||
} | ||
constexpr explicit counting_semaphore(ptrdiff_t _Desired) { | ||
_STL_ASSERT( | ||
_Desired >= 0 && _Desired <= max(), "Desired value for the semaphore falls outside of semaphore range"); | ||
atomic_init(&_Counter, _Desired); | ||
} | ||
~counting_semaphore() = default; | ||
counting_semaphore(const counting_semaphore&) = delete; | ||
counting_semaphore& operator=(const counting_semaphore&) = delete; | ||
|
||
void release(ptrdiff_t _Update = 1) { | ||
_Counter.fetch_add(_Update, memory_order::release); | ||
} | ||
void acquire() { | ||
while (!try_acquire()) { | ||
} | ||
} | ||
bool try_acquire() noexcept { | ||
ptrdiff_t _Old = _Counter.load(memory_order::acquire); | ||
ptrdiff_t _Desired = _Old - 1; | ||
if (_Old > 0 && _Counter.compare_exchange_strong(_Old, _Desired)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
template <class _Rep, class _Period> | ||
bool try_acquire_for(const chrono::duration<_Rep, _Period>& _Rel_time) { | ||
return try_acquire_until(chrono::steady_clock::now() + _Rel_time); | ||
} | ||
template <class _Clock, class _Duration> | ||
bool try_acquire_until(const chrono::time_point<_Clock, _Duration>& _Abs_time) { | ||
while (!try_acquire()) { | ||
if (_Clock::now() >= _Abs_time) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private: | ||
atomic<ptrdiff_t> _Counter; | ||
}; | ||
using binary_semaphore = counting_semaphore<1>; | ||
_STD_END | ||
#pragma pop_macro("new") | ||
_STL_RESTORE_CLANG_WARNINGS | ||
#pragma warning(pop) | ||
#pragma pack(pop) | ||
#endif | ||
#endif | ||
#endif |
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
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.
Ditto I don't think this is acceptable due to busy waiting.