1 /* thread_pool_internal.h -- Internal API for the thread pool.
3 Copyright (c) 2013-2016 Genome Research Ltd.
5 Author: James Bonfield <jkb@sanger.ac.uk>
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 DEALINGS IN THE SOFTWARE. */
26 * This file implements a thread pool for multi-threading applications.
27 * It consists of two distinct interfaces: thread pools an thread job queues.
29 * The pool of threads is given a function pointer and void* data to pass in.
30 * This means the pool can run jobs of multiple types, albeit first come
31 * first served with no job scheduling except to pick tasks from
32 * queues that have room to store the result.
34 * Upon completion, the return value from the function pointer is
35 * added to back to the queue if the result is required. We may have
36 * multiple queues in use for the one pool.
38 * To see example usage, please look at the #ifdef TEST_MAIN code in
42 #ifndef THREAD_POOL_INTERNAL_H
43 #define THREAD_POOL_INTERNAL_H
47 #include "htslib/thread_pool.h"
54 * An input job, before execution.
56 typedef struct hts_tpool_job
{
57 void *(*func
)(void *arg
);
59 struct hts_tpool_job
*next
;
62 struct hts_tpool_process
*q
;
67 * An output, after job has executed.
69 struct hts_tpool_result
{
70 struct hts_tpool_result
*next
;
71 uint64_t serial
; // sequential number for ordering
72 void *data
; // result itself
76 * A per-thread worker struct.
82 pthread_cond_t pending_c
; // when waiting for a job
86 * An IO queue consists of a queue of jobs to execute
87 * (the "input" side) and a queue of job results post-
88 * execution (the "output" side).
90 * We have size limits to prevent either queue from
91 * growing too large and serial numbers to ensure
92 * sequential consumption of the output.
94 * The thread pool may have many hetergeneous tasks, each
95 * using its own io_queue mixed into the same thread pool.
97 struct hts_tpool_process
{
98 struct hts_tpool
*p
; // thread pool
99 hts_tpool_job
*input_head
; // input list
100 hts_tpool_job
*input_tail
;
101 hts_tpool_result
*output_head
; // output list
102 hts_tpool_result
*output_tail
;
103 int qsize
; // max size of i/o queues
104 uint64_t next_serial
; // next serial for output
105 uint64_t curr_serial
; // current serial (next input)
107 int n_input
; // no. items in input queue; was njobs
108 int n_output
; // no. items in output queue
109 int n_processing
; // no. items being processed (executing)
111 int shutdown
; // true if pool is being destroyed
112 int in_only
; // if true, don't queue result up.
113 int wake_dispatch
; // unblocks waiting dispatchers
115 int ref_count
; // used to track safe destruction
117 pthread_cond_t output_avail_c
; // Signalled on each new output
118 pthread_cond_t input_not_full_c
; // Input queue is no longer full
119 pthread_cond_t input_empty_c
; // Input queue has become empty
120 pthread_cond_t none_processing_c
;// n_processing has hit zero
122 struct hts_tpool_process
*next
, *prev
;// to form circular linked list.
126 * The single pool structure itself.
128 * This knows nothing about the nature of the jobs or where their
129 * output is going, but it maintains a list of queues associated with
130 * this pool from which the jobs are taken.
133 int nwaiting
; // how many workers waiting for new jobs
134 int njobs
; // how many total jobs are waiting in all queues
135 int shutdown
; // true if pool is being destroyed
137 // I/O queues to check for jobs in and to put results.
138 // Forms a circular linked list. (q_head may be amended
139 // to point to the most recently updated.)
140 hts_tpool_process
*q_head
;
143 int tsize
; // maximum number of jobs
145 // array of worker IDs free
146 int *t_stack
, t_stack_top
;
148 // A single mutex used when updating this and any associated structure.
149 pthread_mutex_t pool_m
;
151 // Tracking of average number of running jobs.
152 // This can be used to dampen any hysteresis caused by bursty
153 // input availability.
154 int n_count
, n_running
;
156 // Debugging to check wait time.
157 // FIXME: should we just delete these and cull the associated code?
158 long long total_time
, wait_time
;