| // Copyright 2013 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| // +build darwin dragonfly freebsd linux netbsd openbsd |
| // FileMutex is similar to sync.RWMutex, but also synchronizes across processes. |
| // This implementation is based on flock syscall. |
| func MakeFileMutex(filename string) *FileMutex { |
| return &FileMutex{fd: -1} |
| fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY, mkdirPerm) |
| return &FileMutex{fd: fd} |
| func (m *FileMutex) Lock() { |
| if err := syscall.Flock(m.fd, syscall.LOCK_EX); err != nil { |
| func (m *FileMutex) Unlock() { |
| if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil { |
| func (m *FileMutex) RLock() { |
| if err := syscall.Flock(m.fd, syscall.LOCK_SH); err != nil { |
| func (m *FileMutex) RUnlock() { |
| if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil { |