template <typename Derived, bool DebugAccountingEnabled, bool TracingEnabled>

class ChainLockTransaction

Defined at line 78 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

CRTP type that provides the basic environment-independent framework for chainlock transactions.

Environment-specific chain lock transaction types should subclass the ChainLockTransaction CRTP

type and must provide the following definitions to adapt to the host environment:

1. A nested StateSaver template type to handle environment-specific state save, restore, and

relax operations.

Example:

enum class StateType { A = 0, B = 1 };

template

<StateType

state_type>

struct StateSaver;

template

<

>

struct StateSaver

<StateType

::A> {

StateSaver() : state_{SaveStateTypeA()} {}

~StateSaver() { RestoreStateTypeA(state_); }

void Relax(ChainLockTransaction

&

transaction) {

transaction.Unregister();

RestoreStateTypeA(state_); // Environment-specific.

transaction.WaitForConflictResolution(); // Environment-specific.

state_ = SaveStateTypeA(); // Environment-specific.

transaction.Register();

}

StateTypeA state_;

};

TODO(eieio): Complete explanation of derived type requirements.

Public Members

static const bool DebugAccountingEnabled
static const bool ContentionTracingEnabled

Public Methods

template <auto option, typename Callable, typename = EnableIfTransactionHandler<Callable>>
auto UntilDone (Option<option> , CallsiteInfo callsite_info, Callable && callable)

UntilDone provides a common sequence for executing a chain lock transaction and the required

backoff arbitration logic.

The caller is responsble for providing three arguments:

1. An instance of Option

<constant

>: Used to pass a compile-time constant to the environment-

provided StateSaver

<constant

> type, indicating the relevant states to save, such as IRQ

enable flags, preemption flags, etc..., and restore after each transaction attempt.

2. An instance of CallsiteTraits

<Derived

>::CallsiteInfo: Used for debugging and tracing to

identify the code location of the transaction.

3. A callable implementing a single transaction attempt: This can be a function, lambda, or

functor with an appropriate signature and static annotations.

The callable should be invocable matching one of the following signatures:

a. ChainLockTransactionCommon::Result

<

>() : A transaction that has a return type void.

c. ChainLockTransactionCommon::Result

<T

>(): A transaction that has user defined return type T.

b. ChainLockTransactionCommon::DoneType() : A transaction that has an implied return type void.

The overall return type of UntilDone is either T or void, depending on signature of Callable,

as described above. Note that Result

<

>, Result

<T

>, and DoneType are transitively inherited

from ChainLockTransactionCommon through this ChainLockTransaction CRTP type and may be referred

to using the environment-specific transaction type.

A basic transaction is structured as follows:

1. Define a callable that will be executed until the transaction is complete. In this example,

the callable will return zx_status_t when the transaction is complete, which will become the

return value of UntilDone.

auto do_transaction = [

&

]() __TA_REQUIRES(chainlock_transaction_token,

<env

-specific tokens>)

-> ChainLockTransaction::Result

<zx

_status_t> {

if (!object->get_lock().AcquireOrBackoff()) {

return ChainLockTransaction::Action::Backoff; // Execute the backoff protocol and re-enter.

}

if (restart_transaciton_condition) {

object->get_lock().Release();

return ChainLockTransaction::Action::Restart; // Restart the transaction and re-enter.

}

if (!LockedOperationOnObject(object)) {

object->get_lock().Release();

return ZX_ERR_BAD_STATE; // Complete the transaction, returning the error to the caller.

}

object->get_lock().Release();

return ZX_OK; // Complete the transaction, returning success to the caller.

};

In cases where the return user return type is void, the callable can return

ChainLockTransaction::Done to complete the transaction.

2. Execute the transaction using the given options and callable. In this example, the constant

kStateOptionA is a value of type Option

<auto

option> with an environment-specific option value

to pass to the state saver. The callable is invoked cyclically, with intervening backoffs,

until a terminal value (of a user defined return type in this case) is returned.

return ChainLockTransaction::UntilDone(kStateOptionA, CLT_CALLSITE("foo"), do_transaction);

Defined at line 160 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

uint64_t backoff_count ()

Defined at line 208 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void AssertAtLeastOneLockHeld ()

Debug asserts for various transaction states.

Defined at line 211 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void AssertAtMostOneLockHeld ()

Defined at line 216 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void AssertNumLocksHeld (uint32_t expected)

Defined at line 221 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void AssertFinalized ()

Defined at line 227 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void AssertNotFinalized ()

Defined at line 232 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void AssertActive ()

Assert that a transaction is active to static analysis.

Defined at line 239 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void MarkActive ()

Assert that a transaction is active to static analysis without actually checking at runtime for

efficiency in trivial cases.

Defined at line 245 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

Derived & ActiveRef ()

Returns a reference to the active transaction. Asserts that a transaction is active.

Defined at line 248 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

SavedState SaveStateAndUseReservedToken ()

Saves the pointer to the current transaction and allocates a reserved token to sync leaf chain

locks to during critical sequences.

Defined at line 256 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

SavedState SaveStateInitial ()

Create a save state for a bare transaction used to kick off a new thread.

Defined at line 265 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void RestoreState (SavedState saved_state)

Restore the active transaction using the given saved state.

Defined at line 271 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void Finalize ()

Directly finalize the transaction, ending an active contention tracing duration, if any.

Defined at line 280 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void Restart (CallsiteInfo callsite_info)

Restarts this transaction, resetting key state including the callsite info.

Defined at line 287 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

Derived MakeBareTransaction (CallsiteInfo callsite_info)

Escape hatch methods allowing transactions to be instantiated outside the scope of the

preferred UntilDone interface.

Defined at line 362 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

Derived MakeBareTransaction (CallsiteInfo callsite_info, uint32_t lock_held, bool finalized)

Defined at line 366 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void ~ChainLockTransaction<Derived, DebugAccountingEnabled, TracingEnabled> ()

Defined at line 371 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

Protected Methods

void ChainLockTransaction<Derived, DebugAccountingEnabled, TracingEnabled> (CallsiteInfo callsite_info)

Defined at line 374 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void ChainLockTransaction<Derived, DebugAccountingEnabled, TracingEnabled> (CallsiteInfo info, uint32_t locks_held, bool finalized)

Defined at line 377 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void Register ()

Defined at line 380 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void Unregister ()

Defined at line 384 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

void SetFinalized (bool finalized)

Defined at line 389 of file ../../zircon/system/ulib/concurrent/include/lib/concurrent/chainlock_transaction.h

Records