mirror of
https://github.com/golang/go
synced 2024-10-05 06:21:24 -06:00
ff3173849e
- use a lock instead of a thread in once avoids deadlock in recursive once calls - implement os.Setenv - remove "export" from some scripts - remove _ from names in time package - fix time test for non-MTV machines R=r DELTA=265 (87 added, 58 deleted, 120 changed) OCL=25057 CL=25057
45 lines
941 B
Go
45 lines
941 B
Go
// Copyright 2009 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.
|
|
|
|
// For one-time initialization that is not done during init.
|
|
// Wrap the initialization in a niladic function f() and call
|
|
// once.Do(f)
|
|
// If multiple processes call once.Do(f) simultaneously
|
|
// with the same f argument, only one will call f, and the
|
|
// others will block until f finishes running.
|
|
|
|
package once
|
|
|
|
import "sync"
|
|
|
|
type job struct {
|
|
done bool;
|
|
sync.Mutex; // should probably be sync.Notification or some such
|
|
}
|
|
|
|
var jobs = make(map[func()]*job)
|
|
var joblock sync.Mutex;
|
|
|
|
func Do(f func()) {
|
|
joblock.Lock();
|
|
j, present := jobs[f];
|
|
if !present {
|
|
// run it
|
|
j = new(job);
|
|
j.Lock();
|
|
jobs[f] = j;
|
|
joblock.Unlock();
|
|
f();
|
|
j.done = true;
|
|
j.Unlock();
|
|
} else {
|
|
// wait for it
|
|
joblock.Unlock();
|
|
if j.done != true {
|
|
j.Lock();
|
|
j.Unlock();
|
|
}
|
|
}
|
|
}
|