@@ -51,17 +51,21 @@ static void hts_tpool_process_detach_locked(hts_tpool *p,
51
51
52
52
//#define DEBUG
53
53
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 ;
57
59
pthread_t s = pthread_self ();
60
+ int i ;
58
61
for (i = 0 ; i < p -> tsize ; i ++ ) {
59
62
if (pthread_equal (s , p -> t [i ].tid ))
60
63
return i ;
61
64
}
62
65
return -1 ;
63
66
}
64
67
68
+ #ifdef DEBUG
65
69
void DBG_OUT (FILE * fp , char * fmt , ...) {
66
70
va_list args ;
67
71
va_start (args , fmt );
@@ -95,7 +99,7 @@ static int hts_tpool_add_result(hts_tpool_job *j, void *data) {
95
99
pthread_mutex_lock (& q -> p -> pool_m );
96
100
97
101
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 );
99
103
100
104
if (-- q -> n_processing == 0 )
101
105
pthread_cond_signal (& q -> none_processing_c );
@@ -129,9 +133,9 @@ static int hts_tpool_add_result(hts_tpool_job *j, void *data) {
129
133
|| q -> next_serial == INT_MAX ); // ... unless flush in progress.
130
134
if (r -> serial == q -> next_serial ) {
131
135
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 );
133
137
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 ));
135
139
}
136
140
137
141
pthread_mutex_unlock (& q -> p -> pool_m );
@@ -603,7 +607,7 @@ static void *tpool_worker(void *arg) {
603
607
pthread_mutex_unlock (& p -> pool_m );
604
608
605
609
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 );
607
611
608
612
if (hts_tpool_add_result (j , j -> func (j -> arg )) < 0 )
609
613
goto err ;
@@ -625,13 +629,13 @@ static void *tpool_worker(void *arg) {
625
629
shutdown :
626
630
pthread_mutex_unlock (& p -> pool_m );
627
631
#ifdef DEBUG
628
- fprintf (stderr , "%d: Shutting down\n" , worker_id (p ));
632
+ fprintf (stderr , "%d: Shutting down\n" , hts_tpool_worker_id (p ));
629
633
#endif
630
634
return NULL ;
631
635
632
636
err :
633
637
#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 ));
635
639
#endif
636
640
// Hard failure, so shutdown all queues
637
641
pthread_mutex_lock (& p -> pool_m );
0 commit comments