4
4
// We'll generally lean towards Tokio's types as those are more featureful
5
5
// (including `tokio-console` support) and more widely deployed.
6
6
7
+ #[ cfg( all( feature = "_rt-async-global-executor" , not( feature = "_rt-tokio" ) ) ) ]
8
+ pub use async_lock:: { Mutex as AsyncMutex , MutexGuard as AsyncMutexGuard } ;
9
+
7
10
#[ cfg( all( feature = "_rt-async-std" , not( feature = "_rt-tokio" ) ) ) ]
8
11
pub use async_std:: sync:: { Mutex as AsyncMutex , MutexGuard as AsyncMutexGuard } ;
9
12
13
+ #[ cfg( all( feature = "_rt-smol" , not( feature = "_rt-tokio" ) ) ) ]
14
+ pub use smol:: lock:: { Mutex as AsyncMutex , MutexGuard as AsyncMutexGuard } ;
15
+
10
16
#[ cfg( feature = "_rt-tokio" ) ]
11
17
pub use tokio:: sync:: { Mutex as AsyncMutex , MutexGuard as AsyncMutexGuard } ;
12
18
13
19
pub struct AsyncSemaphore {
14
- // We use the semaphore from futures-intrusive as the one from async-std
20
+ // We use the semaphore from futures-intrusive as the one from async-lock
15
21
// is missing the ability to add arbitrary permits, and is not guaranteed to be fair:
16
22
// * https://github.com/smol-rs/async-lock/issues/22
17
23
// * https://github.com/smol-rs/async-lock/issues/23
@@ -20,7 +26,14 @@ pub struct AsyncSemaphore {
20
26
// and there are some soundness concerns (although it turns out any intrusive future is unsound
21
27
// in MIRI due to the necessitated mutable aliasing):
22
28
// https://github.com/launchbadge/sqlx/issues/1668
23
- #[ cfg( all( feature = "_rt-async-std" , not( feature = "_rt-tokio" ) ) ) ]
29
+ #[ cfg( all(
30
+ any(
31
+ feature = "_rt-async-global-executor" ,
32
+ feature = "_rt-async-std" ,
33
+ feature = "_rt-smol"
34
+ ) ,
35
+ not( feature = "_rt-tokio" )
36
+ ) ) ]
24
37
inner : futures_intrusive:: sync:: Semaphore ,
25
38
26
39
#[ cfg( feature = "_rt-tokio" ) ]
@@ -30,12 +43,24 @@ pub struct AsyncSemaphore {
30
43
impl AsyncSemaphore {
31
44
#[ track_caller]
32
45
pub fn new ( fair : bool , permits : usize ) -> Self {
33
- if cfg ! ( not( any( feature = "_rt-async-std" , feature = "_rt-tokio" ) ) ) {
46
+ if cfg ! ( not( any(
47
+ feature = "_rt-async-global-executor" ,
48
+ feature = "_rt-async-std" ,
49
+ feature = "_rt-smol" ,
50
+ feature = "_rt-tokio"
51
+ ) ) ) {
34
52
crate :: rt:: missing_rt ( ( fair, permits) ) ;
35
53
}
36
54
37
55
AsyncSemaphore {
38
- #[ cfg( all( feature = "_rt-async-std" , not( feature = "_rt-tokio" ) ) ) ]
56
+ #[ cfg( all(
57
+ any(
58
+ feature = "_rt-async-global-executor" ,
59
+ feature = "_rt-async-std" ,
60
+ feature = "_rt-smol"
61
+ ) ,
62
+ not( feature = "_rt-tokio" )
63
+ ) ) ]
39
64
inner : futures_intrusive:: sync:: Semaphore :: new ( fair, permits) ,
40
65
#[ cfg( feature = "_rt-tokio" ) ]
41
66
inner : {
@@ -46,18 +71,39 @@ impl AsyncSemaphore {
46
71
}
47
72
48
73
pub fn permits ( & self ) -> usize {
49
- #[ cfg( all( feature = "_rt-async-std" , not( feature = "_rt-tokio" ) ) ) ]
74
+ #[ cfg( all(
75
+ any(
76
+ feature = "_rt-async-global-executor" ,
77
+ feature = "_rt-async-std" ,
78
+ feature = "_rt-smol"
79
+ ) ,
80
+ not( feature = "_rt-tokio" )
81
+ ) ) ]
50
82
return self . inner . permits ( ) ;
51
83
52
84
#[ cfg( feature = "_rt-tokio" ) ]
53
85
return self . inner . available_permits ( ) ;
54
86
55
- #[ cfg( not( any( feature = "_rt-async-std" , feature = "_rt-tokio" ) ) ) ]
87
+ #[ cfg( not( any(
88
+ any(
89
+ feature = "_rt-async-global-executor" ,
90
+ feature = "_rt-async-std" ,
91
+ feature = "_rt-smol"
92
+ ) ,
93
+ feature = "_rt-tokio"
94
+ ) ) ) ]
56
95
crate :: rt:: missing_rt ( ( ) )
57
96
}
58
97
59
98
pub async fn acquire ( & self , permits : u32 ) -> AsyncSemaphoreReleaser < ' _ > {
60
- #[ cfg( all( feature = "_rt-async-std" , not( feature = "_rt-tokio" ) ) ) ]
99
+ #[ cfg( all(
100
+ any(
101
+ feature = "_rt-async-global-executor" ,
102
+ feature = "_rt-async-std" ,
103
+ feature = "_rt-smol"
104
+ ) ,
105
+ not( feature = "_rt-tokio" )
106
+ ) ) ]
61
107
return AsyncSemaphoreReleaser {
62
108
inner : self . inner . acquire ( permits as usize ) . await ,
63
109
} ;
@@ -73,12 +119,26 @@ impl AsyncSemaphore {
73
119
. expect ( "BUG: we do not expose the `.close()` method" ) ,
74
120
} ;
75
121
76
- #[ cfg( not( any( feature = "_rt-async-std" , feature = "_rt-tokio" ) ) ) ]
122
+ #[ cfg( not( any(
123
+ any(
124
+ feature = "_rt-async-global-executor" ,
125
+ feature = "_rt-async-std" ,
126
+ feature = "_rt-smol"
127
+ ) ,
128
+ feature = "_rt-tokio"
129
+ ) ) ) ]
77
130
crate :: rt:: missing_rt ( permits)
78
131
}
79
132
80
133
pub fn try_acquire ( & self , permits : u32 ) -> Option < AsyncSemaphoreReleaser < ' _ > > {
81
- #[ cfg( all( feature = "_rt-async-std" , not( feature = "_rt-tokio" ) ) ) ]
134
+ #[ cfg( all(
135
+ any(
136
+ feature = "_rt-async-global-executor" ,
137
+ feature = "_rt-async-std" ,
138
+ feature = "_rt-smol"
139
+ ) ,
140
+ not( feature = "_rt-tokio" )
141
+ ) ) ]
82
142
return Some ( AsyncSemaphoreReleaser {
83
143
inner : self . inner . try_acquire ( permits as usize ) ?,
84
144
} ) ;
@@ -88,18 +148,39 @@ impl AsyncSemaphore {
88
148
inner : self . inner . try_acquire_many ( permits) . ok ( ) ?,
89
149
} ) ;
90
150
91
- #[ cfg( not( any( feature = "_rt-async-std" , feature = "_rt-tokio" ) ) ) ]
151
+ #[ cfg( not( any(
152
+ any(
153
+ feature = "_rt-async-global-executor" ,
154
+ feature = "_rt-async-std" ,
155
+ feature = "_rt-smol"
156
+ ) ,
157
+ feature = "_rt-tokio"
158
+ ) ) ) ]
92
159
crate :: rt:: missing_rt ( permits)
93
160
}
94
161
95
162
pub fn release ( & self , permits : usize ) {
96
- #[ cfg( all( feature = "_rt-async-std" , not( feature = "_rt-tokio" ) ) ) ]
163
+ #[ cfg( all(
164
+ any(
165
+ feature = "_rt-async-global-executor" ,
166
+ feature = "_rt-async-std" ,
167
+ feature = "_rt-smol"
168
+ ) ,
169
+ not( feature = "_rt-tokio" )
170
+ ) ) ]
97
171
return self . inner . release ( permits) ;
98
172
99
173
#[ cfg( feature = "_rt-tokio" ) ]
100
174
return self . inner . add_permits ( permits) ;
101
175
102
- #[ cfg( not( any( feature = "_rt-async-std" , feature = "_rt-tokio" ) ) ) ]
176
+ #[ cfg( not( any(
177
+ any(
178
+ feature = "_rt-async-global-executor" ,
179
+ feature = "_rt-async-std" ,
180
+ feature = "_rt-smol"
181
+ ) ,
182
+ feature = "_rt-tokio"
183
+ ) ) ) ]
103
184
crate :: rt:: missing_rt ( permits)
104
185
}
105
186
}
@@ -114,30 +195,58 @@ pub struct AsyncSemaphoreReleaser<'a> {
114
195
// and there are some soundness concerns (although it turns out any intrusive future is unsound
115
196
// in MIRI due to the necessitated mutable aliasing):
116
197
// https://github.com/launchbadge/sqlx/issues/1668
117
- #[ cfg( all( feature = "_rt-async-std" , not( feature = "_rt-tokio" ) ) ) ]
198
+ #[ cfg( all(
199
+ any(
200
+ feature = "_rt-async-global-executor" ,
201
+ feature = "_rt-async-std" ,
202
+ feature = "_rt-smol"
203
+ ) ,
204
+ not( feature = "_rt-tokio" )
205
+ ) ) ]
118
206
inner : futures_intrusive:: sync:: SemaphoreReleaser < ' a > ,
119
207
120
208
#[ cfg( feature = "_rt-tokio" ) ]
121
209
inner : tokio:: sync:: SemaphorePermit < ' a > ,
122
210
123
- #[ cfg( not( any( feature = "_rt-async-std" , feature = "_rt-tokio" ) ) ) ]
211
+ #[ cfg( not( any(
212
+ any(
213
+ feature = "_rt-async-global-executor" ,
214
+ feature = "_rt-async-std" ,
215
+ feature = "_rt-smol"
216
+ ) ,
217
+ feature = "_rt-tokio"
218
+ ) ) ) ]
124
219
_phantom : std:: marker:: PhantomData < & ' a ( ) > ,
125
220
}
126
221
127
222
impl AsyncSemaphoreReleaser < ' _ > {
128
223
pub fn disarm ( self ) {
129
- #[ cfg( feature = "_rt-tokio" ) ]
224
+ #[ cfg( all(
225
+ any(
226
+ feature = "_rt-async-global-executor" ,
227
+ feature = "_rt-async-std" ,
228
+ feature = "_rt-smol"
229
+ ) ,
230
+ not( feature = "_rt-tokio" )
231
+ ) ) ]
130
232
{
131
- self . inner . forget ( ) ;
233
+ let mut this = self ;
234
+ this. inner . disarm ( ) ;
132
235
}
133
236
134
- #[ cfg( all ( feature = "_rt-async-std" , not ( feature = "_rt- tokio") ) ) ]
237
+ #[ cfg( feature = "_rt-tokio" ) ]
135
238
{
136
- let mut this = self ;
137
- this. inner . disarm ( ) ;
239
+ self . inner . forget ( ) ;
138
240
}
139
241
140
- #[ cfg( not( any( feature = "_rt-async-std" , feature = "_rt-tokio" ) ) ) ]
242
+ #[ cfg( not( any(
243
+ any(
244
+ feature = "_rt-async-global-executor" ,
245
+ feature = "_rt-async-std" ,
246
+ feature = "_rt-smol"
247
+ ) ,
248
+ feature = "_rt-tokio"
249
+ ) ) ) ]
141
250
crate :: rt:: missing_rt ( ( ) )
142
251
}
143
252
}
0 commit comments