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