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