barrier implimentation using semaphores

This commit is contained in:
Tate Haga 2018-10-16 16:44:48 -04:00
parent cde495241c
commit 1a49eaa734
4 changed files with 48 additions and 0 deletions

View File

@ -34,6 +34,7 @@ extern "C" {
#include "switch/kernel/random.h"
#include "switch/kernel/jit.h"
#include "switch/kernel/ipc.h"
#include "switch/kernel/barrier.h"
#include "switch/services/sm.h"
#include "switch/services/smm.h"

View File

@ -0,0 +1,16 @@
#pragma once
#include "semaphore.h"
// barrier structure
typedef struct barrier{
u64 count;
u64 thread_total;
Semaphore throttle;
Semaphore lock;
Semaphore thread_wait;
} barrier;
void barrierInit(barrier *my_barrier, u64 thread_count);
void barrierWait(barrier *b);

View File

@ -42,3 +42,5 @@ void semaphoreWait(Semaphore *s);
* @return true if no wait and successful lock, false otherwise.
*/
bool semaphoreTryWait(Semaphore *s);

View File

@ -0,0 +1,29 @@
#include "kernel/barrier.h"
void barrierInit(barrier *my_barrier, u64 thread_count){
my_barrier->count = 0;
my_barrier->thread_total = thread_count;
semaphoreInit(&my_barrier->throttle, 0);
semaphoreInit(&my_barrier->lock, 1);
semaphoreInit(&my_barrier->thread_wait, 0);
}
void barrierWait(barrier *b){
semaphoreWait(&b->lock);
if(b->count < b->thread_total){
b->count++;
}
if(b->count < b->thread_total){
semaphoreSignal(&b->lock);
semaphoreWait(&b->thread_wait);
semaphoreSignal(&b->throttle);
}
else if(b->count == b->thread_total){
for(int i = 0; i < b->thread_total-1; i++){
semaphoreSignal(&b->thread_wait);
semaphoreWait(&b->throttle);
}
b->count = 0;
semaphoreSignal(&b->lock);
}
}