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