summaryrefslogtreecommitdiffstats
path: root/c/src/librtems++/src/rtemsTask.cc
blob: 619f7d4071a177f50d1725ec3de2849aeab48db1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
  ------------------------------------------------------------------------
  $Id$
  ------------------------------------------------------------------------

  COPYRIGHT (c) 1997
  Objective Design Systems Ltd Pty (ODS)
  All rights reserved (R) Objective Design Systems Ltd Pty
  
  The license and distribution terms for this file may be found in the
  file LICENSE in this distribution or at
  http://www.OARcorp.com/rtems/license.html.

  ------------------------------------------------------------------------

  See header file.

  ------------------------------------------------------------------------
*/

#include <string.h>
#include <rtems++/rtemsTask.h>
// include to allow it to be compiled
#include <rtems++/rtemsTaskMode.h>

/* ----
    rtemsTask
*/

rtemsTask::rtemsTask(const char* tname,
                     const rtems_task_priority initial_priority,
                     const rtems_unsigned32 stack_size,
                     const rtems_mode preemption,
                     const rtems_mode timeslice,
                     const rtems_mode asr,
                     const rtems_interrupt_level interrupt_level,
                     const FloatingPoint floating_point,
                     const Scope scope)
  : name(rtems_build_name('S', 'E', 'L', 'F')),
    owner(true),
    id(RTEMS_SELF),
    argument(0)
{
  strcpy(name_str, "SELF");
  create(tname,
         initial_priority,
         stack_size,
         preemption,
         timeslice,
         asr,
         interrupt_level,
         floating_point,
         scope);
}

rtemsTask::rtemsTask(const char *tname, rtems_unsigned32 node)
  : name(rtems_build_name('S', 'E', 'L', 'F')),
    owner(false),
    id(RTEMS_SELF),
    argument(0)
{
  strcpy(name_str, "SELF");
  connect(tname, node);
}

rtemsTask::rtemsTask(const rtemsTask& task)
  : name(rtems_build_name('S', 'E', 'L', 'F')),
    owner(false),
    id(RTEMS_SELF),
    argument(0)
{
  name = task.name;
  strcpy(name_str, task.name_str);
  argument = task.argument;
  id = task.id;
}

rtemsTask::rtemsTask()
  : name(rtems_build_name('S', 'E', 'L', 'F')),
    owner(false),
    id(RTEMS_SELF),
    argument(0)
{
  strcpy(name_str, "SELF");
}

rtemsTask::~rtemsTask()
{
  destroy();
}

void rtemsTask::make_self()
{
  strcpy(name_str, "SELF");
  name = rtems_build_name('S', 'E', 'L', 'F');
  id = RTEMS_SELF;
  owner = false;
}

const rtems_status_code rtemsTask::create(const char* tname,
                                          const rtems_task_priority initial_priority,
                                          const rtems_unsigned32 stack_size,
                                          const rtems_mode preemption,
                                          const rtems_mode timeslice,
                                          const rtems_mode asr,
                                          const rtems_interrupt_level interrupt_level,
                                          const FloatingPoint floating_point,
                                          const Scope scope)
{
  if (id)
    return set_status_code(RTEMS_ILLEGAL_ON_SELF);

  owner = true;

  strcpy(name_str, "    ");
  for (int c = 0; (c < 4) && (tname[c] != '\0'); c++)
    name_str[c] = tname[c];
  name = rtems_build_name(name_str[0],
                          name_str[1],
                          name_str[2],
                          name_str[3]);

  // protect the values that be set as the parameters are not enums
  set_status_code(rtems_task_create(name,
                                    initial_priority,
                                    stack_size,
                                    (preemption & RTEMS_PREEMPT_MASK) |
                                    (timeslice & RTEMS_TIMESLICE_MASK) |
                                    (asr & RTEMS_ASR_MASK) |
                                    (interrupt_level & RTEMS_INTERRUPT_MASK),
                                    floating_point | scope,
                                    &id));

  if (unsuccessful())
  {
    make_self();
  }
  
  return last_status_code();
}

const rtems_status_code rtemsTask::destroy()
{
  if (id && owner)
  {
    set_status_code(rtems_task_delete(id));
    make_self();
  }
  else
    set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE);
  
  return last_status_code();
}
  
const rtemsTask& rtemsTask::operator=(const rtemsTask& task)
{
  if (!owner)
  {
    name = task.name;
    strcpy(name_str, task.name_str);
    argument = task.argument;
    id = task.id;
  }
  return *this;
}

const rtems_status_code rtemsTask::connect(const char *sname,
                                           const rtems_unsigned32 node)
{
  if (id && owner)
    return set_status_code(RTEMS_UNSATISFIED);

  // change state to not owner
  owner = false;
  
  strcpy(name_str, "    ");
  for (int c = 0; (c < 4) && (sname[c] != '\0'); c++)
    name_str[c] = sname[c];
  name = rtems_build_name(name_str[0],
                          name_str[1],
                          name_str[2],
                          name_str[3]);
  
  set_status_code(rtems_task_ident(name, node, &id));

  if (unsuccessful())
  {
    make_self();
  }
  
  return last_status_code();
}

const rtems_status_code rtemsTask::start(const rtems_task_argument arg)
{
  if (owner)
  {
    argument = arg;
    // pass the this pointer as the argument
    set_status_code(rtems_task_start(id,
                                     origin,
                                     (rtems_task_argument) this));
  }
  else
    set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE);
  return last_status_code();
}
      
const rtems_status_code rtemsTask::restart(const rtems_task_argument arg)
{
  if (owner)
  {
    argument = arg;
    set_status_code(rtems_task_restart(id, (rtems_task_argument) this));
  }
  else
    set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE);
  
  return last_status_code();
}
  
const rtems_status_code rtemsTask::suspend()
{
  return set_status_code(rtems_task_suspend(id));
}

const rtems_status_code rtemsTask::resume()
{
  return set_status_code(rtems_task_resume(id));
}

const rtems_status_code rtemsTask::wake_after(const rtems_interval micro_secs)
{
  rtems_interval usecs =
    micro_secs && (micro_secs < _TOD_Microseconds_per_tick) ?
    _TOD_Microseconds_per_tick : micro_secs;
  return set_status_code(rtems_task_wake_after(TOD_MICROSECONDS_TO_TICKS(usecs)));
}

const rtems_status_code rtemsTask::wake_when(const rtems_time_of_day& tod)
{
  return set_status_code(rtems_task_wake_when((rtems_time_of_day*) &tod));
}

const rtems_status_code rtemsTask::get_priority(rtems_task_priority& priority)
{
  return set_status_code(rtems_task_set_priority(id,
                                                 RTEMS_CURRENT_PRIORITY,
                                                 &priority));
}

const rtems_status_code rtemsTask::set_priority(const rtems_task_priority priority)
{
  rtems_task_priority old_priority;
  return set_status_code(rtems_task_set_priority(id,
                                                 priority,
                                                 &old_priority));
}

const rtems_status_code rtemsTask::set_priority(const rtems_task_priority priority,
                                                rtems_task_priority& old_priority)
{
  return set_status_code(rtems_task_set_priority(id,
                                                 priority,
                                                 &old_priority));
}
  
const rtems_status_code rtemsTask::get_note(const rtems_unsigned32 notepad,
                                            rtems_unsigned32& note)
{
  return set_status_code(rtems_task_get_note(id, notepad, &note));
}

const rtems_status_code rtemsTask::set_note(const rtems_unsigned32 notepad,
                                            const rtems_unsigned32 note)
{
  return set_status_code(rtems_task_set_note(id, notepad, note));
}

void rtemsTask::body(rtems_task_argument )
{
}

rtems_task rtemsTask::origin(rtems_task_argument argument)
{
  rtemsTask *task = (rtemsTask*) argument;
  task->body(task->argument);
}