odyssey/sources/mm_msg_cache.c

62 lines
1.2 KiB
C
Raw Normal View History

2017-05-19 11:12:00 +00:00
/*
* machinarium.
*
* cooperative multitasking engine.
*/
#include <machinarium.h>
#include <machinarium_private.h>
void mm_msgcache_init(mm_msgcache_t *cache)
2017-05-19 11:12:00 +00:00
{
pthread_spin_init(&cache->lock, PTHREAD_PROCESS_PRIVATE);
mm_list_init(&cache->list);
cache->count = 0;
2017-05-19 11:12:00 +00:00
}
void mm_msgcache_free(mm_msgcache_t *cache)
2017-05-19 11:12:00 +00:00
{
mm_list_t *i, *n;
mm_list_foreach_safe(&cache->list, i, n) {
2017-05-19 11:12:00 +00:00
mm_msg_t *msg = mm_container_of(i, mm_msg_t, link);
2017-05-23 12:34:35 +00:00
mm_buf_free(&msg->data);
2017-05-19 11:12:00 +00:00
free(msg);
}
pthread_spin_destroy(&cache->lock);
2017-05-19 11:12:00 +00:00
}
mm_msg_t*
mm_msgcache_pop(mm_msgcache_t *cache)
2017-05-19 11:12:00 +00:00
{
mm_msg_t *msg = NULL;
pthread_spin_lock(&cache->lock);
if (cache->count > 0) {
mm_list_t *first = mm_list_pop(&cache->list);
cache->count--;
pthread_spin_unlock(&cache->lock);
2017-05-19 11:12:00 +00:00
msg = mm_container_of(first, mm_msg_t, link);
goto init;
}
pthread_spin_unlock(&cache->lock);
2017-05-19 11:12:00 +00:00
msg = malloc(sizeof(mm_msg_t));
if (msg == NULL)
return NULL;
mm_buf_init(&msg->data);
2017-05-19 11:12:00 +00:00
init:
msg->refs = 0;
msg->type = 0;
mm_buf_reset(&msg->data);
2017-05-19 11:12:00 +00:00
mm_list_init(&msg->link);
return msg;
}
void mm_msgcache_push(mm_msgcache_t *cache, mm_msg_t *msg)
2017-05-19 11:12:00 +00:00
{
pthread_spin_lock(&cache->lock);
mm_list_append(&cache->list, &msg->link);
cache->count++;
pthread_spin_unlock(&cache->lock);
2017-05-19 11:12:00 +00:00
}