update POSIX mqueue .

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1124 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong 2010-11-26 10:33:44 +00:00
parent baf30c5f3c
commit a9424c09e1
2 changed files with 29 additions and 51 deletions

View File

@ -3,24 +3,26 @@
#include <stdarg.h> #include <stdarg.h>
#include <errno.h> #include <errno.h>
#ifdef __GNUC__
#include <sys/fcntl.h> #include <sys/fcntl.h>
#endif
static posix_mq_t* posix_mq_list = RT_NULL; static mqd_t posix_mq_list = RT_NULL;
static struct rt_semaphore posix_mq_lock; static struct rt_semaphore posix_mq_lock;
void posix_mq_system_init() void posix_mq_system_init()
{ {
rt_sem_init(&posix_mq_lock, "pmq", 1, RT_IPC_FLAG_FIFO); rt_sem_init(&posix_mq_lock, "pmq", 1, RT_IPC_FLAG_FIFO);
} }
rt_inline void posix_mq_insert(posix_mq_t *pmq) rt_inline void posix_mq_insert(mqd_t pmq)
{ {
pmq->next = posix_mq_list; pmq->next = posix_mq_list;
posix_mq_list = pmq; posix_mq_list = pmq;
} }
static void posix_mq_delete(posix_mq_t *pmq) static void posix_mq_delete(mqd_t pmq)
{ {
posix_mq_t *iter; mqd_t iter;
if (posix_mq_list == pmq) if (posix_mq_list == pmq)
{ {
posix_mq_list = pmq->next; posix_mq_list = pmq->next;
@ -48,9 +50,9 @@ static void posix_mq_delete(posix_mq_t *pmq)
} }
} }
static posix_mq_t *posix_mq_find(const char* name) static mqd_t posix_mq_find(const char* name)
{ {
posix_mq_t *iter; mqd_t iter;
rt_object_t object; rt_object_t object;
for (iter = posix_mq_list; iter != RT_NULL; iter = iter->next) for (iter = posix_mq_list; iter != RT_NULL; iter = iter->next)
@ -81,8 +83,8 @@ int mq_getattr(mqd_t mqdes, struct mq_attr *mqstat)
return -1; return -1;
} }
mqstat->mq_maxmsg = mqdes->mq->mq->max_msgs; mqstat->mq_maxmsg = mqdes->mq->max_msgs;
mqstat->mq_msgsize = mqdes->mq->mq->msg_size; mqstat->mq_msgsize = mqdes->mq->msg_size;
mqstat->mq_curmsgs = 0; mqstat->mq_curmsgs = 0;
mqstat->mq_flags = 0; mqstat->mq_flags = 0;
@ -122,40 +124,27 @@ mqd_t mq_open(const char *name, int oflag, ...)
goto __return; goto __return;
} }
mqdes->flags = oflag;
mqdes->mq = (posix_mq_t*) rt_malloc (sizeof(posix_mq_t));
if (mqdes->mq == RT_NULL)
{
rt_set_errno(ENFILE);
goto __return;
}
/* create RT-Thread message queue */ /* create RT-Thread message queue */
mqdes->mq->mq = rt_mq_create(name, attr->mq_msgsize, attr->mq_maxmsg, RT_IPC_FLAG_FIFO); mqdes->mq = rt_mq_create(name, attr->mq_msgsize, attr->mq_maxmsg, RT_IPC_FLAG_FIFO);
if (mqdes->mq->mq == RT_NULL) /* create failed */ if (mqdes->mq == RT_NULL) /* create failed */
{ {
rt_set_errno(ENFILE); rt_set_errno(ENFILE);
goto __return; goto __return;
} }
/* initialize reference count */ /* initialize reference count */
mqdes->mq->refcount = 1; mqdes->refcount = 1;
mqdes->mq->unlinked = 0; mqdes->unlinked = 0;
/* insert mq to posix mq list */ /* insert mq to posix mq list */
posix_mq_insert(mqdes->mq); posix_mq_insert(mqdes);
} }
else else
{ {
posix_mq_t *mq;
/* find mqueue */ /* find mqueue */
mq = posix_mq_find(name); mqdes = posix_mq_find(name);
if (mq != RT_NULL) if (mqdes != RT_NULL)
{ {
mqdes = (mqd_t) rt_malloc (sizeof(struct mqdes)); mqdes->refcount ++; /* increase reference count */
mqdes->mq = mq;
mqdes->flags = oflag;
mq->refcount ++; /* increase reference count */
} }
else else
{ {
@ -176,9 +165,7 @@ __return:
if (mqdes->mq != RT_NULL) if (mqdes->mq != RT_NULL)
{ {
/* delete RT-Thread message queue */ /* delete RT-Thread message queue */
if (mqdes->mq->mq != RT_NULL) rt_mq_delete(mqdes->mq);
rt_mq_delete(mqdes->mq->mq);
rt_free(mqdes->mq);
} }
rt_free(mqdes); rt_free(mqdes);
} }
@ -195,7 +182,7 @@ ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_pri
return -1; return -1;
} }
result = rt_mq_recv(mqdes->mq->mq, msg_ptr, msg_len, RT_WAITING_FOREVER); result = rt_mq_recv(mqdes->mq, msg_ptr, msg_len, RT_WAITING_FOREVER);
if (result == RT_EOK) if (result == RT_EOK)
return msg_len; return msg_len;
@ -213,7 +200,7 @@ int mq_send(mqd_t mqdes, const char *msg_ptr, size_t msg_len, unsigned msg_prio)
return -1; return -1;
} }
result = rt_mq_send(mqdes->mq->mq, (void*)msg_ptr, msg_len); result = rt_mq_send(mqdes->mq, (void*)msg_ptr, msg_len);
if (result == RT_EOK) if (result == RT_EOK)
return 0; return 0;
@ -236,7 +223,7 @@ ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
tick = libc_time_to_tick(abs_timeout); tick = libc_time_to_tick(abs_timeout);
result = rt_mq_recv(mqdes->mq->mq, msg_ptr, msg_len, tick); result = rt_mq_recv(mqdes->mq, msg_ptr, msg_len, tick);
if (result == RT_EOK) return msg_len; if (result == RT_EOK) return msg_len;
if (result == -RT_ETIMEOUT) if (result == -RT_ETIMEOUT)
@ -270,23 +257,21 @@ int mq_close(mqd_t mqdes)
/* lock posix mqueue list */ /* lock posix mqueue list */
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER); rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);
mqdes->mq->refcount --; mqdes->refcount --;
if (mqdes->mq->refcount == 0) if (mqdes->refcount == 0)
{ {
/* delete from posix mqueue list */ /* delete from posix mqueue list */
if (mqdes->mq->unlinked) if (mqdes->unlinked)
posix_mq_delete(mqdes->mq); posix_mq_delete(mqdes);
mqdes->mq = RT_NULL;
} }
rt_sem_release(&posix_mq_lock); rt_sem_release(&posix_mq_lock);
rt_free(mqdes);
return 0; return 0;
} }
int mq_unlink(const char *name) int mq_unlink(const char *name)
{ {
posix_mq_t *pmq; mqd_t pmq;
/* lock posix mqueue list */ /* lock posix mqueue list */
rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER); rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER);

View File

@ -7,7 +7,7 @@
#include <sys/signal.h> #include <sys/signal.h>
#include <pthread.h> #include <pthread.h>
struct posix_mq struct mqdes
{ {
/* reference count and unlinked */ /* reference count and unlinked */
rt_uint16_t refcount; rt_uint16_t refcount;
@ -16,14 +16,7 @@ struct posix_mq
/* RT-Thread message queue */ /* RT-Thread message queue */
rt_mq_t mq; rt_mq_t mq;
/* next posix mqueue */ /* next posix mqueue */
struct posix_mq* next; struct mqdes* next;
};
typedef struct posix_mq posix_mq_t;
struct mqdes
{
rt_uint32_t flags;
posix_mq_t* mq;
}; };
typedef struct mqdes* mqd_t; typedef struct mqdes* mqd_t;