1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use libc;
use cell::UnsafeCell;
pub struct RWLock { inner: UnsafeCell<libc::pthread_rwlock_t> }
unsafe impl Send for RWLock {}
unsafe impl Sync for RWLock {}
impl RWLock {
pub const fn new() -> RWLock {
RWLock { inner: UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER) }
}
#[inline]
pub unsafe fn read(&self) {
let r = libc::pthread_rwlock_rdlock(self.inner.get());
if r == libc::EDEADLK {
panic!("rwlock read lock would result in deadlock");
} else {
debug_assert_eq!(r, 0);
}
}
#[inline]
pub unsafe fn try_read(&self) -> bool {
libc::pthread_rwlock_tryrdlock(self.inner.get()) == 0
}
#[inline]
pub unsafe fn write(&self) {
let r = libc::pthread_rwlock_wrlock(self.inner.get());
if r == libc::EDEADLK {
panic!("rwlock write lock would result in deadlock");
} else {
debug_assert_eq!(r, 0);
}
}
#[inline]
pub unsafe fn try_write(&self) -> bool {
libc::pthread_rwlock_trywrlock(self.inner.get()) == 0
}
#[inline]
pub unsafe fn read_unlock(&self) {
let r = libc::pthread_rwlock_unlock(self.inner.get());
debug_assert_eq!(r, 0);
}
#[inline]
pub unsafe fn write_unlock(&self) { self.read_unlock() }
#[inline]
pub unsafe fn destroy(&self) {
let r = libc::pthread_rwlock_destroy(self.inner.get());
if cfg!(target_os = "dragonfly") {
debug_assert!(r == 0 || r == libc::EINVAL);
} else {
debug_assert_eq!(r, 0);
}
}
}