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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
 * 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 <memory.h>
#include <boilerplate/ancillaries.h>
#include <copperplate/threadobj.h>
#include <copperplate/heapobj.h>
#include <copperplate/clockobj.h>
#include <copperplate/cluster.h>
#include <psos/psos.h>
#include "internal.h"
#include "reference.h"
#include "task.h"
#include "queue.h"
#include "tm.h"
 
#define queue_magic    0x8181fdfd
 
struct cluster psos_queue_table;
 
static unsigned long anon_qids;
 
struct msgholder {
   int size;
   struct holder link;
   /* Payload data follows. */
};
 
static struct psos_queue *get_queue_from_id(u_long qid, int *err_r)
{
   struct psos_queue *q = mainheap_deref(qid, struct psos_queue);
 
   if (q == NULL || ((uintptr_t)q & (sizeof(uintptr_t)-1)) != 0)
       goto objid_error;
 
   if (q->magic == queue_magic)
       return q;
 
   if (q->magic == ~queue_magic) {
       *err_r = ERR_OBJDEL;
       return NULL;
   }
 
   if ((q->magic >> 16) == 0x8181) {
       *err_r = ERR_OBJTYPE;
       return NULL;
   }
 
objid_error:
   *err_r = ERR_OBJID;
 
   return NULL;
}
 
static void queue_finalize(struct syncobj *sobj)
{
   struct psos_queue *q = container_of(sobj, struct psos_queue, sobj);
   xnfree(q);
}
fnref_register(libpsos, queue_finalize);
 
static u_long __q_create(const char *name, u_long count,
            u_long flags, u_long maxlen, u_long *qid_r)
{
   struct psos_queue *q;
   struct service svc;
   int sobj_flags = 0;
   int ret = SUCCESS;
   char short_name[5];
 
   CANCEL_DEFER(svc);
 
   q = xnmalloc(sizeof(*q));
   if (q == NULL) {
       ret = ERR_NOQCB;
       goto out;
   }
 
   if (name == NULL || *name == '\0')
       sprintf(q->name, "q%lu", ++anon_qids);
   else {
       name = psos_trunc_name(short_name, name);
       namecpy(q->name, name);
   }
 
   if (flags & Q_PRIOR)
       sobj_flags = SYNCOBJ_PRIO;
 
   q->flags = flags;
   q->maxmsg = (flags & Q_LIMIT) ? count : 0;
   q->maxlen = maxlen;
   ret = syncobj_init(&q->sobj, CLOCK_COPPERPLATE, sobj_flags,
              fnref_put(libpsos, queue_finalize));
   if (ret) {
       ret = ERR_NOQCB;
       goto fail_syncinit;
   }
 
   list_init(&q->msg_list);
   q->msgcount = 0;
   q->magic = queue_magic;
   *qid_r = mainheap_ref(q, u_long);
 
   if (cluster_addobj_dup(&psos_queue_table, q->name, &q->cobj)) {
       warning("cannot register queue: %s", q->name);
       ret = ERR_OBJID;
       goto fail_register;
   }
 
   CANCEL_RESTORE(svc);
 
   return 0;
 
fail_register:
   syncobj_uninit(&q->sobj);
fail_syncinit:
   xnfree(q);
out:
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
u_long q_create(const char *name,
       u_long count, u_long flags, u_long *qid_r)
{
   return __q_create(name, count, flags & ~Q_VARIABLE, sizeof(u_long[4]), qid_r);
}
 
u_long q_vcreate(const char *name, u_long flags,
        u_long count, u_long maxlen, u_long *qid_r)
{
   return __q_create(name, count, flags | Q_VARIABLE, maxlen, qid_r);
}
 
static u_long __q_delete(u_long qid, u_long flags)
{
   struct syncstate syns;
   struct msgholder *msg;
   struct psos_queue *q;
   struct service svc;
   int ret, emptyq;
 
   q = get_queue_from_id(qid, &ret);
   if (q == NULL)
       return ret;
 
   CANCEL_DEFER(svc);
 
   if (syncobj_lock(&q->sobj, &syns))
       return ERR_OBJDEL;
 
   if (((flags ^ q->flags) & Q_VARIABLE)) {
       syncobj_unlock(&q->sobj, &syns);
       CANCEL_RESTORE(svc);
       return (flags & Q_VARIABLE) ? ERR_NOTVARQ: ERR_VARQ;
 
   }
 
   emptyq = list_empty(&q->msg_list);
   if (!emptyq) {
       do {
           msg = list_pop_entry(&q->msg_list,
                        struct msgholder, link);
           xnfree(msg);
       } while (!list_empty(&q->msg_list));
   }
 
   cluster_delobj(&psos_queue_table, &q->cobj);
   q->magic = ~queue_magic; /* Prevent further reference. */
   ret = syncobj_destroy(&q->sobj, &syns);
   CANCEL_RESTORE(svc);
   if (ret)
       return ERR_TATQDEL;
 
   return emptyq ? SUCCESS : ERR_MATQDEL;
}
 
u_long q_delete(u_long qid)
{
   return __q_delete(qid, 0);
}
 
u_long q_vdelete(u_long qid)
{
   return __q_delete(qid, Q_VARIABLE);
}
 
static u_long __q_ident(const char *name,
           u_long flags, u_long node, u_long *qid_r)
{
   struct clusterobj *cobj;
   struct psos_queue *q;
   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_queue_table, name);
   CANCEL_RESTORE(svc);
   if (cobj == NULL)
       return ERR_OBJNF;
 
   q = container_of(cobj, struct psos_queue, cobj);
   if (((flags ^ q->flags) & Q_VARIABLE)) /* XXX: unsafe, but well... */
       return (flags & Q_VARIABLE) ? ERR_NOTVARQ: ERR_VARQ;
 
   *qid_r = mainheap_ref(q, u_long);
 
   return SUCCESS;
}
 
u_long q_ident(const char *name, u_long node, u_long *qid_r)
{
   return __q_ident(name, 0, node, qid_r);
}
 
u_long q_vident(const char *name, u_long node, u_long *qid_r)
{
   return __q_ident(name, Q_VARIABLE, node, qid_r);
}
 
static u_long __q_send_inner(struct psos_queue *q, unsigned long flags,
                u_long *buffer, u_long bytes)
{
   struct psos_queue_wait *wait;
   struct threadobj *thobj;
   struct msgholder *msg;
   u_long maxbytes;
 
   thobj = syncobj_peek_grant(&q->sobj);
   if (thobj && threadobj_local_p(thobj)) {
       /* Fast path: direct copy to the receiver's buffer. */
       wait = threadobj_get_wait(thobj);
       maxbytes = wait->size;
       if (bytes > maxbytes)
           bytes = maxbytes;
       if (bytes > 0)
           memcpy(__mptr(wait->ptr), buffer, bytes);
       wait->size = bytes;
       goto done;
   }
 
   if ((q->flags & Q_LIMIT) && q->msgcount >= q->maxmsg)
       return ERR_QFULL;
 
   msg = xnmalloc(bytes + sizeof(*msg));
   if (msg == NULL)
       return ERR_NOMGB;
 
   q->msgcount++;
   msg->size = bytes;
   holder_init(&msg->link);
 
   if (bytes > 0)
       memcpy(msg + 1, buffer, bytes);
 
   if (flags & Q_JAMMED)
       list_prepend(&msg->link, &q->msg_list);
   else
       list_append(&msg->link, &q->msg_list);
 
   if (thobj) {
       /*
        * We could not copy the message directly to the
        * remote buffer, tell the thread to pull it from the
        * pool.
        */
       wait = threadobj_get_wait(thobj);
       wait->size = -1UL;
   }
done:
   if (thobj)
       syncobj_grant_to(&q->sobj, thobj);
 
   return SUCCESS;
}
 
static u_long __q_send(u_long qid, u_long flags, u_long *buffer, u_long bytes)
{
   struct syncstate syns;
   struct psos_queue *q;
   struct service svc;
   int ret;
 
   q = get_queue_from_id(qid, &ret);
   if (q == NULL)
       return ret;
 
   CANCEL_DEFER(svc);
 
   if (syncobj_lock(&q->sobj, &syns)) {
       ret = ERR_OBJDEL;
       goto out;
   }
 
   if (((flags ^ q->flags) & Q_VARIABLE)) {
       ret = (flags & Q_VARIABLE) ? ERR_NOTVARQ: ERR_VARQ;
       goto fail;
   }
 
   if (bytes > q->maxlen) {
       ret = ERR_MSGSIZ;
       goto fail;
   }
 
   ret = __q_send_inner(q, flags, buffer, bytes);
fail:
   syncobj_unlock(&q->sobj, &syns);
out:
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
u_long q_send(u_long qid, u_long msgbuf[4])
{
   return __q_send(qid, 0, msgbuf, sizeof(u_long[4]));
}
 
u_long q_vsend(u_long qid, void *msgbuf, u_long msglen)
{
   return __q_send(qid, Q_VARIABLE, msgbuf, msglen);
}
 
u_long q_urgent(u_long qid, u_long msgbuf[4])
{
   return __q_send(qid, Q_JAMMED, msgbuf, sizeof(u_long[4]));
}
 
u_long q_vurgent(u_long qid, void *msgbuf, u_long msglen)
{
   return __q_send(qid, Q_VARIABLE | Q_JAMMED, msgbuf, msglen);
}
 
static u_long __q_broadcast(u_long qid, u_long flags,
               u_long *buffer, u_long bytes, u_long *count_r)
{
   struct syncstate syns;
   struct psos_queue *q;
   struct service svc;
   int ret = SUCCESS;
 
   q = get_queue_from_id(qid, &ret);
   if (q == NULL)
       return ret;
 
   CANCEL_DEFER(svc);
 
   if (syncobj_lock(&q->sobj, &syns)) {
       ret = ERR_OBJDEL;
       goto out;
   }
 
   if (((flags ^ q->flags) & Q_VARIABLE)) {
       ret = (flags & Q_VARIABLE) ? ERR_NOTVARQ: ERR_VARQ;
       goto fail;
   }
 
   if (bytes > q->maxlen) {
       ret = ERR_MSGSIZ;
       goto fail;
   }
 
   /* Release all pending tasks atomically. */
   *count_r = 0;
   while (syncobj_grant_wait_p(&q->sobj)) {
       ret = __q_send_inner(q, flags, buffer, bytes);
       if (ret)
           break;
       (*count_r)++;
   }
fail:
   syncobj_unlock(&q->sobj, &syns);
out:
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
u_long q_broadcast(u_long qid, u_long msgbuf[4], u_long *count_r)
{
   return __q_broadcast(qid, 0, msgbuf, sizeof(u_long[4]), count_r);
}
 
u_long q_vbroadcast(u_long qid, void *msgbuf, u_long msglen, u_long *count_r)
{
   return __q_broadcast(qid, Q_VARIABLE, msgbuf, msglen, count_r);
}
 
static u_long __q_receive(u_long qid, u_long flags, u_long timeout,
             void *buffer, u_long msglen, u_long *msglen_r)
{
   struct psos_queue_wait *wait = NULL;
   struct timespec ts, *timespec;
   struct msgholder *msg = NULL;
   struct syncstate syns;
   unsigned long nbytes;
   struct psos_queue *q;
   struct service svc;
   int ret = SUCCESS;
 
   q = get_queue_from_id(qid, &ret);
   if (q == NULL)
       return ret;
 
   CANCEL_DEFER(svc);
 
   if (syncobj_lock(&q->sobj, &syns)) {
       ret = ERR_OBJDEL;
       goto out;
   }
 
   if (((flags ^ q->flags) & Q_VARIABLE)) {
       ret = (flags & Q_VARIABLE) ? ERR_NOTVARQ: ERR_VARQ;
       goto fail;
   }
retry:
   if (!list_empty(&q->msg_list)) {
       q->msgcount--;
       msg = list_pop_entry(&q->msg_list, struct msgholder, link);
       nbytes = msg->size;
       if (nbytes > msglen)
           nbytes = msglen;
       if (nbytes > 0)
           memcpy(buffer, msg + 1, nbytes);
       xnfree(msg);
       goto done;
   }
 
   if (flags & Q_NOWAIT) {
       ret = ERR_NOMSG;
       goto fail;
   }
 
   if (timeout != 0) {
       timespec = &ts;
       clockobj_ticks_to_timeout(&psos_clock, timeout, timespec);
   } else
       timespec = NULL;
 
   wait = threadobj_prepare_wait(struct psos_queue_wait);
   wait->ptr = __moff(buffer);
   wait->size = msglen;
 
   ret = syncobj_wait_grant(&q->sobj, timespec, &syns);
   if (ret == -EIDRM) {
       ret = ERR_QKILLD;
       goto out;
   }
 
   if (ret == -ETIMEDOUT) {
       ret = ERR_TIMEOUT;
       goto fail;
   }
   nbytes = wait->size;
   if (nbytes == -1UL)    /* No direct copy? */
       goto retry;
done:
   if (msglen_r)
       *msglen_r = nbytes;
fail:
   syncobj_unlock(&q->sobj, &syns);
out:
   if (wait)
       threadobj_finish_wait();
 
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
u_long q_receive(u_long qid, u_long flags, u_long timeout, u_long msgbuf[4])
{
   return __q_receive(qid, flags & ~Q_VARIABLE,
              timeout, msgbuf, sizeof(u_long[4]), NULL);
}
 
u_long q_vreceive(u_long qid, u_long flags, u_long timeout,
         void *msgbuf, u_long msglen, u_long *msglen_r)
{
   return __q_receive(qid, flags | Q_VARIABLE,
              timeout, msgbuf, msglen, msglen_r);
}