src/lib/ctx.h
changeset 19 ebcc49de97d0
equal deleted inserted replaced
18:f92a24ab046e 19:ebcc49de97d0
       
     1 #ifndef PNGTILE_CTX_H
       
     2 #define PNGTILE_CTX_H
       
     3 
       
     4 /**
       
     5  * Shared context between images, used to provide a threadpool for parralelizing tile operations
       
     6  */
       
     7 #include "pngtile.h"
       
     8 
       
     9 #include <pthread.h>
       
    10 #include <sys/queue.h>
       
    11 #include <stdbool.h>
       
    12 
       
    13 /**
       
    14  * Worker thread
       
    15  */
       
    16 struct pt_thread {
       
    17     /** Shared context */
       
    18     struct pt_ctx *ctx;
       
    19 
       
    20     /** Thread handle */
       
    21     pthread_t tid;
       
    22 
       
    23     /** @see pt_ctx::threads */
       
    24     TAILQ_ENTRY(pt_thread) ctx_threads;
       
    25 };
       
    26 
       
    27 /**
       
    28  * Work function
       
    29  */
       
    30 typedef void (*pt_work_func) (void *arg);
       
    31 
       
    32 /**
       
    33  * Work that needs to be done
       
    34  */
       
    35 struct pt_work {
       
    36     /** Work func */
       
    37     pt_work_func func;
       
    38 
       
    39     /** Work info */
       
    40     void *arg;
       
    41     
       
    42     /** @see pt_ctx::work */
       
    43     TAILQ_ENTRY(pt_work) ctx_work;
       
    44 };
       
    45 
       
    46 /**
       
    47  * Shared context
       
    48  */
       
    49 struct pt_ctx {
       
    50     /** Accepting new work */
       
    51     bool running;
       
    52 
       
    53     /** Threadpool */
       
    54     TAILQ_HEAD(pt_ctx_threads, pt_thread) threads;
       
    55 
       
    56     /** Pending work */
       
    57     TAILQ_HEAD(pt_ctx_work, pt_work) work;
       
    58 
       
    59     /** Control access to ::work */
       
    60     pthread_mutex_t work_mutex;
       
    61 
       
    62     /** Wait for work to become available */
       
    63     pthread_cond_t work_cond;
       
    64 
       
    65     /** Thread is idle */
       
    66     pthread_cond_t idle_cond;
       
    67 };
       
    68 
       
    69 /**
       
    70  * Enqueue a work unit
       
    71  */
       
    72 int pt_ctx_work (struct pt_ctx *ctx, pt_work_func func, void *arg);
       
    73 
       
    74 
       
    75 #endif