当前位置: 首页>>代码示例>>Golang>>正文


Golang sync.Locker类代码示例

本文整理汇总了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)
			}
		}
	}()
}
开发者ID:brgmnn,项目名称:syncthing,代码行数:31,代码来源:util.go

示例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)
			}
		}
	}()
}
开发者ID:nrm21,项目名称:syncthing,代码行数:31,代码来源:util.go

示例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()
	}
}
开发者ID:saltmueller,项目名称:skia-buildbot,代码行数:13,代码来源:util.go

示例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
}
开发者ID:chrishoffman,项目名称:vault,代码行数:55,代码来源:copystructure.go

示例5: sleepWhile

func sleepWhile(l sync.Locker, cond func() bool) {
	for {
		l.Lock()
		val := cond()
		l.Unlock()
		if !val {
			break
		}
		time.Sleep(time.Millisecond)
	}
}
开发者ID:CaptainIlu,项目名称:cloud-torrent,代码行数:11,代码来源:utp_test.go

示例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()
}
开发者ID:skycoin,项目名称:skycoin,代码行数:12,代码来源:wait_event.go

示例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
}
开发者ID:richardbowden,项目名称:terraform,代码行数:54,代码来源:copystructure.go

示例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
}
开发者ID:nytlabs,项目名称:st-core,代码行数:52,代码来源:block.go

示例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
}
开发者ID:reusee,项目名称:dsfile,代码行数:50,代码来源:dsfile.go

示例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
}
开发者ID:lhchavez,项目名称:quark,代码行数:48,代码来源:benchmark.go

示例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()
	}
}
开发者ID:julienschmidt,项目名称:spinlock,代码行数:34,代码来源:rwmutex_test.go

示例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
}
开发者ID:joushou,项目名称:qptools,代码行数:23,代码来源:lockedfile.go

示例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)
			}
		}
	}
}
开发者ID:ngaut,项目名称:goqless,代码行数:21,代码来源:worker.go

示例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
}
开发者ID:Netflix,项目名称:rend,代码行数:55,代码来源:locked.go

示例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")
			}
		}
	}()
}
开发者ID:ericcapricorn,项目名称:syncthing,代码行数:23,代码来源:util.go


注:本文中的sync.Locker类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。