From a5385b1f729cf671f458c0b2128a0d64b0c307a6 Mon Sep 17 00:00:00 2001 From: Christian Mauderer Date: Thu, 20 Mar 2014 09:22:00 +0100 Subject: score: Unify pthread and gxx_wrapper once and move to score. --- cpukit/score/src/once.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 cpukit/score/src/once.c (limited to 'cpukit/score/src/once.c') diff --git a/cpukit/score/src/once.c b/cpukit/score/src/once.c new file mode 100644 index 0000000000..60ae7a78c4 --- /dev/null +++ b/cpukit/score/src/once.c @@ -0,0 +1,56 @@ +/* + * COPYRIGHT (c) 1989-1999. + * On-Line Applications Research Corporation (OAR). + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#ifdef HAVE_CONFIG_H + #include "config.h" +#endif + +#include +#include + +#include + +#define ONCE_STATE_NOT_RUN 0 +#define ONCE_STATE_RUNNING 1 +#define ONCE_STATE_COMPLETE 2 + +int _Once( int *once_state, void ( *init_routine )( void ) ) +{ + int eno = 0; + + if ( *once_state != ONCE_STATE_COMPLETE ) { + _Once_Lock(); + + /* + * Getting to here means the once_control is locked so we have: + * 1. The init has not run and the state is ONCE_STATE_NOT_RUN. + * 2. The init has finished and the state is ONCE_STATE_COMPLETE (already + * caught by the previous if). + * 3. The init is being run by this thread and the state + * ONCE_STATE_RUNNING so we are nesting. This is an error. + */ + + switch ( *once_state ) { + case ONCE_STATE_NOT_RUN: + *once_state = ONCE_STATE_RUNNING; + ( *init_routine )(); + *once_state = ONCE_STATE_COMPLETE; + break; + case ONCE_STATE_RUNNING: + eno = EINVAL; + break; + default: + break; + } + + _Once_Unlock(); + } + + return eno; +} -- cgit v1.2.3