diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2020-11-04 16:19:54 +0100 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2020-11-04 16:19:54 +0100 |
commit | ad148587dcf5244c2d2011dba339786c765c54c4 (patch) | |
tree | bdd914121cd326da2ed26679838878e3edffc841 /src/threading | |
parent | 1145b05ea0bda87dc0827821385810eced08f774 (diff) | |
download | dragonfireclient-ad148587dcf5244c2d2011dba339786c765c54c4.tar.xz |
Make Lint Happy
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, 86 insertions, 100 deletions
diff --git a/src/threading/event.cpp b/src/threading/event.cpp index 885e732c8..fd345fb8b 100644 --- a/src/threading/event.cpp +++ b/src/threading/event.cpp @@ -35,7 +35,6 @@ void Event::wait() notified = false; } - void Event::signal() { MutexAutoLock lock(mutex); diff --git a/src/threading/semaphore.cpp b/src/threading/semaphore.cpp index ce22dcd05..8226bd9a0 100644 --- a/src/threading/semaphore.cpp +++ b/src/threading/semaphore.cpp @@ -23,35 +23,37 @@ 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) { @@ -64,7 +66,6 @@ Semaphore::Semaphore(int val) #endif } - Semaphore::~Semaphore() { #ifdef _WIN32 @@ -81,7 +82,6 @@ Semaphore::~Semaphore() #endif } - void Semaphore::post(unsigned int num) { assert(num > 0); @@ -96,7 +96,6 @@ void Semaphore::post(unsigned int num) #endif } - void Semaphore::wait() { #ifdef _WIN32 @@ -108,7 +107,6 @@ void Semaphore::wait() #endif } - bool Semaphore::wait(unsigned int time_ms) { #ifdef _WIN32 @@ -121,7 +119,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); @@ -139,29 +137,33 @@ 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 e0f808c4d..1714f6e8e 100644 --- a/src/threading/thread.cpp +++ b/src/threading/thread.cpp @@ -30,45 +30,42 @@ 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(); @@ -76,10 +73,8 @@ 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); @@ -109,14 +104,12 @@ bool Thread::start() return true; } - bool Thread::stop() { m_request_stop = true; return true; } - bool Thread::wait() { MutexAutoLock lock(m_mutex); @@ -124,7 +117,6 @@ bool Thread::wait() if (!m_joinable) return false; - m_thread_obj->join(); delete m_thread_obj; @@ -135,7 +127,6 @@ bool Thread::wait() return true; } - bool Thread::kill() { if (!m_running) { @@ -146,28 +137,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) @@ -177,7 +168,6 @@ bool Thread::getReturnValue(void **ret) return true; } - void Thread::threadProc(Thread *thr) { #ifdef _AIX @@ -203,7 +193,6 @@ void Thread::threadProc(Thread *thr) g_logger.deregisterThread(); } - void Thread::setName(const std::string &name) { #if defined(__linux__) @@ -237,8 +226,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) { } @@ -248,17 +237,15 @@ 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__) @@ -271,7 +258,8 @@ 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__) @@ -294,8 +282,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__) @@ -304,8 +292,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 @@ -314,7 +302,6 @@ bool Thread::bindToProcessor(unsigned int proc_number) #endif } - bool Thread::setPriority(int prio) { #ifdef _MSC_VER @@ -341,4 +328,3 @@ bool Thread::setPriority(int prio) #endif } - diff --git a/src/threading/thread.h b/src/threading/thread.h index cea92226f..aee2aa357 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,18 +41,17 @@ 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) @@ -141,7 +140,9 @@ 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); @@ -154,11 +155,9 @@ 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 }; - |