diff options
Diffstat (limited to 'src/itc.c')
-rw-r--r-- | src/itc.c | 162 |
1 files changed, 81 insertions, 81 deletions
@@ -6,133 +6,133 @@ #include <sys/time.h> struct itc { - int nslots; - void **slots; - int inputidx; - int outputidx; - sem_t emptied; - sem_t occupied; + int nslots; + void **slots; + int inputidx; + int outputidx; + sem_t emptied; + sem_t occupied; }; itc *itc_alloc(int nslots) { - itc *ctx; + itc *ctx; - if (nslots <= 0) { - return NULL; - } + if (nslots <= 0) { + return NULL; + } - ctx = calloc(1, sizeof(*ctx)); - ctx->nslots = nslots; - ctx->slots = calloc(nslots, sizeof(*ctx->slots)); - sem_init(&ctx->emptied, 0, nslots); - sem_init(&ctx->occupied, 0, 0); + ctx = calloc(1, sizeof(*ctx)); + ctx->nslots = nslots; + ctx->slots = calloc(nslots, sizeof(*ctx->slots)); + sem_init(&ctx->emptied, 0, nslots); + sem_init(&ctx->occupied, 0, 0); - return ctx; + return ctx; } void itc_free(itc **ctx, itc_free_element free_element) { - if (ctx == NULL || *ctx == NULL) { - return; - } + if (ctx == NULL || *ctx == NULL) { + return; + } - itc_discard_all(*ctx, free_element); + itc_discard_all(*ctx, free_element); - sem_destroy(&(*ctx)->emptied); - sem_destroy(&(*ctx)->occupied); - free((*ctx)->slots); - free(*ctx); - *ctx = NULL; + sem_destroy(&(*ctx)->emptied); + sem_destroy(&(*ctx)->occupied); + free((*ctx)->slots); + free(*ctx); + *ctx = NULL; } void *itc_retrive(itc *ctx, int timeout_ms) { - struct timespec ts; - void *element; + struct timespec ts; + void *element; - if (ctx == NULL) { - return NULL; - } + if (ctx == NULL) { + return NULL; + } - timespec_add_ms(gettimespec(&ts), timeout_ms); + timespec_add_ms(gettimespec(&ts), timeout_ms); - if (sem_timedwait(&ctx->occupied, &ts)) { - return NULL; - } - element = ctx->slots[ctx->outputidx]; - ctx->outputidx = (ctx->outputidx + 1) % ctx->nslots; - sem_post(&ctx->emptied); + if (sem_timedwait(&ctx->occupied, &ts)) { + return NULL; + } + element = ctx->slots[ctx->outputidx]; + ctx->outputidx = (ctx->outputidx + 1) % ctx->nslots; + sem_post(&ctx->emptied); - return element; + return element; } int itc_inject(itc *ctx, int timeout_ms, void *element) { - struct timespec ts; + struct timespec ts; - if (ctx == NULL || element == NULL) { - return -1; - } + if (ctx == NULL || element == NULL) { + return -1; + } - timespec_add_ms(gettimespec(&ts), timeout_ms); + timespec_add_ms(gettimespec(&ts), timeout_ms); - if (sem_timedwait(&ctx->emptied, &ts)) { - return -1; - } - ctx->slots[ctx->inputidx] = element; - ctx->inputidx = (ctx->inputidx + 1) % ctx->nslots; - sem_post(&ctx->occupied); + if (sem_timedwait(&ctx->emptied, &ts)) { + return -1; + } + ctx->slots[ctx->inputidx] = element; + ctx->inputidx = (ctx->inputidx + 1) % ctx->nslots; + sem_post(&ctx->occupied); - return 0; + return 0; } void itc_wait_empty(itc *ctx) { - int i; - - if (ctx == NULL) { - return; - } - - for (i = 0; i < ctx->nslots; i++) { - sem_wait(&ctx->emptied); - } - for (i = 0; i < ctx->nslots; i++) { - sem_post(&ctx->emptied); - } + int i; + + if (ctx == NULL) { + return; + } + + for (i = 0; i < ctx->nslots; i++) { + sem_wait(&ctx->emptied); + } + for (i = 0; i < ctx->nslots; i++) { + sem_post(&ctx->emptied); + } } void itc_discard_all(itc *ctx, itc_free_element free_element) { - void *element; + void *element; - if (ctx == NULL) { - return; - } + if (ctx == NULL) { + return; + } - while ((element = itc_retrive(ctx, 0)) != NULL) { - if (free_element != NULL) { - free_element(element); - } - } + while ((element = itc_retrive(ctx, 0)) != NULL) { + if (free_element != NULL) { + free_element(element); + } + } } int itc_get_queued(itc *ctx) { - int val; - if (ctx == NULL) { - return -1; - } - sem_getvalue(&ctx->occupied, &val); - return val; + int val; + if (ctx == NULL) { + return -1; + } + sem_getvalue(&ctx->occupied, &val); + return val; } int itc_get_slots(itc *ctx) { - if (ctx == NULL) { - return -1; - } - return ctx->nslots; + if (ctx == NULL) { + return -1; + } + return ctx->nslots; } |