/usr/include/boost/thread/synchronized_value.hpp is in libboost1.54-dev 1.54.0-4ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 | // (C) Copyright 2010 Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
// (C) Copyright 2012 Vicente J. Botet Escriba
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_THREAD_SYNCHRONIZED_VALUE_HPP
#define BOOST_THREAD_SYNCHRONIZED_VALUE_HPP
#include <boost/thread/detail/config.hpp>
#include <boost/thread/detail/move.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/lock_types.hpp>
#include <boost/thread/lock_guard.hpp>
#include <boost/thread/lock_algorithms.hpp>
#include <boost/thread/lock_factories.hpp>
#include <boost/thread/strict_lock.hpp>
#include <boost/utility/swap.hpp>
#include <boost/utility/declval.hpp>
//#include <boost/type_traits.hpp>
//#include <boost/thread/detail/is_nothrow_default_constructible.hpp>
//#if ! defined BOOST_NO_CXX11_HDR_TYPE_TRAITS
//#include <type_traits>
//#endif
#if ! defined(BOOST_THREAD_NO_SYNCHRONIZE)
#include <tuple> // todo change to <boost/tuple.hpp> once Boost.Tuple or Boost.Fusion provides Move semantics.
#include <functional>
#endif
#include <boost/config/abi_prefix.hpp>
namespace boost
{
/**
* strict lock providing a const pointer access to the synchronized value type.
*
* @param T the value type.
* @param Lockable the mutex type protecting the value type.
*/
template <typename T, typename Lockable = mutex>
class const_strict_lock_ptr
{
public:
typedef T value_type;
typedef Lockable mutex_type;
protected:
// this should be a strict_lock, but unique_lock is needed to be able to return it.
boost::unique_lock<mutex_type> lk_;
T const& value_;
public:
BOOST_THREAD_MOVABLE_ONLY( const_strict_lock_ptr )
/**
* @param value constant reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
*/
const_strict_lock_ptr(T const& val, Lockable & mtx) :
lk_(mtx), value_(val)
{
}
const_strict_lock_ptr(T const& val, Lockable & mtx, adopt_lock_t tag) BOOST_NOEXCEPT :
lk_(mtx, tag), value_(val)
{
}
/**
* Move constructor.
* @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
*/
const_strict_lock_ptr(BOOST_THREAD_RV_REF(const_strict_lock_ptr) other) BOOST_NOEXCEPT
: lk_(boost::move(BOOST_THREAD_RV(other).lk_)),value_(BOOST_THREAD_RV(other).value_)
{
}
~const_strict_lock_ptr()
{
}
/**
* @return a constant pointer to the protected value
*/
const T* operator->() const
{
return &value_;
}
/**
* @return a constant reference to the protected value
*/
const T& operator*() const
{
return value_;
}
};
/**
* strict lock providing a pointer access to the synchronized value type.
*
* @param T the value type.
* @param Lockable the mutex type protecting the value type.
*/
template <typename T, typename Lockable = mutex>
class strict_lock_ptr : public const_strict_lock_ptr<T,Lockable>
{
typedef const_strict_lock_ptr<T,Lockable> base_type;
public:
BOOST_THREAD_MOVABLE_ONLY( strict_lock_ptr )
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
*/
strict_lock_ptr(T & val, Lockable & mtx) :
base_type(val, mtx)
{
}
strict_lock_ptr(T & val, Lockable & mtx, adopt_lock_t tag) :
base_type(val, mtx, tag)
{
}
/**
* Move constructor.
* @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
*/
strict_lock_ptr(BOOST_THREAD_RV_REF(strict_lock_ptr) other)
: base_type(boost::move(static_cast<base_type&>(other)))
{
}
~strict_lock_ptr()
{
}
/**
* @return a pointer to the protected value
*/
T* operator->()
{
return const_cast<T*>(&this->value_);
}
/**
* @return a reference to the protected value
*/
T& operator*()
{
return const_cast<T&>(this->value_);
}
};
template <typename SV>
struct synchronized_value_strict_lock_ptr
{
typedef strict_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
};
template <typename SV>
struct synchronized_value_strict_lock_ptr<const SV>
{
typedef const_strict_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
};
/**
* unique_lock providing a const pointer access to the synchronized value type.
*
* An object of type const_unique_lock_ptr is a unique_lock that provides a const pointer access to the synchronized value type.
* As unique_lock controls the ownership of a lockable object within a scope.
* Ownership of the lockable object may be acquired at construction or after construction,
* and may be transferred, after acquisition, to another const_unique_lock_ptr object.
* Objects of type const_unique_lock_ptr are not copyable but are movable.
* The behavior of a program is undefined if the mutex and the value type
* pointed do not exist for the entire remaining lifetime of the const_unique_lock_ptr object.
* The supplied Mutex type shall meet the BasicLockable requirements.
*
* @note const_unique_lock_ptr<T, Lockable> meets the Lockable requirements.
* If Lockable meets the TimedLockable requirements, const_unique_lock_ptr<T,Lockable>
* also meets the TimedLockable requirements.
*
* @param T the value type.
* @param Lockable the mutex type protecting the value type.
*/
template <typename T, typename Lockable = mutex>
class const_unique_lock_ptr : public unique_lock<Lockable>
{
typedef unique_lock<Lockable> base_type;
public:
typedef T value_type;
typedef Lockable mutex_type;
protected:
T const& value_;
public:
BOOST_THREAD_MOVABLE_ONLY(const_unique_lock_ptr)
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
*
* @requires If mutex_type is not a recursive mutex the calling thread does not own the mutex.
*
* @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
*/
const_unique_lock_ptr(T const& val, Lockable & mtx)
: base_type(mtx), value_(val)
{
}
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @param tag of type adopt_lock_t used to differentiate the constructor.
* @requires The calling thread own the mutex.
* @effects stores a reference to it and to the value type @c value taking ownership.
*/
const_unique_lock_ptr(T const& val, Lockable & mtx, adopt_lock_t) BOOST_NOEXCEPT
: base_type(mtx, adopt_lock), value_(val)
{
}
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @param tag of type defer_lock_t used to differentiate the constructor.
* @effects stores a reference to it and to the value type @c value c.
*/
const_unique_lock_ptr(T const& val, Lockable & mtx, defer_lock_t) BOOST_NOEXCEPT
: base_type(mtx, defer_lock), value_(val)
{
}
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @param tag of type try_to_lock_t used to differentiate the constructor.
* @requires If mutex_type is not a recursive mutex the calling thread does not own the mutex.
* @effects try to lock the mutex @c mtx, stores a reference to it and to the value type @c value.
*/
const_unique_lock_ptr(T const& val, Lockable & mtx, try_to_lock_t) BOOST_NOEXCEPT
: base_type(mtx, try_to_lock), value_(val)
{
}
/**
* Move constructor.
* @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
*/
const_unique_lock_ptr(BOOST_THREAD_RV_REF(const_unique_lock_ptr) other) BOOST_NOEXCEPT
: base_type(boost::move(static_cast<base_type&>(other))), value_(BOOST_THREAD_RV(other).value_)
{
}
/**
* @effects If owns calls unlock() on the owned mutex.
*/
~const_unique_lock_ptr()
{
}
/**
* @return a constant pointer to the protected value
*/
const T* operator->() const
{
BOOST_ASSERT (this->owns_lock());
return &value_;
}
/**
* @return a constant reference to the protected value
*/
const T& operator*() const
{
BOOST_ASSERT (this->owns_lock());
return value_;
}
};
/**
* unique lock providing a pointer access to the synchronized value type.
*
* @param T the value type.
* @param Lockable the mutex type protecting the value type.
*/
template <typename T, typename Lockable = mutex>
class unique_lock_ptr : public const_unique_lock_ptr<T, Lockable>
{
typedef const_unique_lock_ptr<T, Lockable> base_type;
public:
typedef T value_type;
typedef Lockable mutex_type;
BOOST_THREAD_MOVABLE_ONLY(unique_lock_ptr)
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @effects locks the mutex @c mtx, stores a reference to it and to the value type @c value.
*/
unique_lock_ptr(T & val, Lockable & mtx)
: base_type(val, mtx)
{
}
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @param tag of type adopt_lock_t used to differentiate the constructor.
* @effects stores a reference to it and to the value type @c value taking ownership.
*/
unique_lock_ptr(T & value, Lockable & mtx, adopt_lock_t) BOOST_NOEXCEPT
: base_type(value, mtx, adopt_lock)
{
}
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @param tag of type defer_lock_t used to differentiate the constructor.
* @effects stores a reference to it and to the value type @c value c.
*/
unique_lock_ptr(T & value, Lockable & mtx, defer_lock_t) BOOST_NOEXCEPT
: base_type(value, mtx, defer_lock)
{
}
/**
* @param value reference of the value to protect.
* @param mtx reference to the mutex used to protect the value.
* @param tag of type try_to_lock_t used to differentiate the constructor.
* @effects try to lock the mutex @c mtx, stores a reference to it and to the value type @c value.
*/
unique_lock_ptr(T & value, Lockable & mtx, try_to_lock_t) BOOST_NOEXCEPT
: base_type(value, mtx, try_to_lock)
{
}
/**
* Move constructor.
* @effects takes ownership of the mutex owned by @c other, stores a reference to the mutex and the value type of @c other.
*/
unique_lock_ptr(BOOST_THREAD_RV_REF(unique_lock_ptr) other) BOOST_NOEXCEPT
: base_type(boost::move(static_cast<base_type&>(other)))
{
}
~unique_lock_ptr()
{
}
/**
* @return a pointer to the protected value
*/
T* operator->()
{
BOOST_ASSERT (this->owns_lock());
return const_cast<T*>(&this->value_);
}
/**
* @return a reference to the protected value
*/
T& operator*()
{
BOOST_ASSERT (this->owns_lock());
return const_cast<T&>(this->value_);
}
};
template <typename SV>
struct synchronized_value_unique_lock_ptr
{
typedef unique_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
};
template <typename SV>
struct synchronized_value_unique_lock_ptr<const SV>
{
typedef const_unique_lock_ptr<typename SV::value_type, typename SV::mutex_type> type;
};
/**
* cloaks a value type and the mutex used to protect it together.
* @param T the value type.
* @param Lockable the mutex type protecting the value type.
*/
template <typename T, typename Lockable = mutex>
class synchronized_value
{
#if ! defined(BOOST_THREAD_NO_MAKE_UNIQUE_LOCKS)
#if ! defined BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <typename ...SV>
friend std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> synchronize(SV& ...sv);
#else
template <typename SV1, typename SV2>
friend std::tuple<
typename synchronized_value_strict_lock_ptr<SV1>::type,
typename synchronized_value_strict_lock_ptr<SV2>::type
>
synchronize(SV1& sv1, SV2& sv2);
template <typename SV1, typename SV2, typename SV3>
friend std::tuple<
typename synchronized_value_strict_lock_ptr<SV1>::type,
typename synchronized_value_strict_lock_ptr<SV2>::type,
typename synchronized_value_strict_lock_ptr<SV3>::type
>
synchronize(SV1& sv1, SV2& sv2, SV3& sv3);
#endif
#endif
public:
typedef T value_type;
typedef Lockable mutex_type;
private:
T value_;
mutable mutex_type mtx_;
public:
// construction/destruction
/**
* Default constructor.
*
* @Requires: T is DefaultConstructible
*/
synchronized_value()
//BOOST_NOEXCEPT_IF(is_nothrow_default_constructible<T>::value)
: value_()
{
}
/**
* Constructor from copy constructible value.
*
* Requires: T is CopyConstructible
*/
synchronized_value(T const& other)
//BOOST_NOEXCEPT_IF(is_nothrow_copy_constructible<T>::value)
: value_(other)
{
}
/**
* Move Constructor.
*
* Requires: T is CopyMovable
*/
synchronized_value(BOOST_THREAD_RV_REF(T) other)
//BOOST_NOEXCEPT_IF(is_nothrow_move_constructible<T>::value)
: value_(boost::move(other))
{
}
/**
* Constructor from value type.
*
* Requires: T is DefaultConstructible and Assignable
* Effects: Assigns the value on a scope protected by the mutex of the rhs. The mutex is not copied.
*/
synchronized_value(synchronized_value const& rhs)
{
strict_lock<mutex_type> lk(rhs.mtx_);
value_ = rhs.value_;
}
/**
* Move Constructor from movable value type
*
*/
synchronized_value(BOOST_THREAD_RV_REF(synchronized_value) other)
{
strict_lock<mutex_type> lk(other.mtx_);
value_= boost::move(other.value_);
}
// mutation
/**
* Assignment operator.
*
* Effects: Copies the underlying value on a scope protected by the two mutexes.
* The mutex is not copied. The locks are acquired using lock, so deadlock is avoided.
* For example, there is no problem if one thread assigns a = b and the other assigns b = a.
*
* Return: *this
*/
synchronized_value& operator=(synchronized_value const& rhs)
{
if(&rhs != this)
{
// auto _ = make_unique_locks(mtx_, rhs.mtx_);
unique_lock<mutex_type> lk1(mtx_, defer_lock);
unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
lock(lk1,lk2);
value_ = rhs.value_;
}
return *this;
}
/**
* Assignment operator from a T const&.
* Effects: The operator copies the value on a scope protected by the mutex.
* Return: *this
*/
synchronized_value& operator=(value_type const& val)
{
{
strict_lock<mutex_type> lk(mtx_);
value_ = val;
}
return *this;
}
//observers
/**
* Explicit conversion to value type.
*
* Requires: T is CopyConstructible
* Return: A copy of the protected value obtained on a scope protected by the mutex.
*
*/
T get() const
{
strict_lock<mutex_type> lk(mtx_);
return value_;
}
/**
* Explicit conversion to value type.
*
* Requires: T is CopyConstructible
* Return: A copy of the protected value obtained on a scope protected by the mutex.
*
*/
#if ! defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
explicit operator T() const
{
return get();
}
#endif
/**
* value type getter.
*
* Return: A constant reference to the protected value.
*
* Note: Not thread safe
*
*/
T const& value() const
{
return value_;
}
/**
* mutex getter.
*
* Return: A constant reference to the protecting mutex.
*
* Note: Not thread safe
*
*/
mutex_type const& mutex() const
{
return mtx_;
}
/**
* Swap
*
* Effects: Swaps the data. Again, locks are acquired using lock(). The mutexes are not swapped.
* A swap method accepts a T& and swaps the data inside a critical section.
* This is by far the preferred method of changing the guarded datum wholesale because it keeps the lock only
* for a short time, thus lowering the pressure on the mutex.
*/
void swap(synchronized_value & rhs)
{
if (this == &rhs) {
return;
}
// auto _ = make_unique_locks(mtx_, rhs.mtx_);
unique_lock<mutex_type> lk1(mtx_, defer_lock);
unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
lock(lk1,lk2);
boost::swap(value_, rhs.value_);
}
/**
* Swap with the underlying value type
*
* Effects: Swaps the data on a scope protected by the mutex.
*/
void swap(value_type & rhs)
{
strict_lock<mutex_type> lk(mtx_);
boost::swap(value_, rhs);
}
/**
* Essentially calling a method obj->foo(x, y, z) calls the method foo(x, y, z) inside a critical section as
* long-lived as the call itself.
*/
strict_lock_ptr<T,Lockable> operator->()
{
return BOOST_THREAD_MAKE_RV_REF((strict_lock_ptr<T,Lockable>(value_, mtx_)));
}
/**
* If the synchronized_value object involved is const-qualified, then you'll only be able to call const methods
* through operator->. So, for example, vec->push_back("xyz") won't work if vec were const-qualified.
* The locking mechanism capitalizes on the assumption that const methods don't modify their underlying data.
*/
const_strict_lock_ptr<T,Lockable> operator->() const
{
return BOOST_THREAD_MAKE_RV_REF((const_strict_lock_ptr<T,Lockable>(value_, mtx_)));
}
/**
* The synchronize() factory make easier to lock on a scope.
* As discussed, operator-> can only lock over the duration of a call, so it is insufficient for complex operations.
* With synchronize() you get to lock the object in a scoped and to directly access the object inside that scope.
*
* Example
* void fun(synchronized_value<vector<int>> & vec) {
* auto&& vec=vec.synchronize();
* vec.push_back(42);
* assert(vec.back() == 42);
* }
*/
strict_lock_ptr<T,Lockable> synchronize()
{
return BOOST_THREAD_MAKE_RV_REF((strict_lock_ptr<T,Lockable>(value_, mtx_)));
}
const_strict_lock_ptr<T,Lockable> synchronize() const
{
return BOOST_THREAD_MAKE_RV_REF((const_strict_lock_ptr<T,Lockable>(value_, mtx_)));
}
unique_lock_ptr<T,Lockable> unique_synchronize()
{
return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_)));
}
const_unique_lock_ptr<T,Lockable> unique_synchronize() const
{
return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_)));
}
unique_lock_ptr<T,Lockable> unique_synchronize(defer_lock_t tag)
{
return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, tag)));
}
const_unique_lock_ptr<T,Lockable> unique_synchronize(defer_lock_t tag) const
{
return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, tag)));
}
unique_lock_ptr<T,Lockable> defer_synchronize() BOOST_NOEXCEPT
{
return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, defer_lock)));
}
const_unique_lock_ptr<T,Lockable> defer_synchronize() const BOOST_NOEXCEPT
{
return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, defer_lock)));
}
unique_lock_ptr<T,Lockable> try_to_synchronize() BOOST_NOEXCEPT
{
return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, try_to_lock)));
}
const_unique_lock_ptr<T,Lockable> try_to_synchronize() const BOOST_NOEXCEPT
{
return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, try_to_lock)));
}
unique_lock_ptr<T,Lockable> adopt_synchronize() BOOST_NOEXCEPT
{
return BOOST_THREAD_MAKE_RV_REF((unique_lock_ptr<T,Lockable>(value_, mtx_, adopt_lock)));
}
const_unique_lock_ptr<T,Lockable> adopt_synchronize() const BOOST_NOEXCEPT
{
return BOOST_THREAD_MAKE_RV_REF((const_unique_lock_ptr<T,Lockable>(value_, mtx_, adopt_lock)));
}
#if ! defined __IBMCPP__
private:
#endif
class deref_value
{
private:
friend class synchronized_value;
boost::unique_lock<mutex_type> lk_;
T& value_;
explicit deref_value(synchronized_value& outer):
lk_(outer.mtx_),value_(outer.value_)
{}
public:
BOOST_THREAD_MOVABLE_ONLY(deref_value)
deref_value(BOOST_THREAD_RV_REF(deref_value) other):
lk_(boost::move(BOOST_THREAD_RV(other).lk_)),value_(BOOST_THREAD_RV(other).value_)
{}
operator T&()
{
return value_;
}
deref_value& operator=(T const& newVal)
{
value_=newVal;
return *this;
}
};
class const_deref_value
{
private:
friend class synchronized_value;
boost::unique_lock<mutex_type> lk_;
const T& value_;
explicit const_deref_value(synchronized_value const& outer):
lk_(outer.mtx_), value_(outer.value_)
{}
public:
BOOST_THREAD_MOVABLE_ONLY(const_deref_value)
const_deref_value(BOOST_THREAD_RV_REF(const_deref_value) other):
lk_(boost::move(BOOST_THREAD_RV(other).lk_)), value_(BOOST_THREAD_RV(other).value_)
{}
operator const T&()
{
return value_;
}
};
public:
deref_value operator*()
{
return BOOST_THREAD_MAKE_RV_REF(deref_value(*this));
}
const_deref_value operator*() const
{
return BOOST_THREAD_MAKE_RV_REF(const_deref_value(*this));
}
// io functions
/**
* @requires T is OutputStreamable
* @effects saves the value type on the output stream @c os.
*/
template <typename OStream>
void save(OStream& os) const
{
strict_lock<mutex_type> lk(mtx_);
os << value_;
}
/**
* @requires T is InputStreamable
* @effects loads the value type from the input stream @c is.
*/
template <typename IStream>
void load(IStream& is) const
{
strict_lock<mutex_type> lk(mtx_);
is >> value_;
}
// relational operators
/**
* @requires T is EqualityComparable
*
*/
bool operator==(synchronized_value const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_, defer_lock);
unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
lock(lk1,lk2);
return value_ == rhs.value_;
}
/**
* @requires T is LessThanComparable
*
*/
bool operator<(synchronized_value const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_, defer_lock);
unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
lock(lk1,lk2);
return value_ < rhs.value_;
}
/**
* @requires T is GreaterThanComparable
*
*/
bool operator>(synchronized_value const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_, defer_lock);
unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
lock(lk1,lk2);
return value_ > rhs.value_;
}
bool operator<=(synchronized_value const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_, defer_lock);
unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
lock(lk1,lk2);
return value_ <= rhs.value_;
}
bool operator>=(synchronized_value const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_, defer_lock);
unique_lock<mutex_type> lk2(rhs.mtx_, defer_lock);
lock(lk1,lk2);
return value_ >= rhs.value_;
}
bool operator==(value_type const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_);
return value_ == rhs;
}
bool operator!=(value_type const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_);
return value_ != rhs;
}
bool operator<(value_type const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_);
return value_ < rhs;
}
bool operator<=(value_type const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_);
return value_ <= rhs;
}
bool operator>(value_type const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_);
return value_ > rhs;
}
bool operator>=(value_type const& rhs) const
{
unique_lock<mutex_type> lk1(mtx_);
return value_ >= rhs;
}
};
// Specialized algorithms
/**
*
*/
template <typename T, typename L>
inline void swap(synchronized_value<T,L> & lhs, synchronized_value<T,L> & rhs)
{
lhs.swap(rhs);
}
template <typename T, typename L>
inline void swap(synchronized_value<T,L> & lhs, T & rhs)
{
lhs.swap(rhs);
}
template <typename T, typename L>
inline void swap(T & lhs, synchronized_value<T,L> & rhs)
{
rhs.swap(lhs);
}
//Hash support
template <class T> struct hash;
template <typename T, typename L>
struct hash<synchronized_value<T,L> >;
// Comparison with T
template <typename T, typename L>
bool operator!=(synchronized_value<T,L> const&lhs, synchronized_value<T,L> const& rhs)
{
return ! (lhs==rhs);
}
template <typename T, typename L>
bool operator==(T const& lhs, synchronized_value<T,L> const&rhs)
{
return rhs==lhs;
}
template <typename T, typename L>
bool operator!=(T const& lhs, synchronized_value<T,L> const&rhs)
{
return rhs!=lhs;
}
template <typename T, typename L>
bool operator<(T const& lhs, synchronized_value<T,L> const&rhs)
{
return rhs>=lhs;
}
template <typename T, typename L>
bool operator<=(T const& lhs, synchronized_value<T,L> const&rhs)
{
return rhs>lhs;
}
template <typename T, typename L>
bool operator>(T const& lhs, synchronized_value<T,L> const&rhs)
{
return rhs<=lhs;
}
template <typename T, typename L>
bool operator>=(T const& lhs, synchronized_value<T,L> const&rhs)
{
return rhs<lhs;
}
/**
*
*/
template <typename OStream, typename T, typename L>
inline OStream& operator<<(OStream& os, synchronized_value<T,L> const& rhs)
{
rhs.save(os);
return os;
}
template <typename IStream, typename T, typename L>
inline IStream& operator>>(IStream& is, synchronized_value<T,L> const& rhs)
{
rhs.load(is);
return is;
}
#if ! defined(BOOST_THREAD_NO_SYNCHRONIZE)
#if ! defined BOOST_NO_CXX11_VARIADIC_TEMPLATES
template <typename ...SV>
std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> synchronize(SV& ...sv)
{
boost::lock(sv.mtx_ ...);
typedef std::tuple<typename synchronized_value_strict_lock_ptr<SV>::type ...> t_type;
return t_type(typename synchronized_value_strict_lock_ptr<SV>::type(sv.value_, sv.mtx_, adopt_lock) ...);
}
#else
template <typename SV1, typename SV2>
std::tuple<
typename synchronized_value_strict_lock_ptr<SV1>::type,
typename synchronized_value_strict_lock_ptr<SV2>::type
>
synchronize(SV1& sv1, SV2& sv2)
{
boost::lock(sv1.mtx_, sv2.mtx_);
typedef std::tuple<
typename synchronized_value_strict_lock_ptr<SV1>::type,
typename synchronized_value_strict_lock_ptr<SV2>::type
> t_type;
return t_type(
typename synchronized_value_strict_lock_ptr<SV1>::type(sv1.value_, sv1.mtx_, adopt_lock),
typename synchronized_value_strict_lock_ptr<SV2>::type(sv2.value_, sv2.mtx_, adopt_lock)
);
}
template <typename SV1, typename SV2, typename SV3>
std::tuple<
typename synchronized_value_strict_lock_ptr<SV1>::type,
typename synchronized_value_strict_lock_ptr<SV2>::type,
typename synchronized_value_strict_lock_ptr<SV3>::type
>
synchronize(SV1& sv1, SV2& sv2, SV3& sv3)
{
boost::lock(sv1.mtx_, sv2.mtx_);
typedef std::tuple<
typename synchronized_value_strict_lock_ptr<SV1>::type,
typename synchronized_value_strict_lock_ptr<SV2>::type,
typename synchronized_value_strict_lock_ptr<SV3>::type
> t_type;
return t_type(
typename synchronized_value_strict_lock_ptr<SV1>::type(sv1.value_, sv1.mtx_, adopt_lock),
typename synchronized_value_strict_lock_ptr<SV2>::type(sv2.value_, sv2.mtx_, adopt_lock),
typename synchronized_value_strict_lock_ptr<SV3>::type(sv3.value_, sv3.mtx_, adopt_lock)
);
}
#endif
#endif
}
#include <boost/config/abi_suffix.hpp>
#endif // header
|