task_queue_t *newTaskQueue() { task_queue_t *newTq; newTq = malloc(sizeof(task_queue_t)); TEST_ALLOC(newTq, "newTq at newTaskQueue"); newTq->first = NULL; newTq->last = NULL; newTq->done = NULL; // mutex pour modifier la file newTq->lock = malloc(sizeof(pthread_mutex_t)); pthread_mutex_init(newTq->lock, NULL); // semaphore partagee entre threads newTq->not_empty = malloc(sizeof(sem_t)); sem_init(newTq->not_empty, 0, 0); return newTq; }
int main() { unsigned long long i; list = (struct foo**)malloc(N*sizeof(struct foo)); DYNAMIC_INIT(foo); for (i = 0; i < N; i++) { unsigned long long j; list[i] = TEST_ALLOC(foo); for (j = 0; j < i; j++) if (list[j] == list[i]) { fprintf(stderr, "allocated the same chunk twice: %p\n", list[i]); } list[i]->free = 0; list[i]->n = i; list[i]->moved = 0; } for (i = 0; i + 10 < N; i+=10) { int j; /* free 8 out of 10 */ for (j = 0; j < 8; j++) { free_foo(i+j); } } check_list(); #ifdef TEST_DEFRAGMENT fprintf(stderr, "density before defragment: %f\n", TEST_DENSITY(foo)); TEST_DEFRAGMENT(foo, relocate, NULL); fprintf(stderr, "density after defragment: %f\n", TEST_DENSITY(foo)); #endif check_list(); for (i = 0; i < N; i++) { if (list[i]) free_foo(i); } TEST_DEINIT(foo); free(list); return 0; }
void addTask(task_queue_t *tq, task_t *new_task) { queue_element_t *newElement; pthread_mutex_lock(tq->lock); newElement = malloc(sizeof(queue_element_t)); TEST_ALLOC(newElement, "newElement at addTask"); newElement->task = new_task; newElement->next = NULL; if (tq->first == NULL) { tq->first = newElement; tq->last = newElement; } else { tq->last->next = newElement; tq->last = newElement; } sem_post(tq->not_empty); pthread_mutex_unlock(tq->lock); }