int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) { initialize_original_functions(); //original_pthread_mutex_unlock(&global_lock); //original_pthread_mutex_lock(&global_lock); struct Thread_Arg *thread_arg = (struct Thread_Arg*)malloc(sizeof(struct Thread_Arg)); thread_arg->start_routine = start_routine; thread_arg->arg = arg; if(firstrun) { firstrun = 0; original_pthread_mutex_lock(&global_lock); gl_holder = (long)pthread_self(); //printf("firstrun self: %ld\n",(long)pthread_self()); //printf("firstrun thread: %ld\n",(long)thread); thread_list[gl_holder] = 1; } int ret = original_pthread_create(thread, attr, thread_main, thread_arg); chess_schedule(); return ret; }
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg){ extern int nb_times_thread_create; nb_times_thread_create++; // On déclare un pointeur vers une fonction qui a le même prototype void *(*original_pthread_create) (pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); // On crée une copie du pointeur vers la fonction malloc originale original_pthread_create = dlsym(RTLD_NEXT, "pthread_create"); // On exécute la fonction malloc originale, car exécuter malloc(size) // reviendrait à faire un appel récursif infini ... void *pt = original_pthread_create(thread,attr,start_routine,arg); return EXIT_SUCCESS; }