id stringlengths 22 133 | text stringlengths 40 40.2k | arch stringclasses 1
value | syntax stringclasses 1
value | kind stringclasses 4
values | repo stringclasses 27
values | path stringlengths 5 116 | license stringclasses 6
values | commit stringlengths 40 40 | source_host stringclasses 1
value | category stringclasses 16
values | source_url stringlengths 85 196 | line_start int64 1 4.28k | line_end int64 4 4.31k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:12 | next.release_permits(n, &self.stub);
match self.state.compare_exchange(curr.0, next.0, AcqRel, Acquire) {
Ok(_) => return,
Err(actual) => {
curr = SemState(actual);
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:13 | continue 'outer;
}
self.push_stub(closed);
next_ptr = head.as_ref().next.load(Acquire);
if let Some(next) = NonNull::new(next_ptr) {
self.head.with_mut(|head| *head = next);
self.remove_queued(head, closed);
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:14 | // push the node back into the queue.
if curr.permits_to_acquire() > 0 {
// More permits are requested. The waiter must be re-queued
unsafe {
self.push_waiter(waiter, closed);
}
return;
}
let mut nex... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:15 | // This function is only called when there are pending tasks. Because of
// this, the state must *always* be in pointer mode.
let prev = prev.waiter().unwrap();
// No cycles plz
debug_assert_ne!(prev, waiter);
// Release `task` to the consume end.
prev.as_ref().next.sto... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:16 | /// Creates a new `Permit`.
///
/// The permit begins in the "unacquired" state.
pub(crate) fn new() -> Permit {
use PermitState::Acquired;
Permit {
waiter: None,
state: Acquired(0),
}
}
/// Returns `true` if the permit has been acquired
#[allow(... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:17 | // Request additional permits. If the waiter has been
// dequeued, it must be re-queued.
if !waiter.try_inc_permits_to_acquire(delta as usize) {
let waiter = NonNull::from(&**waiter);
// Ignore the result. The check... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:18 | Ready(Ok(()))
} else {
match semaphore.poll_acquire(cx, num_permits - acquired, self)? {
Ready(()) => {
self.state = Acquired(num_permits);
Ready(Ok(()))
}
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:19 | if res == 0 {
if requested < num_permits {
// Try to acquire the additional permits
semaphore.try_acquire(num_permits - requested)?;
}
self.state = Acquired(num_permits);
Ok(())
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:20 | use PermitState::*;
match self.state {
Waiting(requested) => {
let n = cmp::min(n, requested);
// Decrement
let acquired = self
.waiter
.as_ref()
.unwrap()
.try_dec_permi... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:21 | if let Some(waiter) = self.waiter.take() {
// Set the dropped flag
let state = WaiterState(waiter.state.fetch_or(DROPPED, AcqRel));
if state.is_queued() {
// The waiter is stored in the queue. The semaphore will drop it
std::mem::forget(waiter);
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:22 | }
}
/// Returns `true` if the error was caused by calling `try_acquire` on a
/// semaphore with no available permits.
pub(crate) fn is_no_permits(&self) -> bool {
match self {
TryAcquireError::NoPermits => true,
_ => false,
}
}
}
impl fmt::Display for TryAcq... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:23 | } else {
Ok(state.permits_to_acquire())
}
}
/// Only increments the number of permits *if* the waiter is currently
/// queued.
///
/// # Returns
///
/// `true` if the number of permits to acquire has been incremented. `false`
/// otherwise. On `false`, the caller sho... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:24 | assert_eq!(0, curr.permits_to_acquire());
}
let delta = cmp::min(n, curr.permits_to_acquire());
let rem = curr.permits_to_acquire() - delta;
let mut next = curr;
next.set_permits_to_acquire(rem);
match self.state.compare_exchange(curr.0, next.0,... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:25 | return true;
}
}
Err(actual) => curr = WaiterState(actual),
}
}
}
/// Set the number of permits to acquire.
///
/// This function is only called when the waiter is being inserted into the
/// wait queue. Because of this, there ... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 961 | 1,020 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:26 | match self.state.compare_exchange(curr.0, next.0, AcqRel, Acquire) {
Ok(_) => {
// Update `n`
*n -= assign;
if next.permits_to_acquire() == 0 {
if curr.permits_to_acquire() > 0 {
self.waker.w... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:27 | } else {
SemState(stub as *const _ as usize)
}
}
/// Returns a `State` tracking `ptr` as the tail of the queue.
fn new_ptr(tail: NonNull<Waiter>, closed: bool) -> SemState {
let mut val = tail.as_ptr() as usize;
if closed {
val |= CLOSED_FLAG;
}
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:28 | /// available permits.
fn acquire_permits(&mut self, num: usize, stub: &Waiter) -> bool {
debug_assert!(num > 0);
if self.available_permits() < num {
return false;
}
debug_assert!(self.waiter().is_none());
self.0 -= num << NUM_SHIFT;
if self.0 == NUM_F... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:29 | /// Returns the waiter, if one is set.
fn waiter(self) -> Option<NonNull<Waiter>> {
if self.is_waiter() {
let waiter = NonNull::new(self.as_ptr()).expect("null pointer stored");
Some(waiter)
} else {
None
}
}
/// Assumes `self` represents a point... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 1,121 | 1,180 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:30 | fn is_closed(self) -> bool {
self.0 & CLOSED_FLAG == CLOSED_FLAG
}
/// Converts the state into a `usize` representation.
fn to_usize(self) -> usize {
self.0
}
}
impl fmt::Debug for SemState {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut fmt = fmt.deb... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 1,161 | 1,220 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:31 | fn set_queued(&mut self) {
self.0 |= QUEUED;
}
fn is_closed(self) -> bool {
self.0 & CLOSED == CLOSED
}
fn set_closed(&mut self) {
self.0 |= CLOSED;
}
fn unset_queued(&mut self) {
assert!(self.is_queued());
self.0 -= QUEUED;
}
fn is_dropped(sel... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 3fd043931e6d37f211e682980edc6e12e9d4fc54 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/3fd043931e6d37f211e682980edc6e12e9d4fc54/tokio/src/sync/semaphore_ll.rs | 1,201 | 1,221 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:23 | } else {
Ok(state.permits_to_acquire())
}
}
/// Only increments the number of permits *if* the waiter is currently
/// queued.
///
/// # Returns
///
/// `true` if the number of permits to acquire has been incremented. `false`
/// otherwise. On `false`, the caller sho... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 4010335c84517571e9b2d13f66f7c72170493622 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/4010335c84517571e9b2d13f66f7c72170493622/tokio/src/sync/semaphore_ll.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:21 | if let Some(waiter) = self.waiter.take() {
// Set the dropped flag
let state = WaiterState(waiter.state.fetch_or(DROPPED, AcqRel));
if state.is_queued() {
// The waiter is stored in the queue. The semaphore will drop it
std::mem::forget(waiter);
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | a5c1a7de0399625792b476666ea1326cb8bcd75c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a5c1a7de0399625792b476666ea1326cb8bcd75c/tokio/src/sync/semaphore_ll.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:22 | }
}
/// Returns `true` if the error was caused by calling `try_acquire` on a
/// semaphore with no available permits.
pub(crate) fn is_no_permits(&self) -> bool {
match self {
TryAcquireError::NoPermits => true,
_ => false,
}
}
}
impl fmt::Display for TryAcq... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | a5c1a7de0399625792b476666ea1326cb8bcd75c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/a5c1a7de0399625792b476666ea1326cb8bcd75c/tokio/src/sync/semaphore_ll.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:8 | debug_assert_ne!(curr.0, 0);
debug_assert_ne!(next.0, 0);
match self.state.compare_exchange(curr.0, next.0, AcqRel, Acquire) {
Ok(_) => {
if acquired {
// Successfully acquire permits **without** queuing the
// ... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:9 | /// Closes the semaphore. This prevents the semaphore from issuing new
/// permits and notifies all pending waiters.
pub(crate) fn close(&self) {
// Acquire the `rx_lock`, setting the "closed" flag on the lock.
let prev = self.rx_lock.fetch_or(1, AcqRel);
if prev != 0 {
// A... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:10 | // Release the permits and notify
self.add_permits_locked2(rem, closed);
let n = rem << 1;
let actual = if closed {
let actual = self.rx_lock.fetch_sub(n | 1, AcqRel);
closed = false;
actual
} else {
let ac... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:11 | // permits get assigned back to the semaphore.
let next = match NonNull::new(next_ptr) {
Some(next) => next,
None => {
// This loop is not part of the standard intrusive mpsc
// channel algorithm.... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:12 | match self.state.compare_exchange(curr.0, next.0, AcqRel, Acquire) {
Ok(_) => return,
Err(actual) => {
curr = SemState(actual);
}
}
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:13 | }
self.push_stub(closed);
next_ptr = head.as_ref().next.load(Acquire);
if let Some(next) = NonNull::new(next_ptr) {
self.head.with_mut(|head| *head = next);
self.remove_queued(head, closed);
continue 'outer;
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:14 | if curr.permits_to_acquire() > 0 {
// More permits are requested. The waiter must be re-queued
unsafe {
self.push_waiter(waiter, closed);
}
return;
}
let mut next = curr;
next.unset_queued();
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:16 | ///
/// The permit begins in the "unacquired" state.
pub(crate) fn new() -> Permit {
use PermitState::Acquired;
Permit {
waiter: None,
state: Acquired(0),
}
}
/// Returns `true` if the permit has been acquired
#[allow(dead_code)] // may be used later... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:17 | // Request additional permits. If the waiter has been
// dequeued, it must be re-queued.
if !waiter.try_inc_permits_to_acquire(delta as usize) {
let waiter = NonNull::from(&**waiter);
// Ignore the result. The check... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:18 | } else {
match semaphore.poll_acquire(cx, num_permits - acquired, self)? {
Ready(()) => {
self.state = Acquired(num_permits);
Ready(Ok(()))
}
Pending => {
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:19 | if requested < num_permits {
// Try to acquire the additional permits
semaphore.try_acquire(num_permits - requested)?;
}
self.state = Acquired(num_permits);
Ok(())
} else {
Er... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:20 | match self.state {
Waiting(requested) => {
let n = cmp::min(n, requested);
// Decrement
let acquired = self
.waiter
.as_ref()
.unwrap()
.try_dec_permits_to_acquire(n as usize) as ... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:21 | // Set the dropped flag
let state = WaiterState(waiter.state.fetch_or(DROPPED, AcqRel));
if state.is_queued() {
// The waiter is stored in the queue. The semaphore will drop it
std::mem::forget(waiter);
}
}
}
}
// ===== impl AcquireError ... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:22 | }
/// Returns `true` if the error was caused by calling `try_acquire` on a
/// semaphore with no available permits.
pub(crate) fn is_no_permits(&self) -> bool {
match self {
TryAcquireError::NoPermits => true,
_ => false,
}
}
}
impl fmt::Display for TryAcquireEr... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:23 | Ok(state.permits_to_acquire())
}
}
/// Only increments the number of permits *if* the waiter is currently
/// queued.
///
/// # Returns
///
/// `true` if the number of permits to acquire has been incremented. `false`
/// otherwise. On `false`, the caller should use `Semaphore::p... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:24 | }
let delta = cmp::min(n, curr.permits_to_acquire());
let rem = curr.permits_to_acquire() - delta;
let mut next = curr;
next.set_permits_to_acquire(rem);
match self.state.compare_exchange(curr.0, next.0, AcqRel, Acquire) {
Ok(_) => return n ... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:25 | }
}
Err(actual) => curr = WaiterState(actual),
}
}
}
/// Set the number of permits to acquire.
///
/// This function is only called when the waiter is being inserted into the
/// wait queue. Because of this, there are no concurrent threads that ca... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 961 | 1,020 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:26 | match self.state.compare_exchange(curr.0, next.0, AcqRel, Acquire) {
Ok(_) => {
// Update `n`
*n -= assign;
if next.permits_to_acquire() == 0 {
if curr.permits_to_acquire() > 0 {
self.waker.w... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:27 | SemState(stub as *const _ as usize)
}
}
/// Returns a `State` tracking `ptr` as the tail of the queue.
fn new_ptr(tail: NonNull<Waiter>, closed: bool) -> SemState {
let mut val = tail.as_ptr() as usize;
if closed {
val |= CLOSED_FLAG;
}
SemState(val)
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:28 | fn acquire_permits(&mut self, num: usize, stub: &Waiter) -> bool {
debug_assert!(num > 0);
if self.available_permits() < num {
return false;
}
debug_assert!(self.waiter().is_none());
self.0 -= num << NUM_SHIFT;
if self.0 == NUM_FLAG {
// Set th... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:29 | fn waiter(self) -> Option<NonNull<Waiter>> {
if self.is_waiter() {
let waiter = NonNull::new(self.as_ptr()).expect("null pointer stored");
Some(waiter)
} else {
None
}
}
/// Assumes `self` represents a pointer
fn as_ptr(self) -> *mut Waiter {
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 1,121 | 1,180 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:30 | fn is_closed(self) -> bool {
self.0 & CLOSED_FLAG == CLOSED_FLAG
}
/// Converts the state into a `usize` representation.
fn to_usize(self) -> usize {
self.0
}
}
impl fmt::Debug for SemState {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut fmt = fmt.deb... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 1,161 | 1,220 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:31 | self.0 |= QUEUED;
}
fn is_closed(self) -> bool {
self.0 & CLOSED == CLOSED
}
fn set_closed(&mut self) {
self.0 |= CLOSED;
}
fn unset_queued(&mut self) {
assert!(self.is_queued());
self.0 -= QUEUED;
}
fn is_dropped(self) -> bool {
self.0 & DROPP... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | b44ab273597d3757b5b50eed95c4f8890fa54e42 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/b44ab273597d3757b5b50eed95c4f8890fa54e42/tokio/src/sync/semaphore_ll.rs | 1,201 | 1,220 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:19 | if requested < num_permits {
// Try to acquire the additional permits
semaphore.try_acquire(num_permits - requested)?;
}
self.state = Acquired(num_permits);
Ok(())
} else {
Er... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 1cb1e291c10adf6b4e530cb1475b95ba10fa615f | github | async-runtime | https://github.com/tokio-rs/tokio/blob/1cb1e291c10adf6b4e530cb1475b95ba10fa615f/tokio/src/sync/semaphore_ll.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:1 | #![cfg_attr(not(feature = "sync"), allow(dead_code, unreachable_pub))]
//! Thread-safe, asynchronous counting semaphore.
//!
//! A `Semaphore` instance holds a set of permits. Permits are used to
//! synchronize access to a shared resource.
//!
//! Before accessing the shared resource, callers acquire a permit from th... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | acf8a7da7a64bf08d578db9a9836a8e061765314 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/acf8a7da7a64bf08d578db9a9836a8e061765314/tokio/src/sync/semaphore_ll.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:4 | /// Represents "one requested permit" in the waiter state
const PERMIT_ONE: usize = 0b1000;
/// Masks the waiter state to only contain bits tracking number of requested
/// permits.
const PERMIT_MASK: usize = usize::MAX - (PERMIT_ONE - 1);
/// How much to shift a permit count to pack it into the waker state
const PER... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | acf8a7da7a64bf08d578db9a9836a8e061765314 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/acf8a7da7a64bf08d578db9a9836a8e061765314/tokio/src/sync/semaphore_ll.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:5 | // Allocations are aligned
debug_assert!(ptr.as_ptr() as usize & NUM_FLAG == 0);
let state = SemState::new(permits, &stub);
Semaphore {
state: AtomicUsize::new(state.to_usize()),
head: CausalCell::new(ptr),
rx_lock: AtomicUsize::new(0),
stub,
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | acf8a7da7a64bf08d578db9a9836a8e061765314 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/acf8a7da7a64bf08d578db9a9836a8e061765314/tokio/src/sync/semaphore_ll.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:15 | // This function is only called when there are pending tasks. Because of
// this, the state must *always* be in pointer mode.
let prev = prev.waiter().unwrap();
// No cycles plz
debug_assert_ne!(prev, waiter);
// Release `task` to the consume end.
prev.as_ref().next.sto... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:16 | ///
/// The permit begins in the "unacquired" state.
pub(crate) fn new() -> Permit {
use PermitState::Acquired;
Permit {
waiter: None,
state: Acquired(0),
}
}
/// Returns `true` if the permit has been acquired
pub(crate) fn is_acquired(&self) -> bool... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:17 | // dequeued, it must be re-queued.
if !waiter.try_inc_permits_to_acquire(delta as usize) {
let waiter = NonNull::from(&**waiter);
// Ignore the result. The check for
// `permits_to_acquire()` will converge the s... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:18 | match semaphore.poll_acquire(cx, num_permits - acquired, self)? {
Ready(()) => {
self.state = Acquired(num_permits);
Ready(Ok(()))
}
Pending => {
self.state = Waiti... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:19 | // Try to acquire the additional permits
semaphore.try_acquire(num_permits - requested)?;
}
self.state = Acquired(num_permits);
Ok(())
} else {
Err(TryAcquireError::NoPermits)
}
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:20 | match self.state {
Waiting(requested) => {
let n = cmp::min(n, requested);
// Decrement
let acquired = self
.waiter
.as_ref()
.unwrap()
.try_dec_permits_to_acquire(n as usize) as ... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 761 | 820 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:21 | let state = WaiterState(waiter.state.fetch_or(DROPPED, AcqRel));
if state.is_queued() {
// The waiter is stored in the queue. The semaphore will drop it
std::mem::forget(waiter);
}
}
}
}
// ===== impl AcquireError ====
impl AcquireError {
fn clo... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:22 | /// Returns `true` if the error was caused by calling `try_acquire` on a
/// semaphore with no available permits.
pub(crate) fn is_no_permits(&self) -> bool {
match self {
TryAcquireError::NoPermits => true,
_ => false,
}
}
}
impl fmt::Display for TryAcquireError {
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:23 | }
}
/// Only increments the number of permits *if* the waiter is currently
/// queued.
///
/// # Returns
///
/// `true` if the number of permits to acquire has been incremented. `false`
/// otherwise. On `false`, the caller should use `Semaphore::poll_acquire`.
fn try_inc_permits_to... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 881 | 940 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:24 | let delta = cmp::min(n, curr.permits_to_acquire());
let rem = curr.permits_to_acquire() - delta;
let mut next = curr;
next.set_permits_to_acquire(rem);
match self.state.compare_exchange(curr.0, next.0, AcqRel, Acquire) {
Ok(_) => return n - delta,
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 921 | 980 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:25 | }
Err(actual) => curr = WaiterState(actual),
}
}
}
/// Set the number of permits to acquire.
///
/// This function is only called when the waiter is being inserted into the
/// wait queue. Because of this, there are no concurrent threads that can
/// modify t... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 961 | 1,020 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:26 | Ok(_) => {
// Update `n`
*n -= assign;
if next.permits_to_acquire() == 0 {
if curr.permits_to_acquire() > 0 {
self.waker.wake();
}
return true;
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 1,001 | 1,060 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:27 | }
}
/// Returns a `State` tracking `ptr` as the tail of the queue.
fn new_ptr(tail: NonNull<Waiter>, closed: bool) -> SemState {
let mut val = tail.as_ptr() as usize;
if closed {
val |= CLOSED_FLAG;
}
SemState(val)
}
/// Returns the amount of remaining... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:28 | debug_assert!(num > 0);
if self.available_permits() < num {
return false;
}
debug_assert!(self.waiter().is_none());
self.0 -= num << NUM_SHIFT;
if self.0 == NUM_FLAG {
// Set the state to the stub pointer.
self.0 = stub as *const _ as usize... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:29 | if self.is_waiter() {
let waiter = NonNull::new(self.as_ptr()).expect("null pointer stored");
Some(waiter)
} else {
None
}
}
/// Assumes `self` represents a pointer
fn as_ptr(self) -> *mut Waiter {
(self.0 & !CLOSED_FLAG) as *mut Waiter
}
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 1,121 | 1,180 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:30 | self.0 & CLOSED_FLAG == CLOSED_FLAG
}
/// Converts the state into a `usize` representation.
fn to_usize(self) -> usize {
self.0
}
}
impl fmt::Debug for SemState {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut fmt = fmt.debug_struct("SemState");
if se... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 1,161 | 1,219 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:31 | }
fn is_closed(self) -> bool {
self.0 & CLOSED == CLOSED
}
fn set_closed(&mut self) {
self.0 |= CLOSED;
}
fn unset_queued(&mut self) {
assert!(self.is_queued());
self.0 -= QUEUED;
}
fn is_dropped(self) -> bool {
self.0 & DROPPED == DROPPED
}
} | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | f9ddb93604a830d106475bd4c4cae436fafcc0da | github | async-runtime | https://github.com/tokio-rs/tokio/blob/f9ddb93604a830d106475bd4c4cae436fafcc0da/tokio/src/sync/semaphore_ll.rs | 1,201 | 1,219 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:4 | /// Represents "one requested permit" in the waiter state
const PERMIT_ONE: usize = 0b1000;
/// Masks the waiter state to only contain bits tracking number of requested
/// permits.
const PERMIT_MASK: usize = usize::MAX - (PERMIT_ONE - 1);
/// How much to shift a permit count to pack it into the waker state
const PER... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:5 | // Allocations are aligned
debug_assert!(ptr.as_ptr() as usize & NUM_FLAG == 0);
let state = SemState::new(permits, &stub);
Semaphore {
state: AtomicUsize::new(state.to_usize()),
head: CausalCell::new(ptr),
rx_lock: AtomicUsize::new(0),
stub,
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:6 | }
}
/// Poll for a permit
///
/// Tries to acquire available permits first. If unable to acquire a
/// sufficient number of permits, the caller's waiter is pushed onto the
/// semaphore's wait queue.
fn poll_acquire2<F>(
&self,
num_permits: u16,
mut get_waiter: F,
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:8 | debug_assert_ne!(curr.0, 0);
debug_assert_ne!(next.0, 0);
match self.state.compare_exchange(curr.0, next.0, AcqRel, Acquire) {
Ok(_) => {
if acquired {
// Successfully acquire permits **without** queuing the
// ... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:9 | /// Close the semaphore. This prevents the semaphore from issuing new
/// permits and notifies all pending waiters.
pub(crate) fn close(&self) {
// Acquire the `rx_lock`, setting the "closed" flag on the lock.
let prev = self.rx_lock.fetch_or(1, AcqRel);
if prev != 0 {
// An... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:10 | // Release the permits and notify
self.add_permits_locked2(rem, closed);
let n = rem << 1;
let actual = if closed {
let actual = self.rx_lock.fetch_sub(n | 1, AcqRel);
closed = false;
actual
} else {
let ac... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:15 | // This function is only called when there are pending tasks. Because of
// this, the state must *always* be in pointer mode.
let prev = prev.waiter().unwrap();
// No cycles plz
debug_assert_ne!(prev, waiter);
// Release `task` to the consume end.
prev.as_ref().next.sto... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 561 | 620 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:16 | ///
/// The permit begins in the "unacquired" state.
pub(crate) fn new() -> Permit {
use PermitState::Acquired;
Permit {
waiter: None,
state: Acquired(0),
}
}
/// Returns true if the permit has been acquired
pub(crate) fn is_acquired(&self) -> bool {... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 601 | 660 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:17 | // dequeued, it must be re-queued.
if !waiter.try_inc_permits_to_acquire(delta as usize) {
let waiter = NonNull::from(&**waiter);
// Ignore the result. The check for
// `permits_to_acquire()` will converge the s... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 641 | 700 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:18 | match semaphore.poll_acquire(cx, num_permits - acquired, self)? {
Ready(()) => {
self.state = Acquired(num_permits);
Ready(Ok(()))
}
Pending => {
self.state = Waiti... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 681 | 740 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:19 | // Try to acquire the additional permits
semaphore.try_acquire(num_permits - requested)?;
}
self.state = Acquired(num_permits);
Ok(())
} else {
Err(TryAcquireError::NoPermits)
}
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 721 | 780 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:21 | let state = WaiterState(waiter.state.fetch_or(DROPPED, AcqRel));
if state.is_queued() {
// The waiter is stored in the queue. The semaphore will drop it
std::mem::forget(waiter);
}
}
}
}
// ===== impl AcquireError ====
impl AcquireError {
fn clo... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:22 | /// Returns true if the error was caused by calling `try_acquire` on a
/// semaphore with no available permits.
pub(crate) fn is_no_permits(&self) -> bool {
match self {
TryAcquireError::NoPermits => true,
_ => false,
}
}
}
impl fmt::Display for TryAcquireError {
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:27 | }
}
/// Returns a `State` tracking `ptr` as the tail of the queue.
fn new_ptr(tail: NonNull<Waiter>, closed: bool) -> SemState {
let mut val = tail.as_ptr() as usize;
if closed {
val |= CLOSED_FLAG;
}
SemState(val)
}
/// Returns the amount of remaining... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 1,041 | 1,100 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:28 | debug_assert!(num > 0);
if self.available_permits() < num {
return false;
}
debug_assert!(self.waiter().is_none());
self.0 -= num << NUM_SHIFT;
if self.0 == NUM_FLAG {
// Set the state to the stub pointer.
self.0 = stub as *const _ as usize... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 1,081 | 1,140 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:29 | if self.is_waiter() {
let waiter = NonNull::new(self.as_ptr()).expect("null pointer stored");
Some(waiter)
} else {
None
}
}
/// Assumes `self` represents a pointer
fn as_ptr(self) -> *mut Waiter {
(self.0 & !CLOSED_FLAG) as *mut Waiter
}
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | dcfa895b512e3ed522b81b18baf3e33fd78a600c | github | async-runtime | https://github.com/tokio-rs/tokio/blob/dcfa895b512e3ed522b81b18baf3e33fd78a600c/tokio/src/sync/semaphore_ll.rs | 1,121 | 1,180 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:21 | let state = WaiterState(waiter.state.fetch_or(DROPPED, AcqRel));
if state.is_queued() {
// The waiter is stored in the queue. The semaphore will drop it
std::mem::forget(waiter);
}
}
}
}
// ===== impl AcquireError ====
impl AcquireError {
fn clo... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | efcbf9613f2d5048550f9c828e3be422644f1391 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efcbf9613f2d5048550f9c828e3be422644f1391/tokio/src/sync/semaphore_ll.rs | 801 | 860 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:22 | /// Returns true if the error was caused by calling `try_acquire` on a
/// semaphore with no available permits.
pub(crate) fn is_no_permits(&self) -> bool {
match self {
TryAcquireError::NoPermits => true,
_ => false,
}
}
}
impl fmt::Display for TryAcquireError {
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | efcbf9613f2d5048550f9c828e3be422644f1391 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/efcbf9613f2d5048550f9c828e3be422644f1391/tokio/src/sync/semaphore_ll.rs | 841 | 900 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:1 | #![cfg_attr(not(feature = "sync"), allow(dead_code, unreachable_pub))]
//! Thread-safe, asynchronous counting semaphore.
//!
//! A `Semaphore` instance holds a set of permits. Permits are used to
//! synchronize access to a shared resource.
//!
//! Before accessing the shared resource, callers acquire a permit from th... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 1 | 60 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:2 | stub: Box<WaiterNode>,
}
/// A semaphore permit
///
/// Tracks the lifecycle of a semaphore permit.
///
/// An instance of `Permit` is intended to be used with a **single** instance of
/// `Semaphore`. Using a single instance of `Permit` with multiple semaphore
/// instances will result in unexpected behavior.
///
///... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 41 | 100 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:3 | ///
/// See `NodeState` for more details.
state: AtomicUsize,
/// Task to wake when a permit is made available.
waker: AtomicWaker,
/// Next pointer in the queue of waiting senders.
next: AtomicPtr<WaiterNode>,
}
/// Semaphore state
///
/// The 2 low bits track the modes.
///
/// - Closed
///... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 81 | 140 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:4 | #[repr(usize)]
enum NodeState {
/// Not waiting for a permit and the node is not in the wait queue.
///
/// This is the initial state.
Idle = 0,
/// Not waiting for a permit but the node is in the wait queue.
///
/// This happens when the waiter has previously requested a permit, but has
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 121 | 180 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:5 | let state = SemState::new(permits, &stub);
Semaphore {
state: AtomicUsize::new(state.to_usize()),
head: CausalCell::new(ptr),
rx_lock: AtomicUsize::new(0),
stub,
}
}
/// Returns the current number of available permits
pub(crate) fn available_... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 161 | 220 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:6 | };
}
loop {
let mut next = curr;
if curr.is_closed() {
undo_strong!();
return Ready(Err(AcquireError::closed()));
}
if !next.acquire_permit(&self.stub) {
debug_assert!(curr.waiter().is_some());
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 201 | 260 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:7 | debug_assert_ne!(curr.0, 0);
debug_assert_ne!(next.0, 0);
match next.compare_exchange(&self.state, curr, AcqRel, Acquire) {
Ok(_) => {
match curr.waiter() {
Some(prev_waiter) => {
let waiter = maybe_strong.u... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 241 | 300 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:8 | }
self.add_permits_locked(0, true);
}
/// Add `n` new permits to the semaphore.
pub(crate) fn add_permits(&self, n: usize) {
if n == 0 {
return;
}
// TODO: Handle overflow. A panic is not sufficient, the process must
// abort.
let prev = self.rx... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 281 | 340 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:9 | let actual = self.rx_lock.fetch_sub(n, AcqRel);
closed = actual & 1 == 1;
actual
};
rem = (actual >> 1) - rem;
}
}
/// Release a specific amount of permits to the semaphore
///
/// This function is called by `add_permits` after the add lo... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 321 | 380 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:10 | if head == stub {
let next = match NonNull::new(next_ptr) {
Some(next) => next,
None => {
// This loop is not part of the standard intrusive mpsc
// channel algorithm. This is where we atomically ... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 361 | 420 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:11 | let mut next = curr;
next.release_permits(rem, &self.stub);
match next.compare_exchange(&self.state, curr, AcqRel, Acquire) {
Ok(_) => return None,
Err(actual) => {
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 401 | 460 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:12 | self.head.with_mut(|head| *head = next);
return Some(Arc::from_raw(head.as_ptr()));
}
// Inconsistent state, loop
thread::yield_now();
}
}
}
unsafe fn push_stub(&self, closed: bool) {
let stub = self.stub();
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 441 | 500 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:13 | }
impl fmt::Debug for Semaphore {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Semaphore")
.field("state", &SemState::load(&self.state, Relaxed))
.field("head", &self.head.with(|ptr| ptr))
.field("rx_lock", &self.rx_lock.load(Relaxed))
... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 481 | 540 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:14 | ) -> Poll<Result<(), AcquireError>> {
match self.state {
PermitState::Idle => {}
PermitState::Waiting => {
let waiter = self.waiter.as_ref().unwrap();
if waiter.acquire(cx)? {
self.state = PermitState::Acquired;
ret... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 521 | 580 |
tokio-rs/tokio:tokio/src/sync/semaphore_ll.rs:15 | } else {
return Err(TryAcquireError::no_permits());
}
}
PermitState::Acquired => {
return Ok(());
}
}
match semaphore.poll_permit(None).map_err(to_try_acquire)? {
Ready(()) => {
self.stat... | rust | rust | macro-heavy | tokio-rs/tokio | tokio/src/sync/semaphore_ll.rs | MIT | 9211adbe01661585cd1831214279262024d04816 | github | async-runtime | https://github.com/tokio-rs/tokio/blob/9211adbe01661585cd1831214279262024d04816/tokio/src/sync/semaphore_ll.rs | 561 | 620 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.