00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdarg.h>
00022 #include <SDL/SDL_thread.h>
00023
00024 #include "flgrCoreMalloc.h"
00025 #include "flgrCoreThreads.h"
00026
00027
00036
00037
00041
00042 FLGR_ThreadsArgList* flgr_threads_create_argument_list(int threads_nb) {
00043 FLGR_ThreadsArgList *arglist;
00044 int i;
00045
00046 arglist = (FLGR_ThreadsArgList*) flgr_malloc(sizeof(FLGR_ThreadsArgList));
00047
00048 arglist->threads_nb = threads_nb;
00049 arglist->threads_arg = (FLGR_ThreadsArg **) flgr_malloc(threads_nb * sizeof(FLGR_ThreadsArg*));
00050
00051
00052 for(i=0 ; i<threads_nb ; i++) {
00053 arglist->threads_arg[i] = (FLGR_ThreadsArg *) flgr_malloc(sizeof(FLGR_ThreadsArg));
00054 }
00055
00056 return arglist;
00057 }
00058
00059
00060
00062
00067
00068 void flgr_threads_set_argument(FLGR_ThreadsArgList* arglist, int thread_index, ...) {
00069 FLGR_ThreadsArg *parg = arglist->threads_arg[thread_index];
00070 va_list s;
00071 int j;
00072
00073 va_start(s, thread_index);
00074
00075 parg->argc = va_arg(s, int);
00076
00077 parg->argv = (void **) flgr_malloc( parg->argc * sizeof(void*) );
00078
00079 for(j=0 ; j<parg->argc ; j++) {
00080 parg->argv[j] = va_arg(s, void*);
00081 }
00082
00083 va_end(s);
00084
00085 }
00086
00087
00088
00089
00091
00096
00097 void flgr_threads_destroy_argument_list(FLGR_ThreadsArgList *arglist) {
00098 int i;
00099
00100 if(arglist==NULL) return;
00101
00102 for(i=0 ; i<arglist->threads_nb ; i++) {
00103 flgr_free(arglist->threads_arg[i]->argv);
00104 flgr_free(arglist->threads_arg[i]);
00105 }
00106
00107 flgr_free(arglist->threads_arg);
00108
00109 flgr_free(arglist);
00110 }
00111
00112
00113
00114
00115
00116
00117
00119
00125
00126 FLGR_Ret flgr_threads_start(FLGR_ThreadsArgList *arglist,
00127 int threads_nb_per_fct, int threads_fct_nb, ...) {
00128
00129 FLGR_ThreadFunction *fct_array;
00130 SDL_Thread **threads;
00131 va_list s;
00132 int th_nb;
00133 int i,j,k=0;
00134
00135 th_nb = threads_fct_nb * threads_nb_per_fct;
00136
00137 if(arglist->threads_nb != th_nb) {
00138 POST_ERROR("Threads numbers specified in arglist does not match to number of thread given here!");
00139 return FLGR_RET_PARAM_ERROR;
00140 }
00141
00142 fct_array = (FLGR_ThreadFunction *) flgr_malloc(threads_fct_nb * sizeof(FLGR_ThreadFunction));
00143 threads = (SDL_Thread **) flgr_malloc(th_nb * sizeof(SDL_Thread*));
00144
00145
00146 va_start(s, threads_fct_nb);
00147
00148 for(i=0 ; i<threads_fct_nb ; i++) {
00149 fct_array[i] = va_arg(s, FLGR_ThreadFunction);
00150 }
00151
00152 va_end(s);
00153
00154
00155
00156 for(i=0,k=0 ; i< threads_fct_nb ; i++) {
00157 for(j=0 ; j<threads_nb_per_fct ; j++,k++) {
00158 threads[k] = SDL_CreateThread(fct_array[i], arglist->threads_arg[k]);
00159 }
00160 }
00161
00162
00163 for(i=0 ; i<th_nb ; i++) {
00164 SDL_WaitThread(threads[i], NULL);
00165 }
00166
00167
00168 flgr_free(fct_array);
00169 flgr_free(threads);
00170
00171 return FLGR_RET_OK;
00172 }
00173
00174
00175
00176