1
0
mirror of https://github.com/golang/go synced 2024-11-19 05:54:44 -07:00
go/dashboard/builder/filemutex_flock.go
Dmitriy Vyukov d2a9e7164e dashboard: builder changes for performance dashboard
This CL moves code from code.google.com/p/dvyukov-go-perf-dashboard,
which was previously reviewed.

LGTM=adg
R=adg
CC=golang-codereviews
https://golang.org/cl/95190043
2014-05-13 11:00:11 +04:00

67 lines
1.2 KiB
Go

// 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 linux darwin freebsd
package main
import (
"sync"
"syscall"
)
// FileMutex is similar to sync.RWMutex, but also synchronizes across processes.
// This implementation is based on flock syscall.
type FileMutex struct {
mu sync.RWMutex
fd int
}
func MakeFileMutex(filename string) *FileMutex {
if filename == "" {
return &FileMutex{fd: -1}
}
fd, err := syscall.Open(filename, syscall.O_CREAT|syscall.O_RDONLY, mkdirPerm)
if err != nil {
panic(err)
}
return &FileMutex{fd: fd}
}
func (m *FileMutex) Lock() {
m.mu.Lock()
if m.fd != -1 {
if err := syscall.Flock(m.fd, syscall.LOCK_EX); err != nil {
panic(err)
}
}
}
func (m *FileMutex) Unlock() {
if m.fd != -1 {
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil {
panic(err)
}
}
m.mu.Unlock()
}
func (m *FileMutex) RLock() {
m.mu.RLock()
if m.fd != -1 {
if err := syscall.Flock(m.fd, syscall.LOCK_SH); err != nil {
panic(err)
}
}
}
func (m *FileMutex) RUnlock() {
if m.fd != -1 {
if err := syscall.Flock(m.fd, syscall.LOCK_UN); err != nil {
panic(err)
}
}
m.mu.RUnlock()
}