diff --git a/src/intrinsic.c b/src/intrinsic.c index 26bb56d..4468ede 100644 --- a/src/intrinsic.c +++ b/src/intrinsic.c @@ -10,11 +10,11 @@ int omp_get_num_threads (void) { int omp_get_thread_num (void) { //printf("TBI: omp_get_thread_num still doesn't know who I am ... let's say I am 0\n"); - printf("Called get_thread: \n"); + //printf("Called get_thread: \n"); miniomp_thread_t *thread = pthread_getspecific(miniomp_specifickey); - printf("Casted getspecific \n"); + //printf("Casted getspecific \n"); unsigned int thread_id = thread->id; - printf("%d\n", thread_id); + //printf("THREAD ID IS %d\n", thread_id); return((int)thread_id); } diff --git a/src/parallel.c b/src/parallel.c index 89523ba..45b66dd 100644 --- a/src/parallel.c +++ b/src/parallel.c @@ -20,7 +20,7 @@ void miniomp_thread_init(miniomp_thread_t * thread, unsigned int id, void (*fn) // This is the prototype for the Pthreads starting function void worker(void *args) { - pthread_key_create(&miniomp_specifickey, NULL); + //pthread_key_create(&miniomp_specifickey, NULL); miniomp_thread_t *thread = args; pthread_setspecific(miniomp_specifickey, thread); miniomp_parallel_t *parallel = thread->region; @@ -28,9 +28,9 @@ void worker(void *args) { printf("Thread %d initialized\n", id); void (*fn) (void *) = parallel->fn; void * data = parallel->fn_data; - printf("Thread %d fn and data done\n", id); + //printf("Thread %d fn and data done\n", id); fn(data); - printf("Executed fn in thread %d\n", id); + //printf("Executed fn in thread %d\n", id); } void @@ -41,15 +41,19 @@ GOMP_parallel (void (*fn) (void *), void *data, unsigned num_threads, unsigned i miniomp_parallel = malloc(sizeof(miniomp_parallel_t)); miniomp_parallel->fn = fn; miniomp_parallel->fn_data = data; - miniomp_parallel->id = 0; + miniomp_parallel->id = 0; + pthread_barrier_init(&(miniomp_parallel->barrier), NULL, num_threads); pthread_mutex_init(&(miniomp_parallel->mutex), NULL); + pthread_key_create(&miniomp_specifickey, NULL); for (int i=0; imutex)); + pthread_barrier_destroy(&(miniomp_parallel->barrier)); free(miniomp_threads); free(miniomp_parallel); } diff --git a/src/parallel.h b/src/parallel.h index a79cfb7..123608a 100644 --- a/src/parallel.h +++ b/src/parallel.h @@ -8,6 +8,7 @@ typedef struct { void *fn_data; unsigned int id; pthread_mutex_t mutex; + pthread_barrier_t barrier; // complete the definition of parallel descriptor } miniomp_parallel_t; diff --git a/src/synchronization.c b/src/synchronization.c index 04f74bb..c94216a 100644 --- a/src/synchronization.c +++ b/src/synchronization.c @@ -4,27 +4,36 @@ pthread_mutex_t miniomp_default_lock; void GOMP_critical_start (void) { - printf("TBI: Entering an unnamed critical, don't know if anyone else is alrady in. I proceed\n"); + miniomp_thread_t *thread = pthread_getspecific(miniomp_specifickey); + pthread_mutex_lock(&(thread->region->mutex)); + //printf("TBI: Entering an unnamed critical, don't know if anyone else is alrady in. I proceed\n"); } void GOMP_critical_end (void) { - printf("TBI: Exiting an unnamed critical section. I can not inform anyone else, bye!\n"); + miniomp_thread_t *thread = pthread_getspecific(miniomp_specifickey); + pthread_mutex_unlock(&(thread->region->mutex)); + //printf("TBI: Exiting an unnamed critical section. I can not inform anyone else, bye!\n"); } void GOMP_critical_name_start (void **pptr) { pthread_mutex_t * plock = *pptr; - printf("TBI: Entering a named critical %p (%p), don't know if anyone else is alrady in. I proceed\n", pptr, plock); + pthread_mutex_lock(plock); + printf("TBI: Entering a named critical %p (%p)\n", pptr, plock); // if plock is NULL it means that the lock associated to the name has not yet been allocated and initialized } void GOMP_critical_name_end (void **pptr) { pthread_mutex_t * plock = *pptr; - printf("TBI: Exiting a named critical %p (%p), I can not inform anyone else, bye!\n", pptr, plock); + pthread_mutex_unlock(plock); + printf("TBI: Exiting a named critical %p (%p)\n", pptr, plock); // if plock is still NULL something went wrong } pthread_barrier_t miniomp_barrier; void GOMP_barrier() { - printf("TBI: Entering in barrier, but do not know how to wait for the rest. I proceed\n"); + miniomp_thread_t *thread = pthread_getspecific(miniomp_specifickey); + printf("Thread %d waiting for signal", thread->id); + pthread_barrier_wait(&(thread->region->barrier)); + }