00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef STXXL_MUTEX_HEADER
00014 #define STXXL_MUTEX_HEADER
00015
00016 #include <stxxl/bits/namespace.h>
00017
00018 #ifdef STXXL_BOOST_THREADS
00019
00020 #include <boost/thread/mutex.hpp>
00021
00022 #else
00023
00024 #include <pthread.h>
00025
00026 #include <stxxl/bits/noncopyable.h>
00027 #include <stxxl/bits/common/utils.h>
00028
00029 #endif
00030
00031
00032 __STXXL_BEGIN_NAMESPACE
00033
00034 #ifdef STXXL_BOOST_THREADS
00035
00036
00037
00038 #else
00039
00040 class mutex : private noncopyable
00041 {
00042 pthread_mutex_t _mutex;
00043
00044 public:
00045 mutex()
00046 {
00047 check_pthread_call(pthread_mutex_init(&_mutex, NULL));
00048 }
00049
00050 ~mutex()
00051 {
00052 int res = pthread_mutex_trylock(&_mutex);
00053
00054 if (res == 0 || res == EBUSY) {
00055 check_pthread_call(pthread_mutex_unlock(&_mutex));
00056 } else
00057 stxxl_function_error(resource_error);
00058
00059 check_pthread_call(pthread_mutex_destroy(&_mutex));
00060 }
00061 void lock()
00062 {
00063 check_pthread_call(pthread_mutex_lock(&_mutex));
00064 }
00065 void unlock()
00066 {
00067 check_pthread_call(pthread_mutex_unlock(&_mutex));
00068 }
00069 };
00070
00071 #endif
00072
00073 __STXXL_END_NAMESPACE
00074
00075 #endif // !STXXL_MUTEX_HEADER