blob: 85a104334a27c970f288c30af2d0b0e442ac9c7e [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +00001//= llvm/Support/Unix/RWMutex.inc - Unix Reader/Writer Mutual Exclusion Lock =//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Owen Anderson324f94c2009-06-16 20:19:28 +00003// 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. Spencer447762d2010-11-29 18:16:10 +00007//
Owen Anderson324f94c2009-06-16 20:19:28 +00008//===----------------------------------------------------------------------===//
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 Seaborn552d9e42014-03-01 04:30:32 +000019#include "llvm/Support/Mutex.h"
20
Owen Anderson324f94c2009-06-16 20:19:28 +000021namespace llvm {
22
23using namespace sys;
24
Mark Seaborn552d9e42014-03-01 04:30:32 +000025// 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 Anderson324f94c2009-06-16 20:19:28 +000028
David Blaikie626507f2014-10-31 17:02:30 +000029RWMutexImpl::RWMutexImpl() : data_(new MutexImpl(false)) { }
Mark Seaborn552d9e42014-03-01 04:30:32 +000030
31RWMutexImpl::~RWMutexImpl() {
David Blaikie626507f2014-10-31 17:02:30 +000032 delete static_cast<MutexImpl *>(data_);
Mark Seaborn552d9e42014-03-01 04:30:32 +000033}
Owen Anderson324f94c2009-06-16 20:19:28 +000034
Owen Anderson1498a7a2009-06-18 18:26:15 +000035bool RWMutexImpl::reader_acquire() {
David Blaikie626507f2014-10-31 17:02:30 +000036 return static_cast<MutexImpl *>(data_)->acquire();
Owen Anderson324f94c2009-06-16 20:19:28 +000037}
38
Owen Anderson1498a7a2009-06-18 18:26:15 +000039bool RWMutexImpl::reader_release() {
David Blaikie626507f2014-10-31 17:02:30 +000040 return static_cast<MutexImpl *>(data_)->release();
Owen Anderson324f94c2009-06-16 20:19:28 +000041}
42
Owen Anderson1498a7a2009-06-18 18:26:15 +000043bool RWMutexImpl::writer_acquire() {
David Blaikie626507f2014-10-31 17:02:30 +000044 return static_cast<MutexImpl *>(data_)->acquire();
Owen Anderson324f94c2009-06-16 20:19:28 +000045}
46
Owen Anderson1498a7a2009-06-18 18:26:15 +000047bool RWMutexImpl::writer_release() {
David Blaikie626507f2014-10-31 17:02:30 +000048 return static_cast<MutexImpl *>(data_)->release();
Owen Anderson324f94c2009-06-16 20:19:28 +000049}
50
51}