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