]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/boost/boost/thread/pthread/condition_variable_fwd.hpp
Add subset of boost library headers needed for compilation on PowerPC
[eurobot/public.git] / src / boost / boost / thread / pthread / condition_variable_fwd.hpp
1 #ifndef BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP
2 #define BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // (C) Copyright 2007-8 Anthony Williams
7
8 #include <boost/assert.hpp>
9 #include <boost/throw_exception.hpp>
10 #include <pthread.h>
11 #include <boost/thread/mutex.hpp>
12 #include <boost/thread/locks.hpp>
13 #include <boost/thread/thread_time.hpp>
14 #include <boost/thread/xtime.hpp>
15
16 #include <boost/config/abi_prefix.hpp>
17
18 namespace boost
19 {
20     class condition_variable
21     {
22     private:
23         pthread_mutex_t internal_mutex;
24         pthread_cond_t cond;
25
26         condition_variable(condition_variable&);
27         condition_variable& operator=(condition_variable&);
28
29     public:
30         condition_variable()
31         {
32             int const res=pthread_mutex_init(&internal_mutex,NULL);
33             if(res)
34             {
35                 boost::throw_exception(thread_resource_error());
36             }
37             int const res2=pthread_cond_init(&cond,NULL);
38             if(res2)
39             {
40                 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
41                 boost::throw_exception(thread_resource_error());
42             }
43         }
44         ~condition_variable()
45         {
46             BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
47             int ret;
48             do {
49               ret = pthread_cond_destroy(&cond);
50             } while (ret == EINTR);
51             BOOST_VERIFY(!ret);
52         }
53
54         void wait(unique_lock<mutex>& m);
55
56         template<typename predicate_type>
57         void wait(unique_lock<mutex>& m,predicate_type pred)
58         {
59             while(!pred()) wait(m);
60         }
61
62         inline bool timed_wait(unique_lock<mutex>& m,
63                                boost::system_time const& wait_until);
64         bool timed_wait(unique_lock<mutex>& m,xtime const& wait_until)
65         {
66             return timed_wait(m,system_time(wait_until));
67         }
68
69         template<typename duration_type>
70         bool timed_wait(unique_lock<mutex>& m,duration_type const& wait_duration)
71         {
72             return timed_wait(m,get_system_time()+wait_duration);
73         }
74
75         template<typename predicate_type>
76         bool timed_wait(unique_lock<mutex>& m,boost::system_time const& wait_until,predicate_type pred)
77         {
78             while (!pred())
79             {
80                 if(!timed_wait(m, wait_until))
81                     return pred();
82             }
83             return true;
84         }
85
86         template<typename predicate_type>
87         bool timed_wait(unique_lock<mutex>& m,xtime const& wait_until,predicate_type pred)
88         {
89             return timed_wait(m,system_time(wait_until),pred);
90         }
91
92         template<typename duration_type,typename predicate_type>
93         bool timed_wait(unique_lock<mutex>& m,duration_type const& wait_duration,predicate_type pred)
94         {
95             return timed_wait(m,get_system_time()+wait_duration,pred);
96         }
97
98         typedef pthread_cond_t* native_handle_type;
99         native_handle_type native_handle()
100         {
101             return &cond;
102         }
103
104         void notify_one();
105         void notify_all();
106     };
107 }
108
109 #include <boost/config/abi_suffix.hpp>
110
111 #endif