13 #ifndef STXXL_STATE_HEADER
14 #define STXXL_STATE_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/utils.h>
27 __STXXL_BEGIN_NAMESPACE
29 class state :
private noncopyable
31 #ifdef STXXL_BOOST_THREADS
33 boost::condition cond;
35 pthread_mutex_t mutex;
41 state(
int s = 0) : _state(s)
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));
61 void set_to(
int new_state)
63 #ifdef STXXL_BOOST_THREADS
64 boost::mutex::scoped_lock Lock(mutex);
69 check_pthread_call(pthread_mutex_lock(&mutex));
71 check_pthread_call(pthread_mutex_unlock(&mutex));
72 check_pthread_call(pthread_cond_broadcast(&cond));
75 void wait_for(
int needed_state)
77 #ifdef STXXL_BOOST_THREADS
78 boost::mutex::scoped_lock Lock(mutex);
79 while (needed_state != _state)
83 check_pthread_call(pthread_mutex_lock(&mutex));
84 while (needed_state != _state)
85 check_pthread_call(pthread_cond_wait(&cond, &mutex));
87 check_pthread_call(pthread_mutex_unlock(&mutex));
92 #ifdef STXXL_BOOST_THREADS
93 boost::mutex::scoped_lock Lock(mutex);
97 check_pthread_call(pthread_mutex_lock(&mutex));
99 check_pthread_call(pthread_mutex_unlock(&mutex));
105 __STXXL_END_NAMESPACE
107 #endif // !STXXL_STATE_HEADER