Macros
picotm-lib-global-state.h File Reference

Contains global-state helper macros. More...

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

Macros

#define __PICOTM_GLOBAL_STATE_GET(_name)
 
#define __PICOTM_GLOBAL_STATE_IMPL(_static, _name)
 
#define __PICOTM_GLOBAL_STATE_REF(_name)
 
#define __PICOTM_GLOBAL_STATE_UNREF(_name)
 
#define PICOTM_GLOBAL_STATE_GET(_name)
 
#define PICOTM_GLOBAL_STATE_REF(_name, _error)
 
#define PICOTM_GLOBAL_STATE_STATIC_IMPL(_name)
 
#define PICOTM_GLOBAL_STATE_UNREF(_name)
 

Detailed Description

Picotm provides helper macros for maintaining shared state. Oftentimes a single shared state is used throughout a module. For this case, picotm provides additional helper macros that maintain such global state. The example below declares shared state of type struct shared.

struct shared {
int data1;
int data2;
};
void
init_shared_fields(struct shared* shared, struct picotm_error* error)
{
shared->data1 = 0;
shared->data2 = 0;
}
void
uninit_shared_fields(struct shared* shared)
{
// nothing to do
}
PICOTM_SHARED_STATE(shared, struct shared);
init_shared_fields,
uninit_shared_fields)

A single global state variable for an existing shared state is declared with PICOTM_GLOBAL_STATE_STATIC_IMPL(). This macro receives the name of the shared state and expands to an implementation.

Users acquire a reference to the global state with PICOTM_GLOBAL_STATE_REF() and release a previously acquired reference with a call to PICOTM_GLOBAL_STATE_UNREF().

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

Global state is implemented on top of shared state, so the same rules for thread-safety apply. All references and releases are serialized with each other and the initializer and clean-up functions. Concurrent access to shared data files requires additional concurrency control.

Macro Definition Documentation

◆ __PICOTM_GLOBAL_STATE_GET

#define __PICOTM_GLOBAL_STATE_GET (   _name)
Warning
This is an internal interface. Don't use it in application code.

◆ __PICOTM_GLOBAL_STATE_IMPL

#define __PICOTM_GLOBAL_STATE_IMPL (   _static,
  _name 
)
Warning
This is an internal interface. Don't use it in application code.

◆ __PICOTM_GLOBAL_STATE_REF

#define __PICOTM_GLOBAL_STATE_REF (   _name)
Warning
This is an internal interface. Don't use it in application code.

◆ __PICOTM_GLOBAL_STATE_UNREF

#define __PICOTM_GLOBAL_STATE_UNREF (   _name)
Warning
This is an internal interface. Don't use it in application code.