#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include "utils.h" void dispatchTasks() { while (1) { struct task *curTask = (struct task *)malloc(sizeof(struct task)); curTask -> fileIndex = fileIndexer -> fileIndex; curTask -> fileCursor = fileIndexer -> fileCursor; struct result *curResult = (struct result *)malloc(sizeof(struct result)); curResult -> charArray = (unsigned char *)malloc(sizeof(unsigned char) * CHUNKSIZE); curResult -> countArray = (uint8_t *)malloc(sizeof(uint8_t) * CHUNKSIZE); curResult -> resultLength = 0; pthread_mutex_lock(&taskQueueAccess); lastTask ++; lastResult ++; taskQueue[lastTask] = curTask; resultQueue[lastResult] = curResult; if (firstTask == lastTask) { pthread_cond_signal(&taskInQueue); } if (DEBUG) fprintf(stderr, "task %d enqueued\n", lastTask); pthread_mutex_unlock(&taskQueueAccess); if (fileIndexer -> fileCursor + CHUNKSIZE >= fileLengths[fileIndexer -> fileIndex]) { if (fileIndexer -> fileIndex >= fileCount - 1) { break; } fileIndexer -> fileIndex ++; fileIndexer -> fileCursor = 0; } else { fileIndexer -> fileCursor += CHUNKSIZE; } } }