| 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 | // | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 7 | // | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // This file implements the Unix specific (non-pthread) RWMutex class. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
|  | 14 | //===----------------------------------------------------------------------===// | 
|  | 15 | //=== WARNING: Implementation here must contain only generic UNIX code that | 
|  | 16 | //===          is guaranteed to work on *all* UNIX variants. | 
|  | 17 | //===----------------------------------------------------------------------===// | 
|  | 18 |  | 
| Mark Seaborn | 552d9e4 | 2014-03-01 04:30:32 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Mutex.h" | 
|  | 20 |  | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 21 | namespace llvm { | 
|  | 22 |  | 
|  | 23 | using namespace sys; | 
|  | 24 |  | 
| Mark Seaborn | 552d9e4 | 2014-03-01 04:30:32 +0000 | [diff] [blame] | 25 | // This naive implementation treats readers the same as writers.  This | 
|  | 26 | // will therefore deadlock if a thread tries to acquire a read lock | 
|  | 27 | // multiple times. | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 28 |  | 
| David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 29 | RWMutexImpl::RWMutexImpl() : data_(new MutexImpl(false)) { } | 
| Mark Seaborn | 552d9e4 | 2014-03-01 04:30:32 +0000 | [diff] [blame] | 30 |  | 
|  | 31 | RWMutexImpl::~RWMutexImpl() { | 
| David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 32 | delete static_cast<MutexImpl *>(data_); | 
| Mark Seaborn | 552d9e4 | 2014-03-01 04:30:32 +0000 | [diff] [blame] | 33 | } | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 34 |  | 
| Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 35 | bool RWMutexImpl::reader_acquire() { | 
| David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 36 | return static_cast<MutexImpl *>(data_)->acquire(); | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 39 | bool RWMutexImpl::reader_release() { | 
| David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 40 | return static_cast<MutexImpl *>(data_)->release(); | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
| Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 43 | bool RWMutexImpl::writer_acquire() { | 
| David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 44 | return static_cast<MutexImpl *>(data_)->acquire(); | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Owen Anderson | 1498a7a | 2009-06-18 18:26:15 +0000 | [diff] [blame] | 47 | bool RWMutexImpl::writer_release() { | 
| David Blaikie | 626507f | 2014-10-31 17:02:30 +0000 | [diff] [blame] | 48 | return static_cast<MutexImpl *>(data_)->release(); | 
| Owen Anderson | 324f94c | 2009-06-16 20:19:28 +0000 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
|  | 51 | } |