metal-cos/sys/kern/cv.c

85 lines
1.2 KiB
C
Raw Normal View History

/*
* Copyright (c) 2023 Ali Mashtizadeh
* All rights reserved.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/cdefs.h>
#include <sys/kassert.h>
#include <sys/kconfig.h>
#include <sys/kdebug.h>
#include <sys/kmem.h>
#include <sys/mp.h>
#include <sys/queue.h>
#include <sys/thread.h>
#include <sys/spinlock.h>
#include <sys/waitchannel.h>
#include <sys/mutex.h>
#include <sys/cv.h>
#include <errno.h>
void
CV_Init(CV *cv, const char *name)
{
WaitChannel_Init(&cv->chan, name);
return;
}
void
CV_Destroy(CV *cv)
{
WaitChannel_Destroy(&cv->chan);
return;
}
/**
* CV_Wait --
*
2023-09-10 20:12:25 +00:00
* Wait to be woken up on a condition.
*/
void
CV_Wait(CV *cv, Mutex *mtx)
{
/* Do not go to sleep holding a spinlock! */
ASSERT(Critical_Level() == 0);
WaitChannel_Lock(&cv->chan);
Mutex_Unlock(mtx);
WaitChannel_Sleep(&cv->chan);
Mutex_Lock(mtx);
return;
}
/**
* CV_Signal --
*
2023-09-10 20:12:25 +00:00
* Wake a single thread waiting on the condition.
*/
void
CV_Signal(CV *cv)
{
WaitChannel_Wake(&cv->chan);
return;
}
/**
* CV_Broadcast --
*
2023-09-10 20:12:25 +00:00
* Wake all threads waiting on the condition.
*/
void
CV_Broadcast(CV *cv)
{
WaitChannel_WakeAll(&cv->chan);
return;
}