class Condition

Defined at line 714 of file ../../third_party/abseil-cpp/absl/synchronization/mutex.h

-----------------------------------------------------------------------------

Condition

-----------------------------------------------------------------------------

`Mutex` contains a number of member functions which take a `Condition` as an

argument; clients can wait for conditions to become `true` before attempting

to acquire the mutex. These sections are known as "condition critical"

sections. To use a `Condition`, you simply need to construct it, and use

within an appropriate `Mutex` member function; everything else in the

`Condition` class is an implementation detail.

A `Condition` is specified as a function pointer which returns a boolean.

`Condition` functions should be pure functions -- their results should depend

only on passed arguments, should not consult any external state (such as

clocks), and should have no side-effects, aside from debug logging. Any

objects that the function may access should be limited to those which are

constant while the mutex is blocked on the condition (e.g. a stack variable),

or objects of state protected explicitly by the mutex.

No matter which construction is used for `Condition`, the underlying

function pointer / functor / callable must not throw any

exceptions. Correctness of `Mutex` / `Condition` is not guaranteed in

the face of a throwing `Condition`. (When Abseil is allowed to depend

on C++17, these function pointers will be explicitly marked

`noexcept`; until then this requirement cannot be enforced in the

type system.)

Note: to use a `Condition`, you need only construct it and pass it to a

suitable `Mutex' member function, such as `Mutex::Await()`, or to the

constructor of one of the scope guard classes.

Example using LockWhen/Unlock:

// assume count_ is not internal reference count

int count_ ABSL_GUARDED_BY(mu_);

Condition count_is_zero(+[](int *count) { return *count == 0; },

&count

_);

mu_.LockWhen(count_is_zero);

// ...

mu_.Unlock();

Example using a scope guard:

{

MutexLock lock(

&mu

_, count_is_zero);

// ...

}

When multiple threads are waiting on exactly the same condition, make sure

that they are constructed with the same parameters (same pointer to function

+ arg, or same pointer to object + method), so that the mutex implementation

can avoid redundantly evaluating the same condition for each thread.

Public Members

static Condition kTrue

Public Methods

void Condition (bool (*)(void *) func, void * arg)

A Condition that returns the result of "(*func)(arg)"

template <typename T>
void Condition (bool (*)(T *) func, T * arg)

Templated version for people who are averse to casts.

To use a lambda, prepend it with unary plus, which converts the lambda

into a function pointer:

Condition(+[](T* t) { return ...; }, arg).

Note: lambdas in this case must contain no bound variables.

See class comment for performance advice.

Defined at line 1103 of file ../../third_party/abseil-cpp/absl/synchronization/mutex.h

template <typename T, typename = void>
void Condition (bool (*)(T *) func, typename absl::internal::type_identity<T>::type * arg)

Same as above, but allows for cases where `arg` comes from a pointer that

is convertible to the function parameter type `T*` but not an exact match.

For example, the argument might be `X*` but the function takes `const X*`,

or the argument might be `Derived*` while the function takes `Base*`, and

so on for cases where the argument pointer can be implicitly converted.

Implementation notes: This constructor overload is required in addition to

the one above to allow deduction of `T` from `arg` for cases such as where

a function template is passed as `func`. Also, the dummy `typename = void`

template parameter exists just to work around a MSVC mangling bug.

Defined at line 1112 of file ../../third_party/abseil-cpp/absl/synchronization/mutex.h

template <typename T>
void Condition (T * object, bool (absl::internal::type_identity<T>::type::*)() method)

Templated version for invoking a method that returns a `bool`.

`Condition(object,

&Class

::Method)` constructs a `Condition` that evaluates

`object->Method()`.

Implementation Note: `absl::internal::type_identity` is used to allow

methods to come from base classes. A simpler signature like

`Condition(T*, bool (T::*)())` does not suffice.

Defined at line 1118 of file ../../third_party/abseil-cpp/absl/synchronization/mutex.h

template <typename T>
void Condition (const T * object, bool (absl::internal::type_identity<T>::type::*)() const method)

Same as above, for const members

Defined at line 1127 of file ../../third_party/abseil-cpp/absl/synchronization/mutex.h

void Condition (const bool * cond)

A Condition that returns the value of `*cond`

template <typename T, typename E = decltype(static_cast<bool (T::*)() const>(
                                                &T::operator()))>
void Condition (const T * obj)

Implementation note: The second template parameter ensures that this

constructor doesn't participate in overload resolution if T doesn't have

`bool operator() const`.

Defined at line 793 of file ../../third_party/abseil-cpp/absl/synchronization/mutex.h

bool Eval ()

Evaluates the condition.

bool GuaranteedEqual (const Condition * a, const Condition * b)

Returns `true` if the two conditions are guaranteed to return the same

value if evaluated at the same time, `false` if the evaluation *may* return

different results.

Two `Condition` values are guaranteed equal if both their `func` and `arg`

components are the same. A null pointer is equivalent to a `true`

condition.