MySQL 8.3.0
Source Code Documentation
os_once Class Reference

Execute a given function exactly once in a multi-threaded environment or wait for the function to be executed by another thread. More...

#include <os0once.h>

Public Types

typedef uint32_t state_t
 Control variables' state type. More...
 

Static Public Member Functions

static void do_or_wait_for_done (std::atomic< state_t > *state, void(*do_func)(void *), void *do_func_arg)
 Call a given function or wait its execution to complete if it is already called by another thread. More...
 

Static Public Attributes

static const state_t NEVER_DONE = 0
 Not yet executed. More...
 
static const state_t IN_PROGRESS = 1
 Currently being executed by this or another thread. More...
 
static const state_t DONE = 2
 Finished execution. More...
 

Detailed Description

Execute a given function exactly once in a multi-threaded environment or wait for the function to be executed by another thread.

Example usage: First the user must create a control variable of type os_once::state_t and assign it os_once::NEVER_DONE. Then the user must pass this variable, together with a function to be executed to os_once::do_or_wait_for_done().

Multiple threads can call os_once::do_or_wait_for_done() simultaneously with the same (os_once::state_t) control variable. The provided function will be called exactly once and when os_once::do_or_wait_for_done() returns then this function has completed execution, by this or another thread. In other words os_once::do_or_wait_for_done() will either execute the provided function or will wait for its execution to complete if it is already called by another thread or will do nothing if the function has already completed its execution earlier.

This mimics pthread_once(3), but unfortunately pthread_once(3) does not support passing arguments to the init_routine() function. We should use std::call_once() when we start compiling with C++11 enabled.

Member Typedef Documentation

◆ state_t

typedef uint32_t os_once::state_t

Control variables' state type.

Member Function Documentation

◆ do_or_wait_for_done()

static void os_once::do_or_wait_for_done ( std::atomic< state_t > *  state,
void(*)(void *)  do_func,
void *  do_func_arg 
)
inlinestatic

Call a given function or wait its execution to complete if it is already called by another thread.

Parameters
[in,out]statecontrol variable
[in]do_funcfunction to call
[in,out]do_func_argan argument to pass to do_func().

Member Data Documentation

◆ DONE

const state_t os_once::DONE = 2
static

Finished execution.

◆ IN_PROGRESS

const state_t os_once::IN_PROGRESS = 1
static

Currently being executed by this or another thread.

◆ NEVER_DONE

const state_t os_once::NEVER_DONE = 0
static

Not yet executed.


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