[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++/rtemsSemaphore.h> |
---|
| 22 | |
---|
| 23 | /* ---- |
---|
| 24 | rtemsSemaphore |
---|
| 25 | */ |
---|
| 26 | |
---|
| 27 | rtemsSemaphore::rtemsSemaphore(const char* sname, |
---|
| 28 | const Scope scope, |
---|
| 29 | const rtems_unsigned32 counter, |
---|
| 30 | const WaitMode wait_mode, |
---|
| 31 | const Type type, |
---|
| 32 | const Priority priority, |
---|
| 33 | const Ceiling ceiling, |
---|
| 34 | const rtems_task_priority priority_ceiling) |
---|
| 35 | : name(0), |
---|
| 36 | owner(true), |
---|
| 37 | id(0) |
---|
| 38 | { |
---|
| 39 | strcpy(name_str, "NOID"); |
---|
| 40 | create(sname, |
---|
| 41 | scope, |
---|
| 42 | counter, |
---|
| 43 | wait_mode, |
---|
| 44 | type, |
---|
| 45 | priority, |
---|
| 46 | ceiling, |
---|
| 47 | priority_ceiling); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | rtemsSemaphore::rtemsSemaphore(const char *sname, const rtems_unsigned32 node) |
---|
| 51 | : name(0), |
---|
| 52 | owner(false), |
---|
| 53 | id(0) |
---|
| 54 | { |
---|
| 55 | strcpy(name_str, "NOID"); |
---|
| 56 | connect(sname, node); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | rtemsSemaphore::rtemsSemaphore(const rtemsSemaphore& semaphore) |
---|
| 60 | : name(0), |
---|
| 61 | owner(false), |
---|
| 62 | id(0) |
---|
| 63 | { |
---|
| 64 | name = semaphore.name; |
---|
| 65 | strcpy(name_str, semaphore.name_str); |
---|
| 66 | id = semaphore.id; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | rtemsSemaphore::rtemsSemaphore() |
---|
| 70 | : name(0), |
---|
| 71 | owner(false), |
---|
| 72 | id(0) |
---|
| 73 | { |
---|
| 74 | strcpy(name_str, "NOID"); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | rtemsSemaphore::~rtemsSemaphore() |
---|
| 78 | { |
---|
| 79 | destroy(); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void rtemsSemaphore::make_invalid() |
---|
| 83 | { |
---|
| 84 | strcpy(name_str, "NOID"); |
---|
| 85 | name = 0; |
---|
| 86 | id = 0; |
---|
| 87 | owner = false; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | const rtems_status_code rtemsSemaphore::create(const char* sname, |
---|
| 91 | const Scope scope, |
---|
| 92 | const rtems_unsigned32 counter, |
---|
| 93 | const WaitMode wait_mode, |
---|
| 94 | const Type type, |
---|
| 95 | const Priority priority, |
---|
| 96 | const Ceiling ceiling, |
---|
| 97 | const rtems_task_priority priority_ceiling) |
---|
| 98 | { |
---|
| 99 | if (id) |
---|
| 100 | return set_status_code(RTEMS_ILLEGAL_ON_SELF); |
---|
| 101 | |
---|
| 102 | owner = true; |
---|
| 103 | |
---|
| 104 | strcpy(name_str, " "); |
---|
| 105 | for (int c = 0; (c < 4) && (sname[c] != '\0'); c++) |
---|
| 106 | name_str[c] = sname[c]; |
---|
| 107 | name = rtems_build_name(name_str[0], |
---|
| 108 | name_str[1], |
---|
| 109 | name_str[2], |
---|
| 110 | name_str[3]); |
---|
| 111 | |
---|
| 112 | set_status_code(rtems_semaphore_create(name, |
---|
| 113 | counter, |
---|
| 114 | scope | wait_mode | type | priority | ceiling, |
---|
| 115 | priority_ceiling, |
---|
| 116 | &id)); |
---|
| 117 | |
---|
| 118 | if (unsuccessful()) |
---|
| 119 | { |
---|
| 120 | make_invalid(); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | return last_status_code(); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | const rtems_status_code rtemsSemaphore::destroy() |
---|
| 127 | { |
---|
| 128 | if (id && owner) |
---|
| 129 | { |
---|
| 130 | set_status_code(rtems_semaphore_delete(id)); |
---|
| 131 | make_invalid(); |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE); |
---|
| 135 | |
---|
| 136 | return last_status_code(); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | const rtemsSemaphore& rtemsSemaphore::operator=(const rtemsSemaphore& semaphore) |
---|
| 140 | { |
---|
| 141 | if (!owner) |
---|
| 142 | { |
---|
| 143 | name = semaphore.name; |
---|
| 144 | id = semaphore.id; |
---|
| 145 | } |
---|
| 146 | return *this; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | const rtems_status_code rtemsSemaphore::connect(const char *sname, |
---|
| 150 | const rtems_unsigned32 node) |
---|
| 151 | { |
---|
| 152 | if (id && owner) |
---|
| 153 | return set_status_code(RTEMS_UNSATISFIED); |
---|
| 154 | |
---|
| 155 | // change state to not owner |
---|
| 156 | owner = false; |
---|
| 157 | |
---|
| 158 | strcpy(name_str, " "); |
---|
| 159 | for (int c = 0; (c < 4) && (sname[c] != '\0'); c++) |
---|
| 160 | name_str[c] = sname[c]; |
---|
| 161 | name = rtems_build_name(name_str[0], |
---|
| 162 | name_str[1], |
---|
| 163 | name_str[2], |
---|
| 164 | name_str[3]); |
---|
| 165 | |
---|
| 166 | set_status_code(rtems_semaphore_ident(name, node, &id)); |
---|
| 167 | |
---|
| 168 | if (unsuccessful()) |
---|
| 169 | { |
---|
| 170 | make_invalid(); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | return last_status_code(); |
---|
| 174 | } |
---|