1 | /* |
---|
2 | * Copyright (c) 2013 Gedare Bloom. |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.com/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #include <rtems.h> |
---|
10 | |
---|
11 | #include <stdio.h> |
---|
12 | #include "tmacros.h" |
---|
13 | |
---|
14 | /* configuration information */ |
---|
15 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
16 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
17 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
18 | #define CONFIGURE_MAXIMUM_TASKS 3 |
---|
19 | #define CONFIGURE_MAXIMUM_SEMAPHORES 2 |
---|
20 | #define CONFIGURE_INIT |
---|
21 | #include <rtems/confdefs.h> |
---|
22 | |
---|
23 | rtems_task Task01(rtems_task_argument ignored); |
---|
24 | rtems_task Task02(rtems_task_argument ignored); |
---|
25 | rtems_task Init(rtems_task_argument ignored); |
---|
26 | |
---|
27 | static int getprio(void) |
---|
28 | { |
---|
29 | rtems_status_code status; |
---|
30 | rtems_task_priority pri; |
---|
31 | |
---|
32 | status = rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &pri); |
---|
33 | directive_failed( status, "rtems_task_set_priority"); |
---|
34 | return (int)pri; |
---|
35 | } |
---|
36 | |
---|
37 | rtems_id Task_id[2]; |
---|
38 | rtems_name Task_name[2]; |
---|
39 | |
---|
40 | rtems_id sem_id[2]; |
---|
41 | rtems_name sem_name[2]; |
---|
42 | |
---|
43 | rtems_task Init(rtems_task_argument ignored) |
---|
44 | { |
---|
45 | rtems_status_code status; |
---|
46 | rtems_attribute sem_attr; |
---|
47 | |
---|
48 | printf("\n*** TEST SEM01 ***\n"); |
---|
49 | |
---|
50 | sem_attr = RTEMS_INHERIT_PRIORITY | RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY; |
---|
51 | |
---|
52 | sem_name[0] = rtems_build_name( 'S','0',' ',' '); |
---|
53 | status = rtems_semaphore_create( |
---|
54 | sem_name[0], |
---|
55 | 1, |
---|
56 | sem_attr, |
---|
57 | 0, |
---|
58 | &sem_id[0] |
---|
59 | ); |
---|
60 | directive_failed( status, "rtems_semaphore_create of S0"); |
---|
61 | printf("init: S0 created\n"); |
---|
62 | |
---|
63 | sem_name[1] = rtems_build_name( 'S','1',' ',' '); |
---|
64 | status = rtems_semaphore_create( |
---|
65 | sem_name[1], |
---|
66 | 1, |
---|
67 | sem_attr, |
---|
68 | 0, |
---|
69 | &sem_id[1] |
---|
70 | ); |
---|
71 | directive_failed( status, "rtems_semaphore_create of S1"); |
---|
72 | printf("init: S1 created\n"); |
---|
73 | |
---|
74 | Task_name[0] = rtems_build_name( 'T','A','0','1'); |
---|
75 | status = rtems_task_create( |
---|
76 | Task_name[0], |
---|
77 | 36, |
---|
78 | RTEMS_MINIMUM_STACK_SIZE, |
---|
79 | RTEMS_DEFAULT_MODES, |
---|
80 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
81 | &Task_id[0] |
---|
82 | ); |
---|
83 | directive_failed( status, "rtems_task_create of TA01"); |
---|
84 | printf("init: TA01 created with priority 36\n"); |
---|
85 | |
---|
86 | Task_name[1] = rtems_build_name( 'T','A','0','2'); |
---|
87 | status = rtems_task_create( |
---|
88 | Task_name[1], |
---|
89 | 34, |
---|
90 | RTEMS_MINIMUM_STACK_SIZE, |
---|
91 | RTEMS_DEFAULT_MODES, |
---|
92 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
93 | &Task_id[1] |
---|
94 | ); |
---|
95 | directive_failed( status , "rtems_task_create of TA02\n"); |
---|
96 | printf("init: TA02 created with priority 34\n"); |
---|
97 | |
---|
98 | status = rtems_task_start( Task_id[0], Task01, 0); |
---|
99 | directive_failed( status, "rtems_task_start of TA01"); |
---|
100 | |
---|
101 | status = rtems_task_delete( RTEMS_SELF); |
---|
102 | directive_failed( status, "rtems_task_delete of INIT"); |
---|
103 | } |
---|
104 | |
---|
105 | /* Task01 starts with priority 36 */ |
---|
106 | rtems_task Task01(rtems_task_argument ignored) |
---|
107 | { |
---|
108 | rtems_status_code status; |
---|
109 | printf("TA01: started with priority %d\n", getprio()); |
---|
110 | |
---|
111 | status = rtems_semaphore_obtain( sem_id[0], RTEMS_WAIT, 0 ); |
---|
112 | directive_failed( status, "rtems_semaphore_obtain of S0\n"); |
---|
113 | printf("TA01: priority %d, holding S0\n", getprio()); |
---|
114 | |
---|
115 | status = rtems_semaphore_obtain( sem_id[1], RTEMS_WAIT, 0 ); |
---|
116 | directive_failed( status, "rtems_semaphore_obtain of S1"); |
---|
117 | printf("TA01: priority %d, holding S0, S1\n", getprio()); |
---|
118 | |
---|
119 | /* Start Task 2 (TA02) with priority 34. It will run immediately. */ |
---|
120 | status = rtems_task_start( Task_id[1], Task02, 0); |
---|
121 | directive_failed( status, "rtems_task_start of TA02\n"); |
---|
122 | |
---|
123 | status = rtems_semaphore_release(sem_id[1]); |
---|
124 | directive_failed( status, "rtems_semaphore_release of S1\n"); |
---|
125 | printf("TA01: priority %d, holding S0\n", getprio()); |
---|
126 | |
---|
127 | status = rtems_semaphore_release(sem_id[0]); |
---|
128 | directive_failed( status, "rtems_semaphore_release of S0\n"); |
---|
129 | printf("TA01: priority %d\n", getprio()); |
---|
130 | |
---|
131 | printf("TA01: exiting\n"); |
---|
132 | printf("*** END OF SEM01 ***\n"); |
---|
133 | status = rtems_task_delete( RTEMS_SELF); |
---|
134 | directive_failed( status, "rtems_task_delete TA01"); |
---|
135 | } |
---|
136 | |
---|
137 | /* TA02 starts at Task02 with priority 34 */ |
---|
138 | rtems_task Task02(rtems_task_argument ignored) |
---|
139 | { |
---|
140 | rtems_status_code status; |
---|
141 | |
---|
142 | printf("TA02: started with priority %d\n", getprio()); |
---|
143 | |
---|
144 | /* Obtain S1, which should be held by TA01 by now */ |
---|
145 | status = rtems_semaphore_obtain( sem_id[1], RTEMS_WAIT, 0 ); |
---|
146 | directive_failed( status, " rtems_semaphore_obtain S1"); |
---|
147 | printf("TA02: priority %d, holding S1\n", getprio()); |
---|
148 | |
---|
149 | printf("TA02: exiting\n"); |
---|
150 | status = rtems_task_delete( RTEMS_SELF); |
---|
151 | directive_failed( status, "rtems_task_delete TA02"); |
---|
152 | } |
---|
153 | |
---|