diff options
author | Uros Majstorovic <majstor@majstor.org> | 2019-10-13 05:21:28 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2019-10-13 05:21:28 +0200 |
commit | ca2d9002fcbebcf37f48944dd8d67e90c186ce4f (patch) | |
tree | 603660613302d30afca3687323c53f4434c36126 /code/fe310/eos/msgq.c | |
parent | 8c6c68e92d6b4c8aa5e2fcc784ed52da40845acc (diff) |
fixed msgq bug
Diffstat (limited to 'code/fe310/eos/msgq.c')
-rw-r--r-- | code/fe310/eos/msgq.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/code/fe310/eos/msgq.c b/code/fe310/eos/msgq.c index 2c31662..867bf94 100644 --- a/code/fe310/eos/msgq.c +++ b/code/fe310/eos/msgq.c @@ -3,7 +3,12 @@ #include "eos.h" #include "msgq.h" -#define EOS_MSGQ_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) +#define EOS_MSGQ_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) + +#define EOS_MSGQ_IDX_HALF ((uint8_t)1 << (sizeof(uint8_t) * 8 - 1)) +#define EOS_MSGQ_IDX_LT(a,b) ((uint8_t)((uint8_t)(a) - (uint8_t)(b)) > EOS_MSGQ_IDX_HALF) +#define EOS_MSGQ_IDX_LTE(a,b) ((uint8_t)((uint8_t)(b) - (uint8_t)(a)) < EOS_MSGQ_IDX_HALF) + void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { msgq->idx_r = 0; @@ -13,7 +18,7 @@ void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { } int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len) { - if (msgq->idx_w - msgq->idx_r == msgq->size) return EOS_ERR_Q_FULL; + if ((uint8_t)(msgq->idx_w - msgq->idx_r) == msgq->size) return EOS_ERR_Q_FULL; uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_w, msgq->size); msgq->array[idx].type = type; @@ -37,6 +42,12 @@ void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, ui } void eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char **buffer, uint16_t *len) { + if (msgq->idx_r == msgq->idx_w) { + *buffer = NULL; + *len = 0; + return; + } + uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size); unsigned char _type = msgq->array[idx].type; EOSMsgItem *tmp_item = &msgq->array[idx]; @@ -47,7 +58,7 @@ void eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char **buffer, uin msgq->idx_r++; return; } - for (idx = msgq->idx_r + 1; idx < msgq->idx_w; idx++) { + for (idx = msgq->idx_r + 1; EOS_MSGQ_IDX_LT(idx, msgq->idx_w); idx++) { *tmp_item = msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)]; msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)].type = _type; msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)].buffer = *buffer; |