mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-05 10:52:15 +02:00
barrier implimentation using semaphores
This commit is contained in:
parent
cde495241c
commit
1a49eaa734
@ -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"
|
||||
|
16
nx/include/switch/kernel/barrier.h
Normal file
16
nx/include/switch/kernel/barrier.h
Normal 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);
|
@ -42,3 +42,5 @@ void semaphoreWait(Semaphore *s);
|
||||
* @return true if no wait and successful lock, false otherwise.
|
||||
*/
|
||||
bool semaphoreTryWait(Semaphore *s);
|
||||
|
||||
|
||||
|
29
nx/source/kernel/barrier.c
Normal file
29
nx/source/kernel/barrier.c
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user