source: rtems/c/src/librtems++/include/rtems++/rtemsSemaphore.h @ c499856

4.115
Last change on this file since c499856 was c499856, checked in by Chris Johns <chrisj@…>, on Mar 20, 2014 at 9:10:47 PM

Change all references of rtems.com to rtems.org.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2  ------------------------------------------------------------------------
3
4  COPYRIGHT (c) 1997
5  Objective Design Systems Ltd Pty (ODS)
6  All rights reserved (R) Objective Design Systems Ltd Pty
7
8  The license and distribution terms for this file may be found in the
9  file LICENSE in this distribution or at
10  http://www.rtems.org/license/LICENSE.
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
43class rtemsSemaphore
44  : public rtemsStatusCode
45{
46public:
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,
65                 const uint32_t counter = 1,
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
73  rtemsSemaphore(const char *name, const uint32_t node);
74
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,
85                                         const uint32_t counter = 1,
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();
92
93  // connect to an existing semaphore object, will not be the owner
94  const rtemsSemaphore& operator=(const rtemsSemaphore& semaphore);
95  virtual const rtems_status_code connect(const char *name, uint32_t node);
96
97  // obtain the semaphore, timeout is in micro-seconds
98  inline const rtems_status_code obtain(bool wait = true,
99                                        const uint32_t micro_secs = RTEMS_NO_TIMEOUT);
100
101  // release the semaphore, blocks threads eligble
102  inline const rtems_status_code release();
103
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; }
108
109private:
110
111  // make the object reference no valid RTEMS object
112  void make_invalid();
113
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;
121
122  // the rtems id, object handle
123  rtems_id id;
124};
125
126const rtems_status_code rtemsSemaphore::obtain(const bool wait,
127                                               const uint32_t micro_secs)
128{
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
133  return
134    set_status_code(rtems_semaphore_obtain(id,
135                                           wait ? RTEMS_WAIT : RTEMS_NO_WAIT,
136                                           RTEMS_MICROSECONDS_TO_TICKS(usecs)));
137}
138
139const rtems_status_code rtemsSemaphore::release(void)
140{
141  return set_status_code(rtems_semaphore_release(id));
142}
143
144#endif  // _rtemsSemaphore_h_
Note: See TracBrowser for help on using the repository browser.