TUT HEVC Encoder
Data Structures | Macros | Enumerations | Functions
threadqueue.c File Reference

Lock acquisition order: More...

#include "global.h"
#include "threadqueue.h"
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "threads.h"
Include dependency graph for threadqueue.c:

Data Structures

struct  threadqueue_job_t
 
struct  threadqueue_queue_t
 

Macros

#define THREADQUEUE_LIST_REALLOC_SIZE   32
 
#define PTHREAD_COND_SIGNAL(c)
 
#define PTHREAD_COND_BROADCAST(c)
 
#define PTHREAD_COND_WAIT(c, l)
 
#define PTHREAD_LOCK(l)
 
#define PTHREAD_UNLOCK(l)
 

Enumerations

enum  threadqueue_job_state {
  THREADQUEUE_JOB_STATE_PAUSED , THREADQUEUE_JOB_STATE_WAITING , THREADQUEUE_JOB_STATE_READY , THREADQUEUE_JOB_STATE_RUNNING ,
  THREADQUEUE_JOB_STATE_DONE
}
 

Functions

static void threadqueue_push_job (threadqueue_queue_t *threadqueue, threadqueue_job_t *job)
 Add a job to the queue of jobs ready to run. More...
 
static threadqueue_job_tthreadqueue_pop_job (threadqueue_queue_t *threadqueue)
 Retrieve a job from the queue of jobs ready to run. More...
 
static void * threadqueue_worker (void *threadqueue_opaque)
 Function executed by worker threads. More...
 
threadqueue_queue_tkvz_threadqueue_init (int thread_count)
 Initialize the queue. More...
 
threadqueue_job_tkvz_threadqueue_job_create (void(*fptr)(void *arg), void *arg)
 Create a job and return a pointer to it. More...
 
int kvz_threadqueue_submit (threadqueue_queue_t *const threadqueue, threadqueue_job_t *job)
 
int kvz_threadqueue_job_dep_add (threadqueue_job_t *job, threadqueue_job_t *dependency)
 Add a dependency between two jobs. More...
 
threadqueue_job_tkvz_threadqueue_copy_ref (threadqueue_job_t *job)
 Get a new pointer to a job. More...
 
void kvz_threadqueue_free_job (threadqueue_job_t **job_ptr)
 Free a job. More...
 
int kvz_threadqueue_waitfor (threadqueue_queue_t *threadqueue, threadqueue_job_t *job)
 Wait for a job to be completed. More...
 
int kvz_threadqueue_stop (threadqueue_queue_t *const threadqueue)
 Stop all threads after they finish the current jobs. More...
 
void kvz_threadqueue_free (threadqueue_queue_t *threadqueue)
 Stop all threads and free allocated resources. More...
 

Detailed Description

  1. When locking a job and its dependency, the dependecy must be locked first and then the job depending on it.
  2. When locking a job and the thread queue, the thread queue must be locked first and then the job.
  3. When accessing threadqueue_job_t.next, the thread queue must be locked.

Macro Definition Documentation

◆ PTHREAD_COND_BROADCAST

#define PTHREAD_COND_BROADCAST (   c)
Value:
if (pthread_cond_broadcast((c)) != 0) { \
fprintf(stderr, "pthread_cond_broadcast(%s=%p) failed!\n", #c, c); \
assert(0); \
return 0; \
}

◆ PTHREAD_COND_SIGNAL

#define PTHREAD_COND_SIGNAL (   c)
Value:
if (pthread_cond_signal((c)) != 0) { \
fprintf(stderr, "pthread_cond_signal(%s=%p) failed!\n", #c, c); \
assert(0); \
return 0; \
}

◆ PTHREAD_COND_WAIT

#define PTHREAD_COND_WAIT (   c,
 
)
Value:
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; \
}
l
Definition: plot-threadqueue-log.py:427

◆ PTHREAD_LOCK

#define PTHREAD_LOCK (   l)
Value:
if (pthread_mutex_lock((l)) != 0) { \
fprintf(stderr, "pthread_mutex_lock(%s) failed!\n", #l); \
assert(0); \
return 0; \
}

◆ PTHREAD_UNLOCK

#define PTHREAD_UNLOCK (   l)
Value:
if (pthread_mutex_unlock((l)) != 0) { \
fprintf(stderr, "pthread_mutex_unlock(%s) failed!\n", #l); \
assert(0); \
return 0; \
}

◆ THREADQUEUE_LIST_REALLOC_SIZE

#define THREADQUEUE_LIST_REALLOC_SIZE   32

Enumeration Type Documentation

◆ threadqueue_job_state

Enumerator
THREADQUEUE_JOB_STATE_PAUSED 

Job has been submitted, but is not allowed to run yet.

THREADQUEUE_JOB_STATE_WAITING 

Job is waiting for dependencies.

THREADQUEUE_JOB_STATE_READY 

Job is ready to run.

THREADQUEUE_JOB_STATE_RUNNING 

Job is running.

THREADQUEUE_JOB_STATE_DONE 

Job is completed.

Function Documentation

◆ kvz_threadqueue_copy_ref()

threadqueue_job_t * kvz_threadqueue_copy_ref ( threadqueue_job_t job)

Increment reference count and return the job.

Here is the caller graph for this function:

◆ kvz_threadqueue_free()

void kvz_threadqueue_free ( threadqueue_queue_t threadqueue)
Returns
1 on success, 0 on failure
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvz_threadqueue_free_job()

void kvz_threadqueue_free_job ( threadqueue_job_t **  job_ptr)

Decrement reference count of the job. If no references exist any more, deallocate associated memory and destroy mutexes.

Sets the job pointer to NULL.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvz_threadqueue_init()

threadqueue_queue_t * kvz_threadqueue_init ( int  thread_count)
Returns
1 on success, 0 on failure
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvz_threadqueue_job_create()

threadqueue_job_t * kvz_threadqueue_job_create ( void(*)(void *arg)  fptr,
void *  arg 
)

The job is created in a paused state. Function kvz_threadqueue_submit must be called on the job in order to have it run.

Returns
pointer to the job, or NULL on failure
Here is the caller graph for this function:

◆ kvz_threadqueue_job_dep_add()

int kvz_threadqueue_job_dep_add ( threadqueue_job_t job,
threadqueue_job_t dependency 
)
Parameters
jobjob that should be executed after dependency
dependencyjob that should be executed before job
Returns
1 on success, 0 on failure
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvz_threadqueue_stop()

int kvz_threadqueue_stop ( threadqueue_queue_t *const  threadqueue)

Block until all threads have stopped.

Returns
1 on success, 0 on failure
Here is the caller graph for this function:

◆ kvz_threadqueue_submit()

int kvz_threadqueue_submit ( threadqueue_queue_t *const  threadqueue,
threadqueue_job_t job 
)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvz_threadqueue_waitfor()

int kvz_threadqueue_waitfor ( threadqueue_queue_t threadqueue,
threadqueue_job_t job 
)
Returns
1 on success, 0 on failure
Here is the caller graph for this function:

◆ threadqueue_pop_job()

static threadqueue_job_t * threadqueue_pop_job ( threadqueue_queue_t threadqueue)
static

The caller must have locked the thread queue. The calling function receives the ownership of the job.

Here is the caller graph for this function:

◆ threadqueue_push_job()

static void threadqueue_push_job ( threadqueue_queue_t threadqueue,
threadqueue_job_t job 
)
static

The caller must have locked the thread queue and the job. This function takes the ownership of the job.

Here is the caller graph for this function:

◆ threadqueue_worker()

static void * threadqueue_worker ( void *  threadqueue_opaque)
static
Here is the call graph for this function:
Here is the caller graph for this function: