source: rtems/c/src/librtems++/src/rtemsTask.cc @ fd57015

4.115
Last change on this file since fd57015 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 7.7 KB
Line 
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
28rtemsTask::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
54rtemsTask::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
64rtemsTask::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
76rtemsTask::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
85rtemsTask::~rtemsTask()
86{
87  destroy();
88}
89
90void 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
98const 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
140const 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 
153const 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
165const 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
192const 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     
207const 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 
220const rtems_status_code rtemsTask::suspend()
221{
222  return set_status_code(rtems_task_suspend(id));
223}
224
225const rtems_status_code rtemsTask::resume()
226{
227  return set_status_code(rtems_task_resume(id));
228}
229
230const 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
238const 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
243const 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
250const 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
258const 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 
266const 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, &note));
270}
271
272const 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
278void rtemsTask::body(rtems_task_argument )
279{
280}
281
282rtems_task rtemsTask::origin(rtems_task_argument argument)
283{
284  rtemsTask *task = (rtemsTask*) argument;
285  task->body(task->argument);
286}
Note: See TracBrowser for help on using the repository browser.