blob: 5a33364aa507004781fc30a1cafa780df4d7647c [file] [log] [blame]
Owen Anderson2a8cf9a2009-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
14#include "llvm/Config/config.h"
15#include "llvm/System/RWMutex.h"
Owen Anderson107a5372009-06-20 00:32:27 +000016#include <cstring>
Owen Anderson2a8cf9a2009-06-16 20:19:28 +000017
18//===----------------------------------------------------------------------===//
19//=== WARNING: Implementation here must contain only TRULY operating system
20//=== independent code.
21//===----------------------------------------------------------------------===//
22
23#if !defined(ENABLE_THREADS) || ENABLE_THREADS == 0
24// Define all methods as no-ops if threading is explicitly disabled
25namespace llvm {
26using namespace sys;
Owen Andersonb65e9ed2009-06-18 18:26:15 +000027RWMutexImpl::RWMutexImpl() { }
28RWMutexImpl::~RWMutexImpl() { }
29bool RWMutexImpl::reader_acquire() { return true; }
30bool RWMutexImpl::reader_release() { return true; }
31bool RWMutexImpl::writer_acquire() { return true; }
32bool RWMutexImpl::writer_release() { return true; }
Owen Anderson2a8cf9a2009-06-16 20:19:28 +000033}
34#else
35
36#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_RWLOCK_INIT)
37
38#include <cassert>
39#include <pthread.h>
40#include <stdlib.h>
41
42namespace llvm {
43using namespace sys;
44
45
46// This variable is useful for situations where the pthread library has been
47// compiled with weak linkage for its interface symbols. This allows the
48// threading support to be turned off by simply not linking against -lpthread.
49// In that situation, the value of pthread_mutex_init will be 0 and
50// consequently pthread_enabled will be false. In such situations, all the
51// pthread operations become no-ops and the functions all return false. If
52// pthread_rwlock_init does have an address, then rwlock support is enabled.
53// Note: all LLVM tools will link against -lpthread if its available since it
54// is configured into the LIBS variable.
55// Note: this line of code generates a warning if pthread_rwlock_init is not
56// declared with weak linkage. It's safe to ignore the warning.
57static const bool pthread_enabled = true;
58
59// Construct a RWMutex using pthread calls
Owen Andersonb65e9ed2009-06-18 18:26:15 +000060RWMutexImpl::RWMutexImpl()
Owen Anderson2a8cf9a2009-06-16 20:19:28 +000061 : data_(0)
62{
63 if (pthread_enabled)
64 {
65 // Declare the pthread_rwlock data structures
66 pthread_rwlock_t* rwlock =
67 static_cast<pthread_rwlock_t*>(malloc(sizeof(pthread_rwlock_t)));
Owen Anderson107a5372009-06-20 00:32:27 +000068
69#ifdef __APPLE__
70 // Workaround a bug/mis-feature in Darwin's pthread_rwlock_init.
71 bzero(rwlock, sizeof(pthread_rwlock_t));
72#endif
73
Owen Anderson2a8cf9a2009-06-16 20:19:28 +000074 // Initialize the rwlock
Chris Lattner60e72d92010-08-10 00:34:06 +000075 int errorcode = pthread_rwlock_init(rwlock, NULL);
Owen Anderson2a8cf9a2009-06-16 20:19:28 +000076 assert(errorcode == 0);
77
78 // Assign the data member
79 data_ = rwlock;
80 }
81}
82
83// Destruct a RWMutex
Owen Andersonb65e9ed2009-06-18 18:26:15 +000084RWMutexImpl::~RWMutexImpl()
Owen Anderson2a8cf9a2009-06-16 20:19:28 +000085{
86 if (pthread_enabled)
87 {
88 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
89 assert(rwlock != 0);
90 pthread_rwlock_destroy(rwlock);
91 free(rwlock);
92 }
93}
94
95bool
Owen Andersonb65e9ed2009-06-18 18:26:15 +000096RWMutexImpl::reader_acquire()
Owen Anderson2a8cf9a2009-06-16 20:19:28 +000097{
98 if (pthread_enabled)
99 {
100 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
101 assert(rwlock != 0);
102
103 int errorcode = pthread_rwlock_rdlock(rwlock);
104 return errorcode == 0;
Duncan Sands740eb532009-09-06 10:53:22 +0000105 } else return false;
Owen Anderson2a8cf9a2009-06-16 20:19:28 +0000106}
107
108bool
Owen Andersonb65e9ed2009-06-18 18:26:15 +0000109RWMutexImpl::reader_release()
Owen Anderson2a8cf9a2009-06-16 20:19:28 +0000110{
111 if (pthread_enabled)
112 {
113 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
114 assert(rwlock != 0);
115
116 int errorcode = pthread_rwlock_unlock(rwlock);
117 return errorcode == 0;
Duncan Sands740eb532009-09-06 10:53:22 +0000118 } else return false;
Owen Anderson2a8cf9a2009-06-16 20:19:28 +0000119}
120
121bool
Owen Andersonb65e9ed2009-06-18 18:26:15 +0000122RWMutexImpl::writer_acquire()
Owen Anderson2a8cf9a2009-06-16 20:19:28 +0000123{
124 if (pthread_enabled)
125 {
126 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
127 assert(rwlock != 0);
128
129 int errorcode = pthread_rwlock_wrlock(rwlock);
130 return errorcode == 0;
Duncan Sands740eb532009-09-06 10:53:22 +0000131 } else return false;
Owen Anderson2a8cf9a2009-06-16 20:19:28 +0000132}
133
134bool
Owen Andersonb65e9ed2009-06-18 18:26:15 +0000135RWMutexImpl::writer_release()
Owen Anderson2a8cf9a2009-06-16 20:19:28 +0000136{
137 if (pthread_enabled)
138 {
139 pthread_rwlock_t* rwlock = static_cast<pthread_rwlock_t*>(data_);
140 assert(rwlock != 0);
141
142 int errorcode = pthread_rwlock_unlock(rwlock);
143 return errorcode == 0;
Duncan Sands740eb532009-09-06 10:53:22 +0000144 } else return false;
Owen Anderson2a8cf9a2009-06-16 20:19:28 +0000145}
146
147}
148
149#elif defined(LLVM_ON_UNIX)
150#include "Unix/RWMutex.inc"
151#elif defined( LLVM_ON_WIN32)
152#include "Win32/RWMutex.inc"
153#else
154#warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in System/Mutex.cpp
155#endif
156#endif