2014-05-13 09:28:15 +00:00
# ifndef THREADQUEUE_H_
# define THREADQUEUE_H_
/*****************************************************************************
* This file is part of Kvazaar HEVC encoder .
*
2015-02-23 11:18:48 +00:00
* Copyright ( C ) 2013 - 2015 Tampere University of Technology and others ( see
2014-05-13 09:28:15 +00:00
* COPYING file ) .
*
2015-02-23 11:18:48 +00:00
* 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 .
2014-05-13 09:28:15 +00:00
*
2015-02-23 11:18:48 +00:00
* 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 .
2014-05-13 09:28:15 +00:00
*
2015-02-23 11:18:48 +00:00
* 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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2015-12-17 11:42:57 +00:00
/**
* \ ingroup Threading
* \ file
* Container for worker tasks .
*/
2014-05-19 10:56:27 +00:00
# include "global.h"
2014-05-13 09:28:15 +00:00
# include <pthread.h>
2014-05-16 08:15:05 +00:00
# include "threads.h"
2014-05-13 09:28:15 +00:00
typedef enum {
THREADQUEUE_JOB_STATE_QUEUED = 0 ,
THREADQUEUE_JOB_STATE_RUNNING = 1 ,
THREADQUEUE_JOB_STATE_DONE = 2
} threadqueue_job_state ;
2015-03-04 14:28:56 +00:00
typedef struct threadqueue_job_t {
2014-05-13 09:28:15 +00:00
pthread_mutex_t lock ;
threadqueue_job_state state ;
2014-05-16 08:15:05 +00:00
unsigned int ndepends ; //Number of active dependencies that this job wait for
2014-05-13 09:28:15 +00:00
2015-03-04 14:28:56 +00:00
struct threadqueue_job_t * * rdepends ; //array of pointer to jobs that depend on this one. They have to exist when the thread finishes, because they cannot be run before.
2014-05-13 09:28:15 +00:00
unsigned int rdepends_count ; //number of rdepends
unsigned int rdepends_size ; //allocated size of rdepends
//Job function and state to use
void ( * fptr ) ( void * arg ) ;
void * arg ;
2014-05-15 08:14:07 +00:00
2015-09-14 09:43:28 +00:00
# ifdef KVZ_DEBUG
2014-05-15 08:14:07 +00:00
const char * debug_description ;
int debug_worker_id ;
2015-11-13 20:46:32 +00:00
KVZ_CLOCK_T debug_clock_enqueue ;
KVZ_CLOCK_T debug_clock_start ;
KVZ_CLOCK_T debug_clock_stop ;
KVZ_CLOCK_T debug_clock_dequeue ;
2014-05-15 08:14:07 +00:00
# endif
2015-03-04 14:28:56 +00:00
} threadqueue_job_t ;
2014-05-13 09:28:15 +00:00
typedef struct {
pthread_mutex_t lock ;
pthread_cond_t cond ;
pthread_cond_t cb_cond ;
pthread_t * threads ;
int threads_count ;
int threads_running ;
int stop ; //=>1: threads should stop asap
2014-06-16 05:24:31 +00:00
int fifo ;
2015-03-04 14:28:56 +00:00
threadqueue_job_t * * queue ;
2014-05-21 08:57:07 +00:00
unsigned int queue_start ;
2014-05-13 09:28:15 +00:00
unsigned int queue_count ;
unsigned int queue_size ;
2014-06-03 08:26:15 +00:00
unsigned int queue_waiting_execution ; //Number of jobs without any dependency which could be run
unsigned int queue_waiting_dependency ; //Number of jobs waiting for a dependency to complete
2014-06-13 06:10:10 +00:00
unsigned int queue_running ; //Number of jobs running
2014-05-15 08:14:07 +00:00
2015-09-14 09:43:28 +00:00
# ifdef KVZ_DEBUG
2014-05-15 08:14:07 +00:00
//Format: pointer <tab> worker id <tab> time enqueued <tab> time started <tab> time stopped <tab> time dequeued <tab> job description
//For threads, pointer = "" and job description == "thread", time enqueued and time dequeued are equal to "-"
//For flush, pointer = "" and job description == "FLUSH", time enqueued, time dequeued and time started are equal to "-"
//Each time field, except the first one in the line be expressed in a relative way, by prepending the number of seconds by +.
//Dependencies: pointer -> pointer
FILE * debug_log ;
2015-11-13 20:46:32 +00:00
KVZ_CLOCK_T * debug_clock_thread_start ;
KVZ_CLOCK_T * debug_clock_thread_end ;
2014-05-15 08:14:07 +00:00
# endif
2015-03-04 14:30:20 +00:00
} threadqueue_queue_t ;
2014-05-13 09:28:15 +00:00
2014-06-16 05:24:31 +00:00
//Init a threadqueue (if fifo, then behave as a FIFO with dependencies, otherwise as a LIFO with dependencies)
2015-08-26 08:50:27 +00:00
int kvz_threadqueue_init ( threadqueue_queue_t * threadqueue , int thread_count , int fifo ) ;
2014-05-13 09:28:15 +00:00
2015-08-26 08:50:27 +00:00
//Add a job to the queue, and returs a threadqueue_job handle. If wait == 1, one has to run kvz_threadqueue_job_unwait_job in order to have it run
threadqueue_job_t * kvz_threadqueue_submit ( threadqueue_queue_t * threadqueue , void ( * fptr ) ( void * arg ) , void * arg , int wait , const char * debug_description ) ;
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 * threadqueue , threadqueue_job_t * job ) ;
2014-05-13 09:28:15 +00:00
//Add a dependency between two jobs.
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-13 09:28:15 +00:00
//Blocking call until the queue is empty. Previously set threadqueue_job handles should not be used anymore
2015-08-26 08:50:27 +00:00
int kvz_threadqueue_flush ( threadqueue_queue_t * threadqueue ) ;
2014-05-13 09:28:15 +00:00
2014-07-14 15:17:57 +00:00
//Blocking call until job is executed. Job handles submitted before job should not be used any more as they are removed from the queue.
2015-08-26 08:50:27 +00:00
int kvz_threadqueue_waitfor ( threadqueue_queue_t * threadqueue , threadqueue_job_t * job ) ;
2014-06-05 07:09:25 +00:00
2014-05-13 09:28:15 +00:00
//Free ressources in a threadqueue
2015-08-26 08:50:27 +00:00
int kvz_threadqueue_finalize ( threadqueue_queue_t * threadqueue ) ;
2014-05-13 09:28:15 +00:00
2015-09-14 09:43:28 +00:00
# ifdef KVZ_DEBUG
2015-11-13 20:46:32 +00:00
int threadqueue_log ( threadqueue_queue_t * threadqueue , const KVZ_CLOCK_T * start , const KVZ_CLOCK_T * stop , const char * debug_description ) ;
2014-05-15 13:50:34 +00:00
2015-09-14 09:34:41 +00:00
// Bitmasks for PERFORMANCE_MEASURE_START and PERFORMANCE_MEASURE_END.
# define KVZ_PERF_FRAME (1 << 0)
# define KVZ_PERF_JOB (1 << 1)
# define KVZ_PERF_LCU (1 << 2)
# define KVZ_PERF_SAOREC (1 << 3)
# define KVZ_PERF_BSLEAF (1 << 4)
# define KVZ_PERF_SEARCHCU (1 << 5)
# define KVZ_PERF_SEARCHPX (1 << 6)
2016-01-22 09:24:58 +00:00
# define IMPL_PERFORMANCE_MEASURE_START(mask) KVZ_CLOCK_T start, stop; if ((KVZ_DEBUG) & mask) { KVZ_GET_TIME(&start); }
# define IMPL_PERFORMANCE_MEASURE_END(mask, threadqueue, str, ...) { if ((KVZ_DEBUG) & mask) { KVZ_GET_TIME(&stop); {char job_description[256]; sprintf(job_description, (str), __VA_ARGS__); threadqueue_log((threadqueue), &start, &stop, job_description);}} } \
2015-09-10 11:02:44 +00:00
# ifdef _MSC_VER
// Disable VS conditional expression warning from debug code.
# define WITHOUT_CONSTANT_EXP_WARNING(macro) \
__pragma ( warning ( push ) ) \
__pragma ( warning ( disable : 4127 ) ) \
macro \
__pragma ( warning ( pop ) )
# define PERFORMANCE_MEASURE_START(mask) \
WITHOUT_CONSTANT_EXP_WARNING ( IMPL_PERFORMANCE_MEASURE_START ( mask ) )
# define PERFORMANCE_MEASURE_END(mask, threadqueue, str, ...) \
WITHOUT_CONSTANT_EXP_WARNING ( IMPL_PERFORMANCE_MEASURE_END ( mask , threadqueue , str , # # __VA_ARGS__ ) )
# else
# define PERFORMANCE_MEASURE_START(mask) \
IMPL_PERFORMANCE_MEASURE_START ( mask )
# define PERFORMANCE_MEASURE_END(mask, threadqueue, str, ...) \
IMPL_PERFORMANCE_MEASURE_END ( mask , threadqueue , str , # # __VA_ARGS__ )
# endif
2014-08-11 09:35:36 +00:00
2014-05-15 13:50:34 +00:00
# else
2015-08-31 06:25:17 +00:00
# define PERFORMANCE_MEASURE_START(mask)
# define PERFORMANCE_MEASURE_END(mask, threadqueue, str, ...)
2014-05-15 13:50:34 +00:00
# endif
2014-05-13 09:28:15 +00:00
/* Constraints:
*
* - Always first lock threadqueue , than a job inside it
* - When job A depends on job B , always lock first job B and then job A
2014-06-05 07:09:25 +00:00
* - Jobs should be submitted in an order which is compatible with serial execution .
2014-05-13 09:28:15 +00:00
*
* */
# endif //THREADQUEUE_H_