X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Fos_thread.hpp;h=c4bc130d4693b7d9bb201da8675543acdcd4659d;hb=82311849bbba61d0f1c49bd83abb9ffd52033bf2;hp=fe7faaa004ae98fc318a07da9ea3aa174ee9fbec;hpb=e23365e771016117473a2efbbf535372f5a5070e;p=apitrace diff --git a/common/os_thread.hpp b/common/os_thread.hpp index fe7faaa..c4bc130 100644 --- a/common/os_thread.hpp +++ b/common/os_thread.hpp @@ -42,7 +42,10 @@ namespace os { - class recursive_mutex + /** + * Base class for mutex and recursive_mutex. + */ + class _base_mutex { public: #ifdef _WIN32 @@ -51,7 +54,7 @@ namespace os { typedef pthread_mutex_t native_handle_type; #endif - recursive_mutex(void) { + _base_mutex(void) { #ifdef _WIN32 InitializeCriticalSection(&_native_handle); #else @@ -63,7 +66,7 @@ namespace os { #endif } - ~recursive_mutex() { + ~_base_mutex() { #ifdef _WIN32 DeleteCriticalSection(&_native_handle); #else @@ -89,11 +92,56 @@ namespace os { #endif } - private: + native_handle_type & native_handle() { + return _native_handle; + } + + protected: native_handle_type _native_handle; }; + /** + * Same interface as std::mutex. + */ + class mutex : public _base_mutex + { + public: + inline + mutex(void) { +#ifdef _WIN32 + InitializeCriticalSection(&_native_handle); +#else + pthread_mutex_init(&_native_handle, NULL); +#endif + } + }; + + + /** + * Same interface as std::recursive_mutex. + */ + class recursive_mutex : public _base_mutex + { + public: + inline + 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 + } + }; + + + /** + * Same interface as boost::thread_specific_ptr. + */ template class thread_specific_ptr { @@ -158,6 +206,7 @@ namespace os { } }; + } /* namespace os */ #endif /* _OS_THREAD_HPP_ */