lock.go 736 Bytes
Newer Older
Łukasz Magiera's avatar
Łukasz Magiera committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package fs

import "context"

// This very explicitly doesn't use fs locks - we generally only
//   care about sector ownership in the storage miner process, and
//   using fs locks would make things like seals workers on NFS quite tricky
// TODO: RW eventually
func (f *FS) Lock(ctx context.Context, sector SectorPath) error {
	for {
		f.lk.Lock()
		w, ok := f.locks[sector]
		if !ok {
			f.locks[sector] = make(chan struct{})
			f.lk.Unlock()
			return nil
		}
		f.lk.Unlock()

		log.Infof("Waiting for lock on %s", string(sector))

		select {
		case <-w:
		case <-ctx.Done():
			return ctx.Err()
		}
	}
}

func (f *FS) Unlock(sector SectorPath) {
	f.lk.Lock()
	defer f.lk.Unlock()

	close(f.locks[sector])
	delete(f.locks, sector)
}