Free threadqueue jobs when they are not needed.

- Also add destroying the mutex when the job is freed.

- This makes Kvazaar no longer acquire thousands of OS handles on Windows.
This commit is contained in:
Ari Koivula 2014-07-14 18:17:57 +03:00
parent 5d0df56c94
commit 5662621b3c
2 changed files with 40 additions and 14 deletions

View file

@ -3,6 +3,7 @@
#include <pthread.h>
#include <errno.h> //ETIMEDOUT
#include <stdlib.h>
#include <string.h>
#ifdef _DEBUG
#include <string.h>
@ -283,9 +284,11 @@ int threadqueue_init(threadqueue_queue * const threadqueue, int thread_count, in
return 1;
}
static void threadqueue_free_jobs(threadqueue_queue * const threadqueue) {
int i;
for (i=0; i < threadqueue->queue_count; ++i) {
/**
* \brief Free a single job from the threadqueue index i, destroying it.
*/
static void threadqueue_free_job(threadqueue_queue * const threadqueue, int i)
{
#ifdef _DEBUG
int j;
GET_TIME(&threadqueue->queue[i]->debug_clock_dequeue);
@ -298,7 +301,16 @@ static void threadqueue_free_jobs(threadqueue_queue * const threadqueue) {
FREE_POINTER(threadqueue->queue[i]->debug_description);
#endif
FREE_POINTER(threadqueue->queue[i]->rdepends);
pthread_mutex_destroy(&threadqueue->queue[i]->lock);
FREE_POINTER(threadqueue->queue[i]);
}
static void threadqueue_free_jobs(threadqueue_queue * const threadqueue) {
int i;
for (i=0; i < threadqueue->queue_count; ++i) {
threadqueue_free_job(threadqueue, i);
}
threadqueue->queue_count = 0;
threadqueue->queue_start = 0;
@ -455,6 +467,20 @@ int threadqueue_waitfor(threadqueue_queue * const threadqueue, threadqueue_job *
}
} while (!job_done);
// Free jobs submitted before this job.
int i;
for (i = 0; i < threadqueue->queue_count; ++i) {
if (threadqueue->queue[i] == job) break;
threadqueue_free_job(threadqueue, i);
}
// Move remaining jobs to the beginning of the array.
if (i > 0) {
threadqueue->queue_count -= i;
threadqueue->queue_start = 0;
memmove(threadqueue->queue, &threadqueue->queue[i], threadqueue->queue_count * sizeof(*threadqueue->queue));
memset(&threadqueue->queue[threadqueue->queue_count], 0, i * sizeof(*threadqueue->queue));
}
PTHREAD_UNLOCK(&threadqueue->lock);
return 1;

View file

@ -109,7 +109,7 @@ int threadqueue_job_dep_add(threadqueue_job *job, threadqueue_job *depends_on);
//Blocking call until the queue is empty. Previously set threadqueue_job handles should not be used anymore
int threadqueue_flush(threadqueue_queue * threadqueue);
//Blocking call until job is executed. Job handles submitted before job should not be used any more.
//Blocking call until job is executed. Job handles submitted before job should not be used any more as they are removed from the queue.
int threadqueue_waitfor(threadqueue_queue * threadqueue, threadqueue_job * job);
//Free ressources in a threadqueue