1 | /* |
---|
2 | ------------------------------------------------------------------------ |
---|
3 | |
---|
4 | COPYRIGHT (c) 1997 |
---|
5 | Objective Design Systems Ltd Pty (ODS) |
---|
6 | All rights reserved (R) Objective Design Systems Ltd Pty |
---|
7 | |
---|
8 | The license and distribution terms for this file may be found in the |
---|
9 | file LICENSE in this distribution or at |
---|
10 | http://www.rtems.org/license/LICENSE. |
---|
11 | |
---|
12 | ------------------------------------------------------------------------ |
---|
13 | |
---|
14 | See header file. |
---|
15 | |
---|
16 | ------------------------------------------------------------------------ |
---|
17 | */ |
---|
18 | |
---|
19 | #include <cstring> |
---|
20 | #include <rtems++/rtemsTask.h> |
---|
21 | // include to allow it to be compiled |
---|
22 | #include <rtems++/rtemsTaskMode.h> |
---|
23 | |
---|
24 | /* ---- |
---|
25 | rtemsTask |
---|
26 | */ |
---|
27 | |
---|
28 | rtemsTask::rtemsTask(const char* tname, |
---|
29 | const rtems_task_priority initial_priority, |
---|
30 | const uint32_t stack_size, |
---|
31 | const rtems_mode preemption, |
---|
32 | const rtems_mode timeslice, |
---|
33 | const rtems_mode asr, |
---|
34 | const rtems_interrupt_level interrupt_level, |
---|
35 | const FloatingPoint floating_point, |
---|
36 | const Scope scope) |
---|
37 | : name(rtems_build_name('S', 'E', 'L', 'F')), |
---|
38 | owner(true), |
---|
39 | id(RTEMS_SELF), |
---|
40 | argument(0) |
---|
41 | { |
---|
42 | strcpy(name_str, "SELF"); |
---|
43 | create(tname, |
---|
44 | initial_priority, |
---|
45 | stack_size, |
---|
46 | preemption, |
---|
47 | timeslice, |
---|
48 | asr, |
---|
49 | interrupt_level, |
---|
50 | floating_point, |
---|
51 | scope); |
---|
52 | } |
---|
53 | |
---|
54 | rtemsTask::rtemsTask(const char *tname, uint32_t node) |
---|
55 | : name(rtems_build_name('S', 'E', 'L', 'F')), |
---|
56 | owner(false), |
---|
57 | id(RTEMS_SELF), |
---|
58 | argument(0) |
---|
59 | { |
---|
60 | strcpy(name_str, "SELF"); |
---|
61 | connect(tname, node); |
---|
62 | } |
---|
63 | |
---|
64 | rtemsTask::rtemsTask(const rtemsTask& task) |
---|
65 | : name(rtems_build_name('S', 'E', 'L', 'F')), |
---|
66 | owner(false), |
---|
67 | id(RTEMS_SELF), |
---|
68 | argument(0) |
---|
69 | { |
---|
70 | name = task.name; |
---|
71 | strcpy(name_str, task.name_str); |
---|
72 | argument = task.argument; |
---|
73 | id = task.id; |
---|
74 | } |
---|
75 | |
---|
76 | rtemsTask::rtemsTask() |
---|
77 | : name(rtems_build_name('S', 'E', 'L', 'F')), |
---|
78 | owner(false), |
---|
79 | id(RTEMS_SELF), |
---|
80 | argument(0) |
---|
81 | { |
---|
82 | strcpy(name_str, "SELF"); |
---|
83 | } |
---|
84 | |
---|
85 | rtemsTask::~rtemsTask() |
---|
86 | { |
---|
87 | destroy(); |
---|
88 | } |
---|
89 | |
---|
90 | void rtemsTask::make_self() |
---|
91 | { |
---|
92 | strcpy(name_str, "SELF"); |
---|
93 | name = rtems_build_name('S', 'E', 'L', 'F'); |
---|
94 | id = RTEMS_SELF; |
---|
95 | owner = false; |
---|
96 | } |
---|
97 | |
---|
98 | const rtems_status_code rtemsTask::create(const char* tname, |
---|
99 | const rtems_task_priority initial_priority, |
---|
100 | const uint32_t stack_size, |
---|
101 | const rtems_mode preemption, |
---|
102 | const rtems_mode timeslice, |
---|
103 | const rtems_mode asr, |
---|
104 | const rtems_interrupt_level interrupt_level, |
---|
105 | const FloatingPoint floating_point, |
---|
106 | const Scope scope) |
---|
107 | { |
---|
108 | if (id) |
---|
109 | return set_status_code(RTEMS_ILLEGAL_ON_SELF); |
---|
110 | |
---|
111 | owner = true; |
---|
112 | |
---|
113 | strcpy(name_str, " "); |
---|
114 | for (int c = 0; (c < 4) && (tname[c] != '\0'); c++) |
---|
115 | name_str[c] = tname[c]; |
---|
116 | name = rtems_build_name(name_str[0], |
---|
117 | name_str[1], |
---|
118 | name_str[2], |
---|
119 | name_str[3]); |
---|
120 | |
---|
121 | // protect the values that be set as the parameters are not enums |
---|
122 | set_status_code(rtems_task_create(name, |
---|
123 | initial_priority, |
---|
124 | stack_size, |
---|
125 | (preemption & RTEMS_PREEMPT_MASK) | |
---|
126 | (timeslice & RTEMS_TIMESLICE_MASK) | |
---|
127 | (asr & RTEMS_ASR_MASK) | |
---|
128 | (interrupt_level & RTEMS_INTERRUPT_MASK), |
---|
129 | floating_point | scope, |
---|
130 | &id)); |
---|
131 | |
---|
132 | if (unsuccessful()) |
---|
133 | { |
---|
134 | make_self(); |
---|
135 | } |
---|
136 | |
---|
137 | return last_status_code(); |
---|
138 | } |
---|
139 | |
---|
140 | const rtems_status_code rtemsTask::destroy() |
---|
141 | { |
---|
142 | if (id && owner) |
---|
143 | { |
---|
144 | set_status_code(rtems_task_delete(id)); |
---|
145 | make_self(); |
---|
146 | } |
---|
147 | else |
---|
148 | set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE); |
---|
149 | |
---|
150 | return last_status_code(); |
---|
151 | } |
---|
152 | |
---|
153 | const rtemsTask& rtemsTask::operator=(const rtemsTask& task) |
---|
154 | { |
---|
155 | if (!owner) |
---|
156 | { |
---|
157 | name = task.name; |
---|
158 | strcpy(name_str, task.name_str); |
---|
159 | argument = task.argument; |
---|
160 | id = task.id; |
---|
161 | } |
---|
162 | return *this; |
---|
163 | } |
---|
164 | |
---|
165 | const rtems_status_code rtemsTask::connect(const char *sname, |
---|
166 | const uint32_t node) |
---|
167 | { |
---|
168 | if (id && owner) |
---|
169 | return set_status_code(RTEMS_UNSATISFIED); |
---|
170 | |
---|
171 | // change state to not owner |
---|
172 | owner = false; |
---|
173 | |
---|
174 | strcpy(name_str, " "); |
---|
175 | for (int c = 0; (c < 4) && (sname[c] != '\0'); c++) |
---|
176 | name_str[c] = sname[c]; |
---|
177 | name = rtems_build_name(name_str[0], |
---|
178 | name_str[1], |
---|
179 | name_str[2], |
---|
180 | name_str[3]); |
---|
181 | |
---|
182 | set_status_code(rtems_task_ident(name, node, &id)); |
---|
183 | |
---|
184 | if (unsuccessful()) |
---|
185 | { |
---|
186 | make_self(); |
---|
187 | } |
---|
188 | |
---|
189 | return last_status_code(); |
---|
190 | } |
---|
191 | |
---|
192 | const rtems_status_code rtemsTask::start(const rtems_task_argument arg) |
---|
193 | { |
---|
194 | if (owner) |
---|
195 | { |
---|
196 | argument = arg; |
---|
197 | // pass the this pointer as the argument |
---|
198 | set_status_code(rtems_task_start(id, |
---|
199 | origin, |
---|
200 | (rtems_task_argument) this)); |
---|
201 | } |
---|
202 | else |
---|
203 | set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE); |
---|
204 | return last_status_code(); |
---|
205 | } |
---|
206 | |
---|
207 | const rtems_status_code rtemsTask::restart(const rtems_task_argument arg) |
---|
208 | { |
---|
209 | if (owner) |
---|
210 | { |
---|
211 | argument = arg; |
---|
212 | set_status_code(rtems_task_restart(id, (rtems_task_argument) this)); |
---|
213 | } |
---|
214 | else |
---|
215 | set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE); |
---|
216 | |
---|
217 | return last_status_code(); |
---|
218 | } |
---|
219 | |
---|
220 | const rtems_status_code rtemsTask::suspend() |
---|
221 | { |
---|
222 | return set_status_code(rtems_task_suspend(id)); |
---|
223 | } |
---|
224 | |
---|
225 | const rtems_status_code rtemsTask::resume() |
---|
226 | { |
---|
227 | return set_status_code(rtems_task_resume(id)); |
---|
228 | } |
---|
229 | |
---|
230 | const rtems_status_code rtemsTask::wake_after(const rtems_interval micro_secs) |
---|
231 | { |
---|
232 | rtems_interval usecs = |
---|
233 | (micro_secs < rtems_configuration_get_microseconds_per_tick()) ? |
---|
234 | rtems_configuration_get_microseconds_per_tick() : micro_secs; |
---|
235 | return set_status_code(rtems_task_wake_after(RTEMS_MICROSECONDS_TO_TICKS(usecs))); |
---|
236 | } |
---|
237 | |
---|
238 | const rtems_status_code rtemsTask::wake_when(const rtems_time_of_day& tod) |
---|
239 | { |
---|
240 | return set_status_code(rtems_task_wake_when((rtems_time_of_day*) &tod)); |
---|
241 | } |
---|
242 | |
---|
243 | const rtems_status_code rtemsTask::get_priority(rtems_task_priority& priority) |
---|
244 | { |
---|
245 | return set_status_code(rtems_task_set_priority(id, |
---|
246 | RTEMS_CURRENT_PRIORITY, |
---|
247 | &priority)); |
---|
248 | } |
---|
249 | |
---|
250 | const rtems_status_code rtemsTask::set_priority(const rtems_task_priority priority) |
---|
251 | { |
---|
252 | rtems_task_priority old_priority; |
---|
253 | return set_status_code(rtems_task_set_priority(id, |
---|
254 | priority, |
---|
255 | &old_priority)); |
---|
256 | } |
---|
257 | |
---|
258 | const rtems_status_code rtemsTask::set_priority(const rtems_task_priority priority, |
---|
259 | rtems_task_priority& old_priority) |
---|
260 | { |
---|
261 | return set_status_code(rtems_task_set_priority(id, |
---|
262 | priority, |
---|
263 | &old_priority)); |
---|
264 | } |
---|
265 | |
---|
266 | const rtems_status_code rtemsTask::get_note(const uint32_t notepad, |
---|
267 | uint32_t& note) |
---|
268 | { |
---|
269 | return set_status_code(rtems_task_get_note(id, notepad, ¬e)); |
---|
270 | } |
---|
271 | |
---|
272 | const rtems_status_code rtemsTask::set_note(const uint32_t notepad, |
---|
273 | const uint32_t note) |
---|
274 | { |
---|
275 | return set_status_code(rtems_task_set_note(id, notepad, note)); |
---|
276 | } |
---|
277 | |
---|
278 | void rtemsTask::body(rtems_task_argument ) |
---|
279 | { |
---|
280 | } |
---|
281 | |
---|
282 | rtems_task rtemsTask::origin(rtems_task_argument argument) |
---|
283 | { |
---|
284 | rtemsTask *task = (rtemsTask*) argument; |
---|
285 | task->body(task->argument); |
---|
286 | } |
---|