X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Fos_thread.hpp;h=fe7faaa004ae98fc318a07da9ea3aa174ee9fbec;hb=f91056b5782d48d526f19dacd5a988003fc6f11e;hp=7349255525276b47b36c3b5ba5aa7e3c4f4b6d81;hpb=5926761f1682c26266a78f2674e58beff3bfae88;p=apitrace diff --git a/common/os_thread.hpp b/common/os_thread.hpp index 7349255..fe7faaa 100644 --- a/common/os_thread.hpp +++ b/common/os_thread.hpp @@ -32,6 +32,7 @@ #ifndef _OS_THREAD_HPP_ #define _OS_THREAD_HPP_ + #ifdef _WIN32 #include #else @@ -41,6 +42,58 @@ namespace os { + class recursive_mutex + { + public: +#ifdef _WIN32 + typedef CRITICAL_SECTION native_handle_type; +#else + typedef pthread_mutex_t native_handle_type; +#endif + + recursive_mutex(void) { +#ifdef _WIN32 + InitializeCriticalSection(&_native_handle); +#else + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&_native_handle, &attr); + pthread_mutexattr_destroy(&attr); +#endif + } + + ~recursive_mutex() { +#ifdef _WIN32 + DeleteCriticalSection(&_native_handle); +#else + pthread_mutex_destroy(&_native_handle); +#endif + } + + inline void + lock(void) { +#ifdef _WIN32 + EnterCriticalSection(&_native_handle); +#else + pthread_mutex_lock(&_native_handle); +#endif + } + + inline void + unlock(void) { +#ifdef _WIN32 + LeaveCriticalSection(&_native_handle); +#else + pthread_mutex_unlock(&_native_handle); +#endif + } + + private: + native_handle_type _native_handle; + }; + + template class thread_specific_ptr {