GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — locks/guard.go at master · asciimoth/locks Expand file tree/ Copy pathguard.goMore file actions 138 lines (130 loc) · 2.69 KB / Copy pathguard.goFile metadata and controls138 lines (130 loc) · 2.69 KB 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 //go:build darwin || linux || openbsd || windows package locks import ( "context" "errors" "fmt" "os" "sync" "time" ) type identity struct{ dev, ino, high uint64 } type rangeOwner struct { readers int writer bool } type guard struct { f *os.File key identity refs int mu sync.Mutex ranges [1025]rangeOwner writable bool noParallel bool } var guards = struct { sync.Mutex m map[identity]*guard }{m: map[identity]*guard{}} func getGuard(d *directory, name string, write bool) (*guard, error) { return getGuardMode(d, name, write, false) } func (s *Local) getGuard(d *directory, write bool) (*guard, error) { return getGuardMode(d, "."+s.name+".guard", write, s.options.NoParallel) } func getGuardMode(d *directory, name string, write, noParallel bool) (*guard, error) { guards.Lock() defer guards.Unlock() st, err := d.stat(name) if err != nil { return nil, err } if st == nil && !write { return nil, nil } if st != nil { if st.dir || st.links != 1 { return nil, ErrFormat } if g := guards.m[st.key]; g != nil { if g.noParallel != noParallel { return nil, fmt.Errorf("%w: conflicting NoParallel settings for the same store", ErrFormat) } if write && !g.writable { return nil, ErrDenied } g.refs++ return g, nil } } f, opened, writable, err := d.openGuard(name, st == nil, write) if err != nil { return nil, err } if opened.dir || opened.links != 1 || (st != nil && opened.key != st.key) { return nil, errors.Join(ErrFormat, f.Close()) } g := &guard{f: f, key: opened.key, refs: 1, writable: writable, noParallel: noParallel} guards.m[g.key] = g return g, nil } func (g *guard) close() error { guards.Lock() defer guards.Unlock() g.refs-- if g.refs == 0 { delete(guards.m, g.key) return g.f.Close() } return nil } func (g *guard) lock(ctx context.Context, slot int, exclusive bool) error { for { if e := ctx.Err(); e != nil { return e } g.mu.Lock() r := &g.ranges[slot] if !r.writer && (!exclusive || r.readers == 0) { var e error if r.readers == 0 && !g.noParallel { e = tryRangeLock(g.f, slot, exclusive) } if e == nil { if exclusive { r.writer = true } else { r.readers++ } g.mu.Unlock() return nil } if !lockBusy(e) { g.mu.Unlock() return e } } g.mu.Unlock() timer := time.NewTimer(time.Millisecond) select { case <-ctx.Done(): timer.Stop() return ctx.Err() case <-timer.C: } } } func (g *guard) unlock(slot int) error { g.mu.Lock() defer g.mu.Unlock() r := &g.ranges[slot] if r.writer { r.writer = false } else { r.readers-- } if r.readers > 0 || g.noParallel { return nil } return unlockRange(g.f, slot) } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |