$13 GRAYBYTE WORDPRESS FILE MANAGER $48

SERVER : vnpttt-amd7f72-h1.vietnix.vn #1 SMP Fri May 24 12:42:50 UTC 2024
SERVER IP : 103.200.23.149 | ADMIN IP 216.73.216.22
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/lib/golang/src/sync/

HOME
Current File : /lib/golang/src/sync//oncefunc.go
// Copyright 2022 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.

package sync

// OnceFunc returns a function that invokes f only once. The returned function
// may be called concurrently.
//
// If f panics, the returned function will panic with the same value on every call.
func OnceFunc(f func()) func() {
	// Use a struct so that there's a single heap allocation.
	d := struct {
		f     func()
		once  Once
		valid bool
		p     any
	}{
		f: f,
	}
	return func() {
		d.once.Do(func() {
			defer func() {
				d.f = nil // Do not keep f alive after invoking it.
				d.p = recover()
				if !d.valid {
					// Re-panic immediately so on the first
					// call the user gets a complete stack
					// trace into f.
					panic(d.p)
				}
			}()
			d.f()
			d.valid = true // Set only if f does not panic.
		})
		if !d.valid {
			panic(d.p)
		}
	}
}

// OnceValue returns a function that invokes f only once and returns the value
// returned by f. The returned function may be called concurrently.
//
// If f panics, the returned function will panic with the same value on every call.
func OnceValue[T any](f func() T) func() T {
	// Use a struct so that there's a single heap allocation.
	d := struct {
		f      func() T
		once   Once
		valid  bool
		p      any
		result T
	}{
		f: f,
	}
	return func() T {
		d.once.Do(func() {
			defer func() {
				d.f = nil
				d.p = recover()
				if !d.valid {
					panic(d.p)
				}
			}()
			d.result = d.f()
			d.valid = true
		})
		if !d.valid {
			panic(d.p)
		}
		return d.result
	}
}

// OnceValues returns a function that invokes f only once and returns the values
// returned by f. The returned function may be called concurrently.
//
// If f panics, the returned function will panic with the same value on every call.
func OnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2) {
	// Use a struct so that there's a single heap allocation.
	d := struct {
		f     func() (T1, T2)
		once  Once
		valid bool
		p     any
		r1    T1
		r2    T2
	}{
		f: f,
	}
	return func() (T1, T2) {
		d.once.Do(func() {
			defer func() {
				d.f = nil
				d.p = recover()
				if !d.valid {
					panic(d.p)
				}
			}()
			d.r1, d.r2 = d.f()
			d.valid = true
		})
		if !d.valid {
			panic(d.p)
		}
		return d.r1, d.r2
	}
}

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
16 Dec 2025 9.30 PM
root / root
0755
atomic
--
16 Dec 2025 9.30 PM
root / root
0755
cond.go
4.051 KB
4 Dec 2025 6.06 PM
root / root
0644
hashtriemap.go
4.574 KB
4 Dec 2025 6.06 PM
root / root
0644
map.go
15.639 KB
4 Dec 2025 6.06 PM
root / root
0644
mutex.go
1.948 KB
4 Dec 2025 6.06 PM
root / root
0644
once.go
2.459 KB
4 Dec 2025 6.06 PM
root / root
0644
oncefunc.go
2.288 KB
4 Dec 2025 6.06 PM
root / root
0644
pool.go
9.392 KB
4 Dec 2025 6.06 PM
root / root
0644
poolqueue.go
8.333 KB
4 Dec 2025 6.06 PM
root / root
0644
runtime.go
2.2 KB
4 Dec 2025 6.06 PM
root / root
0644
runtime2.go
0.454 KB
4 Dec 2025 6.06 PM
root / root
0644
runtime2_lockrank.go
0.533 KB
4 Dec 2025 6.06 PM
root / root
0644
rwmutex.go
7.42 KB
4 Dec 2025 6.06 PM
root / root
0644
waitgroup.go
7.407 KB
4 Dec 2025 6.06 PM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF