Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/git/vole.git

Write over SSH with a registered key:

git clone ssh://git@0.0.0.0:2222/vole.git

A free and open-source forge that lives in your Git repo.

Blame · ci/cache/lock_other.go

Line attribution on main · 46 line(s)

a29ce1f7
Keenan Nemetz
2d ago
1 //go:build !unix
2
3 package cache
4
5 import (
6 "os"
7 "sync"
8 )
9
10 // flock is not part of the portable API, so a platform without it serialises
11 // holders inside one process only. Vole ships runner packages for the platforms
12 // where the kernel lock exists; this keeps the rest building, and says what it
13 // does: a job and the trimmer in one process are excluded from each other, and a
14 // second process on the same --dir is not.
15 var (
16 processLockMu sync.Mutex
17 processLocks = map[string]*sync.RWMutex{}
18 )
19
20 func lockFile(f *os.File, exclusive, wait bool) (func(), bool, error) {
21 processLockMu.Lock()
22 mu := processLocks[f.Name()]
23 if mu == nil {
24 mu = &sync.RWMutex{}
25 processLocks[f.Name()] = mu
26 }
27 processLockMu.Unlock()
28 switch {
29 case exclusive && !wait:
30 if !mu.TryLock() {
31 return nil, false, nil
32 }
33 return mu.Unlock, true, nil
34 case exclusive:
35 mu.Lock()
36 return mu.Unlock, true, nil
37 case !wait:
38 if !mu.TryRLock() {
39 return nil, false, nil
40 }
41 return mu.RUnlock, true, nil
42 default:
43 mu.RLock()
44 return mu.RUnlock, true, nil
45 }
46 }