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
|
/*
* pqueues interface for Real Time Linux.
*
* Copyright () 1999 Zentropic Computing, All rights reserved
*
* Authors: Trevor Woolven (trevw@zentropix.com)
*
* Original date: Thu 15 Jul 1999
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _RTAI_MQ_H
#define _RTAI_MQ_H
#include <linux/version.h>
#include <rtai_sem.h>
#define MQ_OPEN_MAX 8 /* Maximum number of message queues per process */
#define MQ_PRIO_MAX 32 /* Maximum number of message priorities */
#define MQ_BLOCK 0 /* Flag to set queue into blocking mode */
#define MQ_NONBLOCK 1 /* Flag to set queue into non-blocking mode */
#define MQ_NAME_MAX 80 /* Maximum length of a queue name string */
#define MQ_MIN_MSG_PRIORITY 0 /* Lowest priority message */
#define MQ_MAX_MSG_PRIORITY MQ_PRIO_MAX /* Highest priority message */
#define MAX_PQUEUES 4 /* Maximum number of message queues in module */
#define MAX_MSGSIZE 50 /* Maximum message size per queue (bytes) */
#define MAX_MSGS 10 /* Maximum number of messages per queue */
#define MAX_BLOCKED_TASKS 10 /* Maximum number of tasks blocked on a */
/* queue at any one time */
#define MSG_HDR_SIZE 16 /* Note that this is hard-coded (urgh!) ensure */
/* it always matches pqueues sizeof(MSG_HDR) */
/* or do it a better way! (sic) */
typedef enum {
FIFO_BASED,
PRIORITY_BASED
} QUEUEING_POLICY;
typedef enum {
POSIX,
VxWORKS
} QUEUE_TYPE;
typedef struct mq_attr {
long mq_maxmsg; /* Maximum number of messages in queue */
long mq_msgsize; /* Maximum size of a message (in bytes) */
long mq_flags; /* Blocking/Non-blocking behaviour specifier */
long mq_curmsgs; /* Number of messages currently in queue */
} MQ_ATTR;
#define INVALID_PQUEUE 0
#ifdef __KERNEL__
#include <linux/types.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,6)
typedef int mqd_t;
#endif
#ifndef __cplusplus
typedef int mq_bool_t;
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
typedef struct msg_hdr {
mq_bool_t in_use;
size_t size; /* Actual message size */
uint priority; /* Usage priority (message/task) */
void *next; /* Pointer to next message on queue */
} MSG_HDR;
typedef struct queue_control {
void *base; /* Pointer to the base of the queue in memory */
void *head; /* Pointer to the element at the front of the queue */
void *tail; /* Pointer to the element at the back of the queue */
MQ_ATTR attrs; /* Queue attributes */
} Q_CTRL;
typedef struct msg {
MSG_HDR hdr;
char data; /* Anchor point for message data */
} MQMSG;
struct notify {
RT_TASK *task;
struct sigevent data;
};
typedef struct _pqueue_descr_struct {
RT_TASK *owner; /* Task that created the queue */
int open_count; /* Count of the number of tasks that have */
/* 'opened' the queue for access */
char q_name[MQ_NAME_MAX]; /* Name supplied for queue */
uint q_id; /* Queue Id (index into static list of queues) */
mq_bool_t marked_for_deletion; /* Queue can be deleted once all tasks have */
/* closed it */
Q_CTRL data; /* Data queue (real messages) */
mode_t permissions; /* Permissions granted by creator (ugo, rwx) */
struct notify notify; /* Notification data (empty -> !empty) */
SEM emp_cond; /* For blocking on empty queue */
SEM full_cond; /* For blocking on full queue */
SEM mutex; /* For synchronisation of queue */
} MSG_QUEUE;
struct _pqueue_access_data {
int q_id;
int oflags; /* Queue access permissions & blocking spec */
};
typedef struct _pqueue_access_struct {
RT_TASK *this_task;
int n_open_pqueues;
struct _pqueue_access_data q_access[MQ_OPEN_MAX];
} *QUEUE_CTRL;
typedef enum {
FOR_READ,
FOR_WRITE
} Q_ACCESS;
/*
* a) A single Posix queue ( (MAX_MSGSIZE + sizeof(MSG_HDR) * MAX_MSGS) ) or
* b) A blocked tasks queue (MAX_BLOCKED_TASKS * sizeof(MSG_HDR) ) or
* c) A Zentropix application data staging structure (sizeof(Z_APPS))
*
* It is assumed that the first two are both bigger than a Z_APPS structure
* and so the choice is made between a) and b).
*
* Note that one control mechanism is used to allocate memory 'chunks' for a
* number of different application uses. This means that if the 'chunk' size
* becomes large in relation to the amount of memory required by one or other
* of these applications, memory usage becomes wasteful.
*
* Set of pointers to Application-Specific extensions to RTAI
* such as POSIX Threads, POSIX Queues, VxWorks Compatibility Library, etc
*/
typedef struct z_apps {
int in_use_count; // Incremented whenever an application is initialised
void *pthreads;
void *pqueues;
void *vxtasks;
// anticipate... pclocks, psosTasks,
} Z_APPS;
#else /* __cplusplus */
extern "C" {
#endif /* !__cplusplus */
int __rtai_mq_init(void);
void __rtai_mq_exit(void);
QUEUEING_POLICY get_task_queueing_policy(void);
QUEUEING_POLICY set_task_queuing_policy(QUEUEING_POLICY policy);
QUEUE_TYPE get_queue_type(void);
QUEUE_TYPE set_queue_type(QUEUE_TYPE type);
void *init_z_apps(void *this_task);
void free_z_apps(void *this_task);
mqd_t mq_open(char *mq_name, int oflags, mode_t permissions, struct mq_attr *mq_attr);
size_t _mq_receive(mqd_t mq, char *msg_buffer, size_t buflen, unsigned int *msgprio, int space);
static inline size_t mq_receive(mqd_t mq, char *msg_buffer, size_t buflen, unsigned int *msgprio)
{
return _mq_receive(mq, msg_buffer, buflen, msgprio, 1);
}
int _mq_send(mqd_t mq, const char *msg, size_t msglen, unsigned int msgprio, int space);
static inline int mq_send(mqd_t mq, const char *msg, size_t msglen, unsigned int msgprio)
{
return _mq_send(mq, msg, msglen, msgprio, 1);
}
int mq_close(mqd_t mq);
int mq_getattr(mqd_t mq, struct mq_attr *attrbuf);
int mq_setattr(mqd_t mq, const struct mq_attr *new_attrs, struct mq_attr *old_attrs);
int mq_notify(mqd_t mq, const struct sigevent *notification);
int mq_unlink(char *mq_name);
size_t _mq_timedreceive(mqd_t mq, char *msg_buffer, size_t buflen, unsigned int *msgprio, const struct timespec *abstime, int space);
static inline size_t mq_timedreceive(mqd_t mq, char *msg_buffer, size_t buflen, unsigned int *msgprio, const struct timespec *abstime)
{
return _mq_timedreceive(mq, msg_buffer, buflen, msgprio, abstime, 1);
}
int _mq_timedsend(mqd_t mq, const char *msg, size_t msglen, unsigned int msgprio, const struct timespec *abstime, int space);
static inline int mq_timedsend(mqd_t mq, const char *msg, size_t msglen, unsigned int msgprio, const struct timespec *abstime)
{
return _mq_timedsend(mq, msg, msglen, msgprio, abstime, 1);
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
#else /* !__KERNEL__ */
#include <signal.h>
#include <rtai_lxrt.h>
#define MQIDX 0
typedef int mqd_t;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
RTAI_PROTO(mqd_t, mq_open,(char *mq_name, int oflags, mode_t permissions, struct mq_attr *mq_attr))
{
struct {char *mq_name; int oflags; mode_t permissions; struct mq_attr *mq_attr; int namesize, attrsize; } arg = { mq_name, oflags, permissions, mq_attr, strlen(mq_name) + 1, sizeof(struct mq_attr) };
return (mqd_t)rtai_lxrt(MQIDX, SIZARG, MQ_OPEN, &arg).i[LOW];
}
RTAI_PROTO(size_t, mq_receive,(mqd_t mq, char *msg_buffer, size_t buflen, unsigned int *msgprio))
{
struct { mqd_t mq; char *msg_buffer; size_t buflen; unsigned int *msgprio; int space; } arg = { mq, msg_buffer, buflen, msgprio, 0 };
return (size_t)rtai_lxrt(MQIDX, SIZARG, MQ_RECEIVE, &arg).i[LOW];
}
RTAI_PROTO(int, mq_send,(mqd_t mq, const char *msg, size_t msglen, unsigned int msgprio))
{
struct { mqd_t mq; const char *msg; size_t msglen; unsigned int msgprio; int space; } arg = { mq, msg, msglen, msgprio, 0 };
return rtai_lxrt(MQIDX, SIZARG, MQ_SEND, &arg).i[LOW];
}
RTAI_PROTO(int, mq_close,(mqd_t mq))
{
struct { mqd_t mq; } arg = { mq };
return rtai_lxrt(MQIDX, SIZARG, MQ_CLOSE, &arg).i[LOW];
}
RTAI_PROTO(int, mq_getattr,(mqd_t mq, struct mq_attr *attrbuf))
{
struct { mqd_t mq; struct mq_attr *attrbuf; int attrsize; } arg = { mq, attrbuf, sizeof(struct mq_attr) };
return rtai_lxrt(MQIDX, SIZARG, MQ_GETATTR, &arg).i[LOW];
}
RTAI_PROTO(int, mq_setattr,(mqd_t mq, const struct mq_attr *new_attrs, struct mq_attr *old_attrs))
{
struct { mqd_t mq; const struct mq_attr *new_attrs; struct mq_attr *old_attrs; int attrsize; } arg = { mq, new_attrs, old_attrs, sizeof(struct mq_attr) };
return rtai_lxrt(MQIDX, SIZARG, MQ_SETATTR, &arg).i[LOW];
}
RTAI_PROTO(int, mq_notify,(mqd_t mq, const struct sigevent *notification))
{
struct { mqd_t mq; const struct sigevent *notification; int size; } arg = { mq, notification, sizeof(struct sigevent) };
return rtai_lxrt(MQIDX, SIZARG, MQ_NOTIFY, &arg).i[LOW];
}
RTAI_PROTO(int, mq_unlink,(char *mq_name))
{
struct { char *mq_name; int size; } arg = { mq_name, strlen(mq_name) + 1};
return rtai_lxrt(MQIDX, SIZARG, MQ_UNLINK, &arg).i[LOW];
}
RTAI_PROTO(size_t, mq_timedreceive,(mqd_t mq, char *msg_buffer, size_t buflen, unsigned int *msgprio, const struct timespec *abstime))
{
struct { mqd_t mq; char *msg_buffer; size_t buflen; unsigned int *msgprio; const struct timespec *abstime; int space; } arg = { mq, msg_buffer, buflen, msgprio, abstime, 0 };
return (size_t)rtai_lxrt(MQIDX, SIZARG, MQ_TIMEDRECEIVE, &arg).i[LOW];
}
RTAI_PROTO(int, mq_timedsend,(mqd_t mq, const char *msg, size_t msglen, unsigned int msgprio, const struct timespec *abstime))
{
struct { mqd_t mq; const char *msg; size_t msglen; unsigned int msgprio; const struct timespec *abstime; int space; } arg = { mq, msg, msglen, msgprio, abstime, 0 };
return rtai_lxrt(MQIDX, SIZARG, MQ_TIMEDSEND, &arg).i[LOW];
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __KERNEL__ */
#endif /* !_RTAI_MQ_H */
|