From d4e6a8d727c24b2f1807f2dbf3d721fe83747a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 18 Aug 2020 11:06:36 +0200 Subject: [PATCH] Implement simple, thread-safe atomic flag type --- atomicflag/atomicflag.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 atomicflag/atomicflag.go diff --git a/atomicflag/atomicflag.go b/atomicflag/atomicflag.go new file mode 100644 index 0000000..6460365 --- /dev/null +++ b/atomicflag/atomicflag.go @@ -0,0 +1,27 @@ +package atomicflag + +import ( + "sync" +) + + +type atomicflag struct { + value bool + mutex sync.RWMutex +} + +func New(value bool) *atomicflag { + return &atomicflag{value: value} +} + +func (f* atomicflag) Get() bool { + f.mutex.RLock() + defer f.mutex.RUnlock() + return f.value +} + +func (f* atomicflag) Set(value bool) { + f.mutex.Lock() + defer f.mutex.Unlock() + f.value = value +}