From c52a08276ff622ea24536cd44e707de63393f92c Mon Sep 17 00:00:00 2001 From: q191201771 <191201771@qq.com> Date: Mon, 22 Aug 2022 20:17:28 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20nazasync:=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=AB=9E=E4=BA=89=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=E9=9D=9E=E9=98=BB=E5=A1=9E=E7=9A=84Once?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/nazasync/once.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 pkg/nazasync/once.go diff --git a/pkg/nazasync/once.go b/pkg/nazasync/once.go new file mode 100644 index 0000000..db8a442 --- /dev/null +++ b/pkg/nazasync/once.go @@ -0,0 +1,32 @@ +// Copyright 2022, Chef. All rights reserved. +// https://github.com/q191201771/naza +// +// Use of this source code is governed by a MIT-style license +// that can be found in the License file. +// +// Author: Chef (191201771@qq.com) + +package nazasync + +import ( + "sync" + "sync/atomic" +) + +type StdOnce struct { + core sync.Once +} + +func (o *StdOnce) Do(f func()) { + o.core.Do(f) +} + +type NonblockingOnce struct { + done uint32 +} + +func (o *NonblockingOnce) Do(f func()) { + if atomic.CompareAndSwapUint32(&o.done, 0, 1) { + f() + } +}