hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2008 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
 
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <boilerplate/ancillaries.h>
#include <copperplate/heapobj.h>
#include <copperplate/cluster.h>
#include <copperplate/clockobj.h>
#include <copperplate/semobj.h>
#include "reference.h"
#include "task.h"
#include "sem.h"
#include "tm.h"
#include "internal.h"
#include <psos/psos.h>
 
#define sem_magic    0x8181fbfb
 
struct cluster psos_sem_table;
 
static unsigned long anon_smids;
 
static struct psos_sem *get_sem_from_id(u_long smid, int *err_r)
{
   struct psos_sem *sem = mainheap_deref(smid, struct psos_sem);
 
   if (sem == NULL || ((uintptr_t)sem & (sizeof(uintptr_t)-1)) != 0)
       goto objid_error;
 
   if (sem->magic == sem_magic)
       return sem;
 
   if (sem->magic == ~sem_magic) {
       *err_r = ERR_OBJDEL;
       return NULL;
   }
 
   if ((sem->magic >> 16) == 0x8181) {
       *err_r = ERR_OBJTYPE;
       return NULL;
   }
 
objid_error:
   *err_r = ERR_OBJID;
 
   return NULL;
}
 
static void sem_finalize(struct semobj *smobj)
{
   struct psos_sem *sem = container_of(smobj, struct psos_sem, smobj);
   xnfree(sem);
}
fnref_register(libpsos, sem_finalize);
 
u_long sm_create(const char *name,
        u_long count, u_long flags, u_long *smid_r)
{
   int smobj_flags = SEMOBJ_WARNDEL;
   struct psos_sem *sem;
   struct service svc;
   char short_name[5];
   int ret;
 
   CANCEL_DEFER(svc);
 
   sem = xnmalloc(sizeof(*sem));
   if (sem == NULL) {
       ret = ERR_NOSCB;
       goto out;
   }
 
   if (name == NULL || *name == '\0')
       sprintf(sem->name, "sm%lu", ++anon_smids);
   else {
       name = psos_trunc_name(short_name, name);
       namecpy(sem->name, name);
   }
 
   if (cluster_addobj_dup(&psos_sem_table, sem->name, &sem->cobj)) {
       warning("cannot register semaphore: %s", sem->name);
       xnfree(sem);
       ret = ERR_OBJID;
       goto out;
   }
 
   if (flags & SM_PRIOR)
       smobj_flags |= SEMOBJ_PRIO;
 
   sem->magic = sem_magic;
   ret = semobj_init(&sem->smobj, smobj_flags, count,
             fnref_put(libpsos, sem_finalize));
   if (ret) {
       cluster_delobj(&psos_sem_table, &sem->cobj);
       xnfree(sem);
       goto out;
   }
 
   *smid_r = mainheap_ref(sem, u_long);
out:
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
u_long sm_delete(u_long smid)
{
   struct psos_sem *sem;
   struct service svc;
   int ret;
 
   sem = get_sem_from_id(smid, &ret);
   if (sem == NULL)
       return ret;
 
   CANCEL_DEFER(svc);
 
   cluster_delobj(&psos_sem_table, &sem->cobj);
   sem->magic = ~sem_magic; /* Prevent further reference. */
   ret = semobj_destroy(&sem->smobj);
   if (ret)
       ret = ret > 0 ? ERR_TATSDEL : ERR_OBJDEL;
 
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
u_long sm_ident(const char *name, u_long node, u_long *smid_r)
{
   struct clusterobj *cobj;
   struct psos_sem *sem;
   struct service svc;
   char short_name[5];
 
   if (node)
       return ERR_NODENO;
 
   name = psos_trunc_name(short_name, name);
 
   CANCEL_DEFER(svc);
   cobj = cluster_findobj(&psos_sem_table, name);
   CANCEL_RESTORE(svc);
   if (cobj == NULL)
       return ERR_OBJNF;
 
   sem = container_of(cobj, struct psos_sem, cobj);
   *smid_r = mainheap_ref(sem, u_long);
 
   return SUCCESS;
}
 
u_long sm_p(u_long smid, u_long flags, u_long timeout)
{
   struct timespec ts, *timespec = &ts;
   struct psos_sem *sem;
   struct service svc;
   int ret;
 
   sem = get_sem_from_id(smid, &ret);
   if (sem == NULL)
       return ret;
 
   CANCEL_DEFER(svc);
 
   if (flags & SM_NOWAIT) {
       timespec->tv_sec = 0;
       timespec->tv_nsec = 0;
   } else if (timeout != 0)
       clockobj_ticks_to_timeout(&psos_clock, timeout, timespec);
   else
       timespec = NULL;
 
   ret = semobj_wait(&sem->smobj, timespec);
   if (ret) {
       if (ret == -EIDRM)
           ret = ERR_SKILLD;
       else if (ret == -ETIMEDOUT)
           ret = ERR_TIMEOUT;
       else if (ret == -EWOULDBLOCK)
           ret = ERR_NOSEM;
       /*
        * There is no explicit flush operation on pSOS
        * semaphores, only an implicit one through deletion.
        */
   }
 
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
u_long sm_v(u_long smid)
{
   struct psos_sem *sem;
   struct service svc;
   int ret;
 
   sem = get_sem_from_id(smid, &ret);
   if (sem == NULL)
       return ret;
 
   CANCEL_DEFER(svc);
 
   ret = semobj_post(&sem->smobj);
   if (ret == -EIDRM)
       ret = ERR_OBJDEL;
 
   CANCEL_RESTORE(svc);
 
   return ret;
}