STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mutex.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/mutex.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2013 Timo Bingmann <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_COMMON_MUTEX_HEADER
16 #define STXXL_COMMON_MUTEX_HEADER
17 
18 #include <stxxl/bits/config.h>
19 #include <stxxl/bits/namespace.h>
20 
21 #if STXXL_STD_THREADS
22  #include <mutex>
23 #elif STXXL_BOOST_THREADS
24  #include <boost/thread/mutex.hpp>
25 #elif STXXL_POSIX_THREADS
26  #include <pthread.h>
27 
28  #include <stxxl/bits/noncopyable.h>
30 #else
31  #error "Thread implementation not detected."
32 #endif
33 
34 
36 
37 #if STXXL_STD_THREADS
38 
39 typedef std::mutex mutex;
40 
41 #elif STXXL_BOOST_THREADS
42 
43 typedef boost::mutex mutex;
44 
45 #elif STXXL_POSIX_THREADS
46 
47 class mutex : private noncopyable
48 {
49  //! mutex handle
50  pthread_mutex_t m_mutex;
51 
52 public:
53  //! construct unlocked mutex
55  {
56  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_init(&m_mutex, NULL));
57  }
58  //! destroy mutex handle
60  {
61  // try simple delete first
62  int res = pthread_mutex_destroy(&m_mutex);
63  if (res == 0) return;
64 
65  // try to lock and unlock mutex
66  res = pthread_mutex_trylock(&m_mutex);
67 
68  if (res == 0 || res == EBUSY) {
69  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_unlock(&m_mutex));
70  } else {
71  STXXL_THROW_ERRNO2(resource_error, "pthread_mutex_trylock() failed", res);
72  }
73 
74  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_destroy(&m_mutex));
75  }
76  //! lock mutex, may block
77  void lock()
78  {
79  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_lock(&m_mutex));
80  }
81  //! unlock mutex
82  void unlock()
83  {
84  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_unlock(&m_mutex));
85  }
86  //! return platform specific handle
87  pthread_mutex_t & native_handle()
88  {
89  return m_mutex;
90  }
91 };
92 
93 #endif
94 
95 #if STXXL_STD_THREADS
96 
97 typedef std::unique_lock<std::mutex> scoped_mutex_lock;
98 
99 #elif STXXL_BOOST_THREADS
100 
101 typedef boost::mutex::scoped_lock scoped_mutex_lock;
102 
103 #else
104 
105 //! Aquire a lock that's valid until the end of scope.
107 {
108  //! mutex reference
110 
111  //! marker if already unlocked by this thread (needs no synchronization)
112  bool is_locked;
113 
114 public:
115  //! lock mutex
117  : m_mutex(m), is_locked(true)
118  {
119  m_mutex.lock();
120  }
121  //! unlock mutex hold when object goes out of scope.
123  {
124  unlock();
125  }
126  //! unlock mutex hold prematurely
127  void unlock()
128  {
129  if (is_locked) {
130  is_locked = false;
131  m_mutex.unlock();
132  }
133  }
134  //! return platform specific handle
135  pthread_mutex_t & native_handle()
136  {
137  return m_mutex.native_handle();
138  }
139 };
140 
141 #endif
142 
144 
145 #endif // !STXXL_COMMON_MUTEX_HEADER
~mutex()
destroy mutex handle
Definition: mutex.h:59
scoped_mutex_lock(mutex &m)
lock mutex
Definition: mutex.h:116
#define STXXL_CHECK_PTHREAD_CALL(expr)
Checks pthread call, if return != 0, throws stxxl::resource_error with "Error in [function] : [pthrea...
void unlock()
unlock mutex hold prematurely
Definition: mutex.h:127
bool is_locked
marker if already unlocked by this thread (needs no synchronization)
Definition: mutex.h:112
#define STXXL_THROW_ERRNO2(exception_type, error_message, errno_value)
Throws exception_type with &quot;Error in [function] : [error_message] : [errno_value message]&quot;.
void lock()
lock mutex, may block
Definition: mutex.h:77
~scoped_mutex_lock()
unlock mutex hold when object goes out of scope.
Definition: mutex.h:122
pthread_mutex_t & native_handle()
return platform specific handle
Definition: mutex.h:135
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
pthread_mutex_t m_mutex
mutex handle
Definition: mutex.h:50
mutex()
construct unlocked mutex
Definition: mutex.h:54
mutex & m_mutex
mutex reference
Definition: mutex.h:109
void unlock()
unlock mutex
Definition: mutex.h:82
pthread_mutex_t & native_handle()
return platform specific handle
Definition: mutex.h:87
#define STXXL_END_NAMESPACE
Definition: namespace.h:17