2008-09-22 17:26:57 -06:00
|
|
|
// 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
|
2009-02-15 23:12:35 -07:00
|
|
|
// once.Do(f)
|
|
|
|
// If multiple processes call once.Do(f) simultaneously
|
2008-09-22 17:26:57 -06:00
|
|
|
// with the same f argument, only one will call f, and the
|
|
|
|
// others will block until f finishes running.
|
|
|
|
|
|
|
|
package once
|
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
import "sync"
|
2008-09-22 17:26:57 -06:00
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
type job struct {
|
|
|
|
done bool;
|
|
|
|
sync.Mutex; // should probably be sync.Notification or some such
|
2008-09-22 17:26:57 -06:00
|
|
|
}
|
|
|
|
|
2009-02-15 23:12:35 -07:00
|
|
|
var jobs = make(map[func()]*job)
|
|
|
|
var joblock sync.Mutex;
|
2008-09-22 17:26:57 -06:00
|
|
|
|
2009-01-30 15:39:31 -07:00
|
|
|
func Do(f func()) {
|
2009-02-15 23:12:35 -07:00
|
|
|
joblock.Lock();
|
|
|
|
j, present := jobs[f];
|
2008-09-22 17:26:57 -06:00
|
|
|
if !present {
|
2009-02-15 23:12:35 -07:00
|
|
|
// run it
|
|
|
|
j = new(job);
|
|
|
|
j.Lock();
|
|
|
|
jobs[f] = j;
|
|
|
|
joblock.Unlock();
|
2008-09-22 17:26:57 -06:00
|
|
|
f();
|
2009-02-15 23:12:35 -07:00
|
|
|
j.done = true;
|
|
|
|
j.Unlock();
|
|
|
|
} else {
|
|
|
|
// wait for it
|
|
|
|
joblock.Unlock();
|
|
|
|
if j.done != true {
|
|
|
|
j.Lock();
|
|
|
|
j.Unlock();
|
|
|
|
}
|
2008-09-22 17:26:57 -06:00
|
|
|
}
|
|
|
|
}
|