13 #ifndef STXXL_SEMAPHORE_HEADER
14 #define STXXL_SEMAPHORE_HEADER
16 #ifdef STXXL_BOOST_THREADS
17 #include <boost/thread/mutex.hpp>
18 #include <boost/thread/condition.hpp>
23 #include <stxxl/bits/noncopyable.h>
24 #include <stxxl/bits/common/error_handling.h>
27 __STXXL_BEGIN_NAMESPACE
29 class semaphore :
private noncopyable
32 #ifdef STXXL_BOOST_THREADS
34 boost::condition cond;
36 pthread_mutex_t mutex;
41 semaphore(
int init_value = 1) : v(init_value)
43 #ifndef STXXL_BOOST_THREADS
44 check_pthread_call(pthread_mutex_init(&mutex, NULL));
45 check_pthread_call(pthread_cond_init(&cond, NULL));
50 #ifndef STXXL_BOOST_THREADS
51 int res = pthread_mutex_trylock(&mutex);
53 if (res == 0 || res == EBUSY) {
54 check_pthread_call(pthread_mutex_unlock(&mutex));
56 stxxl_function_error(resource_error);
57 check_pthread_call(pthread_mutex_destroy(&mutex));
58 check_pthread_call(pthread_cond_destroy(&cond));
65 #ifdef STXXL_BOOST_THREADS
66 boost::mutex::scoped_lock Lock(mutex);
71 check_pthread_call(pthread_mutex_lock(&mutex));
73 check_pthread_call(pthread_mutex_unlock(&mutex));
74 check_pthread_call(pthread_cond_signal(&cond));
82 #ifdef STXXL_BOOST_THREADS
83 boost::mutex::scoped_lock Lock(mutex);
89 check_pthread_call(pthread_mutex_lock(&mutex));
91 check_pthread_call(pthread_cond_wait(&cond, &mutex));
94 check_pthread_call(pthread_mutex_unlock(&mutex));
105 #ifdef STXXL_BOOST_THREADS
106 boost::mutex::scoped_lock Lock(mutex);
109 check_pthread_call(pthread_mutex_lock(&mutex));
111 check_pthread_call(pthread_mutex_unlock(&mutex));
127 __STXXL_END_NAMESPACE
129 #endif // !STXXL_SEMAPHORE_HEADER