2015-02-23 11:18:48 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
|
|
|
* COPYING file).
|
|
|
|
*
|
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* Kvazaar is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <pthread.h>
|
2014-06-12 10:57:14 +00:00
|
|
|
#include <errno.h> //ETIMEDOUT
|
2014-05-13 09:28:15 +00:00
|
|
|
#include <stdlib.h>
|
2014-07-14 15:17:57 +00:00
|
|
|
#include <string.h>
|
2014-05-16 08:15:05 +00:00
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
#include <string.h>
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|
2014-05-13 09:28:15 +00:00
|
|
|
|
|
|
|
#include "global.h"
|
|
|
|
#include "threadqueue.h"
|
|
|
|
#include "threads.h"
|
|
|
|
|
2014-05-15 08:14:07 +00:00
|
|
|
typedef struct {
|
2015-03-04 14:30:20 +00:00
|
|
|
threadqueue_queue_t * threadqueue;
|
2014-05-15 08:14:07 +00:00
|
|
|
int worker_id;
|
|
|
|
} threadqueue_worker_spec;
|
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
#define THREADQUEUE_LIST_REALLOC_SIZE 32
|
|
|
|
|
2014-06-03 08:26:15 +00:00
|
|
|
//#define PTHREAD_COND_SIGNAL(c) fprintf(stderr, "%s:%d pthread_cond_signal(%s=%p)\n", __FUNCTION__, __LINE__, #c, c); if (pthread_cond_signal((c)) != 0) { fprintf(stderr, "pthread_cond_signal(%s=%p) failed!\n", #c, c); assert(0); return 0; }
|
|
|
|
//#define PTHREAD_COND_BROADCAST(c) fprintf(stderr, "%s:%d pthread_cond_broadcast(%s=%p)\n", __FUNCTION__, __LINE__, #c, c); if (pthread_cond_broadcast((c)) != 0) { fprintf(stderr, "pthread_cond_broadcast(%s=%p) failed!\n", #c, c); assert(0); return 0; }
|
|
|
|
//#define PTHREAD_COND_WAIT(c,l) fprintf(stderr, "%s:%d pthread_cond_wait(%s=%p, %s=%p)\n", __FUNCTION__, __LINE__, #c, c, #l, l); if (pthread_cond_wait((c),(l)) != 0) { fprintf(stderr, "pthread_cond_wait(%s=%p, %s=%p) failed!\n", #c, c, #l, l); assert(0); return 0; } else {fprintf(stderr, "%s:%d pthread_cond_wait(%s=%p, %s=%p) (done)\n", __FUNCTION__, __LINE__, #c, c, #l, l);}
|
2014-05-13 09:28:15 +00:00
|
|
|
//#define PTHREAD_LOCK(l) fprintf(stderr, "%s:%d pthread_mutex_lock(%s=%p) (try)\n", __FUNCTION__, __LINE__, #l, l); if (pthread_mutex_lock((l)) != 0) { fprintf(stderr, "pthread_mutex_lock(%s=%p) failed!\n", #l, l); assert(0); return 0; } else {fprintf(stderr, "%s:%d pthread_mutex_lock(%s=%p)\n", __FUNCTION__, __LINE__, #l, l);}
|
|
|
|
//#define PTHREAD_UNLOCK(l) if (pthread_mutex_unlock((l)) != 0) { fprintf(stderr, "pthread_mutex_unlock(%s=%p) failed!\n", #l, l); assert(0); return 0; } else {fprintf(stderr, "%s:%d pthread_mutex_unlock(%s=%p)\n", __FUNCTION__, __LINE__, #l, l);}
|
|
|
|
|
2014-06-13 05:15:26 +00:00
|
|
|
|
2014-06-03 08:26:15 +00:00
|
|
|
#define PTHREAD_COND_SIGNAL(c) if (pthread_cond_signal((c)) != 0) { fprintf(stderr, "pthread_cond_signal(%s=%p) failed!\n", #c, c); assert(0); return 0; }
|
|
|
|
#define PTHREAD_COND_BROADCAST(c) if (pthread_cond_broadcast((c)) != 0) { fprintf(stderr, "pthread_cond_broadcast(%s=%p) failed!\n", #c, c); assert(0); return 0; }
|
2014-06-13 05:15:26 +00:00
|
|
|
|
|
|
|
#ifndef _PTHREAD_DUMP
|
2014-06-03 08:26:15 +00:00
|
|
|
#define PTHREAD_COND_WAIT(c,l) if (pthread_cond_wait((c),(l)) != 0) { fprintf(stderr, "pthread_cond_wait(%s=%p, %s=%p) failed!\n", #c, c, #l, l); assert(0); return 0; }
|
2014-05-13 09:28:15 +00:00
|
|
|
#define PTHREAD_LOCK(l) if (pthread_mutex_lock((l)) != 0) { fprintf(stderr, "pthread_mutex_lock(%s) failed!\n", #l); assert(0); return 0; }
|
|
|
|
#define PTHREAD_UNLOCK(l) if (pthread_mutex_unlock((l)) != 0) { fprintf(stderr, "pthread_mutex_unlock(%s) failed!\n", #l); assert(0); return 0; }
|
|
|
|
|
2014-06-13 05:15:26 +00:00
|
|
|
#else //PTHREAD_DUMP
|
|
|
|
#define PTHREAD_LOCK(l) do { \
|
|
|
|
PERFORMANCE_MEASURE_START(); \
|
|
|
|
if (pthread_mutex_lock((l)) != 0) { fprintf(stderr, "pthread_mutex_lock(%s) failed!\n", #l); assert(0); return 0; } \
|
|
|
|
PERFORMANCE_MEASURE_END(NULL, "pthread_mutex_lock(%s=%p)@%s:%d",#l,l,__FUNCTION__, __LINE__); \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
#define PTHREAD_UNLOCK(l) do { \
|
|
|
|
PERFORMANCE_MEASURE_START(); \
|
|
|
|
if (pthread_mutex_unlock((l)) != 0) { fprintf(stderr, "pthread_mutex_unlock(%s) failed!\n", #l); assert(0); return 0; } \
|
|
|
|
PERFORMANCE_MEASURE_END(NULL, "pthread_mutex_unlock(%s=%p)@%s:%d",#l,l,__FUNCTION__, __LINE__); \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
#define PTHREAD_COND_WAIT(c,l) do { \
|
|
|
|
PERFORMANCE_MEASURE_START(); \
|
|
|
|
if (pthread_cond_wait((c),(l)) != 0) { fprintf(stderr, "pthread_cond_wait(%s=%p, %s=%p) failed!\n", #c, c, #l, l); assert(0); return 0;} \
|
|
|
|
PERFORMANCE_MEASURE_END(NULL, "pthread_cond_wait(%s=%p, %s=%p)@%s:%d",#c, c, #l, l,__FUNCTION__, __LINE__); \
|
|
|
|
} while (0);
|
|
|
|
#endif //PTHREAD_DUMP
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
const struct timespec kvz_time_to_wait = {1, 0};
|
2014-06-16 08:41:50 +00:00
|
|
|
|
2014-05-15 08:14:07 +00:00
|
|
|
static void* threadqueue_worker(void* threadqueue_worker_spec_opaque) {
|
|
|
|
threadqueue_worker_spec * const threadqueue_worker_spec = threadqueue_worker_spec_opaque;
|
2015-03-04 14:30:20 +00:00
|
|
|
threadqueue_queue_t * const threadqueue = threadqueue_worker_spec->threadqueue;
|
2015-03-04 14:28:56 +00:00
|
|
|
threadqueue_job_t * next_job = NULL;
|
2014-05-13 09:28:15 +00:00
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-16 08:15:05 +00:00
|
|
|
GET_TIME(&threadqueue->debug_clock_thread_start[threadqueue_worker_spec->worker_id]);
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
for(;;) {
|
2014-06-03 08:26:15 +00:00
|
|
|
int i = 0;
|
2015-03-04 14:28:56 +00:00
|
|
|
threadqueue_job_t * job = NULL;
|
2014-05-13 09:28:15 +00:00
|
|
|
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
|
|
|
|
2014-06-03 08:26:15 +00:00
|
|
|
while(!threadqueue->stop && threadqueue->queue_waiting_execution == 0 && !next_job) {
|
|
|
|
PTHREAD_COND_WAIT(&threadqueue->cond, &threadqueue->lock);
|
2014-05-13 09:28:15 +00:00
|
|
|
}
|
2014-06-03 08:26:15 +00:00
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
if(threadqueue->stop) {
|
2014-06-03 08:26:15 +00:00
|
|
|
if (next_job) {
|
|
|
|
PTHREAD_LOCK(&next_job->lock);
|
|
|
|
next_job->state = THREADQUEUE_JOB_STATE_QUEUED;
|
|
|
|
PTHREAD_UNLOCK(&next_job->lock);
|
|
|
|
}
|
|
|
|
break;
|
2014-05-13 09:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Find a task (should be fast enough)
|
2014-06-13 06:10:10 +00:00
|
|
|
job = NULL;
|
2014-06-03 08:26:15 +00:00
|
|
|
if (next_job) {
|
|
|
|
assert(next_job->ndepends == 0);
|
2014-06-13 06:10:10 +00:00
|
|
|
job = next_job;
|
2014-06-16 05:24:31 +00:00
|
|
|
} else {
|
|
|
|
//FIXME: if not using OWF, the first is better than the second, otherwise we should use the second order
|
|
|
|
//for (i = threadqueue->queue_count - 1; i >= threadqueue->queue_start; --i) {
|
2014-06-13 06:10:10 +00:00
|
|
|
//for (i = threadqueue->queue_start; i < threadqueue->queue_count; ++i) {
|
2014-06-16 05:24:31 +00:00
|
|
|
|
|
|
|
for (i = (threadqueue->fifo ? threadqueue->queue_start : threadqueue->queue_count - 1);
|
|
|
|
(threadqueue->fifo ? i < threadqueue->queue_count : i >= threadqueue->queue_start);
|
|
|
|
(threadqueue->fifo ? ++i : --i)) {
|
2015-03-04 14:28:56 +00:00
|
|
|
threadqueue_job_t * const i_job = threadqueue->queue[i];
|
2014-06-03 08:26:15 +00:00
|
|
|
|
|
|
|
if (i_job->state == THREADQUEUE_JOB_STATE_QUEUED && i_job->ndepends == 0) {
|
2014-06-13 06:10:10 +00:00
|
|
|
PTHREAD_LOCK(&i_job->lock);
|
|
|
|
if (i_job->state == THREADQUEUE_JOB_STATE_QUEUED && i_job->ndepends == 0) {
|
|
|
|
job = i_job;
|
|
|
|
job->state = THREADQUEUE_JOB_STATE_RUNNING;
|
|
|
|
}
|
|
|
|
PTHREAD_UNLOCK(&i_job->lock);
|
|
|
|
if (job) break;
|
2014-06-03 08:26:15 +00:00
|
|
|
}
|
2014-05-13 09:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Ok we got a job (and we have a lock on it)
|
2014-06-13 06:10:10 +00:00
|
|
|
if (job) {
|
2014-06-12 11:53:50 +00:00
|
|
|
int queue_waiting_dependency_decr, queue_waiting_execution_incr;
|
2014-05-13 09:28:15 +00:00
|
|
|
|
2014-06-13 06:10:10 +00:00
|
|
|
assert(job->state == THREADQUEUE_JOB_STATE_RUNNING);
|
2014-05-13 09:28:15 +00:00
|
|
|
|
2014-05-21 08:57:07 +00:00
|
|
|
//Move the queue_start "pointer" if needed
|
|
|
|
while (threadqueue->queue_start < threadqueue->queue_count && threadqueue->queue[threadqueue->queue_start]->state != THREADQUEUE_JOB_STATE_QUEUED) threadqueue->queue_start++;
|
|
|
|
|
2014-06-13 06:10:10 +00:00
|
|
|
if (!next_job) {
|
|
|
|
--threadqueue->queue_waiting_execution;
|
|
|
|
++threadqueue->queue_running;
|
|
|
|
}
|
2014-05-13 09:28:15 +00:00
|
|
|
|
|
|
|
//Unlock the queue
|
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
job->debug_worker_id = threadqueue_worker_spec->worker_id;
|
2014-05-16 08:15:05 +00:00
|
|
|
GET_TIME(&job->debug_clock_start);
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
job->fptr(job->arg);
|
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
job->debug_worker_id = threadqueue_worker_spec->worker_id;
|
2014-05-16 08:15:05 +00:00
|
|
|
GET_TIME(&job->debug_clock_stop);
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
//Re-lock the job to update its status and treat its dependencies
|
|
|
|
PTHREAD_LOCK(&job->lock);
|
|
|
|
assert(job->state == THREADQUEUE_JOB_STATE_RUNNING);
|
|
|
|
|
|
|
|
job->state = THREADQUEUE_JOB_STATE_DONE;
|
|
|
|
|
2014-06-03 08:26:15 +00:00
|
|
|
next_job = NULL;
|
2014-06-03 07:45:17 +00:00
|
|
|
|
2014-06-12 11:53:50 +00:00
|
|
|
queue_waiting_dependency_decr = 0;
|
|
|
|
queue_waiting_execution_incr = 0;
|
2014-05-13 09:28:15 +00:00
|
|
|
//Decrease counter of dependencies
|
|
|
|
for (i = 0; i < job->rdepends_count; ++i) {
|
2015-03-04 14:28:56 +00:00
|
|
|
threadqueue_job_t * const depjob = job->rdepends[i];
|
2014-05-13 09:28:15 +00:00
|
|
|
//Note that we lock the dependency AFTER locking the source. This avoids a deadlock in dep_add
|
|
|
|
PTHREAD_LOCK(&depjob->lock);
|
|
|
|
|
|
|
|
assert(depjob->state == THREADQUEUE_JOB_STATE_QUEUED);
|
|
|
|
assert(depjob->ndepends > 0);
|
|
|
|
--depjob->ndepends;
|
|
|
|
|
2014-06-03 07:45:17 +00:00
|
|
|
if (depjob->ndepends == 0) {
|
2014-06-03 08:26:15 +00:00
|
|
|
if (!next_job) {
|
|
|
|
next_job = depjob;
|
|
|
|
depjob->state = THREADQUEUE_JOB_STATE_RUNNING;
|
|
|
|
} else {
|
2014-06-12 11:53:50 +00:00
|
|
|
++queue_waiting_execution_incr;
|
2014-06-03 08:26:15 +00:00
|
|
|
}
|
2014-06-12 11:53:50 +00:00
|
|
|
++queue_waiting_dependency_decr;
|
2014-06-03 07:45:17 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
PTHREAD_UNLOCK(&depjob->lock);
|
|
|
|
}
|
|
|
|
//Unlock the job
|
|
|
|
PTHREAD_UNLOCK(&job->lock);
|
|
|
|
|
|
|
|
//Signal the queue that we've done a job
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
2014-06-13 06:10:10 +00:00
|
|
|
if (!next_job) threadqueue->queue_running--;
|
2014-06-12 11:53:50 +00:00
|
|
|
assert(threadqueue->queue_waiting_dependency >= queue_waiting_dependency_decr);
|
|
|
|
threadqueue->queue_waiting_dependency -= queue_waiting_dependency_decr;
|
|
|
|
threadqueue->queue_waiting_execution += queue_waiting_execution_incr;
|
2014-06-13 06:30:20 +00:00
|
|
|
for (i = 0; i < queue_waiting_execution_incr; ++i) {
|
|
|
|
PTHREAD_COND_SIGNAL(&threadqueue->cond);
|
|
|
|
}
|
|
|
|
//We only signal cb_cond since we finished a job
|
2014-06-03 07:45:17 +00:00
|
|
|
pthread_cond_signal(&threadqueue->cb_cond);
|
2014-05-13 09:28:15 +00:00
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
} else {
|
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//We got out of the loop because threadqueue->stop == 1. The queue is locked.
|
|
|
|
assert(threadqueue->stop);
|
|
|
|
--threadqueue->threads_running;
|
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-16 08:15:05 +00:00
|
|
|
GET_TIME(&threadqueue->debug_clock_thread_end[threadqueue_worker_spec->worker_id]);
|
2014-05-15 08:14:07 +00:00
|
|
|
|
2014-05-16 08:15:05 +00:00
|
|
|
fprintf(threadqueue->debug_log, "\t%d\t-\t%lf\t+%lf\t-\tthread\n", threadqueue_worker_spec->worker_id, CLOCK_T_AS_DOUBLE(threadqueue->debug_clock_thread_start[threadqueue_worker_spec->worker_id]), CLOCK_T_DIFF(threadqueue->debug_clock_thread_start[threadqueue_worker_spec->worker_id], threadqueue->debug_clock_thread_end[threadqueue_worker_spec->worker_id]));
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
|
2014-05-16 10:20:26 +00:00
|
|
|
free(threadqueue_worker_spec_opaque);
|
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
pthread_exit(NULL);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-08-26 08:50:27 +00:00
|
|
|
int kvz_threadqueue_init(threadqueue_queue_t * const threadqueue, int thread_count, int fifo) {
|
2014-05-13 09:28:15 +00:00
|
|
|
int i;
|
|
|
|
if (pthread_mutex_init(&threadqueue->lock, NULL) != 0) {
|
|
|
|
fprintf(stderr, "pthread_mutex_init failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (pthread_cond_init(&threadqueue->cond, NULL) != 0) {
|
|
|
|
fprintf(stderr, "pthread_cond_init failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_cond_init(&threadqueue->cb_cond, NULL) != 0) {
|
|
|
|
fprintf(stderr, "pthread_cond_init failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
threadqueue->stop = 0;
|
2014-06-16 05:24:31 +00:00
|
|
|
threadqueue->fifo = !!fifo;
|
2014-05-13 09:28:15 +00:00
|
|
|
threadqueue->threads_running = 0;
|
|
|
|
threadqueue->threads_count = thread_count;
|
|
|
|
|
|
|
|
threadqueue->threads = MALLOC(pthread_t, thread_count);
|
|
|
|
if (!threadqueue->threads) {
|
|
|
|
fprintf(stderr, "Could not malloc threadqueue->threads!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-16 08:15:05 +00:00
|
|
|
threadqueue->debug_clock_thread_start = MALLOC(CLOCK_T, thread_count);
|
2014-05-15 08:14:07 +00:00
|
|
|
assert(threadqueue->debug_clock_thread_start);
|
2014-05-16 08:15:05 +00:00
|
|
|
threadqueue->debug_clock_thread_end = MALLOC(CLOCK_T, thread_count);
|
2014-05-15 08:14:07 +00:00
|
|
|
assert(threadqueue->debug_clock_thread_end);
|
|
|
|
threadqueue->debug_log = fopen("threadqueue.log", "w");
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|
2014-05-13 09:28:15 +00:00
|
|
|
|
|
|
|
threadqueue->queue = NULL;
|
|
|
|
threadqueue->queue_size = 0;
|
|
|
|
threadqueue->queue_count = 0;
|
2014-05-21 08:57:07 +00:00
|
|
|
threadqueue->queue_start = 0;
|
2014-06-03 08:26:15 +00:00
|
|
|
threadqueue->queue_waiting_execution = 0;
|
|
|
|
threadqueue->queue_waiting_dependency = 0;
|
2014-06-13 06:10:10 +00:00
|
|
|
threadqueue->queue_running = 0;
|
2014-05-13 09:28:15 +00:00
|
|
|
|
|
|
|
//Lock the queue before creating threads, to ensure they all have correct information
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
for(i = 0; i < thread_count; i++) {
|
2014-05-15 08:14:07 +00:00
|
|
|
threadqueue_worker_spec *tqws = MALLOC(threadqueue_worker_spec, 1);
|
|
|
|
if (tqws) {
|
|
|
|
tqws->threadqueue = threadqueue;
|
|
|
|
tqws->worker_id = i;
|
|
|
|
if(pthread_create(&(threadqueue->threads[i]), NULL, threadqueue_worker, (void*)tqws) != 0) {
|
2014-05-13 09:28:15 +00:00
|
|
|
fprintf(stderr, "pthread_create failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
threadqueue->threads_running++;
|
2014-05-15 08:14:07 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Could not allocate threadqueue_worker_spec structure!\n");
|
2015-05-04 14:16:53 +00:00
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
2014-05-15 08:14:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-13 09:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-14 15:17:57 +00:00
|
|
|
/**
|
|
|
|
* \brief Free a single job from the threadqueue index i, destroying it.
|
|
|
|
*/
|
2015-03-04 14:30:20 +00:00
|
|
|
static void threadqueue_free_job(threadqueue_queue_t * const threadqueue, int i)
|
2014-07-14 15:17:57 +00:00
|
|
|
{
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
|
|
|
#if KVZ_DEBUG & KVZ_PERF_JOB
|
2014-07-14 15:17:57 +00:00
|
|
|
int j;
|
|
|
|
GET_TIME(&threadqueue->queue[i]->debug_clock_dequeue);
|
|
|
|
fprintf(threadqueue->debug_log, "%p\t%d\t%lf\t+%lf\t+%lf\t+%lf\t%s\n", threadqueue->queue[i], threadqueue->queue[i]->debug_worker_id, CLOCK_T_AS_DOUBLE(threadqueue->queue[i]->debug_clock_enqueue), CLOCK_T_DIFF(threadqueue->queue[i]->debug_clock_enqueue, threadqueue->queue[i]->debug_clock_start), CLOCK_T_DIFF(threadqueue->queue[i]->debug_clock_start, threadqueue->queue[i]->debug_clock_stop), CLOCK_T_DIFF(threadqueue->queue[i]->debug_clock_stop, threadqueue->queue[i]->debug_clock_dequeue), threadqueue->queue[i]->debug_description);
|
|
|
|
|
|
|
|
for (j = 0; j < threadqueue->queue[i]->rdepends_count; ++j) {
|
|
|
|
fprintf(threadqueue->debug_log, "%p->%p\n", threadqueue->queue[i], threadqueue->queue[i]->rdepends[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE_POINTER(threadqueue->queue[i]->debug_description);
|
2014-08-11 09:35:36 +00:00
|
|
|
#endif
|
2014-07-14 15:17:57 +00:00
|
|
|
#endif
|
|
|
|
FREE_POINTER(threadqueue->queue[i]->rdepends);
|
|
|
|
|
|
|
|
pthread_mutex_destroy(&threadqueue->queue[i]->lock);
|
|
|
|
|
|
|
|
FREE_POINTER(threadqueue->queue[i]);
|
|
|
|
}
|
|
|
|
|
2015-03-04 14:30:20 +00:00
|
|
|
static void threadqueue_free_jobs(threadqueue_queue_t * const threadqueue) {
|
2014-05-15 08:14:07 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i < threadqueue->queue_count; ++i) {
|
2014-07-14 15:17:57 +00:00
|
|
|
threadqueue_free_job(threadqueue, i);
|
2014-05-15 08:14:07 +00:00
|
|
|
}
|
|
|
|
threadqueue->queue_count = 0;
|
2014-05-21 08:57:07 +00:00
|
|
|
threadqueue->queue_start = 0;
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
|
|
|
#if KVZ_DEBUG & KVZ_PERF_JOB
|
2014-05-15 08:14:07 +00:00
|
|
|
{
|
2014-05-16 08:15:05 +00:00
|
|
|
CLOCK_T time;
|
|
|
|
GET_TIME(&time);
|
2014-05-15 08:14:07 +00:00
|
|
|
|
2014-05-16 08:15:05 +00:00
|
|
|
fprintf(threadqueue->debug_log, "\t\t-\t-\t%lf\t-\tFLUSH\n", CLOCK_T_AS_DOUBLE(time));
|
2014-05-15 08:14:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
2014-08-11 09:35:36 +00:00
|
|
|
#endif
|
2014-05-15 08:14:07 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
int kvz_threadqueue_finalize(threadqueue_queue_t * const threadqueue) {
|
2014-05-13 09:28:15 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
//Flush the queue
|
2015-08-26 08:50:27 +00:00
|
|
|
if (!kvz_threadqueue_flush(threadqueue)) {
|
2014-05-13 09:28:15 +00:00
|
|
|
fprintf(stderr, "Unable to flush threadqueue!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Lock threadqueue
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
//Free job memory
|
2014-05-15 08:14:07 +00:00
|
|
|
threadqueue_free_jobs(threadqueue);
|
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
if (threadqueue->stop) {
|
|
|
|
fprintf(stderr, "threadqueue already stopping\n");
|
|
|
|
|
|
|
|
if (pthread_mutex_unlock(&threadqueue->lock) != 0) {
|
|
|
|
fprintf(stderr, "pthread_mutex_unlock failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert(0); //We should get here...
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
threadqueue->stop = 1;
|
|
|
|
|
|
|
|
if (pthread_cond_broadcast(&(threadqueue->cond)) != 0) {
|
|
|
|
fprintf(stderr, "pthread_cond_broadcast failed!\n");
|
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//Unlock it now, since all jobs have to stpo
|
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
//Join threads
|
|
|
|
for(i = 0; i < threadqueue->threads_count; i++) {
|
|
|
|
if(pthread_join(threadqueue->threads[i], NULL) != 0) {
|
|
|
|
fprintf(stderr, "pthread_join failed!\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-16 10:20:26 +00:00
|
|
|
FREE_POINTER(threadqueue->debug_clock_thread_start);
|
|
|
|
FREE_POINTER(threadqueue->debug_clock_thread_end);
|
2014-05-15 08:14:07 +00:00
|
|
|
fclose(threadqueue->debug_log);
|
|
|
|
#endif
|
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
//Free allocated stuff
|
|
|
|
FREE_POINTER(threadqueue->queue);
|
|
|
|
threadqueue->queue_count = 0;
|
|
|
|
threadqueue->queue_size = 0;
|
2014-05-21 08:57:07 +00:00
|
|
|
threadqueue->queue_start = 0;
|
2014-05-13 09:28:15 +00:00
|
|
|
|
|
|
|
FREE_POINTER(threadqueue->threads);
|
|
|
|
threadqueue->threads_count = 0;
|
|
|
|
|
|
|
|
if (pthread_mutex_destroy(&threadqueue->lock) != 0) {
|
|
|
|
fprintf(stderr, "pthread_mutex_destroy failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (pthread_cond_destroy(&threadqueue->cond) != 0) {
|
|
|
|
fprintf(stderr, "pthread_cond_destroy failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_cond_destroy(&threadqueue->cb_cond) != 0) {
|
|
|
|
fprintf(stderr, "pthread_cond_destroy failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
int kvz_threadqueue_flush(threadqueue_queue_t * const threadqueue) {
|
2014-05-13 09:28:15 +00:00
|
|
|
int notdone = 1;
|
|
|
|
|
|
|
|
//Lock the queue
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
do {
|
2014-06-13 06:10:10 +00:00
|
|
|
notdone = threadqueue->queue_waiting_execution + threadqueue->queue_waiting_dependency + threadqueue->queue_running;
|
2014-05-13 09:28:15 +00:00
|
|
|
|
|
|
|
if (notdone > 0) {
|
2014-06-12 10:57:14 +00:00
|
|
|
int ret;
|
2014-06-05 07:09:25 +00:00
|
|
|
PTHREAD_COND_BROADCAST(&(threadqueue->cond));
|
2014-06-13 06:10:10 +00:00
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
2014-05-13 09:28:15 +00:00
|
|
|
SLEEP();
|
2014-06-13 06:10:10 +00:00
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
2015-08-26 08:50:27 +00:00
|
|
|
ret = pthread_cond_timedwait(&threadqueue->cb_cond, &threadqueue->lock, &kvz_time_to_wait);
|
2014-06-12 10:57:14 +00:00
|
|
|
if (ret != 0 && ret != ETIMEDOUT) {
|
|
|
|
fprintf(stderr, "pthread_cond_timedwait failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-13 09:28:15 +00:00
|
|
|
}
|
|
|
|
} while (notdone > 0);
|
|
|
|
|
2014-05-15 08:14:07 +00:00
|
|
|
threadqueue_free_jobs(threadqueue);
|
|
|
|
|
2014-06-13 06:10:10 +00:00
|
|
|
assert(threadqueue->queue_waiting_dependency == 0 && threadqueue->queue_waiting_execution == 0 && threadqueue->queue_running == 0);
|
2014-05-13 09:28:15 +00:00
|
|
|
|
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
int kvz_threadqueue_waitfor(threadqueue_queue_t * const threadqueue, threadqueue_job_t * const job) {
|
2014-06-05 07:09:25 +00:00
|
|
|
int job_done = 0;
|
|
|
|
|
|
|
|
//NULL job is clearly OK :-)
|
|
|
|
if (!job) return 1;
|
|
|
|
|
|
|
|
//Lock the queue
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
|
|
|
do {
|
|
|
|
|
|
|
|
PTHREAD_LOCK(&job->lock);
|
|
|
|
job_done = (job->state == THREADQUEUE_JOB_STATE_DONE);
|
|
|
|
PTHREAD_UNLOCK(&job->lock);
|
|
|
|
|
|
|
|
if (!job_done) {
|
2014-06-16 08:41:50 +00:00
|
|
|
int ret;
|
2014-06-05 07:09:25 +00:00
|
|
|
PTHREAD_COND_BROADCAST(&(threadqueue->cond));
|
2014-06-16 08:41:50 +00:00
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
2014-06-05 07:09:25 +00:00
|
|
|
SLEEP();
|
2014-06-16 08:41:50 +00:00
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
2015-08-26 08:50:27 +00:00
|
|
|
ret = pthread_cond_timedwait(&threadqueue->cb_cond, &threadqueue->lock, &kvz_time_to_wait);
|
2014-06-16 08:41:50 +00:00
|
|
|
if (ret != 0 && ret != ETIMEDOUT) {
|
|
|
|
fprintf(stderr, "pthread_cond_timedwait failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-05 07:09:25 +00:00
|
|
|
}
|
|
|
|
} while (!job_done);
|
|
|
|
|
2014-07-14 15:17:57 +00:00
|
|
|
// 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));
|
2015-02-13 09:56:55 +00:00
|
|
|
FILL_ARRAY(&threadqueue->queue[threadqueue->queue_count], 0, i);
|
2014-07-14 15:17:57 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 07:09:25 +00:00
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
threadqueue_job_t * kvz_threadqueue_submit(threadqueue_queue_t * const threadqueue, void (*fptr)(void *arg), void *arg, int wait, const char* const debug_description) {
|
2015-03-04 14:28:56 +00:00
|
|
|
threadqueue_job_t *job;
|
2014-05-13 09:28:15 +00:00
|
|
|
//No lock here... this should be constant
|
|
|
|
if (threadqueue->threads_count == 0) {
|
2014-05-16 06:40:35 +00:00
|
|
|
//FIXME: This should be improved in order to handle dependencies
|
2015-09-14 09:34:41 +00:00
|
|
|
PERFORMANCE_MEASURE_START(KVZ_PERF_JOB);
|
2014-05-13 09:28:15 +00:00
|
|
|
fptr(arg);
|
2015-09-14 09:34:41 +00:00
|
|
|
PERFORMANCE_MEASURE_END(KVZ_PERF_JOB, threadqueue, "%s", debug_description);
|
2014-05-13 09:28:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-14 09:49:50 +00:00
|
|
|
assert(wait == 0 || wait == 1);
|
|
|
|
|
2015-03-04 14:28:56 +00:00
|
|
|
job = MALLOC(threadqueue_job_t, 1);
|
2014-05-13 09:28:15 +00:00
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
if (debug_description) {
|
2015-09-10 11:02:44 +00:00
|
|
|
size_t desc_len = MIN(255, strlen(debug_description));
|
2014-05-15 08:14:07 +00:00
|
|
|
char* desc;
|
|
|
|
|
|
|
|
//Copy description
|
|
|
|
desc = MALLOC(char, desc_len + 1);
|
|
|
|
assert(desc);
|
|
|
|
memcpy(desc, debug_description, desc_len);
|
|
|
|
desc[desc_len] = 0;
|
|
|
|
|
|
|
|
job->debug_description = desc;
|
|
|
|
} else {
|
|
|
|
char* desc;
|
|
|
|
desc = MALLOC(char, 255);
|
|
|
|
sprintf(desc, "(*%p)(%p)", fptr, arg);
|
|
|
|
|
|
|
|
job->debug_description = desc;
|
|
|
|
}
|
2014-05-16 08:15:05 +00:00
|
|
|
GET_TIME(&job->debug_clock_enqueue);
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|
2014-05-15 08:14:07 +00:00
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
if (!job) {
|
|
|
|
fprintf(stderr, "Could not alloc job!\n");
|
|
|
|
assert(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
job->fptr = fptr;
|
|
|
|
job->arg = arg;
|
|
|
|
if (pthread_mutex_init(&job->lock, NULL) != 0) {
|
|
|
|
fprintf(stderr, "pthread_mutex_init(job) failed!\n");
|
|
|
|
assert(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-14 09:49:50 +00:00
|
|
|
job->ndepends = wait;
|
2014-05-13 09:28:15 +00:00
|
|
|
job->rdepends = NULL;
|
|
|
|
job->rdepends_count = 0;
|
|
|
|
job->rdepends_size = 0;
|
|
|
|
job->state = THREADQUEUE_JOB_STATE_QUEUED;
|
|
|
|
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
//Add the reverse dependency
|
|
|
|
if (threadqueue->queue_count >= threadqueue->queue_size) {
|
2015-03-04 14:28:56 +00:00
|
|
|
threadqueue->queue = realloc(threadqueue->queue, sizeof(threadqueue_job_t *) * (threadqueue->queue_size + THREADQUEUE_LIST_REALLOC_SIZE));
|
2014-05-13 09:28:15 +00:00
|
|
|
if (!threadqueue->queue) {
|
|
|
|
fprintf(stderr, "Could not realloc queue!\n");
|
|
|
|
assert(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
threadqueue->queue_size += THREADQUEUE_LIST_REALLOC_SIZE;
|
|
|
|
}
|
|
|
|
threadqueue->queue[threadqueue->queue_count++] = job;
|
|
|
|
|
2014-06-03 07:45:17 +00:00
|
|
|
if (job->ndepends == 0) {
|
2014-06-03 08:26:15 +00:00
|
|
|
++threadqueue->queue_waiting_execution;
|
2014-06-03 07:45:17 +00:00
|
|
|
//Hope a thread can do it...
|
2014-06-03 08:26:15 +00:00
|
|
|
PTHREAD_COND_SIGNAL(&(threadqueue->cond));
|
|
|
|
} else {
|
|
|
|
++threadqueue->queue_waiting_dependency;
|
2014-05-13 09:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
int kvz_threadqueue_job_dep_add(threadqueue_job_t *job, threadqueue_job_t *depends_on) {
|
2014-05-16 06:40:35 +00:00
|
|
|
//If we are not using threads, job are NULL pointers, so we can skip that
|
|
|
|
if (!job && !depends_on) return 1;
|
2014-06-05 07:09:25 +00:00
|
|
|
|
|
|
|
assert(job && depends_on);
|
|
|
|
|
2014-05-13 09:28:15 +00:00
|
|
|
//Lock first the job, and then the dependency
|
|
|
|
PTHREAD_LOCK(&job->lock);
|
|
|
|
PTHREAD_LOCK(&depends_on->lock);
|
|
|
|
|
|
|
|
if (depends_on->state != THREADQUEUE_JOB_STATE_DONE) {
|
|
|
|
job->ndepends++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add the reverse dependency (FIXME: this may be moved in the if above... but we would lose ability to track)
|
|
|
|
if (depends_on->rdepends_count >= depends_on->rdepends_size) {
|
2015-03-04 14:28:56 +00:00
|
|
|
depends_on->rdepends = realloc(depends_on->rdepends, sizeof(threadqueue_job_t *) * (depends_on->rdepends_size + THREADQUEUE_LIST_REALLOC_SIZE));
|
2014-05-13 09:28:15 +00:00
|
|
|
if (!depends_on->rdepends) {
|
|
|
|
fprintf(stderr, "Could not realloc rdepends!\n");
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
depends_on->rdepends_size += THREADQUEUE_LIST_REALLOC_SIZE;
|
|
|
|
}
|
|
|
|
depends_on->rdepends[depends_on->rdepends_count++] = job;
|
|
|
|
|
|
|
|
PTHREAD_UNLOCK(&depends_on->lock);
|
|
|
|
PTHREAD_UNLOCK(&job->lock);
|
|
|
|
|
|
|
|
return 1;
|
2014-05-14 09:49:50 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
int kvz_threadqueue_job_unwait_job(threadqueue_queue_t * const threadqueue, threadqueue_job_t *job) {
|
2014-06-03 07:45:17 +00:00
|
|
|
int ndepends = 0;
|
|
|
|
|
2014-05-16 06:40:35 +00:00
|
|
|
//NULL job => no threads, nothing to do
|
|
|
|
if (!job) return 1;
|
2014-05-14 09:49:50 +00:00
|
|
|
PTHREAD_LOCK(&job->lock);
|
|
|
|
job->ndepends--;
|
2014-06-03 07:45:17 +00:00
|
|
|
ndepends = job->ndepends;
|
2014-05-14 09:49:50 +00:00
|
|
|
PTHREAD_UNLOCK(&job->lock);
|
|
|
|
|
2014-06-03 07:45:17 +00:00
|
|
|
if (ndepends == 0) {
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
2014-06-03 08:26:15 +00:00
|
|
|
assert(threadqueue->queue_waiting_dependency > 0);
|
|
|
|
--threadqueue->queue_waiting_dependency;
|
|
|
|
++threadqueue->queue_waiting_execution;
|
2014-06-03 07:45:17 +00:00
|
|
|
//Hope a thread can do it...
|
2014-06-03 08:26:15 +00:00
|
|
|
PTHREAD_COND_SIGNAL(&(threadqueue->cond));
|
|
|
|
|
2014-06-03 07:45:17 +00:00
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
2014-05-14 09:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2014-05-15 13:50:34 +00:00
|
|
|
|
2015-09-14 09:43:28 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
2015-03-04 14:30:20 +00:00
|
|
|
int threadqueue_log(threadqueue_queue_t * threadqueue, const CLOCK_T *start, const CLOCK_T *stop, const char* debug_description) {
|
2014-05-15 13:50:34 +00:00
|
|
|
int i, thread_id = -1;
|
2014-06-13 05:15:26 +00:00
|
|
|
FILE* output;
|
2014-05-15 13:50:34 +00:00
|
|
|
|
|
|
|
assert(start);
|
|
|
|
|
2014-06-13 05:15:26 +00:00
|
|
|
if (threadqueue) {
|
|
|
|
//We need to lock to output safely
|
|
|
|
PTHREAD_LOCK(&threadqueue->lock);
|
|
|
|
|
|
|
|
output = threadqueue->debug_log;
|
|
|
|
|
|
|
|
//Find the thread
|
|
|
|
for(i = 0; i < threadqueue->threads_count; i++) {
|
|
|
|
if(pthread_equal(threadqueue->threads[i], pthread_self()) != 0) {
|
|
|
|
thread_id = i;
|
|
|
|
break;
|
|
|
|
}
|
2014-05-15 13:50:34 +00:00
|
|
|
}
|
2014-06-13 05:15:26 +00:00
|
|
|
} else {
|
|
|
|
thread_id = -1;
|
|
|
|
output = stderr;
|
2014-05-15 13:50:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (thread_id >= 0) {
|
|
|
|
if (stop) {
|
2014-06-13 05:15:26 +00:00
|
|
|
fprintf(output, "\t%d\t-\t%lf\t+%lf\t-\t%s\n", thread_id, CLOCK_T_AS_DOUBLE(*start), CLOCK_T_DIFF(*start, *stop), debug_description);
|
2014-05-15 13:50:34 +00:00
|
|
|
} else {
|
2014-06-13 05:15:26 +00:00
|
|
|
fprintf(output, "\t%d\t-\t%lf\t-\t-\t%s\n", thread_id, CLOCK_T_AS_DOUBLE(*start), debug_description);
|
2014-05-15 13:50:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (stop) {
|
2014-06-13 05:15:26 +00:00
|
|
|
fprintf(output, "\t\t-\t%lf\t+%lf\t-\t%s\n", CLOCK_T_AS_DOUBLE(*start), CLOCK_T_DIFF(*start, *stop), debug_description);
|
2014-05-15 13:50:34 +00:00
|
|
|
} else {
|
2014-06-13 05:15:26 +00:00
|
|
|
fprintf(output, "\t\t-\t%lf\t-\t-\t%s\n", CLOCK_T_AS_DOUBLE(*start), debug_description);
|
2014-05-15 13:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-13 05:15:26 +00:00
|
|
|
|
|
|
|
if (threadqueue) {
|
|
|
|
PTHREAD_UNLOCK(&threadqueue->lock);
|
|
|
|
}
|
2014-05-15 13:50:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-09-14 09:43:28 +00:00
|
|
|
#endif //KVZ_DEBUG
|