thread/*: add "noexcept"

This commit is contained in:
Max Kellermann 2017-11-26 11:58:53 +01:00
parent 3b3ec402d6
commit 618704f504
11 changed files with 44 additions and 44 deletions

View File

@ -41,26 +41,26 @@ class CriticalSection {
CRITICAL_SECTION critical_section;
public:
CriticalSection() {
CriticalSection() noexcept {
::InitializeCriticalSection(&critical_section);
}
~CriticalSection() {
~CriticalSection() noexcept {
::DeleteCriticalSection(&critical_section);
}
CriticalSection(const CriticalSection &other) = delete;
CriticalSection &operator=(const CriticalSection &other) = delete;
void lock() {
void lock() noexcept {
::EnterCriticalSection(&critical_section);
};
bool try_lock() {
bool try_lock() noexcept {
return ::TryEnterCriticalSection(&critical_section) != 0;
};
void unlock() {
void unlock() noexcept {
::LeaveCriticalSection(&critical_section);
}
};

View File

@ -44,12 +44,12 @@ public:
/**
* No initialisation.
*/
ThreadId() = default;
ThreadId() noexcept = default;
#ifdef WIN32
constexpr ThreadId(DWORD _id):id(_id) {}
constexpr ThreadId(DWORD _id) noexcept:id(_id) {}
#else
constexpr ThreadId(pthread_t _id):id(_id) {}
constexpr ThreadId(pthread_t _id) noexcept:id(_id) {}
#endif
gcc_const

View File

@ -52,11 +52,11 @@ class ScopeUnlock {
Mutex &mutex;
public:
explicit ScopeUnlock(Mutex &_mutex):mutex(_mutex) {
explicit ScopeUnlock(Mutex &_mutex) noexcept:mutex(_mutex) {
mutex.unlock();
};
~ScopeUnlock() {
~ScopeUnlock() noexcept {
mutex.lock();
}

View File

@ -35,7 +35,7 @@
#endif
static inline void
SetThreadName(const char *name)
SetThreadName(const char *name) noexcept
{
#if defined(HAVE_PTHREAD_SETNAME_NP) && !defined(__NetBSD__)
/* not using pthread_setname_np() on NetBSD because it
@ -56,7 +56,7 @@ SetThreadName(const char *name)
template<typename... Args>
static inline void
FormatThreadName(const char *fmt, gcc_unused Args&&... args)
FormatThreadName(const char *fmt, gcc_unused Args&&... args) noexcept
{
#ifdef HAVE_THREAD_NAME
char buffer[16];

View File

@ -46,15 +46,15 @@ public:
#ifdef __GLIBC__
/* optimized constexpr constructor for pthread implementations
that support it */
constexpr PosixCond():cond(PTHREAD_COND_INITIALIZER) {}
constexpr PosixCond() noexcept:cond(PTHREAD_COND_INITIALIZER) {}
#else
/* slow fallback for pthread implementations that are not
compatible with "constexpr" */
PosixCond() {
PosixCond() noexcept {
pthread_cond_init(&cond, nullptr);
}
~PosixCond() {
~PosixCond() noexcept {
pthread_cond_destroy(&cond);
}
#endif
@ -62,20 +62,20 @@ public:
PosixCond(const PosixCond &other) = delete;
PosixCond &operator=(const PosixCond &other) = delete;
void signal() {
void signal() noexcept {
pthread_cond_signal(&cond);
}
void broadcast() {
void broadcast() noexcept {
pthread_cond_broadcast(&cond);
}
void wait(PosixMutex &mutex) {
void wait(PosixMutex &mutex) noexcept {
pthread_cond_wait(&cond, &mutex.mutex);
}
private:
bool timed_wait(PosixMutex &mutex, unsigned timeout_ms) {
bool timed_wait(PosixMutex &mutex, unsigned timeout_ms) noexcept {
struct timeval now;
gettimeofday(&now, nullptr);
@ -93,7 +93,7 @@ private:
public:
bool timed_wait(PosixMutex &mutex,
std::chrono::steady_clock::duration timeout) {
std::chrono::steady_clock::duration timeout) noexcept {
auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
if (timeout_ms < 0)
timeout_ms = 0;

View File

@ -30,7 +30,7 @@
* merge multiple wakeups, which is a trick to save energy.
*/
static inline void
SetThreadTimerSlackNS(unsigned long slack_ns)
SetThreadTimerSlackNS(unsigned long slack_ns) noexcept
{
#if defined(HAVE_PRCTL) && defined(PR_SET_TIMERSLACK)
prctl(PR_SET_TIMERSLACK, slack_ns, 0, 0, 0);
@ -40,13 +40,13 @@ SetThreadTimerSlackNS(unsigned long slack_ns)
}
static inline void
SetThreadTimerSlackUS(unsigned long slack_us)
SetThreadTimerSlackUS(unsigned long slack_us) noexcept
{
SetThreadTimerSlackNS(slack_us * 1000ul);
}
static inline void
SetThreadTimerSlackMS(unsigned long slack_ms)
SetThreadTimerSlackMS(unsigned long slack_ms) noexcept
{
SetThreadTimerSlackNS(slack_ms * 1000000ul);
}

View File

@ -56,7 +56,7 @@ Thread::Start()
}
void
Thread::Join()
Thread::Join() noexcept
{
assert(IsDefined());
assert(!IsInside());
@ -72,7 +72,7 @@ Thread::Join()
}
inline void
Thread::Run()
Thread::Run() noexcept
{
#ifndef WIN32
#ifndef NDEBUG
@ -94,7 +94,7 @@ Thread::Run()
#ifdef WIN32
DWORD WINAPI
Thread::ThreadProc(LPVOID ctx)
Thread::ThreadProc(LPVOID ctx) noexcept
{
Thread &thread = *(Thread *)ctx;
@ -105,7 +105,7 @@ Thread::ThreadProc(LPVOID ctx)
#else
void *
Thread::ThreadProc(void *ctx)
Thread::ThreadProc(void *ctx) noexcept
{
Thread &thread = *(Thread *)ctx;

View File

@ -54,19 +54,19 @@ class Thread {
#endif
public:
explicit Thread(Function _f):f(_f) {}
explicit Thread(Function _f) noexcept:f(_f) {}
Thread(const Thread &) = delete;
#ifndef NDEBUG
~Thread() {
~Thread() noexcept {
/* all Thread objects must be destructed manually by calling
Join(), to clean up */
assert(!IsDefined());
}
#endif
bool IsDefined() const {
bool IsDefined() const noexcept {
#ifdef WIN32
return handle != nullptr;
#else
@ -91,15 +91,15 @@ public:
}
void Start();
void Join();
void Join() noexcept;
private:
void Run();
void Run() noexcept;
#ifdef WIN32
static DWORD WINAPI ThreadProc(LPVOID ctx);
static DWORD WINAPI ThreadProc(LPVOID ctx) noexcept;
#else
static void *ThreadProc(void *ctx);
static void *ThreadProc(void *ctx) noexcept;
#endif
};

View File

@ -41,13 +41,13 @@
#ifdef __linux__
static int
ioprio_set(int which, int who, int ioprio)
ioprio_set(int which, int who, int ioprio) noexcept
{
return syscall(__NR_ioprio_set, which, who, ioprio);
}
static void
ioprio_set_idle()
ioprio_set_idle() noexcept
{
static constexpr int _IOPRIO_WHO_PROCESS = 1;
static constexpr int _IOPRIO_CLASS_IDLE = 3;
@ -61,7 +61,7 @@ ioprio_set_idle()
#endif
void
SetThreadIdlePriority()
SetThreadIdlePriority() noexcept
{
#ifdef __linux__
#ifdef SCHED_IDLE

View File

@ -34,7 +34,7 @@
* Lower the current thread's priority to "idle" (very low).
*/
void
SetThreadIdlePriority();
SetThreadIdlePriority() noexcept;
/**
* Raise the current thread's priority to "real-time" (very high).

View File

@ -41,35 +41,35 @@ class WindowsCond {
CONDITION_VARIABLE cond;
public:
WindowsCond() {
WindowsCond() noexcept {
InitializeConditionVariable(&cond);
}
WindowsCond(const WindowsCond &other) = delete;
WindowsCond &operator=(const WindowsCond &other) = delete;
void signal() {
void signal() noexcept {
WakeConditionVariable(&cond);
}
void broadcast() {
void broadcast() noexcept {
WakeAllConditionVariable(&cond);
}
private:
bool timed_wait(CriticalSection &mutex, DWORD timeout_ms) {
bool timed_wait(CriticalSection &mutex, DWORD timeout_ms) noexcept {
return SleepConditionVariableCS(&cond, &mutex.critical_section,
timeout_ms);
}
public:
bool timed_wait(CriticalSection &mutex,
std::chrono::steady_clock::duration timeout) {
std::chrono::steady_clock::duration timeout) noexcept {
auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
return timed_wait(mutex, timeout_ms);
}
void wait(CriticalSection &mutex) {
void wait(CriticalSection &mutex) noexcept {
timed_wait(mutex, INFINITE);
}
};