14 #ifndef STXXL_STATE_HEADER
15 #define STXXL_STATE_HEADER
17 #ifdef STXXL_BOOST_THREADS
18 #include <boost/thread/mutex.hpp>
19 #include <boost/thread/condition.hpp>
24 #include <stxxl/bits/noncopyable.h>
25 #include <stxxl/bits/common/error_handling.h>
28 __STXXL_BEGIN_NAMESPACE
30 template <
typename Tp =
int>
31 class state :
private noncopyable
33 typedef Tp value_type;
35 #ifdef STXXL_BOOST_THREADS
37 boost::condition cond;
39 pthread_mutex_t mutex;
45 state(value_type s) : _state(s)
47 #ifndef STXXL_BOOST_THREADS
48 check_pthread_call(pthread_mutex_init(&mutex, NULL));
49 check_pthread_call(pthread_cond_init(&cond, NULL));
55 #ifndef STXXL_BOOST_THREADS
56 int res = pthread_mutex_trylock(&mutex);
58 if (res == 0 || res == EBUSY) {
59 check_pthread_call(pthread_mutex_unlock(&mutex));
61 stxxl_function_error(resource_error);
62 check_pthread_call(pthread_mutex_destroy(&mutex));
63 check_pthread_call(pthread_cond_destroy(&cond));
67 void set_to(value_type new_state)
69 #ifdef STXXL_BOOST_THREADS
70 boost::mutex::scoped_lock Lock(mutex);
75 check_pthread_call(pthread_mutex_lock(&mutex));
77 check_pthread_call(pthread_mutex_unlock(&mutex));
78 check_pthread_call(pthread_cond_broadcast(&cond));
82 void wait_for(value_type needed_state)
84 #ifdef STXXL_BOOST_THREADS
85 boost::mutex::scoped_lock Lock(mutex);
86 while (needed_state != _state)
90 check_pthread_call(pthread_mutex_lock(&mutex));
91 while (needed_state != _state)
92 check_pthread_call(pthread_cond_wait(&cond, &mutex));
94 check_pthread_call(pthread_mutex_unlock(&mutex));
98 value_type operator () ()
100 #ifdef STXXL_BOOST_THREADS
101 boost::mutex::scoped_lock Lock(mutex);
105 check_pthread_call(pthread_mutex_lock(&mutex));
107 check_pthread_call(pthread_mutex_unlock(&mutex));
113 __STXXL_END_NAMESPACE
115 #endif // !STXXL_STATE_HEADER