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
/*
 * 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 <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <memory.h>
#include <copperplate/heapobj.h>
#include <copperplate/threadobj.h>
#include <vxworks/errnoLib.h>
#include "reference.h"
#include "taskLib.h"
#include "msgQLib.h"
#include "tickLib.h"
 
/*
 * XXX: In order to keep the following services callable from
 * non-VxWorks tasks (but still Xenomai ones, though), make sure
 * to never depend on the wind_task struct, but rather on the thread
 * base object directly.
 */
 
#define mq_magic    0x4a5b6c7d
 
struct msgholder {
   int size;
   struct holder link;
   /* Payload data follows. */
};
 
static struct wind_mq *find_mq_from_id(MSG_Q_ID qid)
{
   struct wind_mq *mq = mainheap_deref(qid, struct wind_mq);
 
   if (mq == NULL || ((uintptr_t)mq & (sizeof(uintptr_t)-1)) != 0 ||
       mq->magic != mq_magic)
       return NULL;
 
   return mq;
}
 
static void mq_finalize(struct syncobj *sobj)
{
   struct wind_mq *mq = container_of(sobj, struct wind_mq, sobj);
   heapobj_destroy(&mq->pool);
   xnfree(mq);
}
fnref_register(libvxworks, mq_finalize);
 
MSG_Q_ID msgQCreate(int maxMsgs, int maxMsgLength, int options)
{
   int sobj_flags = 0, ret;
   struct wind_mq *mq;
   struct service svc;
 
   if (threadobj_irq_p()) {
       errno = S_intLib_NOT_ISR_CALLABLE;
       return (MSG_Q_ID)0;
   }
 
   if ((options & ~MSG_Q_PRIORITY) || maxMsgs <= 0) {
       errno = S_msgQLib_INVALID_QUEUE_TYPE;
       return (MSG_Q_ID)0;
   }
 
   if (maxMsgLength < 0) {
       errno = S_msgQLib_INVALID_MSG_LENGTH;
       return (MSG_Q_ID)0;
   }
 
   CANCEL_DEFER(svc);
 
   mq = xnmalloc(sizeof(*mq));
   if (mq == NULL)
       goto fail_cballoc;
 
   /*
    * The message pool must come from the main heap because of
    * mq->msg_list (this queue head and messages from the pool
    * must share the same allocation base). Create the heap
    * object accordingly.
    */
   if (heapobj_init_array(&mq->pool, NULL, maxMsgLength +
                  sizeof(struct msgholder), maxMsgs))
       goto fail_bufalloc;
 
   if (options & MSG_Q_PRIORITY)
       sobj_flags = SYNCOBJ_PRIO;
 
   ret = syncobj_init(&mq->sobj, CLOCK_COPPERPLATE, sobj_flags,
              fnref_put(libvxworks, mq_finalize));
   if (ret)
       goto fail_syncinit;
       
   mq->options = options;
   mq->maxmsg = maxMsgs;
   mq->msgsize = maxMsgLength;
   mq->msgcount = 0;
   list_init(&mq->msg_list);
 
   mq->magic = mq_magic;
 
   CANCEL_RESTORE(svc);
 
   return mainheap_ref(mq, MSG_Q_ID);
 
fail_syncinit:
   heapobj_destroy(&mq->pool);
fail_bufalloc:
   xnfree(mq);
fail_cballoc:
   errno = S_memLib_NOT_ENOUGH_MEMORY;
 
   CANCEL_RESTORE(svc);
 
   return (MSG_Q_ID)0;
}
 
STATUS msgQDelete(MSG_Q_ID msgQId)
{
   struct syncstate syns;
   struct wind_mq *mq;
   struct service svc;
 
   if (threadobj_irq_p()) {
       errno = S_intLib_NOT_ISR_CALLABLE;
       return ERROR;
   }
 
   mq = find_mq_from_id(msgQId);
   if (mq == NULL)
       goto objid_error;
 
   CANCEL_DEFER(svc);
 
   if (syncobj_lock(&mq->sobj, &syns)) {
       CANCEL_RESTORE(svc);
   objid_error:
       errno = S_objLib_OBJ_ID_ERROR;
       return ERROR;
   }
 
   mq->magic = ~mq_magic; /* Prevent further reference. */
   syncobj_destroy(&mq->sobj, &syns);
 
   CANCEL_RESTORE(svc);
 
   return OK;
}
 
int msgQReceive(MSG_Q_ID msgQId, char *buffer, UINT maxNBytes, int timeout)
{
   struct wind_queue_wait *wait = NULL;
   struct timespec ts, *timespec;
   struct msgholder *msg = NULL;
   UINT nbytes = (UINT)ERROR;
   struct syncstate syns;
   struct wind_mq *mq;
   struct service svc;
   int ret;
 
   if (threadobj_irq_p()) {
       errno = S_intLib_NOT_ISR_CALLABLE;
       return ERROR;
   }
 
   mq = find_mq_from_id(msgQId);
   if (mq == NULL)
       goto objid_error;
 
   CANCEL_DEFER(svc);
 
   if (syncobj_lock(&mq->sobj, &syns)) {
       CANCEL_RESTORE(svc);
   objid_error:
       errno = S_objLib_OBJ_ID_ERROR;
       return ERROR;
   }
 
retry:
   if (!list_empty(&mq->msg_list)) {
       mq->msgcount--;
       msg = list_pop_entry(&mq->msg_list, struct msgholder, link);
       nbytes = msg->size;
       if (nbytes > maxNBytes)
           nbytes = maxNBytes;
       if (nbytes > 0)
           memcpy(buffer, msg + 1, nbytes);
       heapobj_free(&mq->pool, msg);
       syncobj_drain(&mq->sobj);
       goto done;
   }
 
   if (timeout == NO_WAIT) {
       errno = S_objLib_OBJ_UNAVAILABLE;
       goto done;
   }
 
   if (timeout != WAIT_FOREVER) {
       timespec = &ts;
       clockobj_ticks_to_timeout(&wind_clock, timeout, timespec);
   } else
       timespec = NULL;
 
   wait = threadobj_prepare_wait(struct wind_queue_wait);
   wait->ptr = __moff(buffer);
   wait->size = maxNBytes;
 
   ret = syncobj_wait_grant(&mq->sobj, timespec, &syns);
   if (ret == -EIDRM) {
       errno = S_objLib_OBJ_DELETED;
       goto out;
   }
   if (ret == -ETIMEDOUT) {
       errno = S_objLib_OBJ_TIMEOUT;
       goto done;
   }
   nbytes = wait->size;
   if (nbytes == -1L)    /* No direct copy? */
       goto retry;
   syncobj_drain(&mq->sobj);
done:
   syncobj_unlock(&mq->sobj, &syns);
out:
   if (wait)
       threadobj_finish_wait();
 
   CANCEL_RESTORE(svc);
 
   return nbytes;
}
 
STATUS msgQSend(MSG_Q_ID msgQId, const char *buffer, UINT bytes,
       int timeout, int prio)
{
   struct timespec ts, *timespec;
   struct wind_queue_wait *wait;
   struct threadobj *thobj;
   struct msgholder *msg;
   struct syncstate syns;
   struct wind_mq *mq;
   struct service svc;
   int ret = ERROR;
   UINT maxbytes;
 
   CANCEL_DEFER(svc);
 
   mq = find_mq_from_id(msgQId);
   if (mq == NULL)
       goto objid_error;
 
   if (syncobj_lock(&mq->sobj, &syns)) {
       CANCEL_RESTORE(svc);
   objid_error:
       errno = S_objLib_OBJ_ID_ERROR;
       return ERROR;
   }
 
   if (bytes > mq->msgsize) {
       errno = S_msgQLib_INVALID_MSG_LENGTH;
       goto fail;
   }
 
   thobj = syncobj_peek_grant(&mq->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 (mq->msgcount < mq->maxmsg)
       goto enqueue;
 
   if (timeout == NO_WAIT) {
       errno = S_objLib_OBJ_UNAVAILABLE;
       goto fail;
   }
 
   if (threadobj_irq_p()) {
       errno = S_msgQLib_NON_ZERO_TIMEOUT_AT_INT_LEVEL;
       goto fail;
   }
 
   if (timeout != WAIT_FOREVER) {
       timespec = &ts;
       clockobj_ticks_to_timeout(&wind_clock, timeout, timespec);
   } else
       timespec = NULL;
 
   do {
       ret = syncobj_wait_drain(&mq->sobj, timespec, &syns);
       if (ret == -EIDRM) {
           errno = S_objLib_OBJ_DELETED;
           ret = ERROR;
           goto out;
       }
       if (ret == -ETIMEDOUT) {
           errno = S_objLib_OBJ_TIMEOUT;
           ret = ERROR;
           goto fail;
       }
   } while (mq->msgcount >= mq->maxmsg);
 
enqueue:
   msg = heapobj_alloc(&mq->pool, bytes + sizeof(*msg));
   if (msg == NULL) {
       errno = S_memLib_NOT_ENOUGH_MEMORY;
       ret = ERROR;
       goto fail;
   }
 
   mq->msgcount++;
   assert(mq->msgcount <= mq->maxmsg); /* Paranoid. */
   msg->size = bytes;
   holder_init(&msg->link);
 
   if (bytes > 0)
       memcpy(msg + 1, buffer, bytes);
 
   if (prio == MSG_PRI_NORMAL)
       list_append(&msg->link, &mq->msg_list);
   else
       list_prepend(&msg->link, &mq->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)    /* Wakeup waiter. */
       syncobj_grant_to(&mq->sobj, thobj);
 
   ret = OK;
fail:
   syncobj_unlock(&mq->sobj, &syns);
out:
   CANCEL_RESTORE(svc);
 
   return ret;
}
 
int msgQNumMsgs(MSG_Q_ID msgQId)
{
   struct syncstate syns;
   struct wind_mq *mq;
   struct service svc;
   int msgcount;
 
   mq = find_mq_from_id(msgQId);
   if (mq == NULL)
       goto objid_error;
 
   CANCEL_DEFER(svc);
 
   if (syncobj_lock(&mq->sobj, &syns)) {
       CANCEL_RESTORE(svc);
   objid_error:
       errno = S_objLib_OBJ_ID_ERROR;
       return ERROR;
   }
 
   msgcount = mq->msgcount;
   syncobj_unlock(&mq->sobj, &syns);
 
   CANCEL_RESTORE(svc);
 
   return msgcount;
}