-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposixwrapper.c
134 lines (116 loc) · 2.94 KB
/
posixwrapper.c
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
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <mqueue.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef void *NotifyHandler;
mqd_t pmq_open(const char *name, int oflag, int *errnum) {
mqd_t ret;
ret = mq_open(name, oflag);
if (ret == -1) {
if (errnum != NULL)
*errnum = errno;
}
return ret;
}
mqd_t pmq_open_attr(const char *name, int oflag, int mode, int maxMsg,
int msgSize, int *errnum) {
mqd_t ret;
struct mq_attr attr;
/* initialize the queue attributes */
attr.mq_flags = 0;
attr.mq_maxmsg = maxMsg;
attr.mq_msgsize = msgSize;
attr.mq_curmsgs = 0;
ret = mq_open(name, oflag, mode, &attr);
if (ret == -1) {
if (errnum != NULL)
*errnum = errno;
}
return ret;
}
mqd_t pmq_open_defaults(const char *name, int oflag, int mode, int *errnum) {
mqd_t ret;
void *attr;
ret = mq_open(name, oflag, mode, &attr);
if (ret == -1) {
if (errnum != NULL)
*errnum = errno;
}
return ret;
}
mqd_t pmq_close(mqd_t mqdes, int *errnum) {
mqd_t ret;
ret = mq_close(mqdes);
if (ret == -1) {
if (errnum != NULL)
*errnum = errno;
}
return ret;
}
// Expose sigval instead of comples sigevent structure
mqd_t pmq_notify(mqd_t mqdes, NotifyHandler notifyHandler, int *errnum) {
int ret;
struct sigevent sev;
// Notify via thread
sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = notifyHandler;
// Could be pointer to pthread_attr_t structure
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_int = mqdes;
ret = mq_notify(mqdes, &sev);
if (ret == -1) {
if (errnum != NULL)
*errnum = errno;
}
return ret;
}
// Expose timeout instead of timespec structure.
mqd_t pmq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
unsigned int *msg_prio, int timeout, int *errnum) {
int ret;
struct timespec tm;
char buf[msg_len];
if (timeout == 0) {
ret = mq_receive(mqdes, msg_ptr, msg_len, msg_prio);
} else {
clock_gettime(CLOCK_REALTIME, &tm);
tm.tv_sec += timeout; // Set for 20 seconds
ret = mq_timedreceive(mqdes, buf, msg_len, msg_prio, &tm);
memcpy(msg_ptr, buf, msg_len);
}
if (ret == -1) {
if (errnum != NULL)
*errnum = errno;
}
return ret;
}
mqd_t pmq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
unsigned int msg_prio, int timeout, int *errnum) {
int ret;
struct timespec tm;
if (timeout == 0) {
ret = mq_send(mqdes, msg_ptr, msg_len, msg_prio);
} else {
clock_gettime(CLOCK_REALTIME, &tm);
tm.tv_sec += timeout; // Set for 20 seconds
ret = mq_timedsend(mqdes, msg_ptr, msg_len, msg_prio, &tm);
}
if (ret == -1) {
if (errnum != NULL)
*errnum = errno;
}
return ret;
}
mqd_t pmq_unlink(const char *name, int *errnum) {
mqd_t ret;
ret = mq_unlink(name);
if (ret == -1) {
if (errnum != NULL)
*errnum = errno;
}
return ret;
}