|
| 1 | +#include <fcntl.h> |
| 2 | +#include <pthread.h> |
| 3 | +#include <signal.h> |
| 4 | +#include <unistd.h> |
| 5 | + |
| 6 | +#ifdef __cplusplus |
| 7 | +extern "C" { |
| 8 | +#endif |
| 9 | + |
| 10 | +/// TODO: macOS should have this defined |
| 11 | +#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) |
| 12 | + |
| 13 | +typedef struct mq_info *mqd_t; /* opaque datatype */ |
| 14 | + |
| 15 | +struct mq_attr { |
| 16 | + long mq_flags; /* message queue flag: O_NONBLOCK */ |
| 17 | + long mq_maxmsg; /* max number of messages allowed on queue */ |
| 18 | + long mq_msgsize; /* max size of a message (in bytes) */ |
| 19 | + long mq_curmsgs; /* number of messages currently on queue */ |
| 20 | +}; |
| 21 | + |
| 22 | +/* one mq_hdr{} per queue, at beginning of mapped file */ |
| 23 | +struct mq_hdr { |
| 24 | + struct mq_attr mqh_attr; /* the queue's attributes */ |
| 25 | + long mqh_head; /* index of first message */ |
| 26 | + long mqh_free; /* index of first free message */ |
| 27 | + long mqh_nwait; /* #threads blocked in mq_receive() */ |
| 28 | + pid_t mqh_pid; /* nonzero PID if mqh_event set */ |
| 29 | + struct sigevent mqh_event; /* for mq_notify() */ |
| 30 | + pthread_mutex_t mqh_lock; /* mutex lock */ |
| 31 | + pthread_cond_t mqh_wait; /* and condition variable */ |
| 32 | +}; |
| 33 | + |
| 34 | +/* one mymsg_hdr{} at the front of each message in the mapped file */ |
| 35 | +struct mymsg_hdr { |
| 36 | + long msg_next; /* index of next on linked list */ |
| 37 | + /* msg_next must be first member in struct */ |
| 38 | + ssize_t msg_len; /* actual length */ |
| 39 | + unsigned int msg_prio; /* priority */ |
| 40 | +}; |
| 41 | + |
| 42 | +/* one mq_info{} malloc'ed per process per mq_open() */ |
| 43 | +struct mq_info { |
| 44 | + struct mq_hdr *mqi_hdr; /* start of mmap'ed region */ |
| 45 | + long mqi_magic; /* magic number if open */ |
| 46 | + int mqi_flags; /* flags for this process */ |
| 47 | +}; |
| 48 | +#define MQI_MAGIC 0x98765432 |
| 49 | + |
| 50 | +/* size of message in file is rounded up for alignment */ |
| 51 | +#define MSGSIZE(i) ((((i) + sizeof(long) - 1) / sizeof(long)) * sizeof(long)) |
| 52 | + |
| 53 | +int mq_close(mqd_t); |
| 54 | +int mq_getattr(mqd_t, struct mq_attr *); |
| 55 | +int mq_notify(mqd_t, const struct sigevent *); |
| 56 | +mqd_t mq_open(const char *, int, ...); |
| 57 | +ssize_t mq_receive(mqd_t, char *, size_t, unsigned int *); |
| 58 | +int mq_send(mqd_t, const char *, size_t, unsigned int); |
| 59 | +int mq_setattr(mqd_t, const struct mq_attr *, struct mq_attr *); |
| 60 | +int mq_unlink(const char *name); |
| 61 | +int mq_timedsend(mqd_t mqdes, |
| 62 | + const char *msg_ptr, |
| 63 | + size_t msg_len, |
| 64 | + unsigned msg_prio, |
| 65 | + const struct timespec *abs_timeout); |
| 66 | +ssize_t mq_timedreceive(mqd_t mqdes, |
| 67 | + char *msg_ptr, |
| 68 | + size_t msg_len, |
| 69 | + unsigned *msg_prio, |
| 70 | + const struct timespec *abs_timeout); |
| 71 | + |
| 72 | +#ifdef __cplusplus |
| 73 | +} |
| 74 | +#endif |
0 commit comments