xref: /minix3/external/bsd/libc++/dist/libcxx/src/shared_mutex.cpp (revision 4684ddb6aab0b36791c8099bc705d6140b3d05d0)
1*4684ddb6SLionel Sambuc //===---------------------- shared_mutex.cpp ------------------------------===//
2*4684ddb6SLionel Sambuc //
3*4684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
4*4684ddb6SLionel Sambuc //
5*4684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*4684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*4684ddb6SLionel Sambuc //
8*4684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
9*4684ddb6SLionel Sambuc 
10*4684ddb6SLionel Sambuc #define _LIBCPP_BUILDING_SHARED_MUTEX
11*4684ddb6SLionel Sambuc #include "shared_mutex"
12*4684ddb6SLionel Sambuc 
13*4684ddb6SLionel Sambuc _LIBCPP_BEGIN_NAMESPACE_STD
14*4684ddb6SLionel Sambuc 
15*4684ddb6SLionel Sambuc shared_mutex::shared_mutex()
16*4684ddb6SLionel Sambuc     : __state_(0)
17*4684ddb6SLionel Sambuc {
18*4684ddb6SLionel Sambuc }
19*4684ddb6SLionel Sambuc 
20*4684ddb6SLionel Sambuc // Exclusive ownership
21*4684ddb6SLionel Sambuc 
22*4684ddb6SLionel Sambuc void
23*4684ddb6SLionel Sambuc shared_mutex::lock()
24*4684ddb6SLionel Sambuc {
25*4684ddb6SLionel Sambuc     unique_lock<mutex> lk(__mut_);
26*4684ddb6SLionel Sambuc     while (__state_ & __write_entered_)
27*4684ddb6SLionel Sambuc         __gate1_.wait(lk);
28*4684ddb6SLionel Sambuc     __state_ |= __write_entered_;
29*4684ddb6SLionel Sambuc     while (__state_ & __n_readers_)
30*4684ddb6SLionel Sambuc         __gate2_.wait(lk);
31*4684ddb6SLionel Sambuc }
32*4684ddb6SLionel Sambuc 
33*4684ddb6SLionel Sambuc bool
34*4684ddb6SLionel Sambuc shared_mutex::try_lock()
35*4684ddb6SLionel Sambuc {
36*4684ddb6SLionel Sambuc     unique_lock<mutex> lk(__mut_);
37*4684ddb6SLionel Sambuc     if (__state_ == 0)
38*4684ddb6SLionel Sambuc     {
39*4684ddb6SLionel Sambuc         __state_ = __write_entered_;
40*4684ddb6SLionel Sambuc         return true;
41*4684ddb6SLionel Sambuc     }
42*4684ddb6SLionel Sambuc     return false;
43*4684ddb6SLionel Sambuc }
44*4684ddb6SLionel Sambuc 
45*4684ddb6SLionel Sambuc void
46*4684ddb6SLionel Sambuc shared_mutex::unlock()
47*4684ddb6SLionel Sambuc {
48*4684ddb6SLionel Sambuc     lock_guard<mutex> _(__mut_);
49*4684ddb6SLionel Sambuc     __state_ = 0;
50*4684ddb6SLionel Sambuc     __gate1_.notify_all();
51*4684ddb6SLionel Sambuc }
52*4684ddb6SLionel Sambuc 
53*4684ddb6SLionel Sambuc // Shared ownership
54*4684ddb6SLionel Sambuc 
55*4684ddb6SLionel Sambuc void
56*4684ddb6SLionel Sambuc shared_mutex::lock_shared()
57*4684ddb6SLionel Sambuc {
58*4684ddb6SLionel Sambuc     unique_lock<mutex> lk(__mut_);
59*4684ddb6SLionel Sambuc     while ((__state_ & __write_entered_) || (__state_ & __n_readers_) == __n_readers_)
60*4684ddb6SLionel Sambuc         __gate1_.wait(lk);
61*4684ddb6SLionel Sambuc     unsigned num_readers = (__state_ & __n_readers_) + 1;
62*4684ddb6SLionel Sambuc     __state_ &= ~__n_readers_;
63*4684ddb6SLionel Sambuc     __state_ |= num_readers;
64*4684ddb6SLionel Sambuc }
65*4684ddb6SLionel Sambuc 
66*4684ddb6SLionel Sambuc bool
67*4684ddb6SLionel Sambuc shared_mutex::try_lock_shared()
68*4684ddb6SLionel Sambuc {
69*4684ddb6SLionel Sambuc     unique_lock<mutex> lk(__mut_);
70*4684ddb6SLionel Sambuc     unsigned num_readers = __state_ & __n_readers_;
71*4684ddb6SLionel Sambuc     if (!(__state_ & __write_entered_) && num_readers != __n_readers_)
72*4684ddb6SLionel Sambuc     {
73*4684ddb6SLionel Sambuc         ++num_readers;
74*4684ddb6SLionel Sambuc         __state_ &= ~__n_readers_;
75*4684ddb6SLionel Sambuc         __state_ |= num_readers;
76*4684ddb6SLionel Sambuc         return true;
77*4684ddb6SLionel Sambuc     }
78*4684ddb6SLionel Sambuc     return false;
79*4684ddb6SLionel Sambuc }
80*4684ddb6SLionel Sambuc 
81*4684ddb6SLionel Sambuc void
82*4684ddb6SLionel Sambuc shared_mutex::unlock_shared()
83*4684ddb6SLionel Sambuc {
84*4684ddb6SLionel Sambuc     lock_guard<mutex> _(__mut_);
85*4684ddb6SLionel Sambuc     unsigned num_readers = (__state_ & __n_readers_) - 1;
86*4684ddb6SLionel Sambuc     __state_ &= ~__n_readers_;
87*4684ddb6SLionel Sambuc     __state_ |= num_readers;
88*4684ddb6SLionel Sambuc     if (__state_ & __write_entered_)
89*4684ddb6SLionel Sambuc     {
90*4684ddb6SLionel Sambuc         if (num_readers == 0)
91*4684ddb6SLionel Sambuc             __gate2_.notify_one();
92*4684ddb6SLionel Sambuc     }
93*4684ddb6SLionel Sambuc     else
94*4684ddb6SLionel Sambuc     {
95*4684ddb6SLionel Sambuc         if (num_readers == __n_readers_ - 1)
96*4684ddb6SLionel Sambuc             __gate1_.notify_one();
97*4684ddb6SLionel Sambuc     }
98*4684ddb6SLionel Sambuc }
99*4684ddb6SLionel Sambuc 
100*4684ddb6SLionel Sambuc 
101*4684ddb6SLionel Sambuc _LIBCPP_END_NAMESPACE_STD
102