summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/once.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpukit/score/src/once.c')
-rw-r--r--cpukit/score/src/once.c56
1 files changed, 56 insertions, 0 deletions
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 <rtems/score/onceimpl.h>
+#include <rtems/score/apimutex.h>
+
+#include <errno.h>
+
+#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;
+}