File tree 3 files changed +54
-0
lines changed
3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -206,6 +206,20 @@ impl Semaphore {
206
206
}
207
207
}
208
208
209
+ /// Creates a new closed semaphore with 0 permits.
210
+ #[ cfg( not( all( loom, test) ) ) ]
211
+ pub ( crate ) const fn const_new_closed ( ) -> Self {
212
+ Self {
213
+ permits : AtomicUsize :: new ( Self :: CLOSED ) ,
214
+ waiters : Mutex :: const_new ( Waitlist {
215
+ queue : LinkedList :: new ( ) ,
216
+ closed : true ,
217
+ } ) ,
218
+ #[ cfg( all( tokio_unstable, feature = "tracing" ) ) ]
219
+ resource_span : tracing:: Span :: none ( ) ,
220
+ }
221
+ }
222
+
209
223
/// Returns the current number of available permits.
210
224
pub ( crate ) fn available_permits ( & self ) -> usize {
211
225
self . permits . load ( Acquire ) >> Self :: PERMIT_SHIFT
Original file line number Diff line number Diff line change @@ -149,6 +149,36 @@ impl<T> OnceCell<T> {
149
149
}
150
150
}
151
151
152
+ /// Creates a new `OnceCell` that contains the provided value.
153
+ ///
154
+ /// # Example
155
+ ///
156
+ /// ```
157
+ /// use tokio::sync::OnceCell;
158
+ ///
159
+ /// static ONCE: OnceCell<u32> = OnceCell::const_new_with(1);
160
+ ///
161
+ /// async fn get_global_integer() -> &'static u32 {
162
+ /// ONCE.get_or_init(|| async {
163
+ /// 1 + 1
164
+ /// }).await
165
+ /// }
166
+ ///
167
+ /// #[tokio::main]
168
+ /// async fn main() {
169
+ /// let result = get_global_integer().await;
170
+ /// assert_eq!(*result, 1);
171
+ /// }
172
+ /// ```
173
+ #[ cfg( not( all( loom, test) ) ) ]
174
+ pub const fn const_new_with ( value : T ) -> Self {
175
+ OnceCell {
176
+ value_set : AtomicBool :: new ( true ) ,
177
+ value : UnsafeCell :: new ( MaybeUninit :: new ( value) ) ,
178
+ semaphore : Semaphore :: const_new_closed ( ) ,
179
+ }
180
+ }
181
+
152
182
/// Creates a new empty `OnceCell` instance.
153
183
///
154
184
/// Equivalent to `OnceCell::new`, except that it can be used in static
Original file line number Diff line number Diff line change @@ -219,6 +219,16 @@ impl Semaphore {
219
219
}
220
220
}
221
221
222
+ /// Creates a new closed semaphore with 0 permits.
223
+ #[ cfg( not( all( loom, test) ) ) ]
224
+ pub ( crate ) const fn const_new_closed ( ) -> Self {
225
+ Self {
226
+ ll_sem : ll:: Semaphore :: const_new_closed ( ) ,
227
+ #[ cfg( all( tokio_unstable, feature = "tracing" ) ) ]
228
+ resource_span : tracing:: Span :: none ( ) ,
229
+ }
230
+ }
231
+
222
232
/// Returns the current number of available permits.
223
233
pub fn available_permits ( & self ) -> usize {
224
234
self . ll_sem . available_permits ( )
You can’t perform that action at this time.
0 commit comments