11use crate :: sys:: rwlock as imp;
22
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-
243/// An OS-based reader-writer lock.
254///
265/// This structure is entirely unsafe and serves as the lowest layer of a
@@ -47,20 +26,6 @@ impl RWLock {
4726 self . 0 . read ( )
4827 }
4928
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-
6429 /// Attempts to acquire shared access to this lock, returning whether it
6530 /// succeeded or not.
6631 ///
@@ -83,20 +48,6 @@ impl RWLock {
8348 self . 0 . write ( )
8449 }
8550
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-
10051 /// Attempts to acquire exclusive access to this lock, returning whether it
10152 /// succeeded or not.
10253 ///
@@ -135,3 +86,63 @@ impl RWLock {
13586 self . 0 . destroy ( )
13687 }
13788}
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