source: rtems/c/src/librtems++/src/rtemsSemaphore.cc @ 32a4aea5

5
Last change on this file since 32a4aea5 was c499856, checked in by Chris Johns <chrisj@…>, on 03/20/14 at 21:10:47

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

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