PageRenderTime 30ms CodeModel.GetById 18ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/src/contrib/boost/thread/pthread/mutex.hpp

http://pythonocc.googlecode.com/
C++ Header | 211 lines | 182 code | 25 blank | 4 comment | 8 complexity | 852b479f168265679655b3b7b630ad8e MD5 | raw file
  1#ifndef BOOST_THREAD_PTHREAD_MUTEX_HPP
  2#define BOOST_THREAD_PTHREAD_MUTEX_HPP
  3// (C) Copyright 2007-8 Anthony Williams
  4// Distributed under the Boost Software License, Version 1.0. (See
  5// accompanying file LICENSE_1_0.txt or copy at
  6// http://www.boost.org/LICENSE_1_0.txt)
  7
  8#include <pthread.h>
  9#include <boost/utility.hpp>
 10#include <boost/throw_exception.hpp>
 11#include <boost/thread/exceptions.hpp>
 12#include <boost/thread/locks.hpp>
 13#include <boost/thread/thread_time.hpp>
 14#include <boost/thread/xtime.hpp>
 15#include <boost/assert.hpp>
 16#include <errno.h>
 17#include "timespec.hpp"
 18#include "pthread_mutex_scoped_lock.hpp"
 19
 20#ifdef _POSIX_TIMEOUTS
 21#if _POSIX_TIMEOUTS >= 0
 22#define BOOST_PTHREAD_HAS_TIMEDLOCK
 23#endif
 24#endif
 25
 26#include <boost/config/abi_prefix.hpp>
 27
 28namespace boost
 29{
 30    class mutex:
 31        boost::noncopyable
 32    {
 33    private:
 34        pthread_mutex_t m;
 35    public:
 36        mutex()
 37        {
 38            int const res=pthread_mutex_init(&m,NULL);
 39            if(res)
 40            {
 41                boost::throw_exception(thread_resource_error());
 42            }
 43        }
 44        ~mutex()
 45        {
 46            BOOST_VERIFY(!pthread_mutex_destroy(&m));
 47        }
 48        
 49        void lock()
 50        {
 51            BOOST_VERIFY(!pthread_mutex_lock(&m));
 52        }
 53
 54        void unlock()
 55        {
 56            BOOST_VERIFY(!pthread_mutex_unlock(&m));
 57        }
 58        
 59        bool try_lock()
 60        {
 61            int const res=pthread_mutex_trylock(&m);
 62            BOOST_ASSERT(!res || res==EBUSY);
 63            return !res;
 64        }
 65
 66        typedef pthread_mutex_t* native_handle_type;
 67        native_handle_type native_handle()
 68        {
 69            return &m;
 70        }
 71
 72        typedef unique_lock<mutex> scoped_lock;
 73        typedef detail::try_lock_wrapper<mutex> scoped_try_lock;
 74    };
 75
 76    typedef mutex try_mutex;
 77
 78    class timed_mutex:
 79        boost::noncopyable
 80    {
 81    private:
 82        pthread_mutex_t m;
 83#ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
 84        pthread_cond_t cond;
 85        bool is_locked;
 86#endif
 87    public:
 88        timed_mutex()
 89        {
 90            int const res=pthread_mutex_init(&m,NULL);
 91            if(res)
 92            {
 93                boost::throw_exception(thread_resource_error());
 94            }
 95#ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
 96            int const res2=pthread_cond_init(&cond,NULL);
 97            if(res2)
 98            {
 99                BOOST_VERIFY(!pthread_mutex_destroy(&m));
100                boost::throw_exception(thread_resource_error());
101            }
102            is_locked=false;
103#endif
104        }
105        ~timed_mutex()
106        {
107            BOOST_VERIFY(!pthread_mutex_destroy(&m));
108#ifndef BOOST_PTHREAD_HAS_TIMEDLOCK
109            BOOST_VERIFY(!pthread_cond_destroy(&cond));
110#endif
111        }
112
113        template<typename TimeDuration>
114        bool timed_lock(TimeDuration const & relative_time)
115        {
116            return timed_lock(get_system_time()+relative_time);
117        }
118        bool timed_lock(boost::xtime const & absolute_time)
119        {
120            return timed_lock(system_time(absolute_time));
121        }
122
123#ifdef BOOST_PTHREAD_HAS_TIMEDLOCK
124        void lock()
125        {
126            BOOST_VERIFY(!pthread_mutex_lock(&m));
127        }
128
129        void unlock()
130        {
131            BOOST_VERIFY(!pthread_mutex_unlock(&m));
132        }
133        
134        bool try_lock()
135        {
136            int const res=pthread_mutex_trylock(&m);
137            BOOST_ASSERT(!res || res==EBUSY);
138            return !res;
139        }
140        bool timed_lock(system_time const & abs_time)
141        {
142            struct timespec const timeout=detail::get_timespec(abs_time);
143            int const res=pthread_mutex_timedlock(&m,&timeout);
144            BOOST_ASSERT(!res || res==ETIMEDOUT);
145            return !res;
146        }
147
148        typedef pthread_mutex_t* native_handle_type;
149        native_handle_type native_handle()
150        {
151            return &m;
152        }
153
154#else
155        void lock()
156        {
157            boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
158            while(is_locked)
159            {
160                BOOST_VERIFY(!pthread_cond_wait(&cond,&m));
161            }
162            is_locked=true;
163        }
164
165        void unlock()
166        {
167            boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
168            is_locked=false;
169            BOOST_VERIFY(!pthread_cond_signal(&cond));
170        }
171        
172        bool try_lock()
173        {
174            boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
175            if(is_locked)
176            {
177                return false;
178            }
179            is_locked=true;
180            return true;
181        }
182
183        bool timed_lock(system_time const & abs_time)
184        {
185            struct timespec const timeout=detail::get_timespec(abs_time);
186            boost::pthread::pthread_mutex_scoped_lock const local_lock(&m);
187            while(is_locked)
188            {
189                int const cond_res=pthread_cond_timedwait(&cond,&m,&timeout);
190                if(cond_res==ETIMEDOUT)
191                {
192                    return false;
193                }
194                BOOST_ASSERT(!cond_res);
195            }
196            is_locked=true;
197            return true;
198        }
199#endif
200
201        typedef unique_lock<timed_mutex> scoped_timed_lock;
202        typedef detail::try_lock_wrapper<timed_mutex> scoped_try_lock;
203        typedef scoped_timed_lock scoped_lock;
204    };
205
206}
207
208#include <boost/config/abi_suffix.hpp>
209
210
211#endif