54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
/*
|
|
Copyright 2016 The Rook Authors. All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package installer
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// Once is an object that will perform exactly one action.
|
|
type Once struct {
|
|
m sync.Mutex
|
|
done uint32
|
|
}
|
|
|
|
//simulate sync.Once.Do by executing function only once
|
|
//until rest is called.
|
|
//See http://golang.org/pkg/sync/#Once
|
|
func (o *Once) Do(f func()) {
|
|
if atomic.LoadUint32(&o.done) == 1 {
|
|
return
|
|
}
|
|
// Slow-path.
|
|
o.m.Lock()
|
|
defer o.m.Unlock()
|
|
if o.done == 0 {
|
|
defer atomic.StoreUint32(&o.done, 1)
|
|
f()
|
|
}
|
|
}
|
|
|
|
// Reset indicates that the next call to Do should actually be called
|
|
// once again.
|
|
//See http://golang.org/pkg/sync/#Once
|
|
func (o *Once) Reset() {
|
|
o.m.Lock()
|
|
defer o.m.Unlock()
|
|
atomic.StoreUint32(&o.done, 0)
|
|
}
|