| [2417] | 1 | /*!\file: LaunchThread.cpp
|
|---|
| 2 | * \brief launch thread in a generic way, covering single and multi-threaded cases
|
|---|
| 3 | * This routine attempts to simplify management of multi-threading. When multi-threadeing
|
|---|
| 4 | * is not requested (serial run), LaunchThread will just call the function (provided in argument
|
|---|
| 5 | * list), nothing fancy there. If multi-threading is requested, LaunchThread will launch the
|
|---|
| 6 | * function on multiple threads (num_threads of them), and provide these functions with the
|
|---|
| 7 | * local data they need (folded in the "gate" structure) + the thread id + the number of threads
|
|---|
| 8 | * All this info is collected in the pthread_handle structure.
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | #ifdef HAVE_CONFIG_H
|
|---|
| [9320] | 12 | #include <config.h>
|
|---|
| [2417] | 13 | #else
|
|---|
| 14 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
|---|
| 15 | #endif
|
|---|
| 16 |
|
|---|
| 17 | #ifdef _MULTITHREADING_
|
|---|
| 18 | #include <pthread.h>
|
|---|
| 19 | #endif
|
|---|
| 20 |
|
|---|
| 21 | #include "./issm_threads.h"
|
|---|
| [14904] | 22 | #include "../MemOps/MemOps.h"
|
|---|
| [2417] | 23 | #include "../Exceptions/exceptions.h"
|
|---|
| [3775] | 24 | #include "../../include/include.h"
|
|---|
| [2417] | 25 |
|
|---|
| 26 | void LaunchThread(void* function(void*), void* gate,int num_threads){
|
|---|
| 27 |
|
|---|
| 28 | #ifdef _MULTITHREADING_
|
|---|
| 29 | int i;
|
|---|
| [12435] | 30 | int *status = NULL;
|
|---|
| 31 | pthread_t *threads = NULL;
|
|---|
| 32 | pthread_handle *handles = NULL;
|
|---|
| [13622] | 33 |
|
|---|
| [14054] | 34 | /*check number of threads*/
|
|---|
| [14057] | 35 | if(num_threads<1) _error_("number of threads must be at least 1");
|
|---|
| 36 | if(num_threads>2000) _error_("number of threads seems to be too high ("<<num_threads<<">2000)");
|
|---|
| [14054] | 37 |
|
|---|
| [2417] | 38 | /*dynamically allocate: */
|
|---|
| [12435] | 39 | threads=xNew<pthread_t>(num_threads);
|
|---|
| 40 | handles=xNew<pthread_handle>(num_threads);
|
|---|
| [2417] | 41 |
|
|---|
| 42 | for(i=0;i<num_threads;i++){
|
|---|
| 43 | handles[i].gate=gate;
|
|---|
| 44 | handles[i].id=i;
|
|---|
| 45 | handles[i].num=num_threads;
|
|---|
| 46 | }
|
|---|
| 47 | for(i=0;i<num_threads;i++){
|
|---|
| 48 |
|
|---|
| 49 | if(pthread_create(threads+i,NULL,function,(void*)(handles+i))){
|
|---|
| [13056] | 50 | _error_("pthread_create error");
|
|---|
| [2417] | 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | for(i=0;i<num_threads;i++){
|
|---|
| 54 | if(pthread_join(threads[i],(void**)&status)){
|
|---|
| [13056] | 55 | _error_("pthread_join error");
|
|---|
| [2417] | 56 | }
|
|---|
| 57 | }
|
|---|
| [13622] | 58 |
|
|---|
| [2417] | 59 | /*Free ressources:*/
|
|---|
| [12435] | 60 | xDelete<pthread_t>(threads);
|
|---|
| 61 | xDelete<pthread_handle>(handles);
|
|---|
| [2417] | 62 |
|
|---|
| 63 | #else
|
|---|
| [2418] | 64 | pthread_handle handle;
|
|---|
| 65 | handle.gate=gate;
|
|---|
| 66 | handle.id=0;
|
|---|
| 67 | handle.num=1;
|
|---|
| [13622] | 68 |
|
|---|
| [2418] | 69 | function((void*)&handle);
|
|---|
| [2417] | 70 | #endif
|
|---|
| 71 | }
|
|---|