|
1 // Distributed under the Boost Software License, Version 1.0. (See |
|
2 // accompanying file LICENSE_1_0.txt or copy at |
|
3 // http://www.boost.org/LICENSE_1_0.txt) |
|
4 // (C) Copyright 2007 Anthony Williams |
|
5 #ifndef BOOST_THREAD_LOCKS_HPP |
|
6 #define BOOST_THREAD_LOCKS_HPP |
|
7 #include <boost/thread/detail/config.hpp> |
|
8 #include <boost/thread/exceptions.hpp> |
|
9 #include <boost/thread/detail/move.hpp> |
|
10 #include <algorithm> |
|
11 #include <iterator> |
|
12 #include <boost/thread/thread_time.hpp> |
|
13 #include <boost/detail/workaround.hpp> |
|
14 |
|
15 #include <boost/config/abi_prefix.hpp> |
|
16 |
|
17 namespace boost |
|
18 { |
|
19 struct xtime; |
|
20 |
|
21 #if defined(BOOST_NO_SFINAE) || \ |
|
22 BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) || \ |
|
23 BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590)) |
|
24 #define BOOST_THREAD_NO_AUTO_DETECT_MUTEX_TYPES |
|
25 #endif |
|
26 |
|
27 #ifndef BOOST_THREAD_NO_AUTO_DETECT_MUTEX_TYPES |
|
28 namespace detail |
|
29 { |
|
30 template<typename T> |
|
31 struct has_member_lock |
|
32 { |
|
33 typedef char true_type; |
|
34 struct false_type |
|
35 { |
|
36 true_type dummy[2]; |
|
37 }; |
|
38 |
|
39 template<typename U> |
|
40 static true_type has_member(U*,void (U::*dummy)()=&U::lock); |
|
41 static false_type has_member(void*); |
|
42 |
|
43 BOOST_STATIC_CONSTANT(bool, value=sizeof(has_member_lock<T>::has_member((T*)NULL))==sizeof(true_type)); |
|
44 }; |
|
45 |
|
46 template<typename T> |
|
47 struct has_member_unlock |
|
48 { |
|
49 typedef char true_type; |
|
50 struct false_type |
|
51 { |
|
52 true_type dummy[2]; |
|
53 }; |
|
54 |
|
55 template<typename U> |
|
56 static true_type has_member(U*,void (U::*dummy)()=&U::unlock); |
|
57 static false_type has_member(void*); |
|
58 |
|
59 BOOST_STATIC_CONSTANT(bool, value=sizeof(has_member_unlock<T>::has_member((T*)NULL))==sizeof(true_type)); |
|
60 }; |
|
61 |
|
62 template<typename T> |
|
63 struct has_member_try_lock |
|
64 { |
|
65 typedef char true_type; |
|
66 struct false_type |
|
67 { |
|
68 true_type dummy[2]; |
|
69 }; |
|
70 |
|
71 template<typename U> |
|
72 static true_type has_member(U*,bool (U::*dummy)()=&U::try_lock); |
|
73 static false_type has_member(void*); |
|
74 |
|
75 BOOST_STATIC_CONSTANT(bool, value=sizeof(has_member_try_lock<T>::has_member((T*)NULL))==sizeof(true_type)); |
|
76 }; |
|
77 |
|
78 } |
|
79 |
|
80 |
|
81 template<typename T> |
|
82 struct is_mutex_type |
|
83 { |
|
84 BOOST_STATIC_CONSTANT(bool, value = detail::has_member_lock<T>::value && |
|
85 detail::has_member_unlock<T>::value && |
|
86 detail::has_member_try_lock<T>::value); |
|
87 |
|
88 }; |
|
89 #else |
|
90 template<typename T> |
|
91 struct is_mutex_type |
|
92 { |
|
93 BOOST_STATIC_CONSTANT(bool, value = false); |
|
94 }; |
|
95 #endif |
|
96 |
|
97 struct defer_lock_t |
|
98 {}; |
|
99 struct try_to_lock_t |
|
100 {}; |
|
101 struct adopt_lock_t |
|
102 {}; |
|
103 |
|
104 const defer_lock_t defer_lock={}; |
|
105 const try_to_lock_t try_to_lock={}; |
|
106 const adopt_lock_t adopt_lock={}; |
|
107 |
|
108 template<typename Mutex> |
|
109 class shared_lock; |
|
110 |
|
111 template<typename Mutex> |
|
112 class upgrade_lock; |
|
113 |
|
114 template<typename Mutex> |
|
115 class unique_lock; |
|
116 |
|
117 namespace detail |
|
118 { |
|
119 template<typename Mutex> |
|
120 class try_lock_wrapper; |
|
121 } |
|
122 |
|
123 #ifdef BOOST_THREAD_NO_AUTO_DETECT_MUTEX_TYPES |
|
124 template<typename T> |
|
125 struct is_mutex_type<unique_lock<T> > |
|
126 { |
|
127 BOOST_STATIC_CONSTANT(bool, value = true); |
|
128 }; |
|
129 |
|
130 template<typename T> |
|
131 struct is_mutex_type<shared_lock<T> > |
|
132 { |
|
133 BOOST_STATIC_CONSTANT(bool, value = true); |
|
134 }; |
|
135 |
|
136 template<typename T> |
|
137 struct is_mutex_type<upgrade_lock<T> > |
|
138 { |
|
139 BOOST_STATIC_CONSTANT(bool, value = true); |
|
140 }; |
|
141 |
|
142 template<typename T> |
|
143 struct is_mutex_type<detail::try_lock_wrapper<T> > |
|
144 { |
|
145 BOOST_STATIC_CONSTANT(bool, value = true); |
|
146 }; |
|
147 |
|
148 class mutex; |
|
149 class timed_mutex; |
|
150 class recursive_mutex; |
|
151 class recursive_timed_mutex; |
|
152 class shared_mutex; |
|
153 |
|
154 template<> |
|
155 struct is_mutex_type<mutex> |
|
156 { |
|
157 BOOST_STATIC_CONSTANT(bool, value = true); |
|
158 }; |
|
159 template<> |
|
160 struct is_mutex_type<timed_mutex> |
|
161 { |
|
162 BOOST_STATIC_CONSTANT(bool, value = true); |
|
163 }; |
|
164 template<> |
|
165 struct is_mutex_type<recursive_mutex> |
|
166 { |
|
167 BOOST_STATIC_CONSTANT(bool, value = true); |
|
168 }; |
|
169 template<> |
|
170 struct is_mutex_type<recursive_timed_mutex> |
|
171 { |
|
172 BOOST_STATIC_CONSTANT(bool, value = true); |
|
173 }; |
|
174 template<> |
|
175 struct is_mutex_type<shared_mutex> |
|
176 { |
|
177 BOOST_STATIC_CONSTANT(bool, value = true); |
|
178 }; |
|
179 |
|
180 #endif |
|
181 |
|
182 template<typename Mutex> |
|
183 class lock_guard |
|
184 { |
|
185 private: |
|
186 Mutex& m; |
|
187 |
|
188 explicit lock_guard(lock_guard&); |
|
189 lock_guard& operator=(lock_guard&); |
|
190 public: |
|
191 explicit lock_guard(Mutex& m_): |
|
192 m(m_) |
|
193 { |
|
194 m.lock(); |
|
195 } |
|
196 lock_guard(Mutex& m_,adopt_lock_t): |
|
197 m(m_) |
|
198 {} |
|
199 ~lock_guard() |
|
200 { |
|
201 m.unlock(); |
|
202 } |
|
203 }; |
|
204 |
|
205 |
|
206 template<typename Mutex> |
|
207 class unique_lock |
|
208 { |
|
209 private: |
|
210 Mutex* m; |
|
211 bool is_locked; |
|
212 unique_lock(unique_lock&); |
|
213 explicit unique_lock(upgrade_lock<Mutex>&); |
|
214 unique_lock& operator=(unique_lock&); |
|
215 unique_lock& operator=(upgrade_lock<Mutex>& other); |
|
216 public: |
|
217 unique_lock(): |
|
218 m(0),is_locked(false) |
|
219 {} |
|
220 |
|
221 explicit unique_lock(Mutex& m_): |
|
222 m(&m_),is_locked(false) |
|
223 { |
|
224 lock(); |
|
225 } |
|
226 unique_lock(Mutex& m_,adopt_lock_t): |
|
227 m(&m_),is_locked(true) |
|
228 {} |
|
229 unique_lock(Mutex& m_,defer_lock_t): |
|
230 m(&m_),is_locked(false) |
|
231 {} |
|
232 unique_lock(Mutex& m_,try_to_lock_t): |
|
233 m(&m_),is_locked(false) |
|
234 { |
|
235 try_lock(); |
|
236 } |
|
237 template<typename TimeDuration> |
|
238 unique_lock(Mutex& m_,TimeDuration const& target_time): |
|
239 m(&m_),is_locked(false) |
|
240 { |
|
241 timed_lock(target_time); |
|
242 } |
|
243 unique_lock(Mutex& m_,system_time const& target_time): |
|
244 m(&m_),is_locked(false) |
|
245 { |
|
246 timed_lock(target_time); |
|
247 } |
|
248 #ifdef BOOST_HAS_RVALUE_REFS |
|
249 unique_lock(unique_lock&& other): |
|
250 m(other.m),is_locked(other.is_locked) |
|
251 { |
|
252 other.is_locked=false; |
|
253 other.m=0; |
|
254 } |
|
255 explicit unique_lock(upgrade_lock<Mutex>&& other); |
|
256 |
|
257 unique_lock<Mutex>&& move() |
|
258 { |
|
259 return static_cast<unique_lock<Mutex>&&>(*this); |
|
260 } |
|
261 |
|
262 |
|
263 unique_lock& operator=(unique_lock<Mutex>&& other) |
|
264 { |
|
265 unique_lock temp(other); |
|
266 swap(temp); |
|
267 return *this; |
|
268 } |
|
269 |
|
270 unique_lock& operator=(upgrade_lock<Mutex>&& other) |
|
271 { |
|
272 unique_lock temp(other); |
|
273 swap(temp); |
|
274 return *this; |
|
275 } |
|
276 void swap(unique_lock&& other) |
|
277 { |
|
278 std::swap(m,other.m); |
|
279 std::swap(is_locked,other.is_locked); |
|
280 } |
|
281 #else |
|
282 unique_lock(detail::thread_move_t<unique_lock<Mutex> > other): |
|
283 m(other->m),is_locked(other->is_locked) |
|
284 { |
|
285 other->is_locked=false; |
|
286 other->m=0; |
|
287 } |
|
288 unique_lock(detail::thread_move_t<upgrade_lock<Mutex> > other); |
|
289 |
|
290 operator detail::thread_move_t<unique_lock<Mutex> >() |
|
291 { |
|
292 return move(); |
|
293 } |
|
294 |
|
295 detail::thread_move_t<unique_lock<Mutex> > move() |
|
296 { |
|
297 return detail::thread_move_t<unique_lock<Mutex> >(*this); |
|
298 } |
|
299 |
|
300 unique_lock& operator=(detail::thread_move_t<unique_lock<Mutex> > other) |
|
301 { |
|
302 unique_lock temp(other); |
|
303 swap(temp); |
|
304 return *this; |
|
305 } |
|
306 |
|
307 unique_lock& operator=(detail::thread_move_t<upgrade_lock<Mutex> > other) |
|
308 { |
|
309 unique_lock temp(other); |
|
310 swap(temp); |
|
311 return *this; |
|
312 } |
|
313 void swap(unique_lock& other) |
|
314 { |
|
315 std::swap(m,other.m); |
|
316 std::swap(is_locked,other.is_locked); |
|
317 } |
|
318 void swap(detail::thread_move_t<unique_lock<Mutex> > other) |
|
319 { |
|
320 std::swap(m,other->m); |
|
321 std::swap(is_locked,other->is_locked); |
|
322 } |
|
323 #endif |
|
324 |
|
325 ~unique_lock() |
|
326 { |
|
327 if(owns_lock()) |
|
328 { |
|
329 m->unlock(); |
|
330 } |
|
331 } |
|
332 void lock() |
|
333 { |
|
334 if(owns_lock()) |
|
335 { |
|
336 throw boost::lock_error(); |
|
337 } |
|
338 m->lock(); |
|
339 is_locked=true; |
|
340 } |
|
341 bool try_lock() |
|
342 { |
|
343 if(owns_lock()) |
|
344 { |
|
345 throw boost::lock_error(); |
|
346 } |
|
347 is_locked=m->try_lock(); |
|
348 return is_locked; |
|
349 } |
|
350 template<typename TimeDuration> |
|
351 bool timed_lock(TimeDuration const& relative_time) |
|
352 { |
|
353 is_locked=m->timed_lock(relative_time); |
|
354 return is_locked; |
|
355 } |
|
356 |
|
357 bool timed_lock(::boost::system_time const& absolute_time) |
|
358 { |
|
359 is_locked=m->timed_lock(absolute_time); |
|
360 return is_locked; |
|
361 } |
|
362 bool timed_lock(::boost::xtime const& absolute_time) |
|
363 { |
|
364 is_locked=m->timed_lock(absolute_time); |
|
365 return is_locked; |
|
366 } |
|
367 void unlock() |
|
368 { |
|
369 if(!owns_lock()) |
|
370 { |
|
371 throw boost::lock_error(); |
|
372 } |
|
373 m->unlock(); |
|
374 is_locked=false; |
|
375 } |
|
376 |
|
377 typedef void (unique_lock::*bool_type)(); |
|
378 operator bool_type() const |
|
379 { |
|
380 return is_locked?&unique_lock::lock:0; |
|
381 } |
|
382 bool operator!() const |
|
383 { |
|
384 return !owns_lock(); |
|
385 } |
|
386 bool owns_lock() const |
|
387 { |
|
388 return is_locked; |
|
389 } |
|
390 |
|
391 Mutex* mutex() const |
|
392 { |
|
393 return m; |
|
394 } |
|
395 |
|
396 Mutex* release() |
|
397 { |
|
398 Mutex* const res=m; |
|
399 m=0; |
|
400 is_locked=false; |
|
401 return res; |
|
402 } |
|
403 |
|
404 friend class shared_lock<Mutex>; |
|
405 friend class upgrade_lock<Mutex>; |
|
406 }; |
|
407 |
|
408 #ifdef BOOST_HAS_RVALUE_REFS |
|
409 template<typename Mutex> |
|
410 void swap(unique_lock<Mutex>&& lhs,unique_lock<Mutex>&& rhs) |
|
411 { |
|
412 lhs.swap(rhs); |
|
413 } |
|
414 #else |
|
415 template<typename Mutex> |
|
416 void swap(unique_lock<Mutex>& lhs,unique_lock<Mutex>& rhs) |
|
417 { |
|
418 lhs.swap(rhs); |
|
419 } |
|
420 #endif |
|
421 |
|
422 #ifdef BOOST_HAS_RVALUE_REFS |
|
423 template<typename Mutex> |
|
424 inline unique_lock<Mutex>&& move(unique_lock<Mutex>&& ul) |
|
425 { |
|
426 return ul; |
|
427 } |
|
428 #endif |
|
429 |
|
430 template<typename Mutex> |
|
431 class shared_lock |
|
432 { |
|
433 protected: |
|
434 Mutex* m; |
|
435 bool is_locked; |
|
436 private: |
|
437 explicit shared_lock(shared_lock&); |
|
438 shared_lock& operator=(shared_lock&); |
|
439 public: |
|
440 shared_lock(): |
|
441 m(0),is_locked(false) |
|
442 {} |
|
443 |
|
444 explicit shared_lock(Mutex& m_): |
|
445 m(&m_),is_locked(false) |
|
446 { |
|
447 lock(); |
|
448 } |
|
449 shared_lock(Mutex& m_,adopt_lock_t): |
|
450 m(&m_),is_locked(true) |
|
451 {} |
|
452 shared_lock(Mutex& m_,defer_lock_t): |
|
453 m(&m_),is_locked(false) |
|
454 {} |
|
455 shared_lock(Mutex& m_,try_to_lock_t): |
|
456 m(&m_),is_locked(false) |
|
457 { |
|
458 try_lock(); |
|
459 } |
|
460 shared_lock(Mutex& m_,system_time const& target_time): |
|
461 m(&m_),is_locked(false) |
|
462 { |
|
463 timed_lock(target_time); |
|
464 } |
|
465 |
|
466 shared_lock(detail::thread_move_t<shared_lock<Mutex> > other): |
|
467 m(other->m),is_locked(other->is_locked) |
|
468 { |
|
469 other->is_locked=false; |
|
470 other->m=0; |
|
471 } |
|
472 |
|
473 shared_lock(detail::thread_move_t<unique_lock<Mutex> > other): |
|
474 m(other->m),is_locked(other->is_locked) |
|
475 { |
|
476 if(is_locked) |
|
477 { |
|
478 m->unlock_and_lock_shared(); |
|
479 } |
|
480 other->is_locked=false; |
|
481 other->m=0; |
|
482 } |
|
483 |
|
484 shared_lock(detail::thread_move_t<upgrade_lock<Mutex> > other): |
|
485 m(other->m),is_locked(other->is_locked) |
|
486 { |
|
487 if(is_locked) |
|
488 { |
|
489 m->unlock_upgrade_and_lock_shared(); |
|
490 } |
|
491 other->is_locked=false; |
|
492 other->m=0; |
|
493 } |
|
494 |
|
495 operator detail::thread_move_t<shared_lock<Mutex> >() |
|
496 { |
|
497 return move(); |
|
498 } |
|
499 |
|
500 detail::thread_move_t<shared_lock<Mutex> > move() |
|
501 { |
|
502 return detail::thread_move_t<shared_lock<Mutex> >(*this); |
|
503 } |
|
504 |
|
505 |
|
506 shared_lock& operator=(detail::thread_move_t<shared_lock<Mutex> > other) |
|
507 { |
|
508 shared_lock temp(other); |
|
509 swap(temp); |
|
510 return *this; |
|
511 } |
|
512 |
|
513 shared_lock& operator=(detail::thread_move_t<unique_lock<Mutex> > other) |
|
514 { |
|
515 shared_lock temp(other); |
|
516 swap(temp); |
|
517 return *this; |
|
518 } |
|
519 |
|
520 shared_lock& operator=(detail::thread_move_t<upgrade_lock<Mutex> > other) |
|
521 { |
|
522 shared_lock temp(other); |
|
523 swap(temp); |
|
524 return *this; |
|
525 } |
|
526 |
|
527 #ifdef BOOST_HAS_RVALUE_REFS |
|
528 void swap(shared_lock&& other) |
|
529 { |
|
530 std::swap(m,other.m); |
|
531 std::swap(is_locked,other.is_locked); |
|
532 } |
|
533 #else |
|
534 void swap(shared_lock& other) |
|
535 { |
|
536 std::swap(m,other.m); |
|
537 std::swap(is_locked,other.is_locked); |
|
538 } |
|
539 void swap(boost::detail::thread_move_t<shared_lock<Mutex> > other) |
|
540 { |
|
541 std::swap(m,other->m); |
|
542 std::swap(is_locked,other->is_locked); |
|
543 } |
|
544 #endif |
|
545 |
|
546 Mutex* mutex() const |
|
547 { |
|
548 return m; |
|
549 } |
|
550 |
|
551 ~shared_lock() |
|
552 { |
|
553 if(owns_lock()) |
|
554 { |
|
555 m->unlock_shared(); |
|
556 } |
|
557 } |
|
558 void lock() |
|
559 { |
|
560 if(owns_lock()) |
|
561 { |
|
562 throw boost::lock_error(); |
|
563 } |
|
564 m->lock_shared(); |
|
565 is_locked=true; |
|
566 } |
|
567 bool try_lock() |
|
568 { |
|
569 if(owns_lock()) |
|
570 { |
|
571 throw boost::lock_error(); |
|
572 } |
|
573 is_locked=m->try_lock_shared(); |
|
574 return is_locked; |
|
575 } |
|
576 bool timed_lock(boost::system_time const& target_time) |
|
577 { |
|
578 if(owns_lock()) |
|
579 { |
|
580 throw boost::lock_error(); |
|
581 } |
|
582 is_locked=m->timed_lock_shared(target_time); |
|
583 return is_locked; |
|
584 } |
|
585 template<typename Duration> |
|
586 bool timed_lock(Duration const& target_time) |
|
587 { |
|
588 if(owns_lock()) |
|
589 { |
|
590 throw boost::lock_error(); |
|
591 } |
|
592 is_locked=m->timed_lock_shared(target_time); |
|
593 return is_locked; |
|
594 } |
|
595 void unlock() |
|
596 { |
|
597 if(!owns_lock()) |
|
598 { |
|
599 throw boost::lock_error(); |
|
600 } |
|
601 m->unlock_shared(); |
|
602 is_locked=false; |
|
603 } |
|
604 |
|
605 typedef void (shared_lock<Mutex>::*bool_type)(); |
|
606 operator bool_type() const |
|
607 { |
|
608 return is_locked?&shared_lock::lock:0; |
|
609 } |
|
610 bool operator!() const |
|
611 { |
|
612 return !owns_lock(); |
|
613 } |
|
614 bool owns_lock() const |
|
615 { |
|
616 return is_locked; |
|
617 } |
|
618 |
|
619 }; |
|
620 |
|
621 #ifdef BOOST_HAS_RVALUE_REFS |
|
622 template<typename Mutex> |
|
623 void swap(shared_lock<Mutex>&& lhs,shared_lock<Mutex>&& rhs) |
|
624 { |
|
625 lhs.swap(rhs); |
|
626 } |
|
627 #else |
|
628 template<typename Mutex> |
|
629 void swap(shared_lock<Mutex>& lhs,shared_lock<Mutex>& rhs) |
|
630 { |
|
631 lhs.swap(rhs); |
|
632 } |
|
633 #endif |
|
634 |
|
635 template<typename Mutex> |
|
636 class upgrade_lock |
|
637 { |
|
638 protected: |
|
639 Mutex* m; |
|
640 bool is_locked; |
|
641 private: |
|
642 explicit upgrade_lock(upgrade_lock&); |
|
643 upgrade_lock& operator=(upgrade_lock&); |
|
644 public: |
|
645 upgrade_lock(): |
|
646 m(0),is_locked(false) |
|
647 {} |
|
648 |
|
649 explicit upgrade_lock(Mutex& m_): |
|
650 m(&m_),is_locked(false) |
|
651 { |
|
652 lock(); |
|
653 } |
|
654 upgrade_lock(Mutex& m_,adopt_lock_t): |
|
655 m(&m_),is_locked(true) |
|
656 {} |
|
657 upgrade_lock(Mutex& m_,defer_lock_t): |
|
658 m(&m_),is_locked(false) |
|
659 {} |
|
660 upgrade_lock(Mutex& m_,try_to_lock_t): |
|
661 m(&m_),is_locked(false) |
|
662 { |
|
663 try_lock(); |
|
664 } |
|
665 upgrade_lock(detail::thread_move_t<upgrade_lock<Mutex> > other): |
|
666 m(other->m),is_locked(other->is_locked) |
|
667 { |
|
668 other->is_locked=false; |
|
669 other->m=0; |
|
670 } |
|
671 |
|
672 upgrade_lock(detail::thread_move_t<unique_lock<Mutex> > other): |
|
673 m(other->m),is_locked(other->is_locked) |
|
674 { |
|
675 if(is_locked) |
|
676 { |
|
677 m->unlock_and_lock_upgrade(); |
|
678 } |
|
679 other->is_locked=false; |
|
680 other->m=0; |
|
681 } |
|
682 |
|
683 operator detail::thread_move_t<upgrade_lock<Mutex> >() |
|
684 { |
|
685 return move(); |
|
686 } |
|
687 |
|
688 detail::thread_move_t<upgrade_lock<Mutex> > move() |
|
689 { |
|
690 return detail::thread_move_t<upgrade_lock<Mutex> >(*this); |
|
691 } |
|
692 |
|
693 |
|
694 upgrade_lock& operator=(detail::thread_move_t<upgrade_lock<Mutex> > other) |
|
695 { |
|
696 upgrade_lock temp(other); |
|
697 swap(temp); |
|
698 return *this; |
|
699 } |
|
700 |
|
701 upgrade_lock& operator=(detail::thread_move_t<unique_lock<Mutex> > other) |
|
702 { |
|
703 upgrade_lock temp(other); |
|
704 swap(temp); |
|
705 return *this; |
|
706 } |
|
707 |
|
708 void swap(upgrade_lock& other) |
|
709 { |
|
710 std::swap(m,other.m); |
|
711 std::swap(is_locked,other.is_locked); |
|
712 } |
|
713 |
|
714 ~upgrade_lock() |
|
715 { |
|
716 if(owns_lock()) |
|
717 { |
|
718 m->unlock_upgrade(); |
|
719 } |
|
720 } |
|
721 void lock() |
|
722 { |
|
723 if(owns_lock()) |
|
724 { |
|
725 throw boost::lock_error(); |
|
726 } |
|
727 m->lock_upgrade(); |
|
728 is_locked=true; |
|
729 } |
|
730 bool try_lock() |
|
731 { |
|
732 if(owns_lock()) |
|
733 { |
|
734 throw boost::lock_error(); |
|
735 } |
|
736 is_locked=m->try_lock_upgrade(); |
|
737 return is_locked; |
|
738 } |
|
739 void unlock() |
|
740 { |
|
741 if(!owns_lock()) |
|
742 { |
|
743 throw boost::lock_error(); |
|
744 } |
|
745 m->unlock_upgrade(); |
|
746 is_locked=false; |
|
747 } |
|
748 |
|
749 typedef void (upgrade_lock::*bool_type)(); |
|
750 operator bool_type() const |
|
751 { |
|
752 return is_locked?&upgrade_lock::lock:0; |
|
753 } |
|
754 bool operator!() const |
|
755 { |
|
756 return !owns_lock(); |
|
757 } |
|
758 bool owns_lock() const |
|
759 { |
|
760 return is_locked; |
|
761 } |
|
762 friend class shared_lock<Mutex>; |
|
763 friend class unique_lock<Mutex>; |
|
764 }; |
|
765 |
|
766 |
|
767 #ifdef BOOST_HAS_RVALUE_REFS |
|
768 template<typename Mutex> |
|
769 unique_lock<Mutex>::unique_lock(upgrade_lock<Mutex>&& other): |
|
770 m(other.m),is_locked(other.is_locked) |
|
771 { |
|
772 other.is_locked=false; |
|
773 if(is_locked) |
|
774 { |
|
775 m.unlock_upgrade_and_lock(); |
|
776 } |
|
777 } |
|
778 #else |
|
779 template<typename Mutex> |
|
780 unique_lock<Mutex>::unique_lock(detail::thread_move_t<upgrade_lock<Mutex> > other): |
|
781 m(other->m),is_locked(other->is_locked) |
|
782 { |
|
783 other->is_locked=false; |
|
784 if(is_locked) |
|
785 { |
|
786 m->unlock_upgrade_and_lock(); |
|
787 } |
|
788 } |
|
789 #endif |
|
790 template <class Mutex> |
|
791 class upgrade_to_unique_lock |
|
792 { |
|
793 private: |
|
794 upgrade_lock<Mutex>* source; |
|
795 unique_lock<Mutex> exclusive; |
|
796 |
|
797 explicit upgrade_to_unique_lock(upgrade_to_unique_lock&); |
|
798 upgrade_to_unique_lock& operator=(upgrade_to_unique_lock&); |
|
799 public: |
|
800 explicit upgrade_to_unique_lock(upgrade_lock<Mutex>& m_): |
|
801 source(&m_),exclusive(move(*source)) |
|
802 {} |
|
803 ~upgrade_to_unique_lock() |
|
804 { |
|
805 if(source) |
|
806 { |
|
807 *source=move(exclusive); |
|
808 } |
|
809 } |
|
810 |
|
811 upgrade_to_unique_lock(detail::thread_move_t<upgrade_to_unique_lock<Mutex> > other): |
|
812 source(other->source),exclusive(move(other->exclusive)) |
|
813 { |
|
814 other->source=0; |
|
815 } |
|
816 |
|
817 upgrade_to_unique_lock& operator=(detail::thread_move_t<upgrade_to_unique_lock<Mutex> > other) |
|
818 { |
|
819 upgrade_to_unique_lock temp(other); |
|
820 swap(temp); |
|
821 return *this; |
|
822 } |
|
823 void swap(upgrade_to_unique_lock& other) |
|
824 { |
|
825 std::swap(source,other.source); |
|
826 exclusive.swap(other.exclusive); |
|
827 } |
|
828 typedef void (upgrade_to_unique_lock::*bool_type)(upgrade_to_unique_lock&); |
|
829 operator bool_type() const |
|
830 { |
|
831 return exclusive.owns_lock()?&upgrade_to_unique_lock::swap:0; |
|
832 } |
|
833 bool operator!() const |
|
834 { |
|
835 return !owns_lock(); |
|
836 } |
|
837 bool owns_lock() const |
|
838 { |
|
839 return exclusive.owns_lock(); |
|
840 } |
|
841 }; |
|
842 |
|
843 namespace detail |
|
844 { |
|
845 template<typename Mutex> |
|
846 class try_lock_wrapper: |
|
847 private unique_lock<Mutex> |
|
848 { |
|
849 typedef unique_lock<Mutex> base; |
|
850 public: |
|
851 try_lock_wrapper() |
|
852 {} |
|
853 |
|
854 explicit try_lock_wrapper(Mutex& m): |
|
855 base(m,try_to_lock) |
|
856 {} |
|
857 |
|
858 try_lock_wrapper(Mutex& m_,adopt_lock_t): |
|
859 base(m_,adopt_lock) |
|
860 {} |
|
861 try_lock_wrapper(Mutex& m_,defer_lock_t): |
|
862 base(m_,defer_lock) |
|
863 {} |
|
864 try_lock_wrapper(Mutex& m_,try_to_lock_t): |
|
865 base(m_,try_to_lock) |
|
866 {} |
|
867 try_lock_wrapper(detail::thread_move_t<try_lock_wrapper<Mutex> > other): |
|
868 base(detail::thread_move_t<base>(*other)) |
|
869 {} |
|
870 |
|
871 operator detail::thread_move_t<try_lock_wrapper<Mutex> >() |
|
872 { |
|
873 return move(); |
|
874 } |
|
875 |
|
876 detail::thread_move_t<try_lock_wrapper<Mutex> > move() |
|
877 { |
|
878 return detail::thread_move_t<try_lock_wrapper<Mutex> >(*this); |
|
879 } |
|
880 |
|
881 try_lock_wrapper& operator=(detail::thread_move_t<try_lock_wrapper<Mutex> > other) |
|
882 { |
|
883 try_lock_wrapper temp(other); |
|
884 swap(temp); |
|
885 return *this; |
|
886 } |
|
887 |
|
888 #ifdef BOOST_HAS_RVALUE_REFS |
|
889 void swap(try_lock_wrapper&& other) |
|
890 { |
|
891 base::swap(other); |
|
892 } |
|
893 #else |
|
894 void swap(try_lock_wrapper& other) |
|
895 { |
|
896 base::swap(other); |
|
897 } |
|
898 void swap(detail::thread_move_t<try_lock_wrapper<Mutex> > other) |
|
899 { |
|
900 base::swap(*other); |
|
901 } |
|
902 #endif |
|
903 |
|
904 void lock() |
|
905 { |
|
906 base::lock(); |
|
907 } |
|
908 bool try_lock() |
|
909 { |
|
910 return base::try_lock(); |
|
911 } |
|
912 void unlock() |
|
913 { |
|
914 base::unlock(); |
|
915 } |
|
916 bool owns_lock() const |
|
917 { |
|
918 return base::owns_lock(); |
|
919 } |
|
920 Mutex* mutex() const |
|
921 { |
|
922 return base::mutex(); |
|
923 } |
|
924 Mutex* release() |
|
925 { |
|
926 return base::release(); |
|
927 } |
|
928 bool operator!() const |
|
929 { |
|
930 return !this->owns_lock(); |
|
931 } |
|
932 |
|
933 typedef typename base::bool_type bool_type; |
|
934 operator bool_type() const |
|
935 { |
|
936 return base::operator bool_type(); |
|
937 } |
|
938 }; |
|
939 |
|
940 #ifdef BOOST_HAS_RVALUE_REFS |
|
941 template<typename Mutex> |
|
942 void swap(try_lock_wrapper<Mutex>&& lhs,try_lock_wrapper<Mutex>&& rhs) |
|
943 { |
|
944 lhs.swap(rhs); |
|
945 } |
|
946 #else |
|
947 template<typename Mutex> |
|
948 void swap(try_lock_wrapper<Mutex>& lhs,try_lock_wrapper<Mutex>& rhs) |
|
949 { |
|
950 lhs.swap(rhs); |
|
951 } |
|
952 #endif |
|
953 |
|
954 template<typename MutexType1,typename MutexType2> |
|
955 unsigned try_lock_internal(MutexType1& m1,MutexType2& m2) |
|
956 { |
|
957 boost::unique_lock<MutexType1> l1(m1,boost::try_to_lock); |
|
958 if(!l1) |
|
959 { |
|
960 return 1; |
|
961 } |
|
962 if(!m2.try_lock()) |
|
963 { |
|
964 return 2; |
|
965 } |
|
966 l1.release(); |
|
967 return 0; |
|
968 } |
|
969 |
|
970 template<typename MutexType1,typename MutexType2,typename MutexType3> |
|
971 unsigned try_lock_internal(MutexType1& m1,MutexType2& m2,MutexType3& m3) |
|
972 { |
|
973 boost::unique_lock<MutexType1> l1(m1,boost::try_to_lock); |
|
974 if(!l1) |
|
975 { |
|
976 return 1; |
|
977 } |
|
978 if(unsigned const failed_lock=try_lock_internal(m2,m3)) |
|
979 { |
|
980 return failed_lock+1; |
|
981 } |
|
982 l1.release(); |
|
983 return 0; |
|
984 } |
|
985 |
|
986 |
|
987 template<typename MutexType1,typename MutexType2,typename MutexType3, |
|
988 typename MutexType4> |
|
989 unsigned try_lock_internal(MutexType1& m1,MutexType2& m2,MutexType3& m3, |
|
990 MutexType4& m4) |
|
991 { |
|
992 boost::unique_lock<MutexType1> l1(m1,boost::try_to_lock); |
|
993 if(!l1) |
|
994 { |
|
995 return 1; |
|
996 } |
|
997 if(unsigned const failed_lock=try_lock_internal(m2,m3,m4)) |
|
998 { |
|
999 return failed_lock+1; |
|
1000 } |
|
1001 l1.release(); |
|
1002 return 0; |
|
1003 } |
|
1004 |
|
1005 template<typename MutexType1,typename MutexType2,typename MutexType3, |
|
1006 typename MutexType4,typename MutexType5> |
|
1007 unsigned try_lock_internal(MutexType1& m1,MutexType2& m2,MutexType3& m3, |
|
1008 MutexType4& m4,MutexType5& m5) |
|
1009 { |
|
1010 boost::unique_lock<MutexType1> l1(m1,boost::try_to_lock); |
|
1011 if(!l1) |
|
1012 { |
|
1013 return 1; |
|
1014 } |
|
1015 if(unsigned const failed_lock=try_lock_internal(m2,m3,m4,m5)) |
|
1016 { |
|
1017 return failed_lock+1; |
|
1018 } |
|
1019 l1.release(); |
|
1020 return 0; |
|
1021 } |
|
1022 |
|
1023 |
|
1024 template<typename MutexType1,typename MutexType2> |
|
1025 unsigned lock_helper(MutexType1& m1,MutexType2& m2) |
|
1026 { |
|
1027 boost::unique_lock<MutexType1> l1(m1); |
|
1028 if(!m2.try_lock()) |
|
1029 { |
|
1030 return 1; |
|
1031 } |
|
1032 l1.release(); |
|
1033 return 0; |
|
1034 } |
|
1035 |
|
1036 template<typename MutexType1,typename MutexType2,typename MutexType3> |
|
1037 unsigned lock_helper(MutexType1& m1,MutexType2& m2,MutexType3& m3) |
|
1038 { |
|
1039 boost::unique_lock<MutexType1> l1(m1); |
|
1040 if(unsigned const failed_lock=try_lock_internal(m2,m3)) |
|
1041 { |
|
1042 return failed_lock; |
|
1043 } |
|
1044 l1.release(); |
|
1045 return 0; |
|
1046 } |
|
1047 |
|
1048 template<typename MutexType1,typename MutexType2,typename MutexType3, |
|
1049 typename MutexType4> |
|
1050 unsigned lock_helper(MutexType1& m1,MutexType2& m2,MutexType3& m3, |
|
1051 MutexType4& m4) |
|
1052 { |
|
1053 boost::unique_lock<MutexType1> l1(m1); |
|
1054 if(unsigned const failed_lock=try_lock_internal(m2,m3,m4)) |
|
1055 { |
|
1056 return failed_lock; |
|
1057 } |
|
1058 l1.release(); |
|
1059 return 0; |
|
1060 } |
|
1061 |
|
1062 template<typename MutexType1,typename MutexType2,typename MutexType3, |
|
1063 typename MutexType4,typename MutexType5> |
|
1064 unsigned lock_helper(MutexType1& m1,MutexType2& m2,MutexType3& m3, |
|
1065 MutexType4& m4,MutexType5& m5) |
|
1066 { |
|
1067 boost::unique_lock<MutexType1> l1(m1); |
|
1068 if(unsigned const failed_lock=try_lock_internal(m2,m3,m4,m5)) |
|
1069 { |
|
1070 return failed_lock; |
|
1071 } |
|
1072 l1.release(); |
|
1073 return 0; |
|
1074 } |
|
1075 } |
|
1076 |
|
1077 namespace detail |
|
1078 { |
|
1079 template<bool x> |
|
1080 struct is_mutex_type_wrapper |
|
1081 {}; |
|
1082 |
|
1083 template<typename MutexType1,typename MutexType2> |
|
1084 void lock_impl(MutexType1& m1,MutexType2& m2,is_mutex_type_wrapper<true>) |
|
1085 { |
|
1086 unsigned const lock_count=2; |
|
1087 unsigned lock_first=0; |
|
1088 while(true) |
|
1089 { |
|
1090 switch(lock_first) |
|
1091 { |
|
1092 case 0: |
|
1093 lock_first=detail::lock_helper(m1,m2); |
|
1094 if(!lock_first) |
|
1095 return; |
|
1096 break; |
|
1097 case 1: |
|
1098 lock_first=detail::lock_helper(m2,m1); |
|
1099 if(!lock_first) |
|
1100 return; |
|
1101 lock_first=(lock_first+1)%lock_count; |
|
1102 break; |
|
1103 } |
|
1104 } |
|
1105 } |
|
1106 |
|
1107 template<typename Iterator> |
|
1108 void lock_impl(Iterator begin,Iterator end,is_mutex_type_wrapper<false>); |
|
1109 } |
|
1110 |
|
1111 |
|
1112 template<typename MutexType1,typename MutexType2> |
|
1113 void lock(MutexType1& m1,MutexType2& m2) |
|
1114 { |
|
1115 detail::lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>()); |
|
1116 } |
|
1117 |
|
1118 template<typename MutexType1,typename MutexType2> |
|
1119 void lock(const MutexType1& m1,MutexType2& m2) |
|
1120 { |
|
1121 detail::lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>()); |
|
1122 } |
|
1123 |
|
1124 template<typename MutexType1,typename MutexType2> |
|
1125 void lock(MutexType1& m1,const MutexType2& m2) |
|
1126 { |
|
1127 detail::lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>()); |
|
1128 } |
|
1129 |
|
1130 template<typename MutexType1,typename MutexType2> |
|
1131 void lock(const MutexType1& m1,const MutexType2& m2) |
|
1132 { |
|
1133 detail::lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>()); |
|
1134 } |
|
1135 |
|
1136 template<typename MutexType1,typename MutexType2,typename MutexType3> |
|
1137 void lock(MutexType1& m1,MutexType2& m2,MutexType3& m3) |
|
1138 { |
|
1139 unsigned const lock_count=3; |
|
1140 unsigned lock_first=0; |
|
1141 while(true) |
|
1142 { |
|
1143 switch(lock_first) |
|
1144 { |
|
1145 case 0: |
|
1146 lock_first=detail::lock_helper(m1,m2,m3); |
|
1147 if(!lock_first) |
|
1148 return; |
|
1149 break; |
|
1150 case 1: |
|
1151 lock_first=detail::lock_helper(m2,m3,m1); |
|
1152 if(!lock_first) |
|
1153 return; |
|
1154 lock_first=(lock_first+1)%lock_count; |
|
1155 break; |
|
1156 case 2: |
|
1157 lock_first=detail::lock_helper(m3,m1,m2); |
|
1158 if(!lock_first) |
|
1159 return; |
|
1160 lock_first=(lock_first+2)%lock_count; |
|
1161 break; |
|
1162 } |
|
1163 } |
|
1164 } |
|
1165 |
|
1166 template<typename MutexType1,typename MutexType2,typename MutexType3, |
|
1167 typename MutexType4> |
|
1168 void lock(MutexType1& m1,MutexType2& m2,MutexType3& m3, |
|
1169 MutexType4& m4) |
|
1170 { |
|
1171 unsigned const lock_count=4; |
|
1172 unsigned lock_first=0; |
|
1173 while(true) |
|
1174 { |
|
1175 switch(lock_first) |
|
1176 { |
|
1177 case 0: |
|
1178 lock_first=detail::lock_helper(m1,m2,m3,m4); |
|
1179 if(!lock_first) |
|
1180 return; |
|
1181 break; |
|
1182 case 1: |
|
1183 lock_first=detail::lock_helper(m2,m3,m4,m1); |
|
1184 if(!lock_first) |
|
1185 return; |
|
1186 lock_first=(lock_first+1)%lock_count; |
|
1187 break; |
|
1188 case 2: |
|
1189 lock_first=detail::lock_helper(m3,m4,m1,m2); |
|
1190 if(!lock_first) |
|
1191 return; |
|
1192 lock_first=(lock_first+2)%lock_count; |
|
1193 break; |
|
1194 case 3: |
|
1195 lock_first=detail::lock_helper(m4,m1,m2,m3); |
|
1196 if(!lock_first) |
|
1197 return; |
|
1198 lock_first=(lock_first+3)%lock_count; |
|
1199 break; |
|
1200 } |
|
1201 } |
|
1202 } |
|
1203 |
|
1204 template<typename MutexType1,typename MutexType2,typename MutexType3, |
|
1205 typename MutexType4,typename MutexType5> |
|
1206 void lock(MutexType1& m1,MutexType2& m2,MutexType3& m3, |
|
1207 MutexType4& m4,MutexType5& m5) |
|
1208 { |
|
1209 unsigned const lock_count=5; |
|
1210 unsigned lock_first=0; |
|
1211 while(true) |
|
1212 { |
|
1213 switch(lock_first) |
|
1214 { |
|
1215 case 0: |
|
1216 lock_first=detail::lock_helper(m1,m2,m3,m4,m5); |
|
1217 if(!lock_first) |
|
1218 return; |
|
1219 break; |
|
1220 case 1: |
|
1221 lock_first=detail::lock_helper(m2,m3,m4,m5,m1); |
|
1222 if(!lock_first) |
|
1223 return; |
|
1224 lock_first=(lock_first+1)%lock_count; |
|
1225 break; |
|
1226 case 2: |
|
1227 lock_first=detail::lock_helper(m3,m4,m5,m1,m2); |
|
1228 if(!lock_first) |
|
1229 return; |
|
1230 lock_first=(lock_first+2)%lock_count; |
|
1231 break; |
|
1232 case 3: |
|
1233 lock_first=detail::lock_helper(m4,m5,m1,m2,m3); |
|
1234 if(!lock_first) |
|
1235 return; |
|
1236 lock_first=(lock_first+3)%lock_count; |
|
1237 break; |
|
1238 case 4: |
|
1239 lock_first=detail::lock_helper(m5,m1,m2,m3,m4); |
|
1240 if(!lock_first) |
|
1241 return; |
|
1242 lock_first=(lock_first+4)%lock_count; |
|
1243 break; |
|
1244 } |
|
1245 } |
|
1246 } |
|
1247 |
|
1248 namespace detail |
|
1249 { |
|
1250 template<typename Mutex,bool x=is_mutex_type<Mutex>::value> |
|
1251 struct try_lock_impl_return |
|
1252 { |
|
1253 typedef int type; |
|
1254 }; |
|
1255 |
|
1256 template<typename Iterator> |
|
1257 struct try_lock_impl_return<Iterator,false> |
|
1258 { |
|
1259 typedef Iterator type; |
|
1260 }; |
|
1261 |
|
1262 template<typename MutexType1,typename MutexType2> |
|
1263 int try_lock_impl(MutexType1& m1,MutexType2& m2,is_mutex_type_wrapper<true>) |
|
1264 { |
|
1265 return ((int)detail::try_lock_internal(m1,m2))-1; |
|
1266 } |
|
1267 |
|
1268 template<typename Iterator> |
|
1269 Iterator try_lock_impl(Iterator begin,Iterator end,is_mutex_type_wrapper<false>); |
|
1270 } |
|
1271 |
|
1272 template<typename MutexType1,typename MutexType2> |
|
1273 typename detail::try_lock_impl_return<MutexType1>::type try_lock(MutexType1& m1,MutexType2& m2) |
|
1274 { |
|
1275 return detail::try_lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>()); |
|
1276 } |
|
1277 |
|
1278 template<typename MutexType1,typename MutexType2> |
|
1279 typename detail::try_lock_impl_return<MutexType1>::type try_lock(const MutexType1& m1,MutexType2& m2) |
|
1280 { |
|
1281 return detail::try_lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>()); |
|
1282 } |
|
1283 |
|
1284 template<typename MutexType1,typename MutexType2> |
|
1285 typename detail::try_lock_impl_return<MutexType1>::type try_lock(MutexType1& m1,const MutexType2& m2) |
|
1286 { |
|
1287 return detail::try_lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>()); |
|
1288 } |
|
1289 |
|
1290 template<typename MutexType1,typename MutexType2> |
|
1291 typename detail::try_lock_impl_return<MutexType1>::type try_lock(const MutexType1& m1,const MutexType2& m2) |
|
1292 { |
|
1293 return detail::try_lock_impl(m1,m2,detail::is_mutex_type_wrapper<is_mutex_type<MutexType1>::value>()); |
|
1294 } |
|
1295 |
|
1296 template<typename MutexType1,typename MutexType2,typename MutexType3> |
|
1297 int try_lock(MutexType1& m1,MutexType2& m2,MutexType3& m3) |
|
1298 { |
|
1299 return ((int)detail::try_lock_internal(m1,m2,m3))-1; |
|
1300 } |
|
1301 |
|
1302 template<typename MutexType1,typename MutexType2,typename MutexType3,typename MutexType4> |
|
1303 int try_lock(MutexType1& m1,MutexType2& m2,MutexType3& m3,MutexType4& m4) |
|
1304 { |
|
1305 return ((int)detail::try_lock_internal(m1,m2,m3,m4))-1; |
|
1306 } |
|
1307 |
|
1308 template<typename MutexType1,typename MutexType2,typename MutexType3,typename MutexType4,typename MutexType5> |
|
1309 int try_lock(MutexType1& m1,MutexType2& m2,MutexType3& m3,MutexType4& m4,MutexType5& m5) |
|
1310 { |
|
1311 return ((int)detail::try_lock_internal(m1,m2,m3,m4,m5))-1; |
|
1312 } |
|
1313 |
|
1314 |
|
1315 namespace detail |
|
1316 { |
|
1317 template<typename Iterator> |
|
1318 struct range_lock_guard |
|
1319 { |
|
1320 Iterator begin; |
|
1321 Iterator end; |
|
1322 |
|
1323 range_lock_guard(Iterator begin_,Iterator end_): |
|
1324 begin(begin_),end(end_) |
|
1325 { |
|
1326 lock(begin,end); |
|
1327 } |
|
1328 |
|
1329 void release() |
|
1330 { |
|
1331 begin=end; |
|
1332 } |
|
1333 |
|
1334 ~range_lock_guard() |
|
1335 { |
|
1336 for(;begin!=end;++begin) |
|
1337 { |
|
1338 begin->unlock(); |
|
1339 } |
|
1340 } |
|
1341 }; |
|
1342 |
|
1343 template<typename Iterator> |
|
1344 Iterator try_lock_impl(Iterator begin,Iterator end,is_mutex_type_wrapper<false>) |
|
1345 |
|
1346 { |
|
1347 if(begin==end) |
|
1348 { |
|
1349 return end; |
|
1350 } |
|
1351 typedef typename std::iterator_traits<Iterator>::value_type lock_type; |
|
1352 unique_lock<lock_type> guard(*begin,try_to_lock); |
|
1353 |
|
1354 if(!guard.owns_lock()) |
|
1355 { |
|
1356 return begin; |
|
1357 } |
|
1358 Iterator const failed=try_lock(++begin,end); |
|
1359 if(failed==end) |
|
1360 { |
|
1361 guard.release(); |
|
1362 } |
|
1363 |
|
1364 return failed; |
|
1365 } |
|
1366 } |
|
1367 |
|
1368 |
|
1369 namespace detail |
|
1370 { |
|
1371 template<typename Iterator> |
|
1372 void lock_impl(Iterator begin,Iterator end,is_mutex_type_wrapper<false>) |
|
1373 { |
|
1374 typedef typename std::iterator_traits<Iterator>::value_type lock_type; |
|
1375 |
|
1376 if(begin==end) |
|
1377 { |
|
1378 return; |
|
1379 } |
|
1380 bool start_with_begin=true; |
|
1381 Iterator second=begin; |
|
1382 ++second; |
|
1383 Iterator next=second; |
|
1384 |
|
1385 for(;;) |
|
1386 { |
|
1387 unique_lock<lock_type> begin_lock(*begin,defer_lock); |
|
1388 if(start_with_begin) |
|
1389 { |
|
1390 begin_lock.lock(); |
|
1391 Iterator const failed_lock=try_lock(next,end); |
|
1392 if(failed_lock==end) |
|
1393 { |
|
1394 begin_lock.release(); |
|
1395 return; |
|
1396 } |
|
1397 start_with_begin=false; |
|
1398 next=failed_lock; |
|
1399 } |
|
1400 else |
|
1401 { |
|
1402 detail::range_lock_guard<Iterator> guard(next,end); |
|
1403 if(begin_lock.try_lock()) |
|
1404 { |
|
1405 Iterator const failed_lock=try_lock(second,next); |
|
1406 if(failed_lock==next) |
|
1407 { |
|
1408 begin_lock.release(); |
|
1409 guard.release(); |
|
1410 return; |
|
1411 } |
|
1412 start_with_begin=false; |
|
1413 next=failed_lock; |
|
1414 } |
|
1415 else |
|
1416 { |
|
1417 start_with_begin=true; |
|
1418 next=second; |
|
1419 } |
|
1420 } |
|
1421 } |
|
1422 } |
|
1423 |
|
1424 } |
|
1425 |
|
1426 } |
|
1427 |
|
1428 #include <boost/config/abi_suffix.hpp> |
|
1429 |
|
1430 #endif |