1
1
use crate :: sys:: rwlock as imp;
2
2
3
- #[ cfg( unix) ]
4
- enum GuardType {
5
- Read ,
6
- Write ,
7
- }
8
-
9
- #[ cfg( unix) ]
10
- pub struct RWLockGuard ( & ' static RWLock , GuardType ) ;
11
-
12
- #[ cfg( unix) ]
13
- impl Drop for RWLockGuard {
14
- fn drop ( & mut self ) {
15
- unsafe {
16
- match & self . 1 {
17
- GuardType :: Read => self . 0 . read_unlock ( ) ,
18
- GuardType :: Write => self . 0 . write_unlock ( ) ,
19
- }
20
- }
21
- }
22
- }
23
-
24
3
/// An OS-based reader-writer lock.
25
4
///
26
5
/// This structure is entirely unsafe and serves as the lowest layer of a
@@ -47,20 +26,6 @@ impl RWLock {
47
26
self . 0 . read ( )
48
27
}
49
28
50
- /// Acquires shared access to the underlying lock, blocking the current
51
- /// thread to do so.
52
- ///
53
- /// The lock is automatically unlocked when the returned guard is dropped.
54
- ///
55
- /// Behavior is undefined if the rwlock has been moved between this and any
56
- /// previous method call.
57
- #[ inline]
58
- #[ cfg( unix) ]
59
- pub unsafe fn read_with_guard ( & ' static self ) -> RWLockGuard {
60
- self . read ( ) ;
61
- RWLockGuard ( & self , GuardType :: Read )
62
- }
63
-
64
29
/// Attempts to acquire shared access to this lock, returning whether it
65
30
/// succeeded or not.
66
31
///
@@ -83,20 +48,6 @@ impl RWLock {
83
48
self . 0 . write ( )
84
49
}
85
50
86
- /// Acquires write access to the underlying lock, blocking the current thread
87
- /// to do so.
88
- ///
89
- /// The lock is automatically unlocked when the returned guard is dropped.
90
- ///
91
- /// Behavior is undefined if the rwlock has been moved between this and any
92
- /// previous method call.
93
- #[ inline]
94
- #[ cfg( unix) ]
95
- pub unsafe fn write_with_guard ( & ' static self ) -> RWLockGuard {
96
- self . write ( ) ;
97
- RWLockGuard ( & self , GuardType :: Write )
98
- }
99
-
100
51
/// Attempts to acquire exclusive access to this lock, returning whether it
101
52
/// succeeded or not.
102
53
///
@@ -135,3 +86,63 @@ impl RWLock {
135
86
self . 0 . destroy ( )
136
87
}
137
88
}
89
+
90
+ // the cfg annotations only exist due to dead code warnings. the code itself is portable
91
+ #[ cfg( unix) ]
92
+ pub struct StaticRWLock ( RWLock ) ;
93
+
94
+ #[ cfg( unix) ]
95
+ impl StaticRWLock {
96
+ pub const fn new ( ) -> StaticRWLock {
97
+ StaticRWLock ( RWLock :: new ( ) )
98
+ }
99
+
100
+ /// Acquires shared access to the underlying lock, blocking the current
101
+ /// thread to do so.
102
+ ///
103
+ /// The lock is automatically unlocked when the returned guard is dropped.
104
+ #[ inline]
105
+ pub fn read_with_guard ( & ' static self ) -> RWLockGuard {
106
+ // Safety: All methods require static references, therefore self
107
+ // cannot be moved between invocations.
108
+ unsafe {
109
+ self . 0 . read ( ) ;
110
+ }
111
+ RWLockGuard ( & self . 0 , GuardType :: Read )
112
+ }
113
+
114
+ /// Acquires write access to the underlying lock, blocking the current thread
115
+ /// to do so.
116
+ ///
117
+ /// The lock is automatically unlocked when the returned guard is dropped.
118
+ #[ inline]
119
+ pub fn write_with_guard ( & ' static self ) -> RWLockGuard {
120
+ // Safety: All methods require static references, therefore self
121
+ // cannot be moved between invocations.
122
+ unsafe {
123
+ self . 0 . write ( ) ;
124
+ }
125
+ RWLockGuard ( & self . 0 , GuardType :: Write )
126
+ }
127
+ }
128
+
129
+ #[ cfg( unix) ]
130
+ enum GuardType {
131
+ Read ,
132
+ Write ,
133
+ }
134
+
135
+ #[ cfg( unix) ]
136
+ pub struct RWLockGuard ( & ' static RWLock , GuardType ) ;
137
+
138
+ #[ cfg( unix) ]
139
+ impl Drop for RWLockGuard {
140
+ fn drop ( & mut self ) {
141
+ unsafe {
142
+ match & self . 1 {
143
+ GuardType :: Read => self . 0 . read_unlock ( ) ,
144
+ GuardType :: Write => self . 0 . write_unlock ( ) ,
145
+ }
146
+ }
147
+ }
148
+ }
0 commit comments