Skip to content

Commit 2578c89

Browse files
jkbonfieldwhitwham
authored andcommitted
Add hts_tpool_worker_id() API
This returns a numeric value from 0 to nthreads-1 corresponding to the current running thread, or -1 if unthreaded or the thread does not correspond to one allocated to this pool. It may be used to associate data to a thread rather than to a job. For example maintaining one open file handle per thread spawned.
1 parent c6c1d19 commit 2578c89

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

htslib/thread_pool.h

+8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ HTSLIB_EXPORT
115115
int hts_tpool_size(hts_tpool *p);
116116

117117

118+
/// Return the worker ID index, from 0 to nthreads-1.
119+
/**
120+
* @param p Thread pool
121+
* @return The worker index (0..ntheads-1) or -1 if not found
122+
*/
123+
HTSLIB_EXPORT
124+
int hts_tpool_worker_id(hts_tpool *pool);
125+
118126
/// Add an item to the work pool.
119127
/**
120128
* @param p Thread pool

thread_pool.c

+13-9
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,21 @@ static void hts_tpool_process_detach_locked(hts_tpool *p,
5151

5252
//#define DEBUG
5353

54-
#ifdef DEBUG
55-
static int worker_id(hts_tpool *p) {
56-
int i;
54+
// Return the worker ID index, from 0 to nthreads-1.
55+
// Return <0 on error, but this shouldn't be possible
56+
int hts_tpool_worker_id(hts_tpool *p) {
57+
if (!p)
58+
return -1;
5759
pthread_t s = pthread_self();
60+
int i;
5861
for (i = 0; i < p->tsize; i++) {
5962
if (pthread_equal(s, p->t[i].tid))
6063
return i;
6164
}
6265
return -1;
6366
}
6467

68+
#ifdef DEBUG
6569
void DBG_OUT(FILE *fp, char *fmt, ...) {
6670
va_list args;
6771
va_start(args, fmt);
@@ -95,7 +99,7 @@ static int hts_tpool_add_result(hts_tpool_job *j, void *data) {
9599
pthread_mutex_lock(&q->p->pool_m);
96100

97101
DBG_OUT(stderr, "%d: Adding result to queue %p, serial %"PRId64", %d of %d\n",
98-
worker_id(j->p), q, j->serial, q->n_output+1, q->qsize);
102+
hts_tpool_worker_id(j->p), q, j->serial, q->n_output+1, q->qsize);
99103

100104
if (--q->n_processing == 0)
101105
pthread_cond_signal(&q->none_processing_c);
@@ -129,9 +133,9 @@ static int hts_tpool_add_result(hts_tpool_job *j, void *data) {
129133
|| q->next_serial == INT_MAX); // ... unless flush in progress.
130134
if (r->serial == q->next_serial) {
131135
DBG_OUT(stderr, "%d: Broadcasting result_avail (id %"PRId64")\n",
132-
worker_id(j->p), r->serial);
136+
hts_tpool_worker_id(j->p), r->serial);
133137
pthread_cond_broadcast(&q->output_avail_c);
134-
DBG_OUT(stderr, "%d: Broadcast complete\n", worker_id(j->p));
138+
DBG_OUT(stderr, "%d: Broadcast complete\n", hts_tpool_worker_id(j->p));
135139
}
136140

137141
pthread_mutex_unlock(&q->p->pool_m);
@@ -603,7 +607,7 @@ static void *tpool_worker(void *arg) {
603607
pthread_mutex_unlock(&p->pool_m);
604608

605609
DBG_OUT(stderr, "%d: Processing queue %p, serial %"PRId64"\n",
606-
worker_id(j->p), q, j->serial);
610+
hts_tpool_worker_id(j->p), q, j->serial);
607611

608612
if (hts_tpool_add_result(j, j->func(j->arg)) < 0)
609613
goto err;
@@ -625,13 +629,13 @@ static void *tpool_worker(void *arg) {
625629
shutdown:
626630
pthread_mutex_unlock(&p->pool_m);
627631
#ifdef DEBUG
628-
fprintf(stderr, "%d: Shutting down\n", worker_id(p));
632+
fprintf(stderr, "%d: Shutting down\n", hts_tpool_worker_id(p));
629633
#endif
630634
return NULL;
631635

632636
err:
633637
#ifdef DEBUG
634-
fprintf(stderr, "%d: Failed to add result\n", worker_id(p));
638+
fprintf(stderr, "%d: Failed to add result\n", hts_tpool_worker_id(p));
635639
#endif
636640
// Hard failure, so shutdown all queues
637641
pthread_mutex_lock(&p->pool_m);

0 commit comments

Comments
 (0)