Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package utils
  5. import (
  6. "sync"
  7. "sync/atomic"
  8. )
  9. // Once is a fork of sync.Once to expose a Done() method.
  10. type Once struct {
  11. done uint32
  12. m sync.Mutex
  13. }
  14. func (o *Once) Do(f func()) {
  15. if atomic.LoadUint32(&o.done) == 0 {
  16. o.doSlow(f)
  17. }
  18. }
  19. func (o *Once) doSlow(f func()) {
  20. o.m.Lock()
  21. defer o.m.Unlock()
  22. if o.done == 0 {
  23. defer atomic.StoreUint32(&o.done, 1)
  24. f()
  25. }
  26. }
  27. func (o *Once) Done() bool {
  28. return atomic.LoadUint32(&o.done) == 1
  29. }