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 | * $Id: testthread.c,v 1.1 2002/03/11 22:05:24 pattara Exp $ |
---|
9 | * |
---|
10 | */ |
---|
11 | |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include <pthread.h> |
---|
16 | #include <sys/time.h> |
---|
17 | |
---|
18 | #ifdef RTEMS |
---|
19 | #include <rtems.h> |
---|
20 | /* configuration information */ |
---|
21 | |
---|
22 | #define CONFIGURE_INIT |
---|
23 | |
---|
24 | #include <unistd.h> |
---|
25 | #include <errno.h> |
---|
26 | #include <sched.h> |
---|
27 | |
---|
28 | #include <bsp.h> /* for device driver prototypes */ |
---|
29 | |
---|
30 | rtems_task Init( rtems_task_argument argument); |
---|
31 | |
---|
32 | /* configuration information */ |
---|
33 | |
---|
34 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
35 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
36 | |
---|
37 | #define CONFIGURE_MAXIMUM_TASKS 3 |
---|
38 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
39 | #define CONFIGURE_EXTRA_TASK_STACKS (3 * RTEMS_MINIMUM_STACK_SIZE) |
---|
40 | |
---|
41 | #define CONFIGURE_MAXIMUM_POSIX_THREADS 5 |
---|
42 | #define CONFIGURE_MAXIMUM_POSIX_MUTEXES 5 |
---|
43 | #define CONFIGURE_MAXIMUM_POSIX_CONDITION_VARIABLES 5 |
---|
44 | |
---|
45 | #include <console.h> |
---|
46 | #include <confdefs.h> |
---|
47 | |
---|
48 | #endif /* RTEMS */ |
---|
49 | |
---|
50 | void countTaskDeferred() { |
---|
51 | int i=0; |
---|
52 | int type,state; |
---|
53 | |
---|
54 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type); |
---|
55 | pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &state); |
---|
56 | while (1) { |
---|
57 | printf("countTaskDeferred: elapsed time (second): %2d\n", i++ ); |
---|
58 | sleep(1); |
---|
59 | pthread_testcancel(); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | void countTaskAsync() { |
---|
64 | int i=0; |
---|
65 | int type,state; |
---|
66 | |
---|
67 | pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type); |
---|
68 | pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &state); |
---|
69 | while (1) { |
---|
70 | printf("countTaskAsync: elapsed time (second): %2d\n", i++ ); |
---|
71 | sleep(1); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | #ifdef LINUX |
---|
76 | int main(){ |
---|
77 | #else |
---|
78 | rtems_task Init( rtems_task_argument ignored ) { |
---|
79 | #endif |
---|
80 | |
---|
81 | pthread_t count; |
---|
82 | int taskparameter = 0; |
---|
83 | |
---|
84 | /* Start countTask deferred */ |
---|
85 | { |
---|
86 | int task_ret; |
---|
87 | task_ret = pthread_create(&count, NULL, (void *) countTaskDeferred, (void *) &taskparameter); |
---|
88 | if (task_ret) { |
---|
89 | perror("pthread_create: countTask"); |
---|
90 | exit(EXIT_FAILURE); |
---|
91 | } |
---|
92 | /* sleep for 5 seconds, then cancel it */ |
---|
93 | sleep(5); |
---|
94 | pthread_cancel(count); |
---|
95 | pthread_join(count,NULL); |
---|
96 | } |
---|
97 | |
---|
98 | /* Start countTask asynchronous */ |
---|
99 | { |
---|
100 | int task_ret; |
---|
101 | task_ret = pthread_create(&count, NULL, (void *) countTaskAsync, (void *) &taskparameter); |
---|
102 | if (task_ret) { |
---|
103 | perror("pthread_create: countTask"); |
---|
104 | exit(EXIT_FAILURE); |
---|
105 | } |
---|
106 | /* sleep for 5 seconds, then cancel it */ |
---|
107 | sleep(5); |
---|
108 | pthread_cancel(count); |
---|
109 | pthread_join(count,NULL); |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | fprintf(stderr,"Done.\n"); |
---|
114 | |
---|
115 | #ifdef LINUX |
---|
116 | return 0; |
---|
117 | #else |
---|
118 | exit(EXIT_SUCCESS); |
---|
119 | #endif |
---|
120 | } |
---|
121 | |
---|