-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
mqueue.h
690 lines (637 loc) · 26.3 KB
/
mqueue.h
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/****************************************************************************
* include/nuttx/mqueue.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef ___INCLUDE_NUTTX_MQUEUE_H
#define ___INCLUDE_NUTTX_MQUEUE_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <nuttx/fs/fs.h>
#include <nuttx/signal.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <mqueue.h>
#include <queue.h>
#if CONFIG_MQ_MAXMSGSIZE > 0
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Most internal nxmq_* interfaces are not available in the user space in
* PROTECTED and KERNEL builds. In that context, the application message
* queue interfaces must be used. The differences between the two sets of
* interfaces are: (1) the nxmq_* interfaces do not cause cancellation
* points and (2) they do not modify the errno variable.
*
* This is only important when compiling libraries (libc or libnx) that are
* used both by the OS (libkc.a and libknx.a) or by the applications
* (libc.a and libnx.a). In that case, the correct interface must be
* used for the build context.
*
* REVISIT: In the flat build, the same functions must be used both by
* the OS and by applications. We have to use the normal user functions
* in this case or we will fail to set the errno or fail to create the
* cancellation point.
*/
#if !defined(CONFIG_BUILD_FLAT) && defined(__KERNEL__)
# define _MQ_OPEN nxmq_open
# define _MQ_CLOSE(d) nxmq_close(d)
# define _MQ_UNLINK(n) nxmq_unlink(n)
# define _MQ_SEND(d,m,l,p) nxmq_send(d,m,l,p)
# define _MQ_TIMEDSEND(d,m,l,p,t) nxmq_timedsend(d,m,l,p,t)
# define _MQ_RECEIVE(d,m,l,p) nxmq_receive(d,m,l,p)
# define _MQ_TIMEDRECEIVE(d,m,l,p,t) nxmq_timedreceive(d,m,l,p,t)
# define _MQ_GETERRNO(r) (-(r))
# define _MQ_SETERRNO(r) set_errno(-(r))
# define _MQ_GETERRVAL(r) (r)
#else
# define _MQ_OPEN mq_open
# define _MQ_CLOSE(d) mq_close(d)
# define _MQ_UNLINK(n) mq_unlink(n)
# define _MQ_SEND(d,m,l,p) mq_send(d,m,l,p)
# define _MQ_TIMEDSEND(d,m,l,p,t) mq_timedsend(d,m,l,p,t)
# define _MQ_RECEIVE(d,m,l,p) mq_receive(d,m,l,p)
# define _MQ_TIMEDRECEIVE(d,m,l,p,t) mq_timedreceive(d,m,l,p,t)
# define _MQ_GETERRNO(r) errno
# define _MQ_SETERRNO(r)
# define _MQ_GETERRVAL(r) (-errno)
#endif
/****************************************************************************
* Public Type Declarations
****************************************************************************/
/* This structure defines a message queue */
struct mqueue_inode_s
{
FAR struct inode *inode; /* Containing inode */
sq_queue_t msglist; /* Prioritized message list */
int16_t maxmsgs; /* Maximum number of messages in the queue */
int16_t nmsgs; /* Number of message in the queue */
int16_t nwaitnotfull; /* Number tasks waiting for not full */
int16_t nwaitnotempty; /* Number tasks waiting for not empty */
#if CONFIG_MQ_MAXMSGSIZE < 256
uint8_t maxmsgsize; /* Max size of message in message queue */
#else
uint16_t maxmsgsize; /* Max size of message in message queue */
#endif
pid_t ntpid; /* Notification: Receiving Task's PID */
struct sigevent ntevent; /* Notification description */
struct sigwork_s ntwork; /* Notification work */
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
struct tcb_s; /* Forward reference */
struct mq_attr; /* Forward reference */
struct timespec; /* Forward reference */
struct task_group_s; /* Forward reference */
/****************************************************************************
* Name: nxmq_open
*
* Description:
* This function establish a connection between a named message queue and
* the calling task. This is an internal OS interface. It is functionally
* equivalent to mq_open() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_open() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mq_name - Name of the queue to open
* oflags - open flags
* Optional parameters. When the O_CREAT flag is specified, two optional
* parameters are expected:
*
* 1. mode_t mode (ignored), and
* 2. struct mq_attr *attr. The mq_maxmsg attribute
* is used at the time that the message queue is
* created to determine the maximum number of
* messages that may be placed in the message queue.
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success, mqdes point to the new message queue descriptor.
* A negated errno value is returned on failure.
*
****************************************************************************/
mqd_t nxmq_open(FAR const char *mq_name, int oflags, ...);
/****************************************************************************
* Name: nxmq_close
*
* Description:
* This is an internal OS interface. It is functionally equivalent to
* mq_close() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_close() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mqdes - Message queue descriptor.
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
****************************************************************************/
int nxmq_close(mqd_t mqdes);
/****************************************************************************
* Name: nxmq_unlink
*
* Description:
* This is an internal OS interface. It is functionally equivalent to
* mq_unlink() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_unlink() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mq_name - Name of the message queue
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
****************************************************************************/
int nxmq_unlink(FAR const char *mq_name);
/****************************************************************************
* Name: nxmq_send
*
* Description:
* This function adds the specified message (msg) to the message queue
* (mqdes). This is an internal OS interface. It is functionally
* equivalent to mq_send() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_send() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mqdes - Message queue descriptor
* msg - Message to send
* msglen - The length of the message in bytes
* prio - The priority of the message
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
* (see mq_send() for the list list valid return values).
*
****************************************************************************/
int nxmq_send(mqd_t mqdes, FAR const char *msg, size_t msglen,
unsigned int prio);
/****************************************************************************
* Name: nxmq_timedsend
*
* Description:
* This function adds the specified message (msg) to the message queue
* (mqdes). nxmq_timedsend() behaves just like mq_send(), except that if
* the queue is full and the O_NONBLOCK flag is not enabled for the
* message queue description, then abstime points to a structure which
* specifies a ceiling on the time for which the call will block.
*
* nxmq_timedsend() is functionally equivalent to mq_timedsend() except
* that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_timedsend() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mqdes - Message queue descriptor
* msg - Message to send
* msglen - The length of the message in bytes
* prio - The priority of the message
* abstime - the absolute time to wait until a timeout is decleared
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
* (see mq_timedsend() for the list list valid return values).
*
* EAGAIN The queue was empty, and the O_NONBLOCK flag was set for the
* message queue description referred to by mqdes.
* EINVAL Either msg or mqdes is NULL or the value of prio is invalid.
* EPERM Message queue opened not opened for writing.
* EMSGSIZE 'msglen' was greater than the maxmsgsize attribute of the
* message queue.
* EINTR The call was interrupted by a signal handler.
*
****************************************************************************/
int nxmq_timedsend(mqd_t mqdes, FAR const char *msg, size_t msglen,
unsigned int prio, FAR const struct timespec *abstime);
/****************************************************************************
* Name: nxmq_receive
*
* Description:
* This function receives the oldest of the highest priority messages
* from the message queue specified by "mqdes." This is an internal OS
* interface. It is functionally equivalent to mq_receive except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_receive() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mqdes - Message Queue Descriptor
* msg - Buffer to receive the message
* msglen - Size of the buffer in bytes
* prio - If not NULL, the location to store message priority.
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
* (see mq_receive() for the list list valid return values).
*
****************************************************************************/
ssize_t nxmq_receive(mqd_t mqdes, FAR char *msg, size_t msglen,
FAR unsigned int *prio);
/****************************************************************************
* Name: nxmq_timedreceive
*
* Description:
* This function receives the oldest of the highest priority messages from
* the message queue specified by "mqdes." If the message queue is empty
* and O_NONBLOCK was not set, nxmq_timedreceive() will block until a
* message is added to the message queue (or until a timeout occurs).
*
* nxmq_timedreceive() is an internal OS interface. It is functionally
* equivalent to mq_timedreceive() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_timedreceive() for a more complete description of
* the behavior of this function
*
* Input Parameters:
* mqdes - Message Queue Descriptor
* msg - Buffer to receive the message
* msglen - Size of the buffer in bytes
* prio - If not NULL, the location to store message priority.
* abstime - the absolute time to wait until a timeout is declared.
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
* (see mq_timedreceive() for the list list valid return values).
*
****************************************************************************/
ssize_t nxmq_timedreceive(mqd_t mqdes, FAR char *msg, size_t msglen,
FAR unsigned int *prio,
FAR const struct timespec *abstime);
/****************************************************************************
* Name: nxmq_free_msgq
*
* Description:
* This function deallocates an initialized message queue structure.
* First, it deallocates all of the queued messages in the message
* queue. It is assumed that this message is fully unlinked and
* closed so that no thread will attempt access it while it is being
* deleted.
*
* Input Parameters:
* msgq - Named essage queue to be freed
*
* Returned Value:
* None
*
****************************************************************************/
void nxmq_free_msgq(FAR struct mqueue_inode_s *msgq);
/****************************************************************************
* Name: nxmq_alloc_msgq
*
* Description:
* This function implements a part of the POSIX message queue open logic.
* It allocates and initializes a struct mqueue_inode_s structure.
*
* Input Parameters:
* mode - mode_t value is ignored
* attr - The mq_maxmsg attribute is used at the time that the message
* queue is created to determine the maximum number of
* messages that may be placed in the message queue.
*
* Returned Value:
* The allocated and initialized message queue structure or NULL in the
* event of a failure.
*
****************************************************************************/
FAR struct mqueue_inode_s *nxmq_alloc_msgq(mode_t mode,
FAR struct mq_attr *attr);
/****************************************************************************
* Name: file_mq_open
*
* Description:
* This function establish a connection between a named message queue and
* the calling task. This is an internal OS interface. It is functionally
* equivalent to mq_open() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_open() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mq_name - Name of the queue to open
* oflags - open flags
* Optional parameters. When the O_CREAT flag is specified, two optional
* parameters are expected:
*
* 1. mode_t mode (ignored), and
* 2. struct mq_attr *attr. The mq_maxmsg attribute
* is used at the time that the message queue is
* created to determine the maximum number of
* messages that may be placed in the message queue.
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* NOT NULL Message queue descriptor, NULL failed.
*
****************************************************************************/
int file_mq_open(FAR struct file *mq,
FAR const char *mq_name, int oflags, ...);
/****************************************************************************
* Name: file_mq_close
*
* Description:
* This is an internal OS interface. It is functionally equivalent to
* mq_close() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_close() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mq - Message queue descriptor.
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
****************************************************************************/
int file_mq_close(FAR struct file *mq);
/****************************************************************************
* Name: file_mq_unlink
*
* Description:
* This is an internal OS interface. It is functionally equivalent to
* mq_unlink() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_unlink() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mq_name - Name of the message queue
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
****************************************************************************/
int file_mq_unlink(FAR const char *mq_name);
/****************************************************************************
* Name: file_mq_send
*
* Description:
* This function adds the specified message (msg) to the message queue
* (mq). This is an internal OS interface. It is functionally
* equivalent to mq_send() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_send() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mq - Message queue descriptor
* msg - Message to send
* msglen - The length of the message in bytes
* prio - The priority of the message
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
* (see mq_send() for the list list valid return values).
*
****************************************************************************/
int file_mq_send(FAR struct file *mq, FAR const char *msg, size_t msglen,
unsigned int prio);
/****************************************************************************
* Name: file_mq_timedsend
*
* Description:
* This function adds the specified message (msg) to the message queue
* (mq). file_mq_timedsend() behaves just like mq_send(), except that if
* the queue is full and the O_NONBLOCK flag is not enabled for the
* message queue description, then abstime points to a structure which
* specifies a ceiling on the time for which the call will block.
*
* file_mq_timedsend() is functionally equivalent to mq_timedsend() except
* that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_timedsend() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mq - Message queue descriptor
* msg - Message to send
* msglen - The length of the message in bytes
* prio - The priority of the message
* abstime - the absolute time to wait until a timeout is decleared
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
* (see mq_timedsend() for the list list valid return values).
*
* EAGAIN The queue was empty, and the O_NONBLOCK flag was set for the
* message queue description referred to by mq.
* EINVAL Either msg or mq is NULL or the value of prio is invalid.
* EPERM Message queue opened not opened for writing.
* EMSGSIZE 'msglen' was greater than the maxmsgsize attribute of the
* message queue.
* EINTR The call was interrupted by a signal handler.
*
****************************************************************************/
int file_mq_timedsend(FAR struct file *mq, FAR const char *msg,
size_t msglen, unsigned int prio,
FAR const struct timespec *abstime);
/****************************************************************************
* Name: file_mq_receive
*
* Description:
* This function receives the oldest of the highest priority messages
* from the message queue specified by "mq." This is an internal OS
* interface. It is functionally equivalent to mq_receive except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_receive() for a more complete description of the
* behavior of this function
*
* Input Parameters:
* mq - Message Queue Descriptor
* msg - Buffer to receive the message
* msglen - Size of the buffer in bytes
* prio - If not NULL, the location to store message priority.
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
* (see mq_receive() for the list list valid return values).
*
****************************************************************************/
ssize_t file_mq_receive(FAR struct file *mq, FAR char *msg, size_t msglen,
FAR unsigned int *prio);
/****************************************************************************
* Name: file_mq_timedreceive
*
* Description:
* This function receives the oldest of the highest priority messages from
* the message queue specified by "mq." If the message queue is empty
* and O_NONBLOCK was not set, file_mq_timedreceive() will block until a
* message is added to the message queue (or until a timeout occurs).
*
* file_mq_timedreceive() is an internal OS interface. It is functionally
* equivalent to mq_timedreceive() except that:
*
* - It is not a cancellation point, and
* - It does not modify the errno value.
*
* See comments with mq_timedreceive() for a more complete description of
* the behavior of this function
*
* Input Parameters:
* mq - Message Queue Descriptor
* msg - Buffer to receive the message
* msglen - Size of the buffer in bytes
* prio - If not NULL, the location to store message priority.
* abstime - the absolute time to wait until a timeout is declared.
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
* (see mq_timedreceive() for the list list valid return values).
*
****************************************************************************/
ssize_t file_mq_timedreceive(FAR struct file *mq, FAR char *msg,
size_t msglen, FAR unsigned int *prio,
FAR const struct timespec *abstime);
/****************************************************************************
* Name: file_mq_setattr
*
* Description:
* This function sets the attributes associated with the
* specified message queue "mq". Only the "O_NONBLOCK"
* bit of the "mq_flags" can be changed.
*
* If "oldstat" is non-null, mq_setattr() will store the
* previous message queue attributes at that location (just
* as would have been returned by file_mq_getattr()).
*
* Input Parameters:
* mqdes - Message queue descriptor
* mq_stat - New attributes
* oldstate - Old attributes
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
* Assumptions:
*
****************************************************************************/
int file_mq_setattr(FAR struct file *mq, FAR const struct mq_attr *mq_stat,
FAR struct mq_attr *oldstat);
/****************************************************************************
* Name: file_mq_getattr
*
* Description:
* This functions gets status information and attributes
* associated with the specified message queue.
*
* Input Parameters:
* mq - Message queue descriptor
* mq_stat - Buffer in which to return attributes
*
* Returned Value:
* This is an internal OS interface and should not be used by applications.
* It follows the NuttX internal error return policy: Zero (OK) is
* returned on success. A negated errno value is returned on failure.
*
* Assumptions:
*
****************************************************************************/
int file_mq_getattr(FAR struct file *mq, FAR struct mq_attr *mq_stat);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_MQ_MAXMSGSIZE > 0 */
#endif /* ___INCLUDE_NUTTX_MQUEUE_H */