Skip to content
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

Make this_thread::sleep_until clock-aware #3914

Merged
merged 10 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions stl/inc/thread
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,14 @@ namespace this_thread {
if (_Abs_time <= _Now) {
return;
}

_timespec64 _Tgt;
(void) _To_timespec64_sys_10_day_clamped(_Tgt, _Abs_time - _Now);
_Thrd_sleep(&_Tgt);
constexpr chrono::milliseconds _Clamp{chrono::hours{24}};
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
const auto _Rel = _Abs_time - _Now;
if (_Rel >= _Clamp) {
_Thrd_sleep_for(static_cast<unsigned long>(_Clamp.count()));
} else {
const auto _Rel_ms = chrono::ceil<chrono::milliseconds>(_Rel);
_Thrd_sleep_for(static_cast<unsigned long>(_Rel_ms.count()));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion stl/inc/xthreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ enum class _Thrd_result : int { _Success, _Nomem, _Timedout, _Busy, _Error };
// threads
_CRTIMP2_PURE _Thrd_result __cdecl _Thrd_detach(_Thrd_t);
_CRTIMP2_PURE _Thrd_result __cdecl _Thrd_join(_Thrd_t, int*);
_CRTIMP2_PURE void __cdecl _Thrd_sleep(const _timespec64*);
_CRTIMP2_PURE void __cdecl _Thrd_yield();
_CRTIMP2_PURE unsigned int __cdecl _Thrd_hardware_concurrency();
_CRTIMP2_PURE _Thrd_id_t __cdecl _Thrd_id();
void __stdcall _Thrd_sleep_for(unsigned long /*ms*/); // defined in sharedmutex.cpp for convenience
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved

// mutexes
enum { // mutex types
Expand Down
1 change: 1 addition & 0 deletions stl/src/cthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ _CRTIMP2_PURE _Thrd_result __cdecl _Thrd_detach(_Thrd_t thr) {
return CloseHandle(thr._Hnd) ? _Thrd_result::_Success : _Thrd_result::_Error;
}

// TRANSITION, ABI: _Thrd_sleep() is preserved for binary compatibility
_CRTIMP2_PURE void __cdecl _Thrd_sleep(const _timespec64* xt) { // suspend thread until time xt
_timespec64 now;
_Timespec64_get_sys(&now);
Expand Down
4 changes: 4 additions & 0 deletions stl/src/sharedmutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ void __cdecl _Smtx_unlock_exclusive(_Smtx_t* smtx) { // unlock exclusive shared
void __cdecl _Smtx_unlock_shared(_Smtx_t* smtx) { // unlock non-exclusive shared mutex
ReleaseSRWLockShared(reinterpret_cast<PSRWLOCK>(smtx));
}

void __stdcall _Thrd_sleep_for(const unsigned long ms) { // suspend current thread
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
Sleep(ms);
}
}