mirror of
https://github.com/switchbrew/libnx.git
synced 2025-07-06 03:12:15 +02:00
finished barrier
This commit is contained in:
parent
a9d95be173
commit
fac799648e
@ -28,6 +28,7 @@ extern "C" {
|
|||||||
#include "switch/kernel/condvar.h"
|
#include "switch/kernel/condvar.h"
|
||||||
#include "switch/kernel/thread.h"
|
#include "switch/kernel/thread.h"
|
||||||
#include "switch/kernel/semaphore.h"
|
#include "switch/kernel/semaphore.h"
|
||||||
|
#include "switch/kernel/barrier.h"
|
||||||
#include "switch/kernel/virtmem.h"
|
#include "switch/kernel/virtmem.h"
|
||||||
#include "switch/kernel/detect.h"
|
#include "switch/kernel/detect.h"
|
||||||
#include "switch/kernel/random.h"
|
#include "switch/kernel/random.h"
|
||||||
@ -75,6 +76,7 @@ extern "C" {
|
|||||||
#include "switch/runtime/nxlink.h"
|
#include "switch/runtime/nxlink.h"
|
||||||
|
|
||||||
#include "switch/runtime/util/utf.h"
|
#include "switch/runtime/util/utf.h"
|
||||||
|
#include "switch/runtime/util/list.h"
|
||||||
|
|
||||||
#include "switch/runtime/devices/console.h"
|
#include "switch/runtime/devices/console.h"
|
||||||
#include "switch/runtime/devices/usb_comms.h"
|
#include "switch/runtime/devices/usb_comms.h"
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
typedef struct barrier {
|
typedef struct barrier {
|
||||||
List threads_registered;
|
List threads_registered;
|
||||||
List threads_waiting;
|
List threads_waiting;
|
||||||
RwLock mutex;
|
Mutex mutex;
|
||||||
bool isInited;
|
bool isInited;
|
||||||
} Barrier;
|
} Barrier;
|
||||||
|
|
||||||
|
@ -1,17 +1,27 @@
|
|||||||
#include "kernel/barrier.h"
|
#include "kernel/barrier.h"
|
||||||
|
|
||||||
void barrierInit(Barrier* b) {
|
void barrierInit(Barrier* b) {
|
||||||
rwlockWriteLock(b->mutex);
|
mutexInit(&b->mutex);
|
||||||
|
mutexLock(&b->mutex);
|
||||||
if(b->isInited) {
|
if(b->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
listInit(b->threads_registered);
|
listInit(b->threads_registered);
|
||||||
listInit(b->threads_waiting);
|
listInit(b->threads_waiting);
|
||||||
b->isInited = true;
|
b->isInited = true;
|
||||||
rwlockWriteUnlock(b->mutex);
|
mutexUnlock(&b->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void barrierFree(Barrier* b);
|
void barrierFree(Barrier* b) {
|
||||||
|
mutexLock(&b->mutex);
|
||||||
|
if(!b->isInited) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
listFree(b->threads_registered);
|
||||||
|
listFree(b->threads_waiting);
|
||||||
|
b->isInited = false;
|
||||||
|
mutexUnlock(&b->mutex);
|
||||||
|
}
|
||||||
|
|
||||||
void barrierRegister(Barrier* b, Thread* thread) {
|
void barrierRegister(Barrier* b, Thread* thread) {
|
||||||
if(listIsInserted(b->threads_registered, (void*)thread)) {
|
if(listIsInserted(b->threads_registered, (void*)thread)) {
|
||||||
@ -28,12 +38,19 @@ void barrierWait(Barrier* b, Thread* thread) {
|
|||||||
if(!listIsInserted(b->threads_registered)) {
|
if(!listIsInserted(b->threads_registered)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
threadPause((void*)thread);
|
|
||||||
listInsertLast(b->threads_waiting, thread);
|
|
||||||
|
|
||||||
if(listGetNumNodes(b->threads_registered) == listGetNumNodes(b->threads_waiting)) {
|
mutexLock(&b->mutex);
|
||||||
|
if(listGetNumNodes(b->threads_registered) == listGetNumNodes(b->threads_waiting)+1) {
|
||||||
while(listGetNumNodes(b->threads_waiting) > 0) {
|
while(listGetNumNodes(b->threads_waiting) > 0) {
|
||||||
threadResume(listGetItem(b->threads_waiting, 0));
|
Thread* current_thread = listGetItem(b->threads_waiting, 0);
|
||||||
|
threadResume(current_thread);
|
||||||
|
listDelete(b->threads_waiting, current_thread);
|
||||||
}
|
}
|
||||||
|
mutexUnlock(&b->mutex);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
listInsertLast(b->threads_waiting, thread);
|
||||||
|
mutexUnlock(&b->mutex);
|
||||||
|
threadPause((void*)thread);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,7 +2,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void listInit(List* l) {
|
void listInit(List* l) {
|
||||||
rwlockWriteLock(l->mutex);
|
rwlockWriteLock(&l->mutex);
|
||||||
if(l->isInited) {
|
if(l->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -14,11 +14,11 @@ void listInit(List* l) {
|
|||||||
l->last = header;
|
l->last = header;
|
||||||
l->num_nodes = 0;
|
l->num_nodes = 0;
|
||||||
l->isInited = true;
|
l->isInited = true;
|
||||||
rwlockWriteUnlock(l->mutex);
|
rwlockWriteUnlock(&l->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void listFree(List* l) {
|
void listFree(List* l) {
|
||||||
rwlockWriteLock(l->mutex);
|
rwlockWriteLock(&l->mutex);
|
||||||
if(!l->isInited) {
|
if(!l->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -31,20 +31,20 @@ void listFree(List* l) {
|
|||||||
l->last = NULL;
|
l->last = NULL;
|
||||||
l->num_nodes = 0;
|
l->num_nodes = 0;
|
||||||
l->isInited = false;
|
l->isInited = false;
|
||||||
rwlockWriteUnlock(l->mutex);
|
rwlockWriteUnlock(&l->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void listInsert(List* l, void* item, u32 pos) {
|
void listInsert(List* l, void* item, u32 pos) {
|
||||||
rwlockReadLock(l->mutex);
|
rwlockReadLock(&l->mutex);
|
||||||
if(!l->isInited) {
|
if(!l->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(pos > l->num_nodes || pos < 0) {
|
if(pos > l->num_nodes || pos < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
rwlockReadUnlock(l->mutex);
|
rwlockReadUnlock(&l->mutex);
|
||||||
|
|
||||||
rwlockWriteLock(l->mutex);
|
rwlockWriteLock(&l->mutex);
|
||||||
Node* aux = l->header;
|
Node* aux = l->header;
|
||||||
for(u32 i = pos; i > 0; i--) {
|
for(u32 i = pos; i > 0; i--) {
|
||||||
aux = aux->next;
|
aux = aux->next;
|
||||||
@ -56,11 +56,11 @@ void listInsert(List* l, void* item, u32 pos) {
|
|||||||
aux->next = new;
|
aux->next = new;
|
||||||
|
|
||||||
l->num_nodes++;
|
l->num_nodes++;
|
||||||
rwlockWriteUnlock(l->mutex);
|
rwlockWriteUnlock(&l->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void listInsertLast(List* l, void* item) {
|
void listInsertLast(List* l, void* item) {
|
||||||
rwlockWriteLock(l->mutex);
|
rwlockWriteLock(&l->mutex);
|
||||||
if(!l->isInited) {
|
if(!l->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -70,11 +70,11 @@ void listInsertLast(List* l, void* item) {
|
|||||||
|
|
||||||
l->last->next = new;
|
l->last->next = new;
|
||||||
l->last = new;
|
l->last = new;
|
||||||
rwlockWriteUnlock(l->mutex);
|
rwlockWriteUnlock(&l->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void listDelete(List* l, void* item) {
|
void listDelete(List* l, void* item) {
|
||||||
rwlockWriteLock(l->mutex);
|
rwlockWriteLock(&l->mutex);
|
||||||
if(!l->isInited) {
|
if(!l->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -91,11 +91,11 @@ void listDelete(List* l, void* item) {
|
|||||||
l->num_nodes--;
|
l->num_nodes--;
|
||||||
}
|
}
|
||||||
|
|
||||||
rwlockWriteUnlock(l->mutex);
|
rwlockWriteUnlock(&l->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool listIsInserted(List* l, void* item) {
|
bool listIsInserted(List* l, void* item) {
|
||||||
rwlockReadLock(l->mutex);
|
rwlockReadLock(&l->mutex);
|
||||||
if(!l->isInited) {
|
if(!l->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -104,22 +104,22 @@ bool listIsInserted(List* l, void* item) {
|
|||||||
aux = aux->next;
|
aux = aux->next;
|
||||||
}
|
}
|
||||||
bool result = aux == NULL ? false : true;
|
bool result = aux == NULL ? false : true;
|
||||||
rwlockReadUnlock(l->mutex);
|
rwlockReadUnlock(&l->mutex);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 listGetNumNodes(List* l) {
|
u32 listGetNumNodes(List* l) {
|
||||||
rwlockReadLock(l->mutex);
|
rwlockReadLock(&l->mutex);
|
||||||
if(!l->isInited) {
|
if(!l->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
u32 result = l->num_nodes;
|
u32 result = l->num_nodes;
|
||||||
rwlockReadUnlock(l->mutex);
|
rwlockReadUnlock(&l->mutex);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* listGetItem(List* l, u32 pos) {
|
void* listGetItem(List* l, u32 pos) {
|
||||||
rwlockReadLock(l->mutex);
|
rwlockReadLock(&l->mutex);
|
||||||
if(!l->isInited) {
|
if(!l->isInited) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -131,6 +131,6 @@ void* listGetItem(List* l, u32 pos) {
|
|||||||
aux = aux->next;
|
aux = aux->next;
|
||||||
}
|
}
|
||||||
void* result = aux->item;
|
void* result = aux->item;
|
||||||
rwlockReadUnlock(l->mutex);
|
rwlockReadUnlock(&l->mutex);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user