• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 //
11 // Parts of the pthread code come from Boost Threads code:
12 //
13 //////////////////////////////////////////////////////////////////////////////
14 //
15 // Copyright (C) 2001-2003
16 // William E. Kempf
17 //
18 // Permission to use, copy, modify, distribute and sell this software
19 // and its documentation for any purpose is hereby granted without fee,
20 // provided that the above copyright notice appear in all copies and
21 // that both that copyright notice and this permission notice appear
22 // in supporting documentation.  William E. Kempf makes no representations
23 // about the suitability of this software for any purpose.
24 // It is provided "as is" without express or implied warranty.
25 //////////////////////////////////////////////////////////////////////////////
26 
27 #ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
28 #define BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
29 
30 #ifndef BOOST_CONFIG_HPP
31 #  include <boost/config.hpp>
32 #endif
33 #
34 #if defined(BOOST_HAS_PRAGMA_ONCE)
35 #  pragma once
36 #endif
37 
38 #include <boost/interprocess/detail/config_begin.hpp>
39 #include <boost/interprocess/detail/workaround.hpp>
40 
41 #include <pthread.h>
42 #include <errno.h>
43 #include <boost/interprocess/exceptions.hpp>
44 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
45 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
46 #include <boost/interprocess/exceptions.hpp>
47 #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
48 
49 #ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
50 #  include <boost/interprocess/detail/os_thread_functions.hpp>
51 #  include <boost/interprocess/sync/detail/common_algorithms.hpp>
52 #endif
53 #include <boost/assert.hpp>
54 
55 namespace boost {
56 namespace interprocess {
57 namespace ipcdetail {
58 
59 class posix_condition;
60 
61 class posix_mutex
62 {
63    posix_mutex(const posix_mutex &);
64    posix_mutex &operator=(const posix_mutex &);
65    public:
66 
67    posix_mutex();
68    ~posix_mutex();
69 
70    void lock();
71    bool try_lock();
72    bool timed_lock(const boost::posix_time::ptime &abs_time);
73    void unlock();
74 
75    friend class posix_condition;
76 
77    private:
78    pthread_mutex_t   m_mut;
79 };
80 
posix_mutex()81 inline posix_mutex::posix_mutex()
82 {
83    mutexattr_wrapper mut_attr;
84    mutex_initializer mut(m_mut, mut_attr);
85    mut.release();
86 }
87 
~posix_mutex()88 inline posix_mutex::~posix_mutex()
89 {
90    int res = pthread_mutex_destroy(&m_mut);
91    BOOST_ASSERT(res  == 0);(void)res;
92 }
93 
lock()94 inline void posix_mutex::lock()
95 {
96    if (pthread_mutex_lock(&m_mut) != 0)
97       throw lock_exception();
98 }
99 
try_lock()100 inline bool posix_mutex::try_lock()
101 {
102    int res = pthread_mutex_trylock(&m_mut);
103    if (!(res == 0 || res == EBUSY))
104       throw lock_exception();
105    return res == 0;
106 }
107 
timed_lock(const boost::posix_time::ptime & abs_time)108 inline bool posix_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
109 {
110    #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
111    //Posix does not support infinity absolute time so handle it here
112    if(abs_time.is_pos_infinity()){
113       this->lock();
114       return true;
115    }
116    timespec ts = ptime_to_timespec(abs_time);
117    int res = pthread_mutex_timedlock(&m_mut, &ts);
118    if (res != 0 && res != ETIMEDOUT)
119       throw lock_exception();
120    return res == 0;
121 
122    #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
123 
124    return ipcdetail::try_based_timed_lock(*this, abs_time);
125 
126    #endif   //BOOST_INTERPROCESS_POSIX_TIMEOUTS
127 }
128 
unlock()129 inline void posix_mutex::unlock()
130 {
131    int res = 0;
132    res = pthread_mutex_unlock(&m_mut);
133    (void)res;
134    BOOST_ASSERT(res == 0);
135 }
136 
137 }  //namespace ipcdetail {
138 }  //namespace interprocess {
139 }  //namespace boost {
140 
141 #include <boost/interprocess/detail/config_end.hpp>
142 
143 #endif   //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
144