Update sys_arch.c
authorArpit Agarwal <arpit.ag@samsung.com>
Thu, 27 Jul 2017 19:31:15 +0000 (01:01 +0530)
committerGitHub <noreply@github.com>
Thu, 27 Jul 2017 19:31:15 +0000 (01:01 +0530)
Fix Message Queue implementation (which is based on Circular Q).
Problem with earlier implementation is that in a particular case when front points to  start of queue and rear points to end of queue, now if new data comes it gets stored at rear (now Queue is full) and rear gets updated as (rear+1%max) which is front. Now if fetch API is called it results in false empty scenario as front == rear evaluates to true while the Queue is full.

os/net/lwip/sys/arch/sys_arch.c

index 6d5bfb0..910c9bf 100644 (file)
@@ -144,8 +144,6 @@ void sys_mbox_post(sys_mbox_t *mbox, void *msg)
                mbox->wait_send--;
        }
 
-       mbox->msgs[mbox->rear] = msg;
-       LWIP_DEBUGF(SYS_DEBUG, ("Post SUCCESS\n"));
        if (mbox->rear == mbox->front) {
                first_msg = 1;
        } else {
@@ -153,6 +151,8 @@ void sys_mbox_post(sys_mbox_t *mbox, void *msg)
        }
 
        mbox->rear = tmp;
+       mbox->msgs[mbox->rear] = msg;
+       LWIP_DEBUGF(SYS_DEBUG, ("Post SUCCESS\n"));
 
        /* If msg was posted to an empty queue, Release semaphore for
           some fetch api blocked on this sem due to Empty queue. */
@@ -193,8 +193,6 @@ err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
                goto errout_with_mutex;
        }
 
-       mbox->msgs[mbox->rear] = msg;
-       LWIP_DEBUGF(SYS_DEBUG, ("Post SUCCESS\n"));
        if (mbox->rear == mbox->front) {
                first_msg = 1;
        } else {
@@ -202,6 +200,8 @@ err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
        }
 
        mbox->rear = tmp;
+       mbox->msgs[mbox->rear] = msg;
+       LWIP_DEBUGF(SYS_DEBUG, ("Post SUCCESS\n"));
 
        /* If msg was posted to an empty queue, Release semaphore for
           some fetch api blocked on this sem due to Empty queue. */
@@ -272,6 +272,7 @@ u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
                mbox->wait_fetch--;
        }
 
+       mbox->front = (mbox->front + 1) % mbox->queue_size;
        if (msg != NULL) {
                *msg = mbox->msgs[mbox->front];
                LWIP_DEBUGF(SYS_DEBUG, (" mbox %p msg %p\n", (void *)mbox, *msg));
@@ -279,8 +280,6 @@ u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
                LWIP_DEBUGF(SYS_DEBUG, (" mbox %p, null msg\n", (void *)mbox));
        }
 
-       mbox->front = (mbox->front + 1) % mbox->queue_size;
-
        /* We just fetched a msg, Release semaphore for
           some post api blocked on this sem due to queue full. */
        if (mbox->wait_send) {
@@ -320,6 +319,7 @@ u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
                goto errout_with_mutex;
        }
 
+       mbox->front = (mbox->front + 1) % mbox->queue_size;
        if (msg != NULL) {
                LWIP_DEBUGF(SYS_DEBUG, ("mbox %p msg %p\n", (void *)mbox, *msg));
                *msg = mbox->msgs[mbox->front];
@@ -327,8 +327,6 @@ u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
                LWIP_DEBUGF(SYS_DEBUG, ("mbox %p, null msg\n", (void *)mbox));
        }
 
-       mbox->front = (mbox->front + 1) % mbox->queue_size;
-
        /* We just fetched a msg, Release semaphore for
           some post api blocked on this sem due to queue full. */
        if (mbox->wait_send) {