本文整理匯總了Golang中sync.Locker類的典型用法代碼示例。如果您正苦於以下問題:Golang Locker類的具體用法?Golang Locker怎麽用?Golang Locker使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Locker類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Watch
func (d *deadlockDetector) Watch(name string, mut sync.Locker) {
d.lockers[name] = mut
go func() {
for {
time.Sleep(d.timeout / 4)
ok := make(chan bool, 2)
go func() {
mut.Lock()
mut.Unlock()
ok <- true
}()
go func() {
time.Sleep(d.timeout)
ok <- false
}()
if r := <-ok; !r {
msg := fmt.Sprintf("deadlock detected at %s", name)
for otherName, otherMut := range d.lockers {
if otherHolder, ok := otherMut.(Holder); ok {
holder, goid := otherHolder.Holder()
msg += fmt.Sprintf("\n %s = current holder: %s at routine %d", otherName, holder, goid)
}
}
panic(msg)
}
}
}()
}
示例2: Watch
func (d *deadlockDetector) Watch(name string, mut sync.Locker) {
d.lockers[name] = mut
go func() {
for {
time.Sleep(d.timeout / 4)
ok := make(chan bool, 2)
go func() {
mut.Lock()
_ = 1 // empty critical section
mut.Unlock()
ok <- true
}()
go func() {
time.Sleep(d.timeout)
ok <- false
}()
if r := <-ok; !r {
msg := fmt.Sprintf("deadlock detected at %s", name)
for otherName, otherMut := range d.lockers {
if otherHolder, ok := otherMut.(Holdable); ok {
msg += "\n===" + otherName + "===\n" + otherHolder.Holders()
}
}
panic(msg)
}
}
}()
}
示例3: ChromeProcessesCleaner
// Running benchmarks in parallel leads to multiple chrome instances coming up
// at the same time, when there are crashes chrome processes stick around which
// can severely impact the machine's performance. To stop this from
// happening chrome zombie processes are periodically killed.
func ChromeProcessesCleaner(locker sync.Locker, chromeCleanerTimer time.Duration) {
for _ = range time.Tick(chromeCleanerTimer) {
glog.Info("The chromeProcessesCleaner goroutine has started")
glog.Info("Waiting for all existing tasks to complete before killing zombie chrome processes")
locker.Lock()
util.LogErr(ExecuteCmd("pkill", []string{"-9", "chrome"}, []string{}, PKILL_TIMEOUT, nil, nil))
locker.Unlock()
}
}
示例4: lock
// if this value is a Locker, lock it and add it to the locks slice
func (w *walker) lock(v reflect.Value) {
if !w.useLocks {
return
}
if !v.IsValid() || !v.CanInterface() {
return
}
type rlocker interface {
RLocker() sync.Locker
}
var locker sync.Locker
// We can't call Interface() on a value directly, since that requires
// a copy. This is OK, since the pointer to a value which is a sync.Locker
// is also a sync.Locker.
if v.Kind() == reflect.Ptr {
switch l := v.Interface().(type) {
case rlocker:
// don't lock a mutex directly
if _, ok := l.(*sync.RWMutex); !ok {
locker = l.RLocker()
}
case sync.Locker:
locker = l
}
} else if v.CanAddr() {
switch l := v.Addr().Interface().(type) {
case rlocker:
// don't lock a mutex directly
if _, ok := l.(*sync.RWMutex); !ok {
locker = l.RLocker()
}
case sync.Locker:
locker = l
}
}
// still no callable locker
if locker == nil {
return
}
// don't lock a mutex directly
switch locker.(type) {
case *sync.Mutex, *sync.RWMutex:
return
}
locker.Lock()
w.locks[w.depth] = locker
}
示例5: sleepWhile
func sleepWhile(l sync.Locker, cond func() bool) {
for {
l.Lock()
val := cond()
l.Unlock()
if !val {
break
}
time.Sleep(time.Millisecond)
}
}
示例6: WaitEvents
func WaitEvents(l sync.Locker, evs ...*Event) {
cases := make([]reflect.SelectCase, 0, len(evs))
for _, ev := range evs {
cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ev.C()),
})
}
l.Unlock()
reflect.Select(cases)
l.Lock()
}
示例7: lock
// if this value is a Locker, lock it and add it to the locks slice
func (w *walker) lock(v reflect.Value) {
if !w.useLocks {
return
}
if !v.IsValid() || !v.CanInterface() {
return
}
type rlocker interface {
RLocker() sync.Locker
}
var locker sync.Locker
// first check if we can get a locker from the value
switch l := v.Interface().(type) {
case rlocker:
// don't lock a mutex directly
if _, ok := l.(*sync.RWMutex); !ok {
locker = l.RLocker()
}
case sync.Locker:
locker = l
}
// the value itself isn't a locker, so check the method on a pointer too
if locker == nil && v.CanAddr() {
switch l := v.Addr().Interface().(type) {
case rlocker:
// don't lock a mutex directly
if _, ok := l.(*sync.RWMutex); !ok {
locker = l.RLocker()
}
case sync.Locker:
locker = l
}
}
// still no callable locker
if locker == nil {
return
}
// don't lock a mutex directly
switch locker.(type) {
case *sync.Mutex, *sync.RWMutex:
return
}
locker.Lock()
w.locks[w.depth] = locker
}
示例8: process
// run kernel on inputs, produce outputs
func (b *Block) process() Interrupt {
b.Monitor <- MonitorMessage{
BI_KERNEL,
nil,
}
if b.state.Processed == true {
return nil
}
// block until connected to source if necessary
if b.sourceType != NONE && b.routing.Source == nil {
select {
case f := <-b.routing.InterruptChan:
return f
}
}
// we should only be able to get here if
// - we don't need an shared state
// - we have an external shared state and it has been attached
// if we have a store, lock it
var store sync.Locker
var ok bool
if store, ok = b.routing.Source.(sync.Locker); ok {
store.Lock()
}
// run the kernel
interrupt := b.kernel(b.state.inputValues,
b.state.outputValues,
b.state.internalValues,
b.routing.Source,
b.routing.InterruptChan)
// unlock the store if necessary
if store != nil {
store.Unlock()
}
// if an interrupt was receieved, return it
if interrupt != nil {
return interrupt
}
b.state.Processed = true
return nil
}
示例9: New
func New(obj interface{}, path string, codec Codec, locker sync.Locker) (*File, error) {
// check object
if reflect.TypeOf(obj).Kind() != reflect.Ptr {
return nil, makeErr(nil, "object must be a pointer")
}
// init
file := &File{
obj: obj,
path: path,
codec: codec,
locker: locker,
cbs: make(chan func()),
}
// try lock
done := make(chan struct{})
go func() {
locker.Lock()
close(done)
}()
select {
case <-time.NewTimer(time.Second * 1).C:
return nil, makeErr(nil, "lock fail")
case <-done:
}
// try load from file
dbFile, err := os.Open(path)
if err == nil {
defer dbFile.Close()
err = codec.Decode(dbFile, obj)
if err != nil {
return nil, makeErr(err, "decode error")
}
}
// loop
go func() {
for {
cb, ok := <-file.cbs
if !ok {
return
}
cb()
}
}()
return file, nil
}
示例10: RunHostBenchmark
func RunHostBenchmark(
ctx *common.Context,
inputManager *common.InputManager,
sandbox Sandbox,
ioLock sync.Locker,
) (BenchmarkResults, error) {
ioLock.Lock()
defer ioLock.Unlock()
ctx.Log.Info("Running benchmark")
benchmarkResults := make(BenchmarkResults)
for idx, benchmarkCase := range cases {
input, err := inputManager.Add(
benchmarkCase.hash,
NewRunnerTarInputFactory(
&ctx.Config,
benchmarkCase.hash,
&benchmarkCase,
),
)
if err != nil {
return nil, err
}
defer input.Release(input)
run := common.Run{
AttemptID: uint64(idx),
Source: benchmarkCase.source,
Language: benchmarkCase.language,
InputHash: benchmarkCase.hash,
MaxScore: 1.0,
Debug: false,
}
results, err := Grade(ctx, nil, &run, input, sandbox)
if err != nil {
return nil, err
}
benchmarkResults[benchmarkCase.name] = BenchmarkResult{
Time: results.Time,
WallTime: results.WallTime,
Memory: results.Memory,
}
}
return benchmarkResults, nil
}
示例11: TestRLocker
func TestRLocker(t *testing.T) {
var wl RWMutex
var rl sync.Locker
wlocked := make(chan bool, 1)
rlocked := make(chan bool, 1)
rl = wl.RLocker()
n := 10
go func() {
for i := 0; i < n; i++ {
rl.Lock()
rl.Lock()
rlocked <- true
wl.Lock()
wlocked <- true
}
}()
for i := 0; i < n; i++ {
<-rlocked
rl.Unlock()
select {
case <-wlocked:
t.Fatal("RLocker() didn't read-lock it")
default:
}
rl.Unlock()
<-wlocked
select {
case <-rlocked:
t.Fatal("RLocker() didn't respect the write lock")
default:
}
wl.Unlock()
}
}
示例12: Open
// Open returns a LockedHandle if the open was permitted, holding either the
// read or the write lock, depending on the opening mode.
func (f *LockedFile) Open(user string, mode qp.OpenMode) (ReadWriteAtCloser, error) {
of, err := f.File.Open(user, mode)
if err != nil {
return of, err
}
write := mode&3 == qp.OWRITE || mode&3 == qp.ORDWR
var l sync.Locker
if write {
l = &f.OpenLock
} else {
l = f.OpenLock.RLocker()
}
l.Lock()
return &LockedHandle{
ReadWriteAtCloser: of,
Locker: l,
}, nil
}
示例13: heartbeatStart
func heartbeatStart(job *Job, done chan bool, heartbeat int, l sync.Locker) {
tick := time.NewTicker(time.Duration(heartbeat) * time.Duration(time.Second))
for {
select {
case <-done:
tick.Stop()
return
case <-tick.C:
l.Lock()
success, err := job.HeartbeatWithNoData()
l.Unlock()
if err != nil {
log.Printf("failed HeartbeatWithNoData jid:%v, queue:%v, success:%v, error:%v",
job.Jid, job.Queue, success, err)
} else {
log.Printf("warning, slow, HeartbeatWithNoData jid:%v, queue:%v, success:%v",
job.Jid, job.Queue, success)
}
}
}
}
示例14: GetE
func (l *LockedOrca) GetE(req common.GetRequest) error {
// Lock for each read key, complete the read, and then move on.
// The last key sent through should have a noop at the end to complete the
// whole interaction between the client and this server.
var ret error
var lock sync.Locker
// guarantee that an operation that failed with a panic will unlock its lock
defer func() {
if r := recover(); r != nil {
if lock != nil {
lock.Unlock()
}
panic(r)
}
}()
for idx, key := range req.Keys {
// Acquire read lock (true == read)
lock = l.getlock(key, true)
lock.Lock()
// The last request will have these set to complete the interaction
noopOpaque := uint32(0)
noopEnd := false
if idx == len(req.Keys)-1 {
noopOpaque = req.NoopOpaque
noopEnd = req.NoopEnd
}
subreq := common.GetRequest{
Keys: [][]byte{key},
Opaques: []uint32{req.Opaques[idx]},
Quiet: []bool{req.Quiet[idx]},
NoopOpaque: noopOpaque,
NoopEnd: noopEnd,
}
// Make the actual request
ret = l.wrapped.GetE(subreq)
// release read lock
lock.Unlock()
// Bail out early if there was an error (misses are not errors in this sense)
// This will probably end up breaking the connection anyway, so no worries
// about leaving the gets half-done.
if ret != nil {
break
}
}
return ret
}
示例15: deadlockDetect
func deadlockDetect(mut sync.Locker, timeout time.Duration) {
go func() {
for {
time.Sleep(timeout / 4)
ok := make(chan bool, 2)
go func() {
mut.Lock()
mut.Unlock()
ok <- true
}()
go func() {
time.Sleep(timeout)
ok <- false
}()
if r := <-ok; !r {
panic("deadlock detected")
}
}
}()
}