Browse code

Add spinlock on windows

Steffen Neumann authored on 17/01/2022 13:07:22
Showing6 changed files

1 1
new file mode 100755
... ...
@@ -0,0 +1,304 @@
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
+#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
12
+#define BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
13
+
14
+#ifndef BOOST_CONFIG_HPP
15
+#  include <boost/config.hpp>
16
+#endif
17
+#
18
+#if defined(BOOST_HAS_PRAGMA_ONCE)
19
+#  pragma once
20
+#endif
21
+
22
+#include <boost/interprocess/detail/config_begin.hpp>
23
+#include <boost/interprocess/detail/workaround.hpp>
24
+#include <boost/interprocess/sync/spin/mutex.hpp>
25
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
26
+#include <boost/interprocess/detail/atomic.hpp>
27
+#include <boost/interprocess/sync/scoped_lock.hpp>
28
+#include <boost/interprocess/exceptions.hpp>
29
+#include <boost/interprocess/detail/os_thread_functions.hpp>
30
+#include <boost/interprocess/sync/spin/wait.hpp>
31
+#include <boost/move/utility_core.hpp>
32
+#include <boost/cstdint.hpp>
33
+
34
+namespace boost {
35
+namespace interprocess {
36
+namespace ipcdetail {
37
+
38
+class spin_condition
39
+{
40
+   spin_condition(const spin_condition &);
41
+   spin_condition &operator=(const spin_condition &);
42
+   public:
43
+   spin_condition();
44
+   ~spin_condition();
45
+
46
+   void notify_one();
47
+   void notify_all();
48
+
49
+   template <typename L>
50
+   bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
51
+   {
52
+      if (!lock)
53
+         throw lock_exception();
54
+      //Handle infinity absolute time here to avoid complications in do_timed_wait
55
+      if(abs_time.is_pos_infinity()){
56
+         this->wait(lock);
57
+         return true;
58
+      }
59
+      return this->do_timed_wait(abs_time, *lock.mutex());
60
+   }
61
+
62
+   template <typename L, typename Pr>
63
+   bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
64
+   {
65
+      if (!lock)
66
+         throw lock_exception();
67
+      //Handle infinity absolute time here to avoid complications in do_timed_wait
68
+      if(abs_time.is_pos_infinity()){
69
+         this->wait(lock, pred);
70
+         return true;
71
+      }
72
+      while (!pred()){
73
+         if (!this->do_timed_wait(abs_time, *lock.mutex()))
74
+            return pred();
75
+      }
76
+      return true;
77
+   }
78
+
79
+   template <typename L>
80
+   void wait(L& lock)
81
+   {
82
+      if (!lock)
83
+         throw lock_exception();
84
+      do_wait(*lock.mutex());
85
+   }
86
+
87
+   template <typename L, typename Pr>
88
+   void wait(L& lock, Pr pred)
89
+   {
90
+      if (!lock)
91
+         throw lock_exception();
92
+
93
+      while (!pred())
94
+         do_wait(*lock.mutex());
95
+   }
96
+
97
+   template<class InterprocessMutex>
98
+   void do_wait(InterprocessMutex &mut);
99
+
100
+   template<class InterprocessMutex>
101
+   bool do_timed_wait(const boost::posix_time::ptime &abs_time, InterprocessMutex &mut);
102
+
103
+   private:
104
+   template<class InterprocessMutex>
105
+   bool do_timed_wait(bool tout_enabled, const boost::posix_time::ptime &abs_time, InterprocessMutex &mut);
106
+
107
+   enum { SLEEP = 0, NOTIFY_ONE, NOTIFY_ALL };
108
+   spin_mutex  m_enter_mut;
109
+   volatile boost::uint32_t    m_command;
110
+   volatile boost::uint32_t    m_num_waiters;
111
+   void notify(boost::uint32_t command);
112
+};
113
+
114
+inline spin_condition::spin_condition()
115
+{
116
+   //Note that this class is initialized to zero.
117
+   //So zeroed memory can be interpreted as an initialized
118
+   //condition variable
119
+   m_command      = SLEEP;
120
+   m_num_waiters  = 0;
121
+}
122
+
123
+inline spin_condition::~spin_condition()
124
+{
125
+   //Notify all waiting threads
126
+   //to allow POSIX semantics on condition destruction
127
+   this->notify_all();
128
+}
129
+
130
+inline void spin_condition::notify_one()
131
+{
132
+   this->notify(NOTIFY_ONE);
133
+}
134
+
135
+inline void spin_condition::notify_all()
136
+{
137
+   this->notify(NOTIFY_ALL);
138
+}
139
+
140
+inline void spin_condition::notify(boost::uint32_t command)
141
+{
142
+   //This mutex guarantees that no other thread can enter to the
143
+   //do_timed_wait method logic, so that thread count will be
144
+   //constant until the function writes a NOTIFY_ALL command.
145
+   //It also guarantees that no other notification can be signaled
146
+   //on this spin_condition before this one ends
147
+   m_enter_mut.lock();
148
+
149
+   //Return if there are no waiters
150
+   if(!atomic_read32(&m_num_waiters)) {
151
+      m_enter_mut.unlock();
152
+      return;
153
+   }
154
+
155
+   //Notify that all threads should execute wait logic
156
+   spin_wait swait;
157
+   while(SLEEP != atomic_cas32(const_cast<boost::uint32_t*>(&m_command), command, SLEEP)){
158
+      swait.yield();
159
+   }
160
+   //The enter mutex will rest locked until the last waiting thread unlocks it
161
+}
162
+
163
+template<class InterprocessMutex>
164
+inline void spin_condition::do_wait(InterprocessMutex &mut)
165
+{
166
+   this->do_timed_wait(false, boost::posix_time::ptime(), mut);
167
+}
168
+
169
+template<class InterprocessMutex>
170
+inline bool spin_condition::do_timed_wait
171
+   (const boost::posix_time::ptime &abs_time, InterprocessMutex &mut)
172
+{
173
+   return this->do_timed_wait(true, abs_time, mut);
174
+}
175
+
176
+template<class InterprocessMutex>
177
+inline bool spin_condition::do_timed_wait(bool tout_enabled,
178
+                                     const boost::posix_time::ptime &abs_time,
179
+                                     InterprocessMutex &mut)
180
+{
181
+   boost::posix_time::ptime now = microsec_clock::universal_time();
182
+
183
+   if(tout_enabled){
184
+      if(now >= abs_time) return false;
185
+   }
186
+
187
+   typedef boost::interprocess::scoped_lock<spin_mutex> InternalLock;
188
+   //The enter mutex guarantees that while executing a notification,
189
+   //no other thread can execute the do_timed_wait method.
190
+   {
191
+      //---------------------------------------------------------------
192
+      InternalLock lock;
193
+      if(tout_enabled){
194
+         InternalLock dummy(m_enter_mut, abs_time);
195
+         lock = boost::move(dummy);
196
+      }
197
+      else{
198
+         InternalLock dummy(m_enter_mut);
199
+         lock = boost::move(dummy);
200
+      }
201
+
202
+      if(!lock)
203
+         return false;
204
+      //---------------------------------------------------------------
205
+      //We increment the waiting thread count protected so that it will be
206
+      //always constant when another thread enters the notification logic.
207
+      //The increment marks this thread as "waiting on spin_condition"
208
+      atomic_inc32(const_cast<boost::uint32_t*>(&m_num_waiters));
209
+
210
+      //We unlock the external mutex atomically with the increment
211
+      mut.unlock();
212
+   }
213
+
214
+   //By default, we suppose that no timeout has happened
215
+   bool timed_out  = false, unlock_enter_mut= false;
216
+
217
+   //Loop until a notification indicates that the thread should
218
+   //exit or timeout occurs
219
+   while(1){
220
+      //The thread sleeps/spins until a spin_condition commands a notification
221
+      //Notification occurred, we will lock the checking mutex so that
222
+      spin_wait swait;
223
+      while(atomic_read32(&m_command) == SLEEP){
224
+         swait.yield();
225
+
226
+         //Check for timeout
227
+         if(tout_enabled){
228
+            now = microsec_clock::universal_time();
229
+
230
+            if(now >= abs_time){
231
+               //If we can lock the mutex it means that no notification
232
+               //is being executed in this spin_condition variable
233
+               timed_out = m_enter_mut.try_lock();
234
+
235
+               //If locking fails, indicates that another thread is executing
236
+               //notification, so we play the notification game
237
+               if(!timed_out){
238
+                  //There is an ongoing notification, we will try again later
239
+                  continue;
240
+               }
241
+               //No notification in execution, since enter mutex is locked.
242
+               //We will execute time-out logic, so we will decrement count,
243
+               //release the enter mutex and return false.
244
+               break;
245
+            }
246
+         }
247
+      }
248
+
249
+      //If a timeout occurred, the mutex will not execute checking logic
250
+      if(tout_enabled && timed_out){
251
+         //Decrement wait count
252
+         atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
253
+         unlock_enter_mut = true;
254
+         break;
255
+      }
256
+      else{
257
+         boost::uint32_t result = atomic_cas32
258
+                        (const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ONE);
259
+         if(result == SLEEP){
260
+            //Other thread has been notified and since it was a NOTIFY one
261
+            //command, this thread must sleep again
262
+            continue;
263
+         }
264
+         else if(result == NOTIFY_ONE){
265
+            //If it was a NOTIFY_ONE command, only this thread should
266
+            //exit. This thread has atomically marked command as sleep before
267
+            //so no other thread will exit.
268
+            //Decrement wait count.
269
+            unlock_enter_mut = true;
270
+            atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
271
+            break;
272
+         }
273
+         else{
274
+            //If it is a NOTIFY_ALL command, all threads should return
275
+            //from do_timed_wait function. Decrement wait count.
276
+            unlock_enter_mut = 1 == atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
277
+            //Check if this is the last thread of notify_all waiters
278
+            //Only the last thread will release the mutex
279
+            if(unlock_enter_mut){
280
+               atomic_cas32(const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ALL);
281
+            }
282
+            break;
283
+         }
284
+      }
285
+   }
286
+
287
+   //Unlock the enter mutex if it is a single notification, if this is
288
+   //the last notified thread in a notify_all or a timeout has occurred
289
+   if(unlock_enter_mut){
290
+      m_enter_mut.unlock();
291
+   }
292
+
293
+   //Lock external again before returning from the method
294
+   mut.lock();
295
+   return !timed_out;
296
+}
297
+
298
+}  //namespace ipcdetail
299
+}  //namespace interprocess
300
+}  //namespace boost
301
+
302
+#include <boost/interprocess/detail/config_end.hpp>
303
+
304
+#endif   //BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
0 305
new file mode 100755
... ...
@@ -0,0 +1,54 @@
1
+//////////////////////////////////////////////////////////////////////////////
2
+//
3
+// (C) Copyright Ion Gaztanaga 2006. 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
+#include<boost/interprocess/exceptions.hpp>
12
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
13
+#include <boost/interprocess/sync/scoped_lock.hpp>
14
+
15
+#ifndef BOOST_CONFIG_HPP
16
+#  include <boost/config.hpp>
17
+#endif
18
+#
19
+#if defined(BOOST_HAS_PRAGMA_ONCE)
20
+#  pragma once
21
+#endif
22
+
23
+namespace boost {
24
+namespace interprocess {
25
+
26
+inline barrier::barrier(unsigned int count)
27
+   : m_threshold(count), m_count(count), m_generation(0)
28
+{
29
+   if (count == 0)
30
+      throw std::invalid_argument("count cannot be zero.");
31
+}
32
+
33
+inline barrier::~barrier(){}
34
+
35
+inline bool barrier::wait()
36
+{
37
+   scoped_lock<interprocess_mutex> lock(m_mutex);
38
+   unsigned int gen = m_generation;
39
+
40
+   if (--m_count == 0){
41
+      m_generation++;
42
+      m_count = m_threshold;
43
+      m_cond.notify_all();
44
+      return true;
45
+   }
46
+
47
+   while (gen == m_generation){
48
+      m_cond.wait(lock);
49
+   }
50
+   return false;
51
+}
52
+
53
+}  //namespace interprocess {
54
+}  //namespace boost {
0 55
new file mode 100755
... ...
@@ -0,0 +1,107 @@
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
+#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
12
+#define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
13
+
14
+#ifndef BOOST_CONFIG_HPP
15
+#  include <boost/config.hpp>
16
+#endif
17
+#
18
+#if defined(BOOST_HAS_PRAGMA_ONCE)
19
+#  pragma once
20
+#endif
21
+
22
+#include <boost/interprocess/detail/config_begin.hpp>
23
+#include <boost/interprocess/detail/workaround.hpp>
24
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
25
+#include <boost/assert.hpp>
26
+#include <boost/interprocess/detail/atomic.hpp>
27
+#include <boost/cstdint.hpp>
28
+#include <boost/interprocess/detail/os_thread_functions.hpp>
29
+#include <boost/interprocess/sync/detail/common_algorithms.hpp>
30
+
31
+namespace boost {
32
+namespace interprocess {
33
+namespace ipcdetail {
34
+
35
+class spin_mutex
36
+{
37
+   spin_mutex(const spin_mutex &);
38
+   spin_mutex &operator=(const spin_mutex &);
39
+   public:
40
+
41
+   spin_mutex();
42
+   ~spin_mutex();
43
+
44
+   void lock();
45
+   bool try_lock();
46
+   bool timed_lock(const boost::posix_time::ptime &abs_time);
47
+   void unlock();
48
+   void take_ownership(){}
49
+   private:
50
+   volatile boost::uint32_t m_s;
51
+
52
+   struct common_lock_wrapper
53
+   {
54
+      common_lock_wrapper(spin_mutex &sp)
55
+         : m_sp(sp)
56
+      {}
57
+
58
+      void lock()
59
+      {
60
+         ipcdetail::try_based_lock(m_sp);
61
+      }
62
+
63
+      bool timed_lock(const boost::posix_time::ptime &abs_time)
64
+      {  return m_sp.timed_lock(abs_time);   }
65
+
66
+      spin_mutex &m_sp;
67
+   };
68
+};
69
+
70
+inline spin_mutex::spin_mutex()
71
+   : m_s(0)
72
+{
73
+   //Note that this class is initialized to zero.
74
+   //So zeroed memory can be interpreted as an
75
+   //initialized mutex
76
+}
77
+
78
+inline spin_mutex::~spin_mutex()
79
+{
80
+   //Trivial destructor
81
+}
82
+
83
+inline void spin_mutex::lock(void)
84
+{
85
+   common_lock_wrapper clw(*this);
86
+   ipcdetail::timeout_when_locking_aware_lock(clw);
87
+}
88
+
89
+inline bool spin_mutex::try_lock(void)
90
+{
91
+   boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
92
+   return m_s == 1 && prev_s == 0;
93
+}
94
+
95
+inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
96
+{  return ipcdetail::try_based_timed_lock(*this, abs_time); }
97
+
98
+inline void spin_mutex::unlock(void)
99
+{  ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1);   }
100
+
101
+}  //namespace ipcdetail {
102
+}  //namespace interprocess {
103
+}  //namespace boost {
104
+
105
+#include <boost/interprocess/detail/config_end.hpp>
106
+
107
+#endif   //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
0 108
new file mode 100755
... ...
@@ -0,0 +1,176 @@
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_SPIN_RECURSIVE_MUTEX_HPP
28
+#define BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_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 <boost/interprocess/detail/posix_time_types_wrk.hpp>
42
+#include <boost/interprocess/detail/os_thread_functions.hpp>
43
+#include <boost/interprocess/exceptions.hpp>
44
+#include <boost/interprocess/detail/atomic.hpp>
45
+#include <boost/cstdint.hpp>
46
+#include <boost/interprocess/detail/os_thread_functions.hpp>
47
+#include <boost/interprocess/sync/spin/mutex.hpp>
48
+#include <boost/assert.hpp>
49
+
50
+namespace boost {
51
+namespace interprocess {
52
+namespace ipcdetail {
53
+
54
+class spin_recursive_mutex
55
+{
56
+   spin_recursive_mutex(const spin_recursive_mutex &);
57
+   spin_recursive_mutex &operator=(const spin_recursive_mutex &);
58
+   public:
59
+
60
+   spin_recursive_mutex();
61
+   ~spin_recursive_mutex();
62
+
63
+   void lock();
64
+   bool try_lock();
65
+   bool timed_lock(const boost::posix_time::ptime &abs_time);
66
+   void unlock();
67
+   void take_ownership();
68
+   private:
69
+   spin_mutex     m_mutex;
70
+   unsigned int   m_nLockCount;
71
+   volatile ipcdetail::OS_systemwide_thread_id_t   m_nOwner;
72
+   volatile boost::uint32_t m_s;
73
+};
74
+
75
+inline spin_recursive_mutex::spin_recursive_mutex()
76
+   : m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){}
77
+
78
+inline spin_recursive_mutex::~spin_recursive_mutex(){}
79
+
80
+inline void spin_recursive_mutex::lock()
81
+{
82
+   typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
83
+   const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
84
+   handle_t old_id;
85
+   ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
86
+   if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)){
87
+      if((unsigned int)(m_nLockCount+1) == 0){
88
+         //Overflow, throw an exception
89
+         throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
90
+      }
91
+      ++m_nLockCount;
92
+   }
93
+   else{
94
+      m_mutex.lock();
95
+      ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
96
+      m_nLockCount = 1;
97
+   }
98
+}
99
+
100
+inline bool spin_recursive_mutex::try_lock()
101
+{
102
+   typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
103
+   handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
104
+   handle_t old_id;
105
+   ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
106
+   if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) {  // we own it
107
+      if((unsigned int)(m_nLockCount+1) == 0){
108
+         //Overflow, throw an exception
109
+         throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
110
+      }
111
+      ++m_nLockCount;
112
+      return true;
113
+   }
114
+   if(m_mutex.try_lock()){
115
+      ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
116
+      m_nLockCount = 1;
117
+      return true;
118
+   }
119
+   return false;
120
+}
121
+
122
+inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
123
+{
124
+   typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
125
+   const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
126
+   handle_t old_id;
127
+   ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
128
+   if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) {  // we own it
129
+      if((unsigned int)(m_nLockCount+1) == 0){
130
+         //Overflow, throw an exception
131
+         throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
132
+      }
133
+      ++m_nLockCount;
134
+      return true;
135
+   }
136
+   //m_mutex supports abs_time so no need to check it
137
+   if(m_mutex.timed_lock(abs_time)){
138
+      ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
139
+      m_nLockCount = 1;
140
+      return true;
141
+   }
142
+   return false;
143
+}
144
+
145
+inline void spin_recursive_mutex::unlock()
146
+{
147
+   typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
148
+   handle_t old_id;
149
+   ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
150
+   const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
151
+   (void)old_id;
152
+   (void)thr_id;
153
+   BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id));
154
+   --m_nLockCount;
155
+   if(!m_nLockCount){
156
+      const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id());
157
+      ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner);
158
+      m_mutex.unlock();
159
+   }
160
+}
161
+
162
+inline void spin_recursive_mutex::take_ownership()
163
+{
164
+   typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
165
+   this->m_nLockCount = 1;
166
+   const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
167
+   ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
168
+}
169
+
170
+}  //namespace ipcdetail {
171
+}  //namespace interprocess {
172
+}  //namespace boost {
173
+
174
+#include <boost/interprocess/detail/config_end.hpp>
175
+
176
+#endif   //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
0 177
new file mode 100755
... ...
@@ -0,0 +1,94 @@
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
+#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
12
+#define BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
13
+
14
+#ifndef BOOST_CONFIG_HPP
15
+#  include <boost/config.hpp>
16
+#endif
17
+#
18
+#if defined(BOOST_HAS_PRAGMA_ONCE)
19
+#  pragma once
20
+#endif
21
+
22
+#include <boost/interprocess/detail/config_begin.hpp>
23
+#include <boost/interprocess/detail/workaround.hpp>
24
+#include <boost/interprocess/detail/atomic.hpp>
25
+#include <boost/interprocess/detail/os_thread_functions.hpp>
26
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
27
+#include <boost/interprocess/sync/detail/common_algorithms.hpp>
28
+#include <boost/interprocess/sync/detail/locks.hpp>
29
+#include <boost/cstdint.hpp>
30
+
31
+namespace boost {
32
+namespace interprocess {
33
+namespace ipcdetail {
34
+
35
+class spin_semaphore
36
+{
37
+   spin_semaphore(const spin_semaphore &);
38
+   spin_semaphore &operator=(const spin_semaphore &);
39
+
40
+   public:
41
+   spin_semaphore(unsigned int initialCount);
42
+   ~spin_semaphore();
43
+
44
+   void post();
45
+   void wait();
46
+   bool try_wait();
47
+   bool timed_wait(const boost::posix_time::ptime &abs_time);
48
+
49
+//   int get_count() const;
50
+   private:
51
+   volatile boost::uint32_t m_count;
52
+};
53
+
54
+
55
+inline spin_semaphore::~spin_semaphore()
56
+{}
57
+
58
+inline spin_semaphore::spin_semaphore(unsigned int initialCount)
59
+{  ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount));  }
60
+
61
+inline void spin_semaphore::post()
62
+{
63
+   ipcdetail::atomic_inc32(&m_count);
64
+}
65
+
66
+inline void spin_semaphore::wait()
67
+{
68
+   ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
69
+   return ipcdetail::try_based_lock(lw);
70
+}
71
+
72
+inline bool spin_semaphore::try_wait()
73
+{
74
+   return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
75
+}
76
+
77
+inline bool spin_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
78
+{
79
+   ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
80
+   return ipcdetail::try_based_timed_lock(lw, abs_time);
81
+}
82
+
83
+//inline int spin_semaphore::get_count() const
84
+//{
85
+   //return (int)ipcdetail::atomic_read32(&m_count);
86
+//}
87
+
88
+}  //namespace ipcdetail {
89
+}  //namespace interprocess {
90
+}  //namespace boost {
91
+
92
+#include <boost/interprocess/detail/config_end.hpp>
93
+
94
+#endif   //BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
0 95
new file mode 100755
... ...
@@ -0,0 +1,185 @@
1
+//////////////////////////////////////////////////////////////////////////////
2
+//
3
+// (C) Copyright Peter Dimov 2008.
4
+// (C) Copyright Ion Gaztanaga 2013-2013. Distributed under the Boost
5
+// Software License, Version 1.0. (See accompanying file
6
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
+//
8
+// See http://www.boost.org/libs/interprocess for documentation.
9
+//
10
+//////////////////////////////////////////////////////////////////////////////
11
+
12
+//Parts of this file come from boost/smart_ptr/detail/yield_k.hpp
13
+//Many thanks to Peter Dimov.
14
+
15
+#ifndef BOOST_INTERPROCESS_SYNC_WAIT_HPP_INCLUDED
16
+#define BOOST_INTERPROCESS_SYNC_WAIT_HPP_INCLUDED
17
+
18
+#ifndef BOOST_CONFIG_HPP
19
+#  include <boost/config.hpp>
20
+#endif
21
+#
22
+#if defined(BOOST_HAS_PRAGMA_ONCE)
23
+# pragma once
24
+#endif
25
+
26
+#include <boost/interprocess/detail/config_begin.hpp>
27
+#include <boost/interprocess/detail/workaround.hpp>
28
+#include <boost/interprocess/detail/os_thread_functions.hpp>
29
+
30
+//#define BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
31
+#ifdef BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
32
+#include <iostream>
33
+#endif
34
+
35
+// BOOST_INTERPROCESS_SMT_PAUSE
36
+
37
+#if defined(_MSC_VER) && ( defined(_M_IX86) || defined(_M_X64) )
38
+
39
+extern "C" void _mm_pause();
40
+#pragma intrinsic( _mm_pause )
41
+
42
+#define BOOST_INTERPROCESS_SMT_PAUSE _mm_pause();
43
+
44
+#elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) ) && !defined(_CRAYC)
45
+
46
+#define BOOST_INTERPROCESS_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" );
47
+
48
+#endif
49
+
50
+namespace boost{
51
+namespace interprocess{
52
+namespace ipcdetail {
53
+
54
+template<int Dummy = 0>
55
+class num_core_holder
56
+{
57
+   public:
58
+   static unsigned int get()
59
+   {
60
+      if(!num_cores){
61
+         return ipcdetail::get_num_cores();
62
+      }
63
+      else{
64
+         return num_cores;
65
+      }
66
+   }
67
+
68
+   private:
69
+   static unsigned int num_cores;
70
+};
71
+
72
+template<int Dummy>
73
+unsigned int num_core_holder<Dummy>::num_cores = ipcdetail::get_num_cores();
74
+
75
+}  //namespace ipcdetail {
76
+
77
+class spin_wait
78
+{
79
+   public:
80
+
81
+   static const unsigned int nop_pause_limit = 32u;
82
+   spin_wait()
83
+      : m_count_start(), m_ul_yield_only_counts(), m_k()
84
+   {}
85
+
86
+   #ifdef BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
87
+   ~spin_wait()
88
+   {
89
+      if(m_k){
90
+         std::cout << "final m_k: " << m_k
91
+                   << " system tick(us): " << ipcdetail::get_system_tick_us() << std::endl;
92
+      }
93
+   }
94
+   #endif
95
+
96
+   unsigned int count() const
97
+   {  return m_k;  }
98
+
99
+   void yield()
100
+   {
101
+      //Lazy initialization of limits
102
+      if( !m_k){
103
+         this->init_limits();
104
+      }
105
+      //Nop tries
106
+      if( m_k < (nop_pause_limit >> 2) ){
107
+
108
+      }
109
+      //Pause tries if the processor supports it
110
+      #if defined(BOOST_INTERPROCESS_SMT_PAUSE)
111
+      else if( m_k < nop_pause_limit ){
112
+         BOOST_INTERPROCESS_SMT_PAUSE
113
+      }
114
+      #endif
115
+      //Yield/Sleep strategy
116
+      else{
117
+         //Lazy initialization of tick information
118
+         if(m_k == nop_pause_limit){
119
+            this->init_tick_info();
120
+         }
121
+         else if( this->yield_or_sleep() ){
122
+            ipcdetail::thread_yield();
123
+         }
124
+         else{
125
+            ipcdetail::thread_sleep_tick();
126
+         }
127
+      }
128
+      ++m_k;
129
+   }
130
+
131
+   void reset()
132
+   {
133
+      m_k = 0u;
134
+   }
135
+
136
+   private:
137
+
138
+   void init_limits()
139
+   {
140
+      unsigned int num_cores = ipcdetail::num_core_holder<0>::get();
141
+      m_k = num_cores > 1u ? 0u : nop_pause_limit;
142
+   }
143
+
144
+   void init_tick_info()
145
+   {
146
+      m_ul_yield_only_counts = ipcdetail::get_system_tick_in_highres_counts();
147
+      m_count_start = ipcdetail::get_current_system_highres_count();
148
+   }
149
+
150
+   //Returns true if yield must be called, false is sleep must be called
151
+   bool yield_or_sleep()
152
+   {
153
+      if(!m_ul_yield_only_counts){  //If yield-only limit was reached then yield one in every two tries
154
+         return (m_k & 1u) != 0;
155
+      }
156
+      else{ //Try to see if we've reched yield-only time limit
157
+         const ipcdetail::OS_highres_count_t now = ipcdetail::get_current_system_highres_count();
158
+         const ipcdetail::OS_highres_count_t elapsed = ipcdetail::system_highres_count_subtract(now, m_count_start);
159
+         if(!ipcdetail::system_highres_count_less_ul(elapsed, m_ul_yield_only_counts)){
160
+            #ifdef BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
161
+            std::cout << "elapsed!\n"
162
+                      << "  m_ul_yield_only_counts: " << m_ul_yield_only_counts
163
+                     << " system tick(us): " << ipcdetail::get_system_tick_us() << '\n'
164
+                      << "  m_k: " << m_k << " elapsed counts: ";
165
+                     ipcdetail::ostream_highres_count(std::cout, elapsed) << std::endl;
166
+            #endif
167
+            //Yield-only time reached, now it's time to sleep
168
+            m_ul_yield_only_counts = 0ul;
169
+            return false;
170
+         }
171
+      }
172
+      return true;   //Otherwise yield
173
+   }
174
+
175
+   ipcdetail::OS_highres_count_t m_count_start;
176
+   unsigned long m_ul_yield_only_counts;
177
+   unsigned int  m_k;
178
+};
179
+
180
+} // namespace interprocess
181
+} // namespace boost
182
+
183
+#include <boost/interprocess/detail/config_end.hpp>
184
+
185
+#endif // #ifndef BOOST_INTERPROCESS_SYNC_WAIT_HPP_INCLUDED