summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/src/pthreadonce.c
blob: 87a3b53ef6198bd34d37683f030c6160817fd384 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * @file
 *
 * @brief Call to function by Thread will call init_routine with no Arguments
 * @ingroup POSIXAPI
 */

/*
 *  16.1.8 Dynamic Package Initialization, P1003.1c/Draft 10, p. 154
 *
 *  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.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <pthread.h>
#include <errno.h>

#include <rtems/score/apimutex.h>

#define PTHREAD_ONCE_INIT_NOT_RUN  0
#define PTHREAD_ONCE_INIT_RUNNING  1
#define PTHREAD_ONCE_INIT_COMPLETE 2

int pthread_once(
  pthread_once_t  *once_control,
  void           (*init_routine)(void)
)
{
  int r = 0;

  if ( !once_control || !init_routine )
    return EINVAL;

  if ( once_control->is_initialized != 1 )
    return EINVAL;

  if ( once_control->init_executed != PTHREAD_ONCE_INIT_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 PTHREAD_ONCE_INIT_NOT_RUN.
     *  2. The init has finished and the state is PTHREAD_ONCE_INIT_RUN.
     *  3. The init is being run by this thread and the state
     *     PTHREAD_ONCE_INIT_RUNNING so we are nesting. This is an error.
     */

    switch ( once_control->init_executed ) {
      case PTHREAD_ONCE_INIT_NOT_RUN:
        once_control->init_executed = PTHREAD_ONCE_INIT_RUNNING;
        (*init_routine)();
        once_control->init_executed = PTHREAD_ONCE_INIT_COMPLETE;
        break;
      case PTHREAD_ONCE_INIT_RUNNING:
        r = EINVAL;
        break;
      default:
        break;
    }

    _Once_Unlock();
  }

  return r;
}