blob: 83c6d1d52b4c8f44b7541735317114896d35e7af [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
Michael J. Spencer447762d2010-11-29 18:16:10 +000014#include "llvm/Support/RWMutex.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/Config/config.h"
Owen Anderson324f94c2009-06-16 20:19:28 +000016
17//===----------------------------------------------------------------------===//
18//=== WARNING: Implementation here must contain only TRULY operating system
19//=== independent code.
20//===----------------------------------------------------------------------===//
21
Dylan Noblesmithefddf202011-11-28 00:48:58 +000022#if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
Owen Anderson324f94c2009-06-16 20:19:28 +000023// Define all methods as no-ops if threading is explicitly disabled
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000024
25using namespace llvm;
Owen Anderson324f94c2009-06-16 20:19:28 +000026using namespace sys;
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000027
28RWMutexImpl::RWMutexImpl() = default;
29RWMutexImpl::~RWMutexImpl() = default;
30
Owen Anderson1498a7a2009-06-18 18:26:15 +000031bool RWMutexImpl::reader_acquire() { return true; }
32bool RWMutexImpl::reader_release() { return true; }
33bool RWMutexImpl::writer_acquire() { return true; }
34bool RWMutexImpl::writer_release() { return true; }
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000035
Owen Anderson324f94c2009-06-16 20:19:28 +000036#else
37
38#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
39
40#include <cassert>
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000041#include <cstdlib>
Owen Anderson324f94c2009-06-16 20:19:28 +000042#include <pthread.h>
Owen Anderson324f94c2009-06-16 20:19:28 +000043
Eugene Zelenko454d0ce2017-02-15 22:17:02 +000044using namespace llvm;
Owen Anderson324f94c2009-06-16 20:19:28 +000045using namespace sys;
46
Owen Anderson324f94c2009-06-16 20:19:28 +000047// Construct a RWMutex using pthread calls
Owen Anderson1498a7a2009-06-18 18:26:15 +000048RWMutexImpl::RWMutexImpl()
Owen Anderson324f94c2009-06-16 20:19:28 +000049{
David Blaikiefdcd6692012-01-15 01:09:13 +000050 // Declare the pthread_rwlock data structures
51 pthread_rwlock_t* rwlock =
52 static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
Owen Anderson96c51a82009-06-20 00:32:27 +000053
54#ifdef __APPLE__
David Blaikiefdcd6692012-01-15 01:09:13 +000055 // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
56 bzero(rwlock, sizeof(pthread_rwlock_t));
Owen Anderson96c51a82009-06-20 00:32:27 +000057#endif
58
David Blaikiefdcd6692012-01-15 01:09:13 +000059 // Initialize the rwlock
Craig Topperc10719f2014-04-07 04:17:22 +000060 int errorcode = pthread_rwlock_init(rwlock, nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000061 (void)errorcode;
62 assert(errorcode == 0);
Owen Anderson324f94c2009-06-16 20:19:28 +000063
David Blaikiefdcd6692012-01-15 01:09:13 +000064 // Assign the data member
65 data_ = rwlock;
Owen Anderson324f94c2009-06-16 20:19:28 +000066}
67
68// Destruct a RWMutex
Owen Anderson1498a7a2009-06-18 18:26:15 +000069RWMutexImpl::~RWMutexImpl()
Owen Anderson324f94c2009-06-16 20:19:28 +000070{
David Blaikiefdcd6692012-01-15 01:09:13 +000071 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000072 assert(rwlock != nullptr);
David Blaikiefdcd6692012-01-15 01:09:13 +000073 pthread_rwlock_destroy(rwlock);
74 free(rwlock);
Owen Anderson324f94c2009-06-16 20:19:28 +000075}
76
77bool
Owen Anderson1498a7a2009-06-18 18:26:15 +000078RWMutexImpl::reader_acquire()
Owen Anderson324f94c2009-06-16 20:19:28 +000079{
David Blaikiefdcd6692012-01-15 01:09:13 +000080 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000081 assert(rwlock != nullptr);
Owen Anderson324f94c2009-06-16 20:19:28 +000082
David Blaikiefdcd6692012-01-15 01:09:13 +000083 int errorcode = pthread_rwlock_rdlock(rwlock);
84 return errorcode == 0;
Owen Anderson324f94c2009-06-16 20:19:28 +000085}
86
87bool
Owen Anderson1498a7a2009-06-18 18:26:15 +000088RWMutexImpl::reader_release()
Owen Anderson324f94c2009-06-16 20:19:28 +000089{
David Blaikiefdcd6692012-01-15 01:09:13 +000090 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +000091 assert(rwlock != nullptr);
Owen Anderson324f94c2009-06-16 20:19:28 +000092
David Blaikiefdcd6692012-01-15 01:09:13 +000093 int errorcode = pthread_rwlock_unlock(rwlock);
94 return errorcode == 0;
Owen Anderson324f94c2009-06-16 20:19:28 +000095}
96
97bool
Owen Anderson1498a7a2009-06-18 18:26:15 +000098RWMutexImpl::writer_acquire()
Owen Anderson324f94c2009-06-16 20:19:28 +000099{
David Blaikiefdcd6692012-01-15 01:09:13 +0000100 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000101 assert(rwlock != nullptr);
Owen Anderson324f94c2009-06-16 20:19:28 +0000102
David Blaikiefdcd6692012-01-15 01:09:13 +0000103 int errorcode = pthread_rwlock_wrlock(rwlock);
104 return errorcode == 0;
Owen Anderson324f94c2009-06-16 20:19:28 +0000105}
106
107bool
Owen Anderson1498a7a2009-06-18 18:26:15 +0000108RWMutexImpl::writer_release()
Owen Anderson324f94c2009-06-16 20:19:28 +0000109{
David Blaikiefdcd6692012-01-15 01:09:13 +0000110 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
Craig Topper2617dcc2014-04-15 06:32:26 +0000111 assert(rwlock != nullptr);
Owen Anderson324f94c2009-06-16 20:19:28 +0000112
David Blaikiefdcd6692012-01-15 01:09:13 +0000113 int errorcode = pthread_rwlock_unlock(rwlock);
114 return errorcode == 0;
Owen Anderson324f94c2009-06-16 20:19:28 +0000115}
116
Owen Anderson324f94c2009-06-16 20:19:28 +0000117#elif defined(LLVM_ON_UNIX)
118#include "Unix/RWMutex.inc"
119#elif defined( LLVM_ON_WIN32)
Michael J. Spencer447762d2010-11-29 18:16:10 +0000120#include "Windows/RWMutex.inc"
Owen Anderson324f94c2009-06-16 20:19:28 +0000121#else
Daniel Dunbar037fc932011-10-11 20:02:52 +0000122#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
Owen Anderson324f94c2009-06-16 20:19:28 +0000123#endif
124#endif