summaryrefslogtreecommitdiffstats
path: root/testsuites/psxtests/psxcancel/init.c
blob: 4f2d08d068c9dcd2b945a0fd5ebd030b2f628526 (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
/*
 * Thread Test Program
 *
 *  - test of POSIX's pthread_init() function from rtemstask Init()
 *
 *     ott@linux.thai.net
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 */


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>

#ifdef __rtems__
#include <rtems.h>
/* configuration information */

#define CONFIGURE_INIT

#include <unistd.h>
#include <errno.h>
#include <sched.h>

#include <bsp.h> /* for device driver prototypes */
#include <pmacros.h>

rtems_task Init( rtems_task_argument argument);

/* configuration information */

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_MAXIMUM_TASKS             3
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_EXTRA_TASK_STACKS         (3 * RTEMS_MINIMUM_STACK_SIZE)

#define CONFIGURE_MAXIMUM_POSIX_THREADS 5
#define CONFIGURE_MAXIMUM_POSIX_MUTEXES 5
#define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 5

#include <rtems/console.h>
#include <rtems/confdefs.h>

#endif /* __rtems__ */

void countTaskAsync(void);
void countTaskDeferred(void);

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

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

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

  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type);
  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &state);
  while (1) {
    printf("countTaskAsync: elapsed time (second): %2d\n", i++ );
    sleep(1);
    }
}

#ifdef __linux__
int main(){
#else
  rtems_task Init( rtems_task_argument ignored ) {
#endif

  pthread_t count;
  int taskparameter = 0;

  puts( "\n\n*** POSIX CANCEL TEST ***" );

  /* Start countTask deferred */
  {
    int task_ret;
    task_ret = pthread_create(&count, NULL, (void *) countTaskDeferred, (void *) &taskparameter);
    if (task_ret) {
      perror("pthread_create: countTask");
      rtems_test_exit(EXIT_FAILURE);
    }
    /* sleep for 5 seconds, then cancel it */
    sleep(5);
    pthread_cancel(count);
    pthread_join(count,NULL);
  }

  /* Start countTask asynchronous */
  {
    int task_ret;
    task_ret = pthread_create(&count, NULL, (void *) countTaskAsync, (void *) &taskparameter);
    if (task_ret) {
      perror("pthread_create: countTask");
      rtems_test_exit(EXIT_FAILURE);
    }
    /* sleep for 5 seconds, then cancel it */
    sleep(5);
    pthread_cancel(count);
    pthread_join(count,NULL);
  }


  puts( "*** END OF POSIX CANCEL TEST ***" );

#ifdef __linux__
  return 0;
#else
  rtems_test_exit(EXIT_SUCCESS);
#endif
}