1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
17 pub struct RWLock { inner: UnsafeCell<ffi::pthread_rwlock_t> }
19 pub const RWLOCK_INIT
: RWLock
= RWLock
{
20 inner
: UnsafeCell { value: ffi::PTHREAD_RWLOCK_INITIALIZER }
,
23 unsafe impl Send
for RWLock {}
24 unsafe impl Sync
for RWLock {}
28 pub unsafe fn read(&self) {
29 let r
= ffi
::pthread_rwlock_rdlock(self.inner
.get());
31 // According to the pthread_rwlock_rdlock spec, this function **may**
32 // fail with EDEADLK if a deadlock is detected. On the other hand
33 // pthread mutexes will *never* return EDEADLK if they are initialized
34 // as the "fast" kind (which ours always are). As a result, a deadlock
35 // situation may actually return from the call to pthread_rwlock_rdlock
36 // instead of blocking forever (as mutexes and Windows rwlocks do). Note
37 // that not all unix implementations, however, will return EDEADLK for
40 // We roughly maintain the deadlocking behavior by panicking to ensure
41 // that this lock acquisition does not succeed.
42 if r
== libc
::EDEADLK
{
43 panic
!("rwlock read lock would result in deadlock");
45 debug_assert_eq
!(r
, 0);
49 pub unsafe fn try_read(&self) -> bool
{
50 ffi
::pthread_rwlock_tryrdlock(self.inner
.get()) == 0
53 pub unsafe fn write(&self) {
54 let r
= ffi
::pthread_rwlock_wrlock(self.inner
.get());
55 // see comments above for why we check for EDEADLK
56 if r
== libc
::EDEADLK
{
57 panic
!("rwlock write lock would result in deadlock");
59 debug_assert_eq
!(r
, 0);
63 pub unsafe fn try_write(&self) -> bool
{
64 ffi
::pthread_rwlock_trywrlock(self.inner
.get()) == 0
67 pub unsafe fn read_unlock(&self) {
68 let r
= ffi
::pthread_rwlock_unlock(self.inner
.get());
69 debug_assert_eq
!(r
, 0);
72 pub unsafe fn write_unlock(&self) { self.read_unlock() }
74 pub unsafe fn destroy(&self) {
75 let r
= ffi
::pthread_rwlock_destroy(self.inner
.get());
76 // On DragonFly pthread_rwlock_destroy() returns EINVAL if called on a
77 // rwlock that was just initialized with
78 // ffi::PTHREAD_RWLOCK_INITIALIZER. Once it is used (locked/unlocked)
79 // or pthread_rwlock_init() is called, this behaviour no longer occurs.
80 if cfg
!(target_os
= "dragonfly") {
81 debug_assert
!(r
== 0 || r
== libc
::EINVAL
);
83 debug_assert_eq
!(r
, 0);