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
use std::{mem, fmt};
use crate::Semaphore;
use std::sync::Arc;
use std::thread::panicking;
use std::fmt::{Debug, Formatter};

/// A guard returned by [`Semaphore::acquire`] that will call [`Semaphore::release`] when it
/// is dropped (falls out of scope).
/// # Examples
/// ```
/// # use futures::executor::block_on;
/// use async_weighted_semaphore::{Semaphore, SemaphoreGuard};
/// # block_on(async{
/// let semaphore = Semaphore::new(1);
/// let guard: SemaphoreGuard = semaphore.acquire(1).await.unwrap();
/// # })
/// ```
#[must_use]
pub struct SemaphoreGuard<'a> {
    semaphore: &'a Semaphore,
    amount: usize,
    panicking: bool,
}

/// A guard returned by [`Semaphore::acquire_arc`] that will call [`Semaphore::release`] when it
/// is dropped (falls out of scope). Can be sent between threads.
#[must_use]
pub struct SemaphoreGuardArc {
    semaphore: Option<Arc<Semaphore>>,
    amount: usize,
    panicking: bool,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SemaphoreGuardSplitErr {
    Underflow
}

impl<'a> SemaphoreGuard<'a> {
    pub fn new(semaphore: &'a Semaphore, amount: usize) -> Self {
        SemaphoreGuard { semaphore, amount, panicking: panicking() }
    }

    /// Combine two `SemaphoreGuard`s into one, with the sum of the originals' permits.
    ///
    /// # Examples
    /// ```
    /// # use async_weighted_semaphore::Semaphore;
    /// # tokio_test::block_on(async {
    /// let semaphore = Semaphore::new(15);
    /// let mut g1 = semaphore.acquire(10).await.unwrap();
    /// let g2 = semaphore.acquire(5).await.unwrap();
    /// g1.extend(g2);
    /// # })
    /// ```
    pub fn extend(&mut self, other: SemaphoreGuard<'a>) {
        assert!(std::ptr::eq(self.semaphore, other.semaphore),
            "Can't extend a guard with a guard from a different Semaphore");
        self.amount = self.amount.saturating_add(other.forget());
    }

    /// Drop the guard without calling [`Semaphore::release`]. This is useful when `release`s don't
    /// correspond one-to-one with `acquires` or it's difficult to send the guard to the releaser.
    /// # Examples
    /// ```
    /// # use async_weighted_semaphore::{Semaphore, PoisonError, SemaphoreGuardArc};
    /// use async_channel::{Sender, SendError};
    /// // Limit size of a producer-consumer queue. Receivers may wait for any number of items
    /// // to be available.
    /// async fn send<T>(semaphore: &Semaphore,
    ///                  sender: &Sender<T>,
    ///                  message: T
    ///         ) -> Result<(), SendError<T>>{
    ///     match semaphore.acquire(1).await {
    ///         // A semaphore can be poisoned to prevent deadlock when a channel closes.
    ///         Err(PoisonError) => Err(SendError(message)),
    ///         Ok(guard) => {
    ///             sender.send(message).await?;
    ///             guard.forget();
    ///             Ok(())
    ///         }
    ///     }
    /// }
    /// ```
    pub fn forget(self) -> usize {
        let amount = self.amount;
        mem::forget(self);
        amount
    }

    /// Split this `SemaphoreGuard` into two.
    ///
    /// The new guard will have `permits` permits, and this guard's permits will be reduced
    /// accordingly.
    ///
    /// # Examples
    /// ```
    /// # use async_weighted_semaphore::Semaphore;
    /// # tokio_test::block_on(async {
    /// let semaphore = Semaphore::new(15);
    /// let mut g1 = semaphore.acquire(15).await.unwrap();
    /// let g2 = g1.split(5).unwrap();
    /// # })
    /// ```
    pub fn split(&mut self, permits: usize) -> Result<SemaphoreGuard<'a>, SemaphoreGuardSplitErr> {
        if self.amount >= permits {
            self.amount -= permits;
            Ok(SemaphoreGuard {
                semaphore: self.semaphore.clone(),
                amount: permits,
                panicking: self.panicking
            })
        } else {
            Err(SemaphoreGuardSplitErr::Underflow)
        }
    }
}

impl SemaphoreGuardArc {
    pub fn new(semaphore: Arc<Semaphore>, amount: usize) -> Self {
        SemaphoreGuardArc { semaphore: Some(semaphore), amount, panicking: panicking() }
    }

    /// Combine two `SemaphoreGuardArc`s into one, with the sum of the originals' permits.
    ///
    /// # Examples
    /// ```
    /// # use async_weighted_semaphore::Semaphore;
    /// # tokio_test::block_on(async {
    /// let semaphore = Semaphore::new(15);
    /// let mut g1 = semaphore.acquire(10).await.unwrap();
    /// let g2 = semaphore.acquire(5).await.unwrap();
    /// g1.extend(g2);
    /// # })
    /// ```
    pub fn extend(&mut self, other: SemaphoreGuardArc) {
        let sem1 = self.semaphore.as_ref().unwrap();
        let sem2 = other.semaphore.as_ref().unwrap();
        assert!(Arc::ptr_eq(sem1, sem2),
            "Can't extend a guard with a guard from a different Semaphore");
        self.amount = self.amount.saturating_add(other.forget());
    }

    /// Drop the guard without calling [`Semaphore::release`]. This is useful when `release`s don't
    /// correspond one-to-one with `acquires` or it's difficult to send the guard to the releaser.
    /// # Examples
    /// ```
    /// # use async_weighted_semaphore::{Semaphore, PoisonError, SemaphoreGuardArc};
    /// # use std::sync::Arc;
    /// use async_channel::{Sender, SendError};
    /// // Limit size of a producer-consumer queue. Receivers may wait for any number of items
    /// // to be available.
    /// async fn send<T>(semaphore: &Arc<Semaphore>,
    ///                  sender: &Sender<T>,
    ///                  message: T
    ///         ) -> Result<(), SendError<T>>{
    ///     match semaphore.acquire_arc(1).await {
    ///         // A semaphore can be poisoned to prevent deadlock when a channel closes.
    ///         Err(PoisonError) => Err(SendError(message)),
    ///         Ok(guard) => {
    ///             sender.send(message).await?;
    ///             guard.forget();
    ///             Ok(())
    ///         }
    ///     }
    /// }
    /// ```
    pub fn forget(mut self) -> usize {
        self.semaphore = None;
        let amount = self.amount;
        mem::forget(self);
        amount
    }

    /// Split this `SemaphoreGuardArc` into two.
    ///
    /// The new guard will have `permits` permits, and this guard's permits will be reduced
    /// accordingly.
    ///
    /// # Examples
    /// ```
    /// # use async_weighted_semaphore::Semaphore;
    /// # use std::sync::Arc;
    /// # tokio_test::block_on(async {
    /// let semaphore = Arc::new(Semaphore::new(15));
    /// let mut g1 = semaphore.acquire_arc(15).await.unwrap();
    /// let g2 = g1.split(5).unwrap();
    /// # })
    /// ```
    pub fn split(&mut self, permits: usize) -> Result<SemaphoreGuardArc, SemaphoreGuardSplitErr> {
        if self.amount >= permits {
            self.amount -= permits;
            Ok(SemaphoreGuardArc {
                semaphore: self.semaphore.clone(),
                amount: permits,
                panicking: self.panicking
            })
        } else {
            Err(SemaphoreGuardSplitErr::Underflow)
        }
    }
}


impl<'a> Drop for SemaphoreGuard<'a> {
    fn drop(&mut self) {
        if !self.panicking && panicking() {
            self.semaphore.poison();
        } else {
            self.semaphore.release(self.amount);
        }
    }
}


impl Drop for SemaphoreGuardArc {
    fn drop(&mut self) {
        if let Some(semaphore) = self.semaphore.take() {
            if !self.panicking && panicking() {
                semaphore.poison();
            } else {
                semaphore.release(self.amount);
            }
        }
    }
}

impl<'a> Debug for SemaphoreGuard<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "SemaphoreGuard({})", self.amount)
    }
}

impl Debug for SemaphoreGuardArc {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "SemaphoreGuardArc({})", self.amount)
    }
}