diff options
Diffstat (limited to 'src/threading')
-rw-r--r-- | src/threading/event.cpp | 1 | ||||
-rw-r--r-- | src/threading/semaphore.cpp | 74 | ||||
-rw-r--r-- | src/threading/thread.cpp | 86 | ||||
-rw-r--r-- | src/threading/thread.h | 25 |
4 files changed, 100 insertions, 86 deletions
diff --git a/src/threading/event.cpp b/src/threading/event.cpp index fd345fb8b..885e732c8 100644 --- a/src/threading/event.cpp +++ b/src/threading/event.cpp @@ -35,6 +35,7 @@ void Event::wait() notified = false; } + void Event::signal() { MutexAutoLock lock(mutex); diff --git a/src/threading/semaphore.cpp b/src/threading/semaphore.cpp index 8226bd9a0..ce22dcd05 100644 --- a/src/threading/semaphore.cpp +++ b/src/threading/semaphore.cpp @@ -23,38 +23,36 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <cstdlib> #include <cassert> -#define UNUSED(expr) \ - do { \ - (void)(expr); \ - } while (0) +#define UNUSED(expr) do { (void)(expr); } while (0) #ifdef _WIN32 -#include <climits> -#define MAX_SEMAPHORE_COUNT LONG_MAX - 1 + #include <climits> + #define MAX_SEMAPHORE_COUNT LONG_MAX - 1 #else -#include <cerrno> -#include <sys/time.h> -#include <pthread.h> -#if defined(__MACH__) && defined(__APPLE__) -#include <mach/mach.h> -#include <mach/task.h> -#include <mach/semaphore.h> -#include <sys/semaphore.h> -#include <unistd.h> - -#undef sem_t -#undef sem_init -#undef sem_wait -#undef sem_post -#undef sem_destroy -#define sem_t semaphore_t -#define sem_init(s, p, c) semaphore_create(mach_task_self(), (s), 0, (c)) -#define sem_wait(s) semaphore_wait(*(s)) -#define sem_post(s) semaphore_signal(*(s)) -#define sem_destroy(s) semaphore_destroy(mach_task_self(), *(s)) -#endif + #include <cerrno> + #include <sys/time.h> + #include <pthread.h> + #if defined(__MACH__) && defined(__APPLE__) + #include <mach/mach.h> + #include <mach/task.h> + #include <mach/semaphore.h> + #include <sys/semaphore.h> + #include <unistd.h> + + #undef sem_t + #undef sem_init + #undef sem_wait + #undef sem_post + #undef sem_destroy + #define sem_t semaphore_t + #define sem_init(s, p, c) semaphore_create(mach_task_self(), (s), 0, (c)) + #define sem_wait(s) semaphore_wait(*(s)) + #define sem_post(s) semaphore_signal(*(s)) + #define sem_destroy(s) semaphore_destroy(mach_task_self(), *(s)) + #endif #endif + Semaphore::Semaphore(int val) { #ifdef _WIN32 @@ -66,6 +64,7 @@ Semaphore::Semaphore(int val) #endif } + Semaphore::~Semaphore() { #ifdef _WIN32 @@ -82,6 +81,7 @@ Semaphore::~Semaphore() #endif } + void Semaphore::post(unsigned int num) { assert(num > 0); @@ -96,6 +96,7 @@ void Semaphore::post(unsigned int num) #endif } + void Semaphore::wait() { #ifdef _WIN32 @@ -107,6 +108,7 @@ void Semaphore::wait() #endif } + bool Semaphore::wait(unsigned int time_ms) { #ifdef _WIN32 @@ -119,7 +121,7 @@ bool Semaphore::wait(unsigned int time_ms) return false; } #else -#if defined(__MACH__) && defined(__APPLE__) +# if defined(__MACH__) && defined(__APPLE__) mach_timespec_t wait_time; wait_time.tv_sec = time_ms / 1000; wait_time.tv_nsec = 1000000 * (time_ms % 1000); @@ -137,33 +139,29 @@ bool Semaphore::wait(unsigned int time_ms) if (ret) errno = EINVAL; } -#else +# else int ret; if (time_ms > 0) { struct timespec wait_time; struct timeval now; if (gettimeofday(&now, NULL) == -1) { - std::cerr << "Semaphore::wait(ms): Unable to get time with " - "gettimeofday!" - << std::endl; + std::cerr << "Semaphore::wait(ms): Unable to get time with gettimeofday!" << std::endl; abort(); } - wait_time.tv_nsec = - ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000); - wait_time.tv_sec = (time_ms / 1000) + - (wait_time.tv_nsec / (1000 * 1000 * 1000)) + - now.tv_sec; + wait_time.tv_nsec = ((time_ms % 1000) * 1000 * 1000) + (now.tv_usec * 1000); + wait_time.tv_sec = (time_ms / 1000) + (wait_time.tv_nsec / (1000 * 1000 * 1000)) + now.tv_sec; wait_time.tv_nsec %= 1000 * 1000 * 1000; ret = sem_timedwait(&semaphore, &wait_time); } else { ret = sem_trywait(&semaphore); } -#endif +# endif assert(!ret || (errno == ETIMEDOUT || errno == EINTR || errno == EAGAIN)); return !ret; #endif } + diff --git a/src/threading/thread.cpp b/src/threading/thread.cpp index 1714f6e8e..e0f808c4d 100644 --- a/src/threading/thread.cpp +++ b/src/threading/thread.cpp @@ -30,42 +30,45 @@ DEALINGS IN THE SOFTWARE. // for setName #if defined(__linux__) -#include <sys/prctl.h> + #include <sys/prctl.h> #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) -#include <pthread_np.h> + #include <pthread_np.h> #elif defined(_MSC_VER) -struct THREADNAME_INFO -{ - DWORD dwType; // Must be 0x1000 - LPCSTR szName; // Pointer to name (in user addr space) - DWORD dwThreadID; // Thread ID (-1=caller thread) - DWORD dwFlags; // Reserved for future use, must be zero -}; + struct THREADNAME_INFO { + DWORD dwType; // Must be 0x1000 + LPCSTR szName; // Pointer to name (in user addr space) + DWORD dwThreadID; // Thread ID (-1=caller thread) + DWORD dwFlags; // Reserved for future use, must be zero + }; #endif // for bindToProcessor #if __FreeBSD_version >= 702106 -typedef cpuset_t cpu_set_t; + typedef cpuset_t cpu_set_t; #elif defined(__sun) || defined(sun) -#include <sys/types.h> -#include <sys/processor.h> -#include <sys/procset.h> + #include <sys/types.h> + #include <sys/processor.h> + #include <sys/procset.h> #elif defined(_AIX) -#include <sys/processor.h> -#include <sys/thread.h> + #include <sys/processor.h> + #include <sys/thread.h> #elif defined(__APPLE__) -#include <mach/mach_init.h> -#include <mach/thread_act.h> + #include <mach/mach_init.h> + #include <mach/thread_act.h> #endif + Thread::Thread(const std::string &name) : - m_name(name), m_request_stop(false), m_running(false) + m_name(name), + m_request_stop(false), + m_running(false) { #ifdef _AIX m_kernel_thread_id = -1; #endif } + Thread::~Thread() { kill(); @@ -73,8 +76,10 @@ Thread::~Thread() // Make sure start finished mutex is unlocked before it's destroyed if (m_start_finished_mutex.try_lock()) m_start_finished_mutex.unlock(); + } + bool Thread::start() { MutexAutoLock lock(m_mutex); @@ -104,12 +109,14 @@ bool Thread::start() return true; } + bool Thread::stop() { m_request_stop = true; return true; } + bool Thread::wait() { MutexAutoLock lock(m_mutex); @@ -117,6 +124,7 @@ bool Thread::wait() if (!m_joinable) return false; + m_thread_obj->join(); delete m_thread_obj; @@ -127,6 +135,7 @@ bool Thread::wait() return true; } + bool Thread::kill() { if (!m_running) { @@ -137,28 +146,28 @@ bool Thread::kill() m_running = false; #if defined(_WIN32) - // See - // https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method - TerminateThread((HANDLE)m_thread_obj->native_handle(), 0); - CloseHandle((HANDLE)m_thread_obj->native_handle()); + // See https://msdn.microsoft.com/en-us/library/hh920601.aspx#thread__native_handle_method + TerminateThread((HANDLE) m_thread_obj->native_handle(), 0); + CloseHandle((HANDLE) m_thread_obj->native_handle()); #else // We need to pthread_kill instead on Android since NDKv5's pthread // implementation is incomplete. -#ifdef __ANDROID__ +# ifdef __ANDROID__ pthread_kill(getThreadHandle(), SIGKILL); -#else +# else pthread_cancel(getThreadHandle()); -#endif +# endif wait(); #endif - m_retval = nullptr; - m_joinable = false; + m_retval = nullptr; + m_joinable = false; m_request_stop = false; return true; } + bool Thread::getReturnValue(void **ret) { if (m_running) @@ -168,6 +177,7 @@ bool Thread::getReturnValue(void **ret) return true; } + void Thread::threadProc(Thread *thr) { #ifdef _AIX @@ -193,6 +203,7 @@ void Thread::threadProc(Thread *thr) g_logger.deregisterThread(); } + void Thread::setName(const std::string &name) { #if defined(__linux__) @@ -226,8 +237,8 @@ void Thread::setName(const std::string &name) info.dwFlags = 0; __try { - RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD), - (ULONG_PTR *)&info); + RaiseException(0x406D1388, 0, + sizeof(info) / sizeof(DWORD), (ULONG_PTR *)&info); } __except (EXCEPTION_CONTINUE_EXECUTION) { } @@ -237,15 +248,17 @@ void Thread::setName(const std::string &name) // Silently ignore the request. #else -#warning "Unrecognized platform, thread names will not be available." + #warning "Unrecognized platform, thread names will not be available." #endif } + unsigned int Thread::getNumberOfProcessors() { return std::thread::hardware_concurrency(); } + bool Thread::bindToProcessor(unsigned int proc_number) { #if defined(__ANDROID__) @@ -258,8 +271,7 @@ bool Thread::bindToProcessor(unsigned int proc_number) #elif __MINGW32__ - return SetThreadAffinityMask( - pthread_gethandle(getThreadHandle()), 1 << proc_number); + return SetThreadAffinityMask(pthread_gethandle(getThreadHandle()), 1 << proc_number); #elif __FreeBSD_version >= 702106 || defined(__linux__) || defined(__DragonFly__) @@ -282,8 +294,8 @@ bool Thread::bindToProcessor(unsigned int proc_number) pthread_spu_t answer; - return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP, &answer, proc_number, - getThreadHandle()) == 0; + return pthread_processor_bind_np(PTHREAD_BIND_ADVISORY_NP, + &answer, proc_number, getThreadHandle()) == 0; #elif defined(__APPLE__) @@ -292,8 +304,8 @@ bool Thread::bindToProcessor(unsigned int proc_number) thread_port_t threadport = pthread_mach_thread_np(getThreadHandle()); tapol.affinity_tag = proc_number + 1; return thread_policy_set(threadport, THREAD_AFFINITY_POLICY, - (thread_policy_t)&tapol, - THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS; + (thread_policy_t)&tapol, + THREAD_AFFINITY_POLICY_COUNT) == KERN_SUCCESS; #else @@ -302,6 +314,7 @@ bool Thread::bindToProcessor(unsigned int proc_number) #endif } + bool Thread::setPriority(int prio) { #ifdef _MSC_VER @@ -328,3 +341,4 @@ bool Thread::setPriority(int prio) #endif } + diff --git a/src/threading/thread.h b/src/threading/thread.h index aee2aa357..cea92226f 100644 --- a/src/threading/thread.h +++ b/src/threading/thread.h @@ -33,7 +33,7 @@ DEALINGS IN THE SOFTWARE. #include <mutex> #ifdef _AIX -#include <sys/thread.h> // for tid_t + #include <sys/thread.h> // for tid_t #endif /* @@ -41,17 +41,18 @@ DEALINGS IN THE SOFTWARE. * even divisions between the minimum and maximum reported thread priority. */ #if !defined(_WIN32) -#define THREAD_PRIORITY_LOWEST 0 -#define THREAD_PRIORITY_BELOW_NORMAL 1 -#define THREAD_PRIORITY_NORMAL 2 -#define THREAD_PRIORITY_ABOVE_NORMAL 3 -#define THREAD_PRIORITY_HIGHEST 4 + #define THREAD_PRIORITY_LOWEST 0 + #define THREAD_PRIORITY_BELOW_NORMAL 1 + #define THREAD_PRIORITY_NORMAL 2 + #define THREAD_PRIORITY_ABOVE_NORMAL 3 + #define THREAD_PRIORITY_HIGHEST 4 #endif -class Thread -{ + + +class Thread { public: - Thread(const std::string &name = ""); + Thread(const std::string &name=""); virtual ~Thread(); DISABLE_CLASS_COPY(Thread) @@ -140,9 +141,7 @@ protected: private: std::thread::native_handle_type getThreadHandle() - { - return m_thread_obj->native_handle(); - } + { return m_thread_obj->native_handle(); } static void threadProc(Thread *thr); @@ -155,9 +154,11 @@ private: std::thread *m_thread_obj = nullptr; + #ifdef _AIX // For AIX, there does not exist any mapping from pthread_t to tid_t // available to us, so we maintain one ourselves. This is set on thread start. tid_t m_kernel_thread_id; #endif }; + |