Skip to content

Commit

Permalink
update(cherrymp): change mq to sem for fast run
Browse files Browse the repository at this point in the history
  • Loading branch information
sakumisu committed Nov 26, 2024
1 parent 1a39169 commit b3bbaf3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
3 changes: 3 additions & 0 deletions third_party/cherrymp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CherryMP

CherryMP is a tiny block memory pool based on CherryRB, support nonos or os(but we suggest you use in os), and only transfer data address not data content.
74 changes: 57 additions & 17 deletions third_party/cherrymp/chry_mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,63 @@
int chry_mempool_create(struct chry_mempool *pool, void *block, uint32_t block_size, uint32_t block_count)
{
uintptr_t addr;
uint8_t *ringbuf;
uint8_t *ringbuf1;
uint8_t *ringbuf2;

ringbuf = usb_osal_malloc(sizeof(uintptr_t) * block_count);
memset(ringbuf, 0, sizeof(uintptr_t) * block_count);
ringbuf1 = usb_osal_malloc(sizeof(uintptr_t) * block_count);
if (ringbuf1 == NULL) {
return -1;
}
memset(ringbuf1, 0, sizeof(uintptr_t) * block_count);

if (chry_ringbuffer_init(&pool->rb, ringbuf, sizeof(uintptr_t) * block_count) == -1) {
usb_osal_free(ringbuf);
if (chry_ringbuffer_init(&pool->in, ringbuf1, sizeof(uintptr_t) * block_count) == -1) {
usb_osal_free(ringbuf1);
return -1;
}

for (uint32_t i = 0; i < block_count; i++) {
addr = ((uintptr_t)block + i * block_size);
chry_ringbuffer_write(&pool->rb, &addr, sizeof(uintptr_t));
ringbuf2 = usb_osal_malloc(sizeof(uintptr_t) * block_count);
if (ringbuf2 == NULL) {
usb_osal_free(ringbuf1);
return -1;
}
pool->mq = usb_osal_mq_create(block_count);
if (pool->mq == NULL) {
memset(ringbuf2, 0, sizeof(uintptr_t) * block_count);

if (chry_ringbuffer_init(&pool->out, ringbuf2, sizeof(uintptr_t) * block_count) == -1) {
usb_osal_free(ringbuf1);
usb_osal_free(ringbuf2);
return -1;
}

pool->out_sem = usb_osal_sem_create(block_count);
if (pool->out_sem == NULL) {
usb_osal_free(ringbuf1);
usb_osal_free(ringbuf2);
return -1;
}

for (uint32_t i = 0; i < block_count; i++) {
addr = ((uintptr_t)block + i * block_size);
chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
}

return 0;
}

void chry_mempool_delete(struct chry_mempool *pool)
{
usb_osal_mq_delete(pool->mq);
chry_ringbuffer_reset(&pool->rb);
usb_osal_free(pool->rb.pool);
usb_osal_sem_delete(pool->out_sem);
chry_ringbuffer_reset(&pool->in);
chry_ringbuffer_reset(&pool->out);
usb_osal_free(pool->in.pool);
usb_osal_free(pool->out.pool);
}

uintptr_t *chry_mempool_alloc(struct chry_mempool *pool)
{
uintptr_t *addr;
uint32_t len;

len = chry_ringbuffer_read(&pool->rb, (uintptr_t *)&addr, sizeof(uintptr_t));
len = chry_ringbuffer_read(&pool->in, (uintptr_t *)&addr, sizeof(uintptr_t));
if (len == 0) {
return NULL;
} else {
Expand All @@ -54,15 +77,32 @@ int chry_mempool_free(struct chry_mempool *pool, uintptr_t *item)
uintptr_t addr;

addr = (uintptr_t)item;
return chry_ringbuffer_write(&pool->rb, &addr, sizeof(uintptr_t));
return chry_ringbuffer_write(&pool->in, &addr, sizeof(uintptr_t));
}

int chry_mempool_send(struct chry_mempool *pool, uintptr_t *item)
{
return usb_osal_mq_send(pool->mq, (uintptr_t)item);
uintptr_t addr;

addr = (uintptr_t)item;
chry_ringbuffer_write(&pool->out, &addr, sizeof(uintptr_t));
return usb_osal_sem_give(pool->out_sem);
}

int chry_mempool_recv(struct chry_mempool *pool, uintptr_t **item, uint32_t timeout)
{
return usb_osal_mq_recv(pool->mq, (uintptr_t *)item, timeout);
uint32_t len;
int ret;

ret = usb_osal_sem_take(pool->out_sem, timeout);
if (ret < 0) {
return -1;
}

len = chry_ringbuffer_read(&pool->out, (uintptr_t *)item, sizeof(uintptr_t));
if (len == 0) {
return -1;
} else {
return 0;
}
}
5 changes: 3 additions & 2 deletions third_party/cherrymp/chry_mempool.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include "chry_ringbuffer.h"

struct chry_mempool {
chry_ringbuffer_t rb;
usb_osal_mq_t mq;
chry_ringbuffer_t in;
chry_ringbuffer_t out;
usb_osal_sem_t out_sem;
};

#ifdef __cplusplus
Expand Down

0 comments on commit b3bbaf3

Please sign in to comment.