Implement semaphore using pipe?
Posted: Fri Mar 22, 2024 3:02 am
I'm thinking if we can provide semaphore as a special type of pipe. Semaphores are just pipes without data.
Semaphore is a shared variable, tasks can up/down its value. Tasks might switch to pending state during up/down.
Pipe is a shared buffer, with a variable indicating current buffer size. Tasks can write to a pipe, increasing the buffer size. Tasks can read from a pipe, decreasing the buffer size. Writing and reading pipe might pend the caller task.
If we remove the data copy part from the pipe, then it becomes semaphore. And mutex is just pipe without data and size limit set to ONE.
So we can provide pipe, semaphore and mutex using one code base?
Scheduling related code is difficult to debug. Doing so could make kernel simpler.
Semaphore is a shared variable, tasks can up/down its value. Tasks might switch to pending state during up/down.
Pipe is a shared buffer, with a variable indicating current buffer size. Tasks can write to a pipe, increasing the buffer size. Tasks can read from a pipe, decreasing the buffer size. Writing and reading pipe might pend the caller task.
If we remove the data copy part from the pipe, then it becomes semaphore. And mutex is just pipe without data and size limit set to ONE.
So we can provide pipe, semaphore and mutex using one code base?
Scheduling related code is difficult to debug. Doing so could make kernel simpler.