blob: 8182319541e29fcc563f737444f99853fdeef899 [file] [log] [blame]
Owen Anderson324f94c2009-06-16 20:19:28 +00001//===- RWMutex.cpp - Reader/Writer Mutual Exclusion Lock --------*- C++ -*-===//
2//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the llvm::sys::RWMutex class.
11//
12//===----------------------------------------------------------------------===//
13
Serge Pavlov76d8cce2018-02-20 05:41:26 +000014#include "llvm/Support/Allocator.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/RWMutex.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/Config/config.h"
Owen Anderson324f94c2009-06-16 20:19:28 +000017
18//===----------------------------------------------------------------------===//
19//=== WARNING: Implementation here must contain only TRULY operating system
20//=== independent code.
21//===----------------------------------------------------------------------===//
22
Dylan Noblesmithefddf202011-11-28 00:48:58 +000023#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Owen Anderson324f94c2009-06-16 20:19:28 +000024// Define all methods as no-ops if threading is explicitly disabled
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000025
26using namespace llvm;
Owen Anderson324f94c2009-06-16 20:19:28 +000027using namespace sys;
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000028
29RWMutexImpl::RWMutexImpl() = default;
30RWMutexImpl::~RWMutexImpl() = default;
31
Owen Anderson1498a7a2009-06-18 18:26:15 +000032bool RWMutexImpl::reader_acquire() { return true; }
33bool RWMutexImpl::reader_release() { return true; }
34bool RWMutexImpl::writer_acquire() { return true; }
35bool RWMutexImpl::writer_release() { return true; }
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000036
Owen Anderson324f94c2009-06-16 20:19:28 +000037#else
38
39#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
40
41#include <cassert>
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000042#include <cstdlib>
Owen Anderson324f94c2009-06-16 20:19:28 +000043#include <pthread.h>
Owen Anderson324f94c2009-06-16 20:19:28 +000044
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000045using namespace llvm;
Owen Anderson324f94c2009-06-16 20:19:28 +000046using namespace sys;
47
Owen Anderson324f94c2009-06-16 20:19:28 +000048// Construct a RWMutex using pthread calls
Owen Anderson1498a7a2009-06-18 18:26:15 +000049RWMutexImpl::RWMutexImpl()
Owen Anderson324f94c2009-06-16 20:19:28 +000050{
David Blaikiefdcd6692012-01-15 01:09:13 +000051 // Declare the pthread_rwlock data structures
52 pthread_rwlock_t* rwlock =
Serge Pavlov76d8cce2018-02-20 05:41:26 +000053 static_cast<pthread_rwlock_t*>(safe_malloc(sizeof(pthread_rwlock_t)));
Owen Anderson96c51a82009-06-20 00:32:27 +000054
55#ifdef __APPLE__
David Blaikiefdcd6692012-01-15 01:09:13 +000056 // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
57 bzero(rwlock, sizeof(pthread_rwlock_t));
Owen Anderson96c51a82009-06-20 00:32:27 +000058#endif
59
David Blaikiefdcd6692012-01-15 01:09:13 +000060 // Initialize the rwlock
Craig Topperc10719f2014-04-07 04:17:22 +000061 int errorcode = pthread_rwlock_init(rwlock, nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000062 (void)errorcode;
63 assert(errorcode == 0);
Owen Anderson324f94c2009-06-16 20:19:28 +000064
David Blaikiefdcd6692012-01-15 01:09:13 +000065 // Assign the data member
66 data_ = rwlock;
Owen Anderson324f94c2009-06-16 20:19:28 +000067}
68
69// Destruct a RWMutex
Owen Anderson1498a7a2009-06-18 18:26:15 +000070RWMutexImpl::~RWMutexImpl()
Owen Anderson324f94c2009-06-16 20:19:28 +000071{
David Blaikiefdcd6692012-01-15 01:09:13 +000072 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000073 assert(rwlock != nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000074 pthread_rwlock_destroy(rwlock);
75 free(rwlock);
Owen Anderson324f94c2009-06-16 20:19:28 +000076}
77
78bool
Owen Anderson1498a7a2009-06-18 18:26:15 +000079RWMutexImpl::reader_acquire()
Owen Anderson324f94c2009-06-16 20:19:28 +000080{
David Blaikiefdcd6692012-01-15 01:09:13 +000081 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000082 assert(rwlock != nullptr);
Owen Anderson324f94c2009-06-16 20:19:28 +000083
David Blaikiefdcd6692012-01-15 01:09:13 +000084 int errorcode = pthread_rwlock_rdlock(rwlock);
85 return errorcode == 0;
Owen Anderson324f94c2009-06-16 20:19:28 +000086}
87
88bool
Owen Anderson1498a7a2009-06-18 18:26:15 +000089RWMutexImpl::reader_release()
Owen Anderson324f94c2009-06-16 20:19:28 +000090{
David Blaikiefdcd6692012-01-15 01:09:13 +000091 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000092 assert(rwlock != nullptr);
Owen Anderson324f94c2009-06-16 20:19:28 +000093
David Blaikiefdcd6692012-01-15 01:09:13 +000094 int errorcode = pthread_rwlock_unlock(rwlock);
95 return errorcode == 0;
Owen Anderson324f94c2009-06-16 20:19:28 +000096}
97
98bool
Owen Anderson1498a7a2009-06-18 18:26:15 +000099RWMutexImpl::writer_acquire()
Owen Anderson324f94c2009-06-16 20:19:28 +0000100{
David Blaikiefdcd6692012-01-15 01:09:13 +0000101 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000102 assert(rwlock != nullptr);
Owen Anderson324f94c2009-06-16 20:19:28 +0000103
David Blaikiefdcd6692012-01-15 01:09:13 +0000104 int errorcode = pthread_rwlock_wrlock(rwlock);
105 return errorcode == 0;
Owen Anderson324f94c2009-06-16 20:19:28 +0000106}
107
108bool
Owen Anderson1498a7a2009-06-18 18:26:15 +0000109RWMutexImpl::writer_release()
Owen Anderson324f94c2009-06-16 20:19:28 +0000110{
David Blaikiefdcd6692012-01-15 01:09:13 +0000111 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000112 assert(rwlock != nullptr);
Owen Anderson324f94c2009-06-16 20:19:28 +0000113
David Blaikiefdcd6692012-01-15 01:09:13 +0000114 int errorcode = pthread_rwlock_unlock(rwlock);
115 return errorcode == 0;
Owen Anderson324f94c2009-06-16 20:19:28 +0000116}
117
Owen Anderson324f94c2009-06-16 20:19:28 +0000118#elif defined(LLVM_ON_UNIX)
119#include "Unix/RWMutex.inc"
120#elif defined( LLVM_ON_WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +0000121#include "Windows/RWMutex.inc"
Owen Anderson324f94c2009-06-16 20:19:28 +0000122#else
Daniel Dunbar037fc932011-10-11 20:02:52 +0000123#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
Owen Anderson324f94c2009-06-16 20:19:28 +0000124#endif
125#endif