proxygen
folly::detail::LifoSemHead Class Reference

#include <LifoSem.h>

Public Member Functions

uint32_t idx () const
 
uint32_t value () const
 
constexpr bool isNodeIdx () const
 
constexpr bool isShutdown () const
 
constexpr bool isLocked () const
 
constexpr uint32_t seq () const
 
LifoSemHead withPop (uint32_t idxNext) const
 
LifoSemHead withPush (uint32_t _idx) const
 Returns the LifoSemHead that results from pushing a new waiter node. More...
 
LifoSemHead withValueIncr (uint32_t delta) const
 
LifoSemHead withValueDecr (uint32_t delta) const
 Returns the LifoSemHead that results from decrementing the value. More...
 
LifoSemHead withShutdown () const
 
LifoSemHead withLock () const
 
LifoSemHead withoutLock (uint32_t idxNext) const
 
constexpr bool operator== (const LifoSemHead &rhs) const
 
constexpr bool operator!= (const LifoSemHead &rhs) const
 

Static Public Member Functions

static constexpr LifoSemHead fresh (uint32_t value)
 

Public Attributes

uint64_t bits
 

Private Types

enum  { IsNodeIdxShift = 32, IsShutdownShift = 33, IsLockedShift = 34, SeqShift = 35 }
 
enum  : uint64_t {
  IsNodeIdxMask = uint64_t(1) << IsNodeIdxShift, IsShutdownMask = uint64_t(1) << IsShutdownShift, IsLockedMask = uint64_t(1) << IsLockedShift, SeqIncr = uint64_t(1) << SeqShift,
  SeqMask = ~(SeqIncr - 1)
}
 

Detailed Description

LifoSemHead is a 64-bit struct that holds a 32-bit value, some state bits, and a sequence number used to avoid ABA problems in the lock-free management of the LifoSem's wait lists. The value can either hold an integral semaphore value (if there are no waiters) or a node index (see IndexedMemPool) for the head of a list of wait nodes

Definition at line 209 of file LifoSem.h.

Member Enumeration Documentation

anonymous enum
private
Enumerator
IsNodeIdxShift 
IsShutdownShift 
IsLockedShift 
SeqShift 

Definition at line 222 of file LifoSem.h.

Member Function Documentation

static constexpr LifoSemHead folly::detail::LifoSemHead::fresh ( uint32_t  value)
inlinestatic

This should only be used for initial construction, not for setting the value, because it clears the sequence number

Definition at line 267 of file LifoSem.h.

267  {
268  return LifoSemHead{value};
269  }
uint32_t value() const
Definition: LifoSem.h:246
uint32_t folly::detail::LifoSemHead::idx ( ) const
inline

Definition at line 241 of file LifoSem.h.

References uint32_t.

241  {
242  assert(isNodeIdx());
243  assert(uint32_t(bits) != 0);
244  return uint32_t(bits);
245  }
constexpr bool isNodeIdx() const
Definition: LifoSem.h:250
constexpr bool folly::detail::LifoSemHead::isLocked ( ) const
inline

Definition at line 256 of file LifoSem.h.

256  {
257  return (bits & IsLockedMask) != 0;
258  }
constexpr bool folly::detail::LifoSemHead::isNodeIdx ( ) const
inline

Definition at line 250 of file LifoSem.h.

250  {
251  return (bits & IsNodeIdxMask) != 0;
252  }
constexpr bool folly::detail::LifoSemHead::isShutdown ( ) const
inline

Definition at line 253 of file LifoSem.h.

253  {
254  return (bits & IsShutdownMask) != 0;
255  }
constexpr bool folly::detail::LifoSemHead::operator!= ( const LifoSemHead rhs) const
inline

Definition at line 339 of file LifoSem.h.

References Atom, and folly::detail::rhs.

339  {
340  return !(*this == rhs);
341  }
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
constexpr bool folly::detail::LifoSemHead::operator== ( const LifoSemHead rhs) const
inline

Definition at line 336 of file LifoSem.h.

References bits.

336  {
337  return bits == rhs.bits;
338  }
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
constexpr uint32_t folly::detail::LifoSemHead::seq ( ) const
inline

Definition at line 259 of file LifoSem.h.

References uint32_t.

uint32_t folly::detail::LifoSemHead::value ( ) const
inline

Definition at line 246 of file LifoSem.h.

References uint32_t.

246  {
247  assert(!isNodeIdx());
248  return uint32_t(bits);
249  }
constexpr bool isNodeIdx() const
Definition: LifoSem.h:250
LifoSemHead folly::detail::LifoSemHead::withLock ( ) const
inline

Definition at line 323 of file LifoSem.h.

323  {
324  assert(!isLocked());
325  return LifoSemHead{bits | IsLockedMask};
326  }
constexpr bool isLocked() const
Definition: LifoSem.h:256
LifoSemHead folly::detail::LifoSemHead::withoutLock ( uint32_t  idxNext) const
inline

Definition at line 330 of file LifoSem.h.

References withPop().

330  {
331  assert(isLocked());
332  // We need to treat this as a pop, as we may change the list head.
333  return LifoSemHead{bits & ~IsLockedMask}.withPop(idxNext);
334  }
constexpr bool isLocked() const
Definition: LifoSem.h:256
LifoSemHead folly::detail::LifoSemHead::withPop ( uint32_t  idxNext) const
inline

Returns the LifoSemHead that results from popping a waiter node, given the current waiter node's next ptr

Definition at line 273 of file LifoSem.h.

Referenced by withoutLock().

273  {
274  assert(!isLocked());
275  assert(isNodeIdx());
276  if (idxNext == 0) {
277  // no isNodeIdx bit or data bits. Wraparound of seq bits is okay
278  return LifoSemHead{(bits & (SeqMask | IsShutdownMask)) + SeqIncr};
279  } else {
280  // preserve sequence bits (incremented with wraparound okay) and
281  // isNodeIdx bit, replace all data bits
282  return LifoSemHead{(bits & (SeqMask | IsShutdownMask | IsNodeIdxMask)) +
283  SeqIncr + idxNext};
284  }
285  }
constexpr bool isNodeIdx() const
Definition: LifoSem.h:250
constexpr bool isLocked() const
Definition: LifoSem.h:256
LifoSemHead folly::detail::LifoSemHead::withPush ( uint32_t  _idx) const
inline

Returns the LifoSemHead that results from pushing a new waiter node.

Definition at line 288 of file LifoSem.h.

References folly::value().

288  {
289  assert(!isLocked());
290  assert(isNodeIdx() || value() == 0);
291  assert(!isShutdown());
292  assert(_idx != 0);
293  return LifoSemHead{(bits & SeqMask) | IsNodeIdxMask | _idx};
294  }
constexpr bool isNodeIdx() const
Definition: LifoSem.h:250
constexpr bool isShutdown() const
Definition: LifoSem.h:253
constexpr bool isLocked() const
Definition: LifoSem.h:256
uint32_t value() const
Definition: LifoSem.h:246
LifoSemHead folly::detail::LifoSemHead::withShutdown ( ) const
inline

Returns the LifoSemHead with the same state as the current node, but with the shutdown bit set

Definition at line 318 of file LifoSem.h.

318  {
319  return LifoSemHead{bits | IsShutdownMask};
320  }
LifoSemHead folly::detail::LifoSemHead::withValueDecr ( uint32_t  delta) const
inline

Returns the LifoSemHead that results from decrementing the value.

Definition at line 310 of file LifoSem.h.

References folly::value().

310  {
311  assert(!isLocked());
312  assert(delta > 0 && delta <= value());
313  return LifoSemHead{bits + SeqIncr - delta};
314  }
constexpr bool isLocked() const
Definition: LifoSem.h:256
uint32_t value() const
Definition: LifoSem.h:246
LifoSemHead folly::detail::LifoSemHead::withValueIncr ( uint32_t  delta) const
inline

Returns the LifoSemHead with value increased by delta, with saturation if the maximum value is reached

Definition at line 298 of file LifoSem.h.

References UNLIKELY.

298  {
299  assert(!isLocked());
300  assert(!isNodeIdx());
301  auto rv = LifoSemHead{bits + SeqIncr + delta};
302  if (UNLIKELY(rv.isNodeIdx())) {
303  // value has overflowed into the isNodeIdx bit
304  rv = LifoSemHead{(rv.bits & ~IsNodeIdxMask) | (IsNodeIdxMask - 1)};
305  }
306  return rv;
307  }
constexpr bool isNodeIdx() const
Definition: LifoSem.h:250
constexpr bool isLocked() const
Definition: LifoSem.h:256
#define UNLIKELY(x)
Definition: Likely.h:48

Member Data Documentation

uint64_t folly::detail::LifoSemHead::bits

Definition at line 237 of file LifoSem.h.

Referenced by operator==().


The documentation for this class was generated from the following file: