STXXL  1.4.1
 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 
35 
36 #if STXXL_STD_THREADS
37 
38 typedef std::mutex mutex;
39 
40 #elif STXXL_BOOST_THREADS
41 
42 typedef boost::mutex mutex;
43 
44 #elif STXXL_POSIX_THREADS
45 
46 class mutex : private noncopyable
47 {
48  //! mutex handle
49  pthread_mutex_t m_mutex;
50 
51 public:
52  //! construct unlocked mutex
54  {
55  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_init(&m_mutex, NULL));
56  }
57  //! destroy mutex handle
59  {
60  // try simple delete first
61  int res = pthread_mutex_destroy(&m_mutex);
62  if (res == 0) return;
63 
64  // try to lock and unlock mutex
65  res = pthread_mutex_trylock(&m_mutex);
66 
67  if (res == 0 || res == EBUSY) {
68  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_unlock(&m_mutex));
69  } else {
70  STXXL_THROW_ERRNO2(resource_error, "pthread_mutex_trylock() failed", res);
71  }
72 
73  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_destroy(&m_mutex));
74  }
75  //! lock mutex, may block
76  void lock()
77  {
78  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_lock(&m_mutex));
79  }
80  //! unlock mutex
81  void unlock()
82  {
83  STXXL_CHECK_PTHREAD_CALL(pthread_mutex_unlock(&m_mutex));
84  }
85  //! return platform specific handle
86  pthread_mutex_t & native_handle()
87  {
88  return m_mutex;
89  }
90 };
91 
92 #endif
93 
94 #if STXXL_STD_THREADS
95 
96 typedef std::unique_lock<std::mutex> scoped_mutex_lock;
97 
98 #elif STXXL_BOOST_THREADS
99 
100 typedef boost::mutex::scoped_lock scoped_mutex_lock;
101 
102 #else
103 
104 //! Aquire a lock that's valid until the end of scope.
106 {
107  //! mutex reference
109 
110  //! marker if already unlocked by this thread (needs no synchronization)
111  bool is_locked;
112 
113 public:
114  //! lock mutex
116  : m_mutex(m), is_locked(true)
117  {
118  m_mutex.lock();
119  }
120  //! unlock mutex hold when object goes out of scope.
122  {
123  unlock();
124  }
125  //! unlock mutex hold prematurely
126  void unlock()
127  {
128  if (is_locked) {
129  is_locked = false;
130  m_mutex.unlock();
131  }
132  }
133  //! return platform specific handle
134  pthread_mutex_t & native_handle()
135  {
136  return m_mutex.native_handle();
137  }
138 };
139 
140 #endif
141 
143 
144 #endif // !STXXL_COMMON_MUTEX_HEADER
~mutex()
destroy mutex handle
Definition: mutex.h:58
scoped_mutex_lock(mutex &m)
lock mutex
Definition: mutex.h:115
#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:126
bool is_locked
marker if already unlocked by this thread (needs no synchronization)
Definition: mutex.h:111
#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:76
~scoped_mutex_lock()
unlock mutex hold when object goes out of scope.
Definition: mutex.h:121
pthread_mutex_t & native_handle()
return platform specific handle
Definition: mutex.h:134
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:105
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
pthread_mutex_t m_mutex
mutex handle
Definition: mutex.h:49
mutex()
construct unlocked mutex
Definition: mutex.h:53
mutex & m_mutex
mutex reference
Definition: mutex.h:108
void unlock()
unlock mutex
Definition: mutex.h:81
pthread_mutex_t & native_handle()
return platform specific handle
Definition: mutex.h:86
#define STXXL_END_NAMESPACE
Definition: namespace.h:17