summaryrefslogtreecommitdiffstats
path: root/c/src/lib/librtems++/rtemsSemaphore.cc
blob: 1e0d3de47206ea8a095ddb3f716429444a6ed724 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
  ------------------------------------------------------------------------
  $Id$
  ------------------------------------------------------------------------

  COPYRIGHT (c) 1997
  Objective Design Systems Ltd Pty (ODS)
  All rights reserved (R) Objective Design Systems Ltd Pty
  
  The license and distribution terms for this file may be found in the
  file LICENSE in this distribution or at
  http://www.OARcorp.com/rtems/license.html.

  ------------------------------------------------------------------------

  See header file.

  ------------------------------------------------------------------------
*/

#include <rtems++/rtemsSemaphore.h>

/* ----
    rtemsSemaphore
*/

rtemsSemaphore::rtemsSemaphore(const char* sname,
                               const Scope scope,
                               const rtems_unsigned32 counter,
                               const WaitMode wait_mode,
                               const Type type,
                               const Priority priority,
                               const Ceiling ceiling,
                               const rtems_task_priority priority_ceiling)
  : name(0),
    owner(true),
    id(0)
{
  strcpy(name_str, "NOID");
  create(sname,
         scope,
         counter,
         wait_mode,
         type,
         priority,
         ceiling,
         priority_ceiling);  
}

rtemsSemaphore::rtemsSemaphore(const char *sname, const rtems_unsigned32 node)
  : name(0),
    owner(false),
    id(0)
{
  strcpy(name_str, "NOID");
  connect(sname, node);
}

rtemsSemaphore::rtemsSemaphore(const rtemsSemaphore& semaphore)
  : name(0),
    owner(false),
    id(0)
{
  name = semaphore.name;
  strcpy(name_str, semaphore.name_str);
  id = semaphore.id;
}

rtemsSemaphore::rtemsSemaphore()
  : name(0),
    owner(false),
    id(0)
{
  strcpy(name_str, "NOID");
}

rtemsSemaphore::~rtemsSemaphore()
{
  destroy();
}

void rtemsSemaphore::make_invalid()
{
  strcpy(name_str, "NOID");
  name = 0;
  id = 0;
  owner = false;
}

const rtems_status_code rtemsSemaphore::create(const char* sname,
                                               const Scope scope,
                                               const rtems_unsigned32 counter,
                                               const WaitMode wait_mode,
                                               const Type type,
                                               const Priority priority,
                                               const Ceiling ceiling,
                                               const rtems_task_priority priority_ceiling)
{
  if (id)
    return set_status_code(RTEMS_ILLEGAL_ON_SELF);

  owner = true;
  
  strcpy(name_str, "    ");
  for (int c = 0; (c < 4) && (sname[c] != '\0'); c++)
    name_str[c] = sname[c];
  name = rtems_build_name(name_str[0],
                          name_str[1],
                          name_str[2],
                          name_str[3]);
  
  set_status_code(rtems_semaphore_create(name,
                                         counter,
                                         scope | wait_mode | type | priority | ceiling,
                                         priority_ceiling,
                                         &id));

  if (unsuccessful())
  {
    make_invalid();
  }
  
  return last_status_code();
}

const rtems_status_code rtemsSemaphore::destroy()
{
  if (id && owner)
  {
    set_status_code(rtems_semaphore_delete(id));
    make_invalid();
  }
  else
    set_status_code(RTEMS_NOT_OWNER_OF_RESOURCE);
  
  return last_status_code();
}

const rtemsSemaphore& rtemsSemaphore::operator=(const rtemsSemaphore& semaphore)
{
  if (!owner)
  {
    name = semaphore.name;
    id = semaphore.id;
  }
  return *this;
}

const rtems_status_code rtemsSemaphore::connect(const char *sname,
                                                const rtems_unsigned32 node)
{
  if (id && owner)
    return set_status_code(RTEMS_UNSATISFIED);

  // change state to not owner
  owner = false;
  
  strcpy(name_str, "    ");
  for (int c = 0; (c < 4) && (sname[c] != '\0'); c++)
    name_str[c] = sname[c];
  name = rtems_build_name(name_str[0],
                          name_str[1],
                          name_str[2],
                          name_str[3]);
  
  set_status_code(rtems_semaphore_ident(name, node, &id));

  if (unsuccessful())
  {
    make_invalid();
  }
  
  return last_status_code();
}