~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/sync/once.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 
5
5
package sync
6
6
 
 
7
import (
 
8
        "sync/atomic"
 
9
)
 
10
 
7
11
// Once is an object that will perform exactly one action.
8
12
type Once struct {
9
13
        m    Mutex
10
 
        done bool
 
14
        done int32
11
15
}
12
16
 
13
17
// Do calls the function f if and only if the method is being called for the
26
30
// Do to be called, it will deadlock.
27
31
//
28
32
func (o *Once) Do(f func()) {
 
33
        if atomic.AddInt32(&o.done, 0) == 1 {
 
34
                return
 
35
        }
 
36
        // Slow-path.
29
37
        o.m.Lock()
30
38
        defer o.m.Unlock()
31
 
        if !o.done {
32
 
                o.done = true
 
39
        if o.done == 0 {
33
40
                f()
 
41
                atomic.CompareAndSwapInt32(&o.done, 0, 1)
34
42
        }
35
43
}