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.
Implement semaphore using pipe?
-
- Member
- Posts: 70
- Joined: Fri Jun 28, 2013 1:48 am
- Contact:
Implement semaphore using pipe?
Reinventing the Wheel, code: https://github.com/songziming/wheel
Re: Implement semaphore using pipe?
This idea is nothing new. I remember a text about "make -j" talking about using pipes as semaphores. Personally, though, I think that simply implementing futexes, and then implementing pipes, semaphores, mutexes, rwlocks, and barriers with them is the better idea. And futexes are a fundamentally very simple system, in which you merely suspend and awaken threads.
Carpe diem!