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