00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef STXXL_SWITCH_HEADER
00016 #define STXXL_SWITCH_HEADER
00017
00018 #ifdef STXXL_BOOST_THREADS
00019 #include <boost/thread/mutex.hpp>
00020 #include <boost/thread/condition.hpp>
00021 #else
00022 #include <pthread.h>
00023 #endif
00024
00025 #include <stxxl/bits/noncopyable.h>
00026 #include <stxxl/bits/common/error_handling.h>
00027
00028
00029 __STXXL_BEGIN_NAMESPACE
00030
00031
00032
00033 class onoff_switch : private noncopyable
00034 {
00035 #ifdef STXXL_BOOST_THREADS
00036 boost::mutex mutex;
00037 boost::condition cond;
00038 #else
00039 pthread_mutex_t mutex;
00040 pthread_cond_t cond;
00041 #endif
00042 bool _on;
00043
00044 public:
00045 onoff_switch(bool flag = false) : _on(flag)
00046 {
00047 #ifndef STXXL_BOOST_THREADS
00048 check_pthread_call(pthread_mutex_init(&mutex, NULL));
00049 check_pthread_call(pthread_cond_init(&cond, NULL));
00050 #endif
00051 }
00052 ~onoff_switch()
00053 {
00054 #ifndef STXXL_BOOST_THREADS
00055 int res = pthread_mutex_trylock(&mutex);
00056
00057 if (res == 0 || res == EBUSY) {
00058 check_pthread_call(pthread_mutex_unlock(&mutex));
00059 } else
00060 stxxl_function_error(resource_error);
00061 check_pthread_call(pthread_mutex_destroy(&mutex));
00062
00063
00064 check_pthread_call(pthread_cond_destroy(&cond));
00065 #endif
00066 }
00067 void on()
00068 {
00069 #ifdef STXXL_BOOST_THREADS
00070 boost::mutex::scoped_lock Lock(mutex);
00071 _on = true;
00072 Lock.unlock();
00073 cond.notify_one();
00074 #else
00075 check_pthread_call(pthread_mutex_lock(&mutex));
00076 _on = true;
00077 check_pthread_call(pthread_mutex_unlock(&mutex));
00078 check_pthread_call(pthread_cond_signal(&cond));
00079 #endif
00080 }
00081 void off()
00082 {
00083 #ifdef STXXL_BOOST_THREADS
00084 boost::mutex::scoped_lock Lock(mutex);
00085 _on = false;
00086 Lock.unlock();
00087 cond.notify_one();
00088 #else
00089 check_pthread_call(pthread_mutex_lock(&mutex));
00090 _on = false;
00091 check_pthread_call(pthread_mutex_unlock(&mutex));
00092 check_pthread_call(pthread_cond_signal(&cond));
00093 #endif
00094 }
00095 void wait_for_on()
00096 {
00097 #ifdef STXXL_BOOST_THREADS
00098 boost::mutex::scoped_lock Lock(mutex);
00099 if (!_on)
00100 cond.wait(Lock);
00101
00102 #else
00103 check_pthread_call(pthread_mutex_lock(&mutex));
00104 if (!_on)
00105 check_pthread_call(pthread_cond_wait(&cond, &mutex));
00106
00107 check_pthread_call(pthread_mutex_unlock(&mutex));
00108 #endif
00109 }
00110 void wait_for_off()
00111 {
00112 #ifdef STXXL_BOOST_THREADS
00113 boost::mutex::scoped_lock Lock(mutex);
00114 if (_on)
00115 cond.wait(Lock);
00116
00117 #else
00118 check_pthread_call(pthread_mutex_lock(&mutex));
00119 if (_on)
00120 check_pthread_call(pthread_cond_wait(&cond, &mutex));
00121
00122 check_pthread_call(pthread_mutex_unlock(&mutex));
00123 #endif
00124 }
00125 bool is_on()
00126 {
00127 #ifdef STXXL_BOOST_THREADS
00128 boost::mutex::scoped_lock Lock(mutex);
00129 return _on;
00130 #else
00131 bool res;
00132 check_pthread_call(pthread_mutex_lock(&mutex));
00133 res = _on;
00134 check_pthread_call(pthread_mutex_unlock(&mutex));
00135 return res;
00136 #endif
00137 }
00138 };
00139
00140 __STXXL_END_NAMESPACE
00141
00142 #endif // !STXXL_SWITCH_HEADER