blob: 8b47dfa0f85cd5714741df4affe8f26be700c067 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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. Spencer447762d2010-11-29 18:16:10 +00006//
Owen Anderson324f94c2009-06-16 20:19:28 +00007//===----------------------------------------------------------------------===//
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 Seaborn552d9e42014-03-01 04:30:32 +000018#include "llvm/Support/Mutex.h"
19
Owen Anderson324f94c2009-06-16 20:19:28 +000020namespace llvm {
21
22using namespace sys;
23
Mark Seaborn552d9e42014-03-01 04:30:32 +000024// 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 Anderson324f94c2009-06-16 20:19:28 +000027
David Blaikie626507f2014-10-31 17:02:30 +000028RWMutexImpl::RWMutexImpl() : data_(new MutexImpl(false)) { }
Mark Seaborn552d9e42014-03-01 04:30:32 +000029
30RWMutexImpl::~RWMutexImpl() {
David Blaikie626507f2014-10-31 17:02:30 +000031 delete static_cast<MutexImpl *>(data_);
Mark Seaborn552d9e42014-03-01 04:30:32 +000032}
Owen Anderson324f94c2009-06-16 20:19:28 +000033
Owen Anderson1498a7a2009-06-18 18:26:15 +000034bool RWMutexImpl::reader_acquire() {
David Blaikie626507f2014-10-31 17:02:30 +000035 return static_cast<MutexImpl *>(data_)->acquire();
Owen Anderson324f94c2009-06-16 20:19:28 +000036}
37
Owen Anderson1498a7a2009-06-18 18:26:15 +000038bool RWMutexImpl::reader_release() {
David Blaikie626507f2014-10-31 17:02:30 +000039 return static_cast<MutexImpl *>(data_)->release();
Owen Anderson324f94c2009-06-16 20:19:28 +000040}
41
Owen Anderson1498a7a2009-06-18 18:26:15 +000042bool RWMutexImpl::writer_acquire() {
David Blaikie626507f2014-10-31 17:02:30 +000043 return static_cast<MutexImpl *>(data_)->acquire();
Owen Anderson324f94c2009-06-16 20:19:28 +000044}
45
Owen Anderson1498a7a2009-06-18 18:26:15 +000046bool RWMutexImpl::writer_release() {
David Blaikie626507f2014-10-31 17:02:30 +000047 return static_cast<MutexImpl *>(data_)->release();
Owen Anderson324f94c2009-06-16 20:19:28 +000048}
49
50}