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; CRITICAL_SECTION critical_section;
public: public:
CriticalSection() { CriticalSection() noexcept {
::InitializeCriticalSection(&critical_section); ::InitializeCriticalSection(&critical_section);
} }
~CriticalSection() { ~CriticalSection() noexcept {
::DeleteCriticalSection(&critical_section); ::DeleteCriticalSection(&critical_section);
} }
CriticalSection(const CriticalSection &other) = delete; CriticalSection(const CriticalSection &other) = delete;
CriticalSection &operator=(const CriticalSection &other) = delete; CriticalSection &operator=(const CriticalSection &other) = delete;
void lock() { void lock() noexcept {
::EnterCriticalSection(&critical_section); ::EnterCriticalSection(&critical_section);
}; };
bool try_lock() { bool try_lock() noexcept {
return ::TryEnterCriticalSection(&critical_section) != 0; return ::TryEnterCriticalSection(&critical_section) != 0;
}; };
void unlock() { void unlock() noexcept {
::LeaveCriticalSection(&critical_section); ::LeaveCriticalSection(&critical_section);
} }
}; };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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