Charles Davis | 54c9eb6 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //= llvm/Support/Unix/RWMutex.inc - Unix Reader/Writer Mutual Exclusion Lock =// |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 6 | // |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Unix specific (non-pthread) RWMutex class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 15 | //=== is guaranteed to work on *all* UNIX variants. |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Mark Seaborn | 552d9e4 | 2014-03-01 04:30:32 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Mutex.h" |
| 19 | |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | |
| 22 | using namespace sys; |
| 23 | |
Mark Seaborn | 552d9e4 | 2014-03-01 04:30:32 +0000 | [diff] [blame] | 24 | // This naive implementation treats readers the same as writers. This |
| 25 | // will therefore deadlock if a thread tries to acquire a read lock |
| 26 | // multiple times. |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 27 | |
David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 28 | RWMutexImpl::RWMutexImpl() : data_(new MutexImpl(false)) { } |
Mark Seaborn | 552d9e4 | 2014-03-01 04:30:32 +0000 | [diff] [blame] | 29 | |
| 30 | RWMutexImpl::~RWMutexImpl() { |
David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 31 | delete static_cast<MutexImpl *>(data_); |
Mark Seaborn | 552d9e4 | 2014-03-01 04:30:32 +0000 | [diff] [blame] | 32 | } |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 33 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 34 | bool RWMutexImpl::reader_acquire() { |
David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 35 | return static_cast<MutexImpl *>(data_)->acquire(); |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 38 | bool RWMutexImpl::reader_release() { |
David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 39 | return static_cast<MutexImpl *>(data_)->release(); |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 42 | bool RWMutexImpl::writer_acquire() { |
David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 43 | return static_cast<MutexImpl *>(data_)->acquire(); |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 46 | bool RWMutexImpl::writer_release() { |
David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 47 | return static_cast<MutexImpl *>(data_)->release(); |
Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | } |