summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxcancel/init.c
blob: 2007529d42738fabbddf6efa3bd1b2f63b9a2d48 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 *  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 <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>

#if defined(__rtems__)
  #include <rtems.h>
  #include <bsp.h>
  #include <pmacros.h>
#endif

const char rtems_test_name[] = "PSXCANCEL";

/* forward declarations to avoid warnings */
void *POSIX_Init(void *argument);
void countTask_cancel_handler(void *ignored);
void *countTaskDeferred(void *ignored);
void *countTaskAsync(void *ignored);

volatile bool countTask_handler;

void countTask_cancel_handler(void *ignored)
{
  countTask_handler = true;
}

void *countTaskDeferred(void *ignored)
{
  int i=0;
  int type,state;
  int sc;

  sc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type);
  fatal_posix_service_status( sc, 0, "cancel state deferred" );
  rtems_test_assert( type == PTHREAD_CANCEL_ENABLE );
  sc = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &state);
  fatal_posix_service_status( sc, 0, "cancel type deferred" );
  rtems_test_assert( state == PTHREAD_CANCEL_DEFERRED );
  while (1) {
    printf("countTaskDeferred: elapsed time (second): %2d\n", i++ );
    sleep(1);
    pthread_testcancel();
  }
}

void *countTaskAsync(void *ignored)
{
  int i=0;
  int type,state;
  int sc;

  sc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type);
  fatal_posix_service_status( sc, 0, "cancel state async" );
  rtems_test_assert( type == PTHREAD_CANCEL_ENABLE );
  sc = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &state);
  fatal_posix_service_status( sc, 0, "cancel type async" );
  rtems_test_assert( state == PTHREAD_CANCEL_DEFERRED );
  pthread_cleanup_push(countTask_cancel_handler, NULL);
  while (1) {
    printf("countTaskAsync: elapsed time (second): %2d\n", i++ );
    sleep(1);
  }
  countTask_handler = false;
  pthread_cleanup_pop(1);
  if ( countTask_handler == false ){
    puts("countTask_cancel_handler not executed");
    rtems_test_exit(0);
  }
}

#if defined(__rtems__)
  void *POSIX_Init(void *ignored)
#else
  int main(int argc, char **argv)
#endif
{
  pthread_t task;
  int       taskparameter = 0;
  int       sc;
  int       old;

  TEST_BEGIN();

  /* generate some error conditions */
  puts( "Init - pthread_setcancelstate - NULL oldstate - EINVAL" );
  sc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  fatal_posix_service_status( sc, EINVAL, "cancel state EINVAL" );

  puts( "Init - pthread_setcancelstate - bad state - EINVAL" );
  sc = pthread_setcancelstate(12, &old);
  fatal_posix_service_status( sc, EINVAL, "cancel state EINVAL" );

  puts( "Init - pthread_setcanceltype - NULL oldtype - EINVAL" );
  sc = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  fatal_posix_service_status( sc, EINVAL, "cancel type EINVAL" );

  puts( "Init - pthread_setcanceltype - bad type - EINVAL" );
  sc = pthread_setcanceltype(12, &old);
  fatal_posix_service_status( sc, EINVAL, "cancel type EINVAL" );

  puts( "Init - pthread_cancel - bad ID - EINVAL" );
  sc = pthread_cancel(0x100);
  fatal_posix_service_status( sc, EINVAL, "cancel bad Id" );

  /* Start countTask deferred */
  {
    sc = pthread_create(&task, NULL, countTaskDeferred, &taskparameter);
    if (sc) {
      perror("pthread_create: countTask");
      rtems_test_exit(EXIT_FAILURE);
    }
    /* sleep for 5 seconds, then cancel it */
    sleep(5);
    sc = pthread_cancel(task);
    fatal_posix_service_status( sc, 0, "cancel deferred" );
    sc = pthread_join(task, NULL);
    fatal_posix_service_status( sc, 0, "join deferred" );
  }

  /* Start countTask asynchronous */
  {
    sc = pthread_create(&task, NULL, countTaskAsync, &taskparameter);
    if (sc) {
      perror("pthread_create: countTask");
      rtems_test_exit(EXIT_FAILURE);
    }
    /* sleep for 5 seconds, then cancel it */
    sleep(5);
    sc = pthread_cancel(task);
    fatal_posix_service_status( sc, 0, "cancel async" );
    sc = pthread_join(task, NULL);
    fatal_posix_service_status( sc, 0, "join async" );
  }


  TEST_END();

  #if defined(__rtems__)
    rtems_test_exit(EXIT_SUCCESS);
    return NULL;
  #else
    return 0;
  #endif
}

/* configuration information */
#if defined(__rtems__)

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_MAXIMUM_POSIX_THREADS 2
#define CONFIGURE_POSIX_INIT_THREAD_TABLE

#define CONFIGURE_INIT
#include <rtems/confdefs.h>

#endif /* __rtems__ */