ranked-semaphore 0.1.4

A high-performance ranked semaphore with priority support
Documentation
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::config::{PriorityConfig, QueueStrategy};
use crate::wait_queue::queue::WaitQueue;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;

/// A priority-aware semaphore for controlling access to shared resources.
///
/// RankedSemaphore maintains a count of available permits and allows tasks to
/// acquire permits with different priorities. Higher priority tasks are served
/// before lower priority tasks when permits become available.
///
/// The semaphore supports:
/// - Priority-based scheduling with configurable queue strategies
/// - Multiple permit acquisition
/// - Both borrowed and owned permit types
/// - Graceful shutdown via the `close()` method
///
/// # Examples
///
/// ```rust
/// use ranked_semaphore::RankedSemaphore;
/// use std::sync::Arc;
///
/// # #[tokio::main]
/// # async fn main() {
/// let sem = Arc::new(RankedSemaphore::new_fifo(2));
///
/// // High priority task
/// let high_permit = sem.acquire_with_priority(10).await.unwrap();
///
/// // Low priority task will wait
/// let low_permit = sem.acquire_with_priority(1).await.unwrap();
/// # }
/// ```
#[derive(Debug)]
pub struct RankedSemaphore {
    /// Combined permit count and status flags using bit operations
    /// Bit layout: [permit_count << 1 | closed_flag]
    /// This optimization reduces atomic operations by 50% compared to separate fields
    pub(crate) permits: AtomicUsize,
    /// Wait queue (used only when needed)
    pub(crate) waiters: Mutex<WaitQueue>,
}

impl RankedSemaphore {
    /// Maximum permits (reserve 3 bits for flags, same as tokio)
    pub const MAX_PERMITS: usize = usize::MAX >> 3;

    /// Bit flag constants for state encoding (aligned with tokio)
    pub(crate) const CLOSED: usize = 1;
    pub(crate) const PERMIT_SHIFT: usize = 1;

    /// Creates a new semaphore with FIFO (First In, First Out) queue strategy.
    ///
    /// All waiters regardless of priority will be served in the order they arrive.
    /// This is the most common queue strategy for fair resource allocation.
    ///
    /// # Arguments
    ///
    /// * `permits` - The initial number of permits available
    ///
    /// # Panics
    ///
    /// Panics if `permits` exceeds `MAX_PERMITS` (usize::MAX >> 3).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::RankedSemaphore;
    ///
    /// let sem = RankedSemaphore::new_fifo(3);
    /// assert_eq!(sem.available_permits(), 3);
    /// ```
    pub fn new_fifo(permits: usize) -> Self {
        if permits > Self::MAX_PERMITS {
            panic!("permits exceed MAX_PERMITS");
        }
        Self::new(permits, QueueStrategy::Fifo)
    }

    /// Creates a new semaphore with LIFO (Last In, First Out) queue strategy.
    ///
    /// All waiters regardless of priority will be served in reverse order of arrival.
    /// This can be useful for scenarios where you want to prioritize recently arrived tasks.
    ///
    /// # Arguments
    ///
    /// * `permits` - The initial number of permits available
    ///
    /// # Panics
    ///
    /// Panics if `permits` exceeds `MAX_PERMITS` (usize::MAX >> 3).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::RankedSemaphore;
    ///
    /// let sem = RankedSemaphore::new_lifo(3);
    /// assert_eq!(sem.available_permits(), 3);
    /// ```
    pub fn new_lifo(permits: usize) -> Self {
        if permits > Self::MAX_PERMITS {
            panic!("permits exceed MAX_PERMITS");
        }
        Self::new(permits, QueueStrategy::Lifo)
    }

    /// Creates a new semaphore with the specified default queue strategy.
    ///
    /// All waiters will use the specified queue strategy regardless of their priority.
    /// For more fine-grained control over queue strategies per priority level,
    /// use `new_with_config()`.
    ///
    /// # Arguments
    ///
    /// * `permits` - The initial number of permits available
    /// * `default_strategy` - The queue strategy to use for all waiters
    ///
    /// # Panics
    ///
    /// Panics if `permits` exceeds `MAX_PERMITS` (usize::MAX >> 3).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::{RankedSemaphore, QueueStrategy};
    ///
    /// let sem = RankedSemaphore::new(3, QueueStrategy::Fifo);
    /// assert_eq!(sem.available_permits(), 3);
    /// ```
    pub fn new(permits: usize, default_strategy: QueueStrategy) -> Self {
        if permits > Self::MAX_PERMITS {
            panic!("permits exceed MAX_PERMITS");
        }
        let config = PriorityConfig::new().default_strategy(default_strategy);
        Self::new_with_config(permits, config)
    }

    /// Creates a new semaphore with custom priority-based configuration.
    ///
    /// This allows fine-grained control over queue strategies for different
    /// priority levels. Different priorities can use different queue strategies
    /// (FIFO or LIFO) based on the configuration rules.
    ///
    /// # Arguments
    ///
    /// * `permits` - The initial number of permits available
    /// * `config` - The priority configuration specifying queue strategies
    ///
    /// # Panics
    ///
    /// Panics if `permits` exceeds `MAX_PERMITS` (usize::MAX >> 3).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::{RankedSemaphore, PriorityConfig, QueueStrategy};
    ///
    /// let config = PriorityConfig::new()
    ///     .default_strategy(QueueStrategy::Fifo)
    ///     .exact(10, QueueStrategy::Lifo); // Priority 10 uses LIFO
    ///
    /// let sem = RankedSemaphore::new_with_config(3, config);
    /// assert_eq!(sem.available_permits(), 3);
    /// ```
    pub fn new_with_config(permits: usize, config: PriorityConfig) -> Self {
        if permits > Self::MAX_PERMITS {
            panic!("permits exceed MAX_PERMITS");
        }
        Self {
            permits: AtomicUsize::new(permits << Self::PERMIT_SHIFT),
            waiters: Mutex::new(WaitQueue::new(config)),
        }
    }

    /// Returns the current number of available permits.
    ///
    /// This value represents permits that can be acquired immediately without waiting.
    /// Note that this value can change rapidly in concurrent environments.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::RankedSemaphore;
    ///
    /// let sem = RankedSemaphore::new_fifo(3);
    /// assert_eq!(sem.available_permits(), 3);
    ///
    /// let _permit = sem.try_acquire().unwrap();
    /// assert_eq!(sem.available_permits(), 2);
    /// ```
    pub fn available_permits(&self) -> usize {
        self.permits.load(Ordering::Acquire) >> Self::PERMIT_SHIFT
    }

    /// Returns `true` if the semaphore has been closed.
    ///
    /// A closed semaphore will not issue new permits and all pending
    /// acquire operations will return `AcquireError::Closed`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::RankedSemaphore;
    ///
    /// let sem = RankedSemaphore::new_fifo(3);
    /// assert!(!sem.is_closed());
    ///
    /// sem.close();
    /// assert!(sem.is_closed());
    /// ```
    pub fn is_closed(&self) -> bool {
        self.permits.load(Ordering::Acquire) & Self::CLOSED == Self::CLOSED
    }

    /// Adds permits to the semaphore and notifies waiting tasks.
    ///
    /// If there are tasks waiting for permits, they will be notified in priority order.
    /// Any excess permits (beyond what waiting tasks need) are added to the
    /// semaphore's available permit count.
    ///
    /// # Arguments
    ///
    /// * `added` - The number of permits to add
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::RankedSemaphore;
    ///
    /// let sem = RankedSemaphore::new_fifo(1);
    /// let _permit = sem.try_acquire().unwrap();
    /// assert_eq!(sem.available_permits(), 0);
    ///
    /// sem.add_permits(2);
    /// assert_eq!(sem.available_permits(), 2);
    /// ```
    pub fn add_permits(&self, added: usize) {
        if added == 0 {
            return;
        }

        // Assign permits to the wait queue, following tokio's approach
        self.add_permits_locked(added, self.waiters.lock().unwrap());
    }

    /// Add permits to the semaphore and wake eligible waiters.
    ///
    /// If `rem` exceeds the number of permits needed by waiting tasks, the
    /// remainder are returned to the semaphore's available permit count.
    ///
    /// This implementation processes waiters in batches to minimize thundering herd
    /// effects and reduce lock contention.
    pub(crate) fn add_permits_locked(
        &self,
        mut rem: usize,
        waiters: std::sync::MutexGuard<'_, crate::wait_queue::queue::WaitQueue>,
    ) {
        let mut lock = Some(waiters);

        // Process waiters in batches like tokio to prevent thundering herd
        while rem > 0 {
            let mut waiters = lock.take().unwrap_or_else(|| self.waiters.lock().unwrap());

            // Check if queue is empty
            if waiters.is_empty() {
                drop(waiters);
                break;
            }

            // Select waiters to notify in this batch
            let (wake_list, permits_assigned) = waiters.select_waiters_to_notify(rem);
            rem -= permits_assigned;

            // If we couldn't assign any permits, we're done
            if permits_assigned == 0 || wake_list.is_empty() {
                drop(waiters);
                break;
            }

            // Drop the lock before waking to reduce lock contention
            drop(waiters);

            // **Critical check**: if the semaphore was closed while we didn't have the lock,
            // we must not wake up the waiters. They will be woken by the `close()` call.
            if self.is_closed() {
                // Return the permits that were assigned to waiters back to the semaphore
                // since we won't wake them due to the semaphore being closed.
                self.permits
                    .fetch_add(permits_assigned << Self::PERMIT_SHIFT, Ordering::Release);
                return;
            }

            // Wake this batch of waiters
            let mut wake_list = wake_list;
            wake_list.wake_all();

            // If WakeList was not at capacity, we processed all available waiters
            if !wake_list.was_full() {
                break;
            }
        }

        // Add any remaining permits back to the semaphore
        if rem > 0 {
            let prev = self
                .permits
                .fetch_add(rem << Self::PERMIT_SHIFT, Ordering::Release);
            let prev_permits = prev >> Self::PERMIT_SHIFT;

            // Check for overflow after the operation
            if prev_permits + rem > Self::MAX_PERMITS {
                panic!(
                    "number of added permits ({}) would overflow MAX_PERMITS ({})",
                    rem,
                    Self::MAX_PERMITS
                );
            }
        }
    }

    /// Closes the semaphore and cancels all pending waiters.
    ///
    /// After calling this method:
    /// - No new permits will be issued
    /// - All pending `acquire` operations will return `AcquireError::Closed`
    /// - All `try_acquire` operations will return `TryAcquireError::Closed`
    ///
    /// This operation is irreversible - once closed, a semaphore cannot be reopened.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::RankedSemaphore;
    ///
    /// let sem = RankedSemaphore::new_fifo(3);
    /// sem.close();
    /// 
    /// assert!(sem.is_closed());
    /// assert!(sem.try_acquire().is_err());
    /// ```
    pub fn close(&self) {
        self.permits.fetch_or(Self::CLOSED, Ordering::Release);

        let mut waiters = self.waiters.lock().unwrap();
        waiters.close();
    }

    /// Permanently removes permits from the semaphore.
    ///
    /// This operation reduces the number of available permits without releasing
    /// them back to the semaphore. It's useful for scenarios where the resource
    /// pool needs to be dynamically reduced.
    ///
    /// # Arguments
    ///
    /// * `n` - The number of permits to remove
    ///
    /// # Returns
    ///
    /// The number of permits that were actually removed. This may be less than
    /// `n` if there were insufficient permits available.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use ranked_semaphore::RankedSemaphore;
    ///
    /// let sem = RankedSemaphore::new_fifo(5);
    /// assert_eq!(sem.available_permits(), 5);
    ///
    /// let removed = sem.forget_permits(3);
    /// assert_eq!(removed, 3);
    /// assert_eq!(sem.available_permits(), 2);
    ///
    /// // Trying to remove more permits than available
    /// let removed = sem.forget_permits(10);
    /// assert_eq!(removed, 2); // Only 2 were available
    /// assert_eq!(sem.available_permits(), 0);
    /// ```
    ///
    /// # Warning
    ///
    /// Use this method with caution. Removing too many permits can lead to
    /// resource starvation if tasks are waiting for permits that will never
    /// become available.
    pub fn forget_permits(&self, n: usize) -> usize {
        if n == 0 {
            return 0;
        }

        let mut curr_bits = self.permits.load(Ordering::Acquire);
        loop {
            let curr_permits = curr_bits >> Self::PERMIT_SHIFT;
            let removed = curr_permits.min(n);
            let new_permits = curr_permits - removed;
            let new_bits = (new_permits << Self::PERMIT_SHIFT) | (curr_bits & Self::CLOSED);

            match self.permits.compare_exchange_weak(
                curr_bits,
                new_bits,
                Ordering::AcqRel,
                Ordering::Acquire,
            ) {
                Ok(_) => return removed,
                Err(actual) => curr_bits = actual,
            }
        }
    }
}