00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00042 #include <vector>
00043
00044 #include "simplethreads.h"
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #undef DEBUG
00055 #undef DEBUG_HEAVY
00056
00057
00058
00059
00060
00062 struct thread_data_struct
00064 {
00065 int thread_id;
00066 pthread_mutex_t *mutex;
00067 char *job_flag;
00068
00069 int *jobs_done;
00070 int jobs;
00071 void (*do_a_job)(const int, const int, void * const, void * const);
00072 void *individual_job_data;
00073
00074
00075
00076
00077 void *results;
00078
00079
00080
00081
00082
00083 bool show_progress;
00084 };
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 void *do_jobs(void *thread_data_ptr)
00105 {
00106
00107
00108
00109 const int &thread_id=((thread_data_struct *)thread_data_ptr)->thread_id;
00110 int *jobs_done=((thread_data_struct *)thread_data_ptr)->jobs_done;
00111 int jobs=((thread_data_struct *)thread_data_ptr)->jobs;
00112 void * const results=((thread_data_struct *)thread_data_ptr)->results;
00113 pthread_mutex_t *mutex=((thread_data_struct *)thread_data_ptr)->mutex;
00114 char *job_flag=((thread_data_struct *)thread_data_ptr)->job_flag;
00115 void (*do_a_job)(const int, const int, void * const, void * const)=
00116 ((thread_data_struct *)thread_data_ptr)->do_a_job;
00117 void *individual_job_data=
00118 ((thread_data_struct *)thread_data_ptr)->individual_job_data;
00119 bool show_progress=((thread_data_struct *)thread_data_ptr)->show_progress;
00120
00121 #ifdef DEBUG
00122 printf("simplethreads: do_jobs called for thread %d.\n", thread_id);
00123 #endif
00124
00125
00126
00127
00128
00129 #ifdef DEBUG_HEAVY
00130 printf("simplethreads: do_jobs(%d) waiting for lock.\n", thread_id);
00131 #endif
00132 int err;
00133 if ((err=pthread_mutex_lock(mutex))!=0)
00134 CRIT_ERR(printf("mutex_lock error: %d (thread=%d)\n", err, thread_id));
00135 #ifdef DEBUG_HEAVY
00136 printf("simplethreads: do_jobs(%d) got lock.\n", thread_id);
00137 #endif
00138
00139 int stride=std::max(jobs/20, 1);
00140
00141
00142
00143
00144
00145
00146
00147 while (*jobs_done<jobs)
00148 {
00149 int i;
00150 for (i=0; (i<jobs) && (job_flag[i]!=0); )
00151 i++;
00152 if (i==jobs)
00153 CRIT_ERR(puts("Shouldn't happen!"));
00154
00155
00156
00157
00158
00159
00160
00161 job_flag[i]=1;
00162 (*jobs_done)++;
00163 if ((show_progress) && ((*jobs_done)%stride==0))
00164 printf("%d ", (jobs-(*jobs_done))/stride), fflush(stdout);
00165 #ifdef DEBUG_HEAVY
00166 printf("simplethreads: do_jobs(%d) unlocking.\n", thread_id);
00167 #endif
00168 if ((err=pthread_mutex_unlock(mutex))!=0)
00169 CRIT_ERR(printf("mutex_unlock err: %d (thread %d)\n", err, thread_id));
00170 #ifdef DEBUG_HEAVY
00171 printf("simplethreads: do_jobs(%d) unlocked.\n", thread_id);
00172 #endif
00173
00174
00175
00176
00177 do_a_job(i, thread_id, individual_job_data, results);
00178
00179
00180
00181
00182
00183
00184 #ifdef DEBUG_HEAVY
00185 printf("simplethreads: do_jobs(%d) waiting for lock 2.\n", thread_id);
00186 #endif
00187 int err;
00188 if ((err=pthread_mutex_lock(mutex))!=0)
00189 CRIT_ERR(printf("mutex_lock error: %d (thread=%d)\n", err, thread_id));
00190 #ifdef DEBUG_HEAVY
00191 printf("simplethreads: do_jobs(%d) got lock 2.\n", thread_id);
00192 #endif
00193 }
00194 if (show_progress)
00195 printf("\n");
00196
00197
00198
00199
00200
00201
00202 #ifdef DEBUG_HEAVY
00203 printf("simplethreads: do_jobs(%d) unlocking 2.\n", thread_id);
00204 #endif
00205 if ((err=pthread_mutex_unlock(mutex))!=0)
00206 CRIT_ERR(printf("mutex_unlock err: %d (thread %d)\n", err, thread_id));
00207 #ifdef DEBUG_HEAVY
00208 printf("simplethreads: do_jobs(%d) unlocked 2.\n", thread_id);
00209 #endif
00210
00211 return NULL;
00212
00213
00214
00215
00216
00217
00218 }
00219
00220
00221
00222
00223
00224
00225 void do_threaded_jobs(void (*do_a_job)(const int,
00226 const int,
00227 void * const,
00228 void * const),
00229 void * job_data_struct_array,
00230 const int threads,
00231 const int jobs,
00232 const bool show_progress,
00233 void * const results)
00234 {
00235
00236
00237
00238 std::vector<char> job_flag(jobs, 0);
00239 int jobs_done=0;
00240
00241
00242 pthread_mutex_t trace_admin_mutex;
00243 int err;
00244 #ifdef DEBUG_HEAVY
00245 printf("simplethreads: do_threaded_jobs initializing lock.\n");
00246 #endif
00247 if ((err=pthread_mutex_init(&trace_admin_mutex, NULL))!=0)
00248 CRIT_ERR(printf("pthread_mutex_init error: %d\n", err));
00249 #ifdef DEBUG_HEAVY
00250 printf("simplethreads: do_threaded_jobs initialized lock.\n");
00251 #endif
00252
00253
00254
00255
00256
00257 std::vector<thread_data_struct> thread_data(threads);
00258 int i;
00259 for (i=0; i<threads; i++)
00260 {
00261 thread_data[i].thread_id=i;
00262 thread_data[i].mutex=&trace_admin_mutex;
00263 thread_data[i].job_flag=&job_flag[0];
00264 thread_data[i].jobs_done=&jobs_done;
00265 thread_data[i].jobs=jobs;
00266 thread_data[i].do_a_job=do_a_job;
00267 thread_data[i].individual_job_data=job_data_struct_array;
00268 thread_data[i].results=results;
00269 thread_data[i].show_progress=show_progress;
00270 }
00271
00272
00273
00274
00275
00276
00277 std::vector<pthread_t> thread(threads);
00278 for (i=0; i<threads; i++)
00279 {
00280 #ifdef DEBUG_HEAVY
00281 printf("simplethreads: do_threaded_jobs creating thread %d.\n", i);
00282 #endif
00283 if ((err=pthread_create(&thread[i],
00284 NULL,
00285 do_jobs,
00286 (void *)&(thread_data[i])))!=0)
00287 CRIT_ERR(printf("pthread_create error: %d\n", err));
00288 #ifdef DEBUG_HEAVY
00289 printf("simplethreads: do_threaded_jobs created thread %d.\n", i);
00290 #endif
00291 }
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 for (i=0; i<threads; i++)
00317 {
00318 void *retval;
00319 #ifdef DEBUG
00320 printf("simplethreads: do_threaded_jobs joining thread %d.\n", i);
00321 #endif
00322 if ((err=pthread_join(thread[i], &retval))!=0)
00323 CRIT_ERR(printf("pthread_join error on thread %d: %d\n", i, err));
00324 #ifdef DEBUG
00325 printf("simplethreads: do_threaded_jobs joined thread %d.\n", i);
00326 #endif
00327 }
00328 #ifdef DEBUG_HEAVY
00329 printf("simplethreads: do_threaded_jobs destroying mutex.\n");
00330 #endif
00331 if ((err=pthread_mutex_destroy(&trace_admin_mutex))!=0)
00332 CRIT_ERR(printf("pthread_mutex_destroy error: %d\n", err));
00333 #ifdef DEBUG_HEAVY
00334 printf("simplethreads: do_threaded_jobs destroyed mutex.\n");
00335 #endif
00336
00337
00338
00339
00340
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370