summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxonce01/init.c
blob: 9f7f76b95d3243bae2dbf4680fd3b48eb3f17b67 (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
/*
 *  COPYRIGHT (c) 1989-2009.
 *  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

#define TEST_INIT

#define CONFIGURE_INIT
#include "system.h"

const char rtems_test_name[] = "PSXONCE 1";

static pthread_once_t nesting_once = PTHREAD_ONCE_INIT;

static void Test_init_routine_nesting( void )
{
  int status;
  puts( "Test_init_routine_nesting: invoked" );
  puts( "Test_init_routine_nesting: pthread_once - EINVAL (init_routine_nesting does not execute)" );
  status = pthread_once( &nesting_once, Test_init_routine_nesting );
  rtems_test_assert( status == EINVAL );
}

static int test_init_routine_call_counter = 0;

static void Test_init_routine( void )
{
  puts( "Test_init_routine: invoked" );
  ++test_init_routine_call_counter;
}

rtems_task Init(rtems_task_argument argument)
{
  int status;
  pthread_once_t once = PTHREAD_ONCE_INIT;

  TEST_BEGIN();

  puts( "Init: pthread_once - EINVAL (NULL once_control)" );
  status = pthread_once( NULL, Test_init_routine );
  rtems_test_assert( status == EINVAL );

  puts( "Init: pthread_once - EINVAL (NULL init_routine)" );
  status = pthread_once( &once, NULL );
  rtems_test_assert( status == EINVAL );

  puts( "Init: pthread_once - SUCCESSFUL (init_routine executes)" );
  status = pthread_once( &once, Test_init_routine );
  rtems_test_assert( !status );
  printf( "Init: call counter: %d\n", test_init_routine_call_counter );
  rtems_test_assert( test_init_routine_call_counter == 1 );

  puts( "Init: pthread_once - SUCCESSFUL (init_routine does not execute)" );
  status = pthread_once( &once, Test_init_routine );
  rtems_test_assert( !status );
  printf( "Init: call counter: %d\n", test_init_routine_call_counter );
  rtems_test_assert( test_init_routine_call_counter == 1 );

  puts( "Init: pthread_once - SUCCESSFUL (init_routine_nesting executes)" );
  status = pthread_once( &nesting_once, Test_init_routine_nesting );
  rtems_test_assert( !status );

  TEST_END();
  rtems_test_exit( 0 );
}