picotm-lib-thread-state.h File Reference

Contains thread-state helper macros. More...

#include <stddef.h>
#include "compiler.h"
#include "picotm-lib-state.h"

Detailed Description

On top of the set of state helper macros, picotm provides thread-state helper macros. These macros provide a single per-thread instance of a state variable.

Thread-local state requires the definition of a regular state variable.

struct state {
int data1;
int data2;
};
void
init_state_fields(struct state* state, struct picotm_error* error)
{
state->data1 = 0;
state->data2 = 0;
}
void
uninit_state_fields(struct state* state)
{
// nothing to do
}
PICOTM_STATE(state, struct state);
PICOTM_STATE_STATIC_IMPL(state, struct state,
init_state_fields,
uninit_state_fields)

PICOTM_THREAD_STATE_STATIC_IMPL() defines the thread-local state for the state variable.

PICOTM_THREAD_STATE_STATIC_IMPL(state)

The macros PICOTM_THREAD_STATE_ACQUIRE() and PICOTM_THREAD_STATE_RELEASE() acquire and release the state variable in the same way as their state counterparts. The difference is that there's only one instance of the state variable for each thread.

struct picotm_error error = PICOTM_ERROR_INITIALIZER;
struct state* state = PICOTM_THREAD_STATE_ACQUIRE(state, true, &error);
if (picotm_error_is_set(&error)) {
// perform error handling
}
// ...
// do something with 'state'
// ...
// In thread-local cleanup code
PICOTM_THREAD_STATE_RELEASE(state, state_var);