From a9424c09e12e09c309d30eb79e2b59f177133aac Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Fri, 26 Nov 2010 10:33:44 +0000 Subject: [PATCH] update POSIX mqueue . git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1124 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/pthreads/mqueue.c | 69 ++++++++++++++---------------------- components/pthreads/mqueue.h | 11 ++---- 2 files changed, 29 insertions(+), 51 deletions(-) diff --git a/components/pthreads/mqueue.c b/components/pthreads/mqueue.c index f4869a0476..8f084741dd 100644 --- a/components/pthreads/mqueue.c +++ b/components/pthreads/mqueue.c @@ -3,24 +3,26 @@ #include #include +#ifdef __GNUC__ #include +#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; void posix_mq_system_init() { 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; 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) { 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; 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; } - mqstat->mq_maxmsg = mqdes->mq->mq->max_msgs; - mqstat->mq_msgsize = mqdes->mq->mq->msg_size; + mqstat->mq_maxmsg = mqdes->mq->max_msgs; + mqstat->mq_msgsize = mqdes->mq->msg_size; mqstat->mq_curmsgs = 0; mqstat->mq_flags = 0; @@ -122,40 +124,27 @@ mqd_t mq_open(const char *name, int oflag, ...) 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 */ - mqdes->mq->mq = rt_mq_create(name, attr->mq_msgsize, attr->mq_maxmsg, RT_IPC_FLAG_FIFO); - if (mqdes->mq->mq == RT_NULL) /* create failed */ + mqdes->mq = rt_mq_create(name, attr->mq_msgsize, attr->mq_maxmsg, RT_IPC_FLAG_FIFO); + if (mqdes->mq == RT_NULL) /* create failed */ { rt_set_errno(ENFILE); goto __return; } /* initialize reference count */ - mqdes->mq->refcount = 1; - mqdes->mq->unlinked = 0; + mqdes->refcount = 1; + mqdes->unlinked = 0; /* insert mq to posix mq list */ - posix_mq_insert(mqdes->mq); + posix_mq_insert(mqdes); } else { - posix_mq_t *mq; - /* find mqueue */ - mq = posix_mq_find(name); - if (mq != RT_NULL) + mqdes = posix_mq_find(name); + if (mqdes != RT_NULL) { - mqdes = (mqd_t) rt_malloc (sizeof(struct mqdes)); - mqdes->mq = mq; - mqdes->flags = oflag; - mq->refcount ++; /* increase reference count */ + mqdes->refcount ++; /* increase reference count */ } else { @@ -176,9 +165,7 @@ __return: if (mqdes->mq != RT_NULL) { /* delete RT-Thread message queue */ - if (mqdes->mq->mq != RT_NULL) - rt_mq_delete(mqdes->mq->mq); - rt_free(mqdes->mq); + rt_mq_delete(mqdes->mq); } 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; } - 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) 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; } - 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) 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); - 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_ETIMEOUT) @@ -270,23 +257,21 @@ int mq_close(mqd_t mqdes) /* lock posix mqueue list */ rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER); - mqdes->mq->refcount --; - if (mqdes->mq->refcount == 0) + mqdes->refcount --; + if (mqdes->refcount == 0) { /* delete from posix mqueue list */ - if (mqdes->mq->unlinked) - posix_mq_delete(mqdes->mq); - mqdes->mq = RT_NULL; + if (mqdes->unlinked) + posix_mq_delete(mqdes); } rt_sem_release(&posix_mq_lock); - rt_free(mqdes); return 0; } int mq_unlink(const char *name) { - posix_mq_t *pmq; + mqd_t pmq; /* lock posix mqueue list */ rt_sem_take(&posix_mq_lock, RT_WAITING_FOREVER); diff --git a/components/pthreads/mqueue.h b/components/pthreads/mqueue.h index fb99e58068..c6d17b80ff 100644 --- a/components/pthreads/mqueue.h +++ b/components/pthreads/mqueue.h @@ -7,7 +7,7 @@ #include #include -struct posix_mq +struct mqdes { /* reference count and unlinked */ rt_uint16_t refcount; @@ -16,14 +16,7 @@ struct posix_mq /* RT-Thread message queue */ rt_mq_t mq; /* next posix mqueue */ - struct posix_mq* next; -}; -typedef struct posix_mq posix_mq_t; - -struct mqdes -{ - rt_uint32_t flags; - posix_mq_t* mq; + struct mqdes* next; }; typedef struct mqdes* mqd_t;