Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: sync: sync.Once.Do return done #53485

Closed
eNV25 opened this issue Jun 21, 2022 · 7 comments
Closed

proposal: sync: sync.Once.Do return done #53485

eNV25 opened this issue Jun 21, 2022 · 7 comments

Comments

@eNV25
Copy link

eNV25 commented Jun 21, 2022

Issue

It would be useful to know whether a function protected by sync.Once has been run or not.

Example:

var once sync.Once
var timer *time.Timer

func reset(d time.Duration) {
	done := false
	once.Do(func() {
		timer = time.AfterFunc(d, func() { ... })
		done = true
	})
	if !done {
		timer.Reset(d)
	}
}

Proposal

The above code relies on making new variable and setting it in the closure. It could be more cleanly expressed if sync.Once.Do return true or false depending on whether the function has run.

var once sync.Once
var timer *time.Timer

func reset(d time.Duration) {
	if !once.Do(func() {
		timer = time.AfterFunc(d, func() { ... })
	}) {
		timer.Reset(d)
	}
}
package sync
...
func (o *Once) Do(f func()) bool {
	if atomic.LoadUint32(&o.done) == 0 {
		o.doSlow(f)
		return true //
	}
	return false //
}
...
@eNV25 eNV25 added the Proposal label Jun 21, 2022
@gopherbot gopherbot added this to the Proposal milestone Jun 21, 2022
@thediveo
Copy link

thediveo commented Jun 21, 2022

just curious (and for clarification): since sync.Once.Do must be concurrency safe what would be your expectation of the returned value(s) when a second goroutine g2 calls sync.Once.Do while a first goroutine g1 was the first one ever and thus now executes the Do function? Should g2 get a return value of true too, or false? (it won't return until g1 has finished the Do(func) anyway.)

@seankhliao
Copy link
Member

Duplicate of #41690

@seankhliao seankhliao marked this as a duplicate of #41690 Jun 21, 2022
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2022
@eNV25
Copy link
Author

eNV25 commented Jun 21, 2022

@seankhliao that's not the same issue.

@eNV25
Copy link
Author

eNV25 commented Jun 21, 2022

False duplicate.

@seankhliao
Copy link
Member

it's just a slightly different api
the reasoning for declining that one also applied here

@eNV25
Copy link
Author

eNV25 commented Jun 21, 2022

Issue #41690 is adding a new method. This is for adding a new return value for an existing method.

I have read the reason why that issues was closed and it doesn't apply. In this case running the function and getting the return value are the same action.

Please read @robpike's comment again.
#41690 (comment)

@ianlancetaylor
Copy link
Contributor

I agree that this is slightly different, but we still aren't going to do it. The Go 1 compatibility guarantee does not permit us to add a result type to the Do method. We aren't going to add a new method for a very niche operation that can already be done in a more complicated way. Sorry.

@golang golang locked and limited conversation to collaborators Jun 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants