#include "arena.h" #include "ll.h" #include "encryption.h" #include "scanner.h" #include "shared.h" #include "threadpool.h" #include #include #include #include #include typedef int (*thingcb)(void*); struct thing { thingcb cb; void *arg; }; typedef struct queue { mtx_t *cqmutex; cnd_t *cqcnd; unsigned char cstatus; int len; int used; struct thing **queue; } ccqueue; #define QUEUE_LEN 10 int consumer(void *queue) { if(!queue) thrd_exit(-1); ccqueue *real = (ccqueue *)queue; for(struct thing *thing = NULL;;) { cnd_wait(real->cqcnd, real->cqmutex); if(real->cstatus) thrd_exit(-1); if(real->used > 0) { thing = real->queue[real->used - 1]; real->used--; } if(thing != NULL) if(thing->arg != NULL) thing->cb(thing->arg); } thrd_exit(0); } int testcb(void *data) { if(!data) return -1; printf("%s\n", (char*)data); return 0; } int main() { // error(1, ENOTSUP, "No main file lol"); // Manually implement a threadpool and consumer in a "dumb" way to make sure I'm doing the correct thing ccqueue *cqueue = xcalloc(1, sizeof(*cqueue)); cqueue->cqmutex = xcalloc(1, sizeof(*cqueue->cqmutex)); mtx_init(cqueue->cqmutex, mtx_plain); cqueue->cqcnd = xcalloc(1, sizeof(*cqueue->cqcnd)); cnd_init(cqueue->cqcnd); cqueue->cstatus = 0; cqueue->len = QUEUE_LEN; cqueue->used = 0; cqueue->queue = xcalloc(cqueue->len, sizeof(struct thing *)); thrd_t *thread = xcalloc(1, sizeof(*thread)); thrd_create(thread, consumer, (void *)cqueue); mtx_lock(cqueue->cqmutex); cqueue->queue[cqueue->used] = xcalloc(1, sizeof(struct thing)); cqueue->queue[cqueue->used]->cb = testcb; cqueue->queue[cqueue->used]->arg = "This is some data"; cqueue->used++; mtx_unlock(cqueue->cqmutex); cnd_signal(cqueue->cqcnd); sleep(1); mtx_lock(cqueue->cqmutex); cqueue->cstatus++; mtx_unlock(cqueue->cqmutex); cnd_broadcast(cqueue->cqcnd); // Destroy and free everything mtx_destroy(cqueue->cqmutex); cnd_destroy(cqueue->cqcnd); free(cqueue->cqmutex); free(cqueue->cqcnd); free(cqueue->queue); free(cqueue); return 0; } // Ok well now I'm pissed. The canceling works fine but it's not actually consuming the thing