1 | #!/usr/bin/env ysh
|
2 |
|
3 | module stdlib/synch || return 0
|
4 |
|
5 | ############################
|
6 | ### FIFO File Desriptors ###
|
7 | ############################
|
8 |
|
9 | proc fifo-fd-new(; out_fd) {
|
10 | # WARN: this section should be critical but for now it's not
|
11 | # A solution may be retry on fail.
|
12 | #====================
|
13 | var fifo = $(mktemp -u)
|
14 | mkfifo $fifo
|
15 | #====================
|
16 | exec {fd}<>$fifo
|
17 | call out_fd->setValue(fd)
|
18 | }
|
19 |
|
20 | proc fifo-fd-destroy(; fd) {
|
21 | var fifoFile = $(readlink /proc/$$/fd/$fd)
|
22 | exec {fd}>&-
|
23 | exec {fd}<&-
|
24 | rm $fifoFile
|
25 | }
|
26 |
|
27 | #################
|
28 | ### Semaphore ###
|
29 | #################
|
30 |
|
31 | proc sema-new(; value, out_sema) {
|
32 | fifo-fd-new (&sema)
|
33 | sema-up (sema, value)
|
34 | call out_sema->setValue(sema)
|
35 | }
|
36 |
|
37 | proc sema-down(; sema) {
|
38 | read <&$sema
|
39 | }
|
40 |
|
41 | proc sema-up(; sema, delta = 1) {
|
42 | fork {
|
43 | for _ in (0 .. delta) {
|
44 | echo >&$sema
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | proc sema-destroy(; sema) {
|
50 | fifo-fd-destroy (sema)
|
51 | }
|