Implement semaphore using pipe?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
songziming
Member
Member
Posts: 71
Joined: Fri Jun 28, 2013 1:48 am
Contact:

Implement semaphore using pipe?

Post by songziming »

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.
Reinventing the Wheel, code: https://github.com/songziming/wheel
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: Implement semaphore using pipe?

Post by nullplan »

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!
Post Reply