1 | /* |
---|
2 | * The license and distribution terms for this file may be |
---|
3 | * found in the file LICENSE in this distribution or at |
---|
4 | * http://www.rtems.com/license/LICENSE. |
---|
5 | * |
---|
6 | * $Id$ |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <pthread.h> |
---|
13 | #include <sys/time.h> |
---|
14 | #include <unistd.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <sched.h> |
---|
17 | |
---|
18 | |
---|
19 | #if defined(__rtems__) |
---|
20 | #include <rtems.h> |
---|
21 | #include <bsp.h> |
---|
22 | #include <pmacros.h> |
---|
23 | #endif |
---|
24 | |
---|
25 | void *countTaskDeferred(void *ignored) |
---|
26 | { |
---|
27 | int i=0; |
---|
28 | int type,state; |
---|
29 | |
---|
30 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type); |
---|
31 | pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &state); |
---|
32 | while (1) { |
---|
33 | printf("countTaskDeferred: elapsed time (second): %2d\n", i++ ); |
---|
34 | sleep(1); |
---|
35 | pthread_testcancel(); |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | void *countTaskAsync(void *ignored) |
---|
40 | { |
---|
41 | int i=0; |
---|
42 | int type,state; |
---|
43 | |
---|
44 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type); |
---|
45 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &state); |
---|
46 | while (1) { |
---|
47 | printf("countTaskAsync: elapsed time (second): %2d\n", i++ ); |
---|
48 | sleep(1); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | #if defined(__rtems__) |
---|
53 | void *POSIX_Init(void *ignored) |
---|
54 | #else |
---|
55 | int main(int argc, char **argv) |
---|
56 | #endif |
---|
57 | { |
---|
58 | pthread_t task; |
---|
59 | int taskparameter = 0; |
---|
60 | |
---|
61 | puts( "\n\n*** POSIX CANCEL TEST ***" ); |
---|
62 | |
---|
63 | /* Start countTask deferred */ |
---|
64 | { |
---|
65 | int task_ret; |
---|
66 | task_ret = pthread_create(&task, NULL, countTaskDeferred, &taskparameter); |
---|
67 | if (task_ret) { |
---|
68 | perror("pthread_create: countTask"); |
---|
69 | rtems_test_exit(EXIT_FAILURE); |
---|
70 | } |
---|
71 | /* sleep for 5 seconds, then cancel it */ |
---|
72 | sleep(5); |
---|
73 | pthread_cancel(task); |
---|
74 | pthread_join(task, NULL); |
---|
75 | } |
---|
76 | |
---|
77 | /* Start countTask asynchronous */ |
---|
78 | { |
---|
79 | int task_ret; |
---|
80 | task_ret = pthread_create(&task, NULL, countTaskAsync, &taskparameter); |
---|
81 | if (task_ret) { |
---|
82 | perror("pthread_create: countTask"); |
---|
83 | rtems_test_exit(EXIT_FAILURE); |
---|
84 | } |
---|
85 | /* sleep for 5 seconds, then cancel it */ |
---|
86 | sleep(5); |
---|
87 | pthread_cancel(task); |
---|
88 | pthread_join(task, NULL); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | puts( "*** END OF POSIX CANCEL TEST ***" ); |
---|
93 | |
---|
94 | #if defined(__rtems__) |
---|
95 | rtems_test_exit(EXIT_SUCCESS); |
---|
96 | return NULL; |
---|
97 | #else |
---|
98 | return 0; |
---|
99 | #endif |
---|
100 | } |
---|
101 | |
---|
102 | /* configuration information */ |
---|
103 | #if defined(__rtems__) |
---|
104 | |
---|
105 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
106 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
107 | |
---|
108 | #define CONFIGURE_MAXIMUM_POSIX_THREADS 2 |
---|
109 | #define CONFIGURE_POSIX_INIT_THREAD_TABLE |
---|
110 | |
---|
111 | #define CONFIGURE_INIT |
---|
112 | #include <rtems/confdefs.h> |
---|
113 | |
---|
114 | #endif /* __rtems__ */ |
---|
115 | |
---|