[0074691a] | 1 | /* |
---|
| 2 | ------------------------------------------------------------------------ |
---|
| 3 | |
---|
| 4 | COPYRIGHT (c) 1997 |
---|
| 5 | Objective Design Systems Ltd Pty (ODS) |
---|
| 6 | All rights reserved (R) Objective Design Systems Ltd Pty |
---|
[1b4f2b30] | 7 | |
---|
[0074691a] | 8 | The license and distribution terms for this file may be found in the |
---|
| 9 | file LICENSE in this distribution or at |
---|
[c499856] | 10 | http://www.rtems.org/license/LICENSE. |
---|
[0074691a] | 11 | |
---|
| 12 | ------------------------------------------------------------------------ |
---|
| 13 | |
---|
| 14 | rtemsSemaphore class. |
---|
| 15 | |
---|
| 16 | This class allows the user to create a RTEMS semaphore, or to use an |
---|
| 17 | already existing semaphore. The type of semaphore is decitated by |
---|
| 18 | the constructor used. |
---|
| 19 | |
---|
| 20 | The first constructor with the semaphore parameters creates a RTEMS |
---|
| 21 | semaphore object. The destructor of this object also deletes the |
---|
| 22 | semaphore object. The last status code should be checked after |
---|
| 23 | construction to see if the semaphore create was successfull. |
---|
| 24 | |
---|
| 25 | The second constructor connects to an existing. The last status code |
---|
| 26 | should be checked after construction to see if the semaphore |
---|
| 27 | existed. |
---|
| 28 | |
---|
| 29 | The third constructor is a copy constructor. Connects to an existing |
---|
| 30 | object which is in scope. |
---|
| 31 | |
---|
| 32 | ------------------------------------------------------------------------ */ |
---|
| 33 | |
---|
| 34 | #if !defined(_rtemsSemaphore_h_) |
---|
| 35 | #define _rtemsSemaphore_h_ |
---|
| 36 | |
---|
| 37 | #include <rtems++/rtemsStatusCode.h> |
---|
| 38 | |
---|
| 39 | /* ---- |
---|
| 40 | rtemsSemaphore |
---|
| 41 | */ |
---|
| 42 | |
---|
| 43 | class rtemsSemaphore |
---|
| 44 | : public rtemsStatusCode |
---|
| 45 | { |
---|
| 46 | public: |
---|
| 47 | // attribute a semaphore can have |
---|
| 48 | enum WaitMode { wait_by_fifo = RTEMS_FIFO, |
---|
| 49 | wait_by_priority = RTEMS_PRIORITY }; |
---|
| 50 | enum Type { binary = RTEMS_BINARY_SEMAPHORE, |
---|
| 51 | counting = RTEMS_COUNTING_SEMAPHORE }; |
---|
| 52 | enum Priority { no_priority_inherit = RTEMS_NO_INHERIT_PRIORITY, |
---|
| 53 | inherit_priority = RTEMS_INHERIT_PRIORITY }; |
---|
| 54 | enum Ceiling { no_priority_ceiling = RTEMS_NO_PRIORITY_CEILING, |
---|
| 55 | priority_ceiling = RTEMS_PRIORITY_CEILING }; |
---|
| 56 | enum Scope { local = RTEMS_LOCAL, |
---|
| 57 | global = RTEMS_GLOBAL }; |
---|
| 58 | |
---|
| 59 | // only the first 4 characters of the name are taken, |
---|
| 60 | // the counter must be set to 1 for binary semaphores |
---|
| 61 | |
---|
| 62 | // create a semaphore object |
---|
| 63 | rtemsSemaphore(const char* name, |
---|
| 64 | const Scope scope = local, |
---|
[600a241e] | 65 | const uint32_t counter = 1, |
---|
[0074691a] | 66 | const WaitMode wait_mode = wait_by_fifo, |
---|
| 67 | const Type type = binary, |
---|
| 68 | const Priority priority = no_priority_inherit, |
---|
| 69 | const Ceiling ceiling = no_priority_ceiling, |
---|
| 70 | const rtems_task_priority priority_ceiling = 0); |
---|
| 71 | |
---|
| 72 | // connect to an existing semaphore object by name |
---|
[600a241e] | 73 | rtemsSemaphore(const char *name, const uint32_t node); |
---|
[1b4f2b30] | 74 | |
---|
[0074691a] | 75 | // attach this object to an other objects semaphore |
---|
| 76 | rtemsSemaphore(const rtemsSemaphore& semaphore); |
---|
| 77 | rtemsSemaphore(); |
---|
| 78 | |
---|
| 79 | // only the creator's destructor will delete the actual object |
---|
| 80 | virtual ~rtemsSemaphore(); |
---|
| 81 | |
---|
| 82 | // create or destroy (delete) a semaphore |
---|
| 83 | virtual const rtems_status_code create(const char* name, |
---|
| 84 | const Scope scope = local, |
---|
[600a241e] | 85 | const uint32_t counter = 1, |
---|
[0074691a] | 86 | const WaitMode wait_mode = wait_by_fifo, |
---|
| 87 | const Type type = binary, |
---|
| 88 | const Priority priority = no_priority_inherit, |
---|
| 89 | const Ceiling ceiling = no_priority_ceiling, |
---|
| 90 | const rtems_task_priority priority_ceiling = 0); |
---|
| 91 | virtual const rtems_status_code destroy(); |
---|
[1b4f2b30] | 92 | |
---|
[0074691a] | 93 | // connect to an existing semaphore object, will not be the owner |
---|
[1c9a4c75] | 94 | const rtemsSemaphore& operator=(const rtemsSemaphore& semaphore); |
---|
[600a241e] | 95 | virtual const rtems_status_code connect(const char *name, uint32_t node); |
---|
[1b4f2b30] | 96 | |
---|
[0074691a] | 97 | // obtain the semaphore, timeout is in micro-seconds |
---|
| 98 | inline const rtems_status_code obtain(bool wait = true, |
---|
[600a241e] | 99 | const uint32_t micro_secs = RTEMS_NO_TIMEOUT); |
---|
[1b4f2b30] | 100 | |
---|
[0074691a] | 101 | // release the semaphore, blocks threads eligble |
---|
| 102 | inline const rtems_status_code release(); |
---|
[1b4f2b30] | 103 | |
---|
[0074691a] | 104 | // object id, and name |
---|
| 105 | const rtems_id id_is() const { return id; } |
---|
| 106 | const rtems_name name_is() const { return name; } |
---|
| 107 | const char *name_string() const { return name_str; } |
---|
[1b4f2b30] | 108 | |
---|
[0074691a] | 109 | private: |
---|
| 110 | |
---|
| 111 | // make the object reference no valid RTEMS object |
---|
| 112 | void make_invalid(); |
---|
[1b4f2b30] | 113 | |
---|
[0074691a] | 114 | // semaphore name |
---|
| 115 | rtems_name name; |
---|
| 116 | char name_str[5]; |
---|
| 117 | |
---|
| 118 | // owner, true if this object owns the semaphore |
---|
| 119 | // will delete the semaphore when it destructs |
---|
| 120 | bool owner; |
---|
[1b4f2b30] | 121 | |
---|
[0074691a] | 122 | // the rtems id, object handle |
---|
| 123 | rtems_id id; |
---|
| 124 | }; |
---|
| 125 | |
---|
| 126 | const rtems_status_code rtemsSemaphore::obtain(const bool wait, |
---|
[600a241e] | 127 | const uint32_t micro_secs) |
---|
[0074691a] | 128 | { |
---|
[bbd7ea2] | 129 | rtems_interval usecs = micro_secs && |
---|
| 130 | (micro_secs < rtems_configuration_get_microseconds_per_tick()) ? |
---|
| 131 | rtems_configuration_get_microseconds_per_tick() : micro_secs; |
---|
| 132 | |
---|
[0074691a] | 133 | return |
---|
| 134 | set_status_code(rtems_semaphore_obtain(id, |
---|
| 135 | wait ? RTEMS_WAIT : RTEMS_NO_WAIT, |
---|
[e85d043] | 136 | RTEMS_MICROSECONDS_TO_TICKS(usecs))); |
---|
[0074691a] | 137 | } |
---|
| 138 | |
---|
| 139 | const rtems_status_code rtemsSemaphore::release(void) |
---|
[1c9a4c75] | 140 | { |
---|
[0074691a] | 141 | return set_status_code(rtems_semaphore_release(id)); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | #endif // _rtemsSemaphore_h_ |
---|