2023-09-10 00:18:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023 Ali Mashtizadeh
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
2023-09-02 21:51:10 +00:00
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
2023-09-02 21:51:10 +00:00
|
|
|
* CV_Wait --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Wait to be woken up on a condition.
|
2023-09-02 21:51:10 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
CV_Wait(CV *cv, Mutex *mtx)
|
|
|
|
{
|
2023-09-06 17:51:47 +00:00
|
|
|
/* Do not go to sleep holding a spinlock! */
|
|
|
|
ASSERT(Critical_Level() == 0);
|
|
|
|
|
2023-09-02 21:51:10 +00:00
|
|
|
WaitChannel_Lock(&cv->chan);
|
|
|
|
Mutex_Unlock(mtx);
|
|
|
|
WaitChannel_Sleep(&cv->chan);
|
|
|
|
Mutex_Lock(mtx);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
2023-09-02 21:51:10 +00:00
|
|
|
* CV_Signal --
|
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Wake a single thread waiting on the condition.
|
2023-09-02 21:51:10 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
CV_Signal(CV *cv)
|
|
|
|
{
|
|
|
|
WaitChannel_Wake(&cv->chan);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-10 00:18:08 +00:00
|
|
|
/**
|
2023-10-16 19:07:17 +00:00
|
|
|
* CV_Broadcast --
|
2023-09-02 21:51:10 +00:00
|
|
|
*
|
2023-09-10 20:12:25 +00:00
|
|
|
* Wake all threads waiting on the condition.
|
2023-09-02 21:51:10 +00:00
|
|
|
*/
|
|
|
|
void
|
2023-10-16 19:07:17 +00:00
|
|
|
CV_Broadcast(CV *cv)
|
2023-09-02 21:51:10 +00:00
|
|
|
{
|
|
|
|
WaitChannel_WakeAll(&cv->chan);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|