dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/utils/grpcsync/event.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* 19 * 20 * Copyright 2018 gRPC authors. 21 * 22 */ 23 24 // Package grpcsync implements additional synchronization primitives built upon 25 // the sync package. 26 package grpcsync 27 28 import ( 29 "sync" 30 "sync/atomic" 31 ) 32 33 // Event represents a one-time event that may occur in the future. 34 type Event struct { 35 fired int32 36 c chan struct{} 37 o sync.Once 38 } 39 40 // Fire causes e to complete. It is safe to call multiple times, and 41 // concurrently. It returns true iff this call to Fire caused the signaling 42 // channel returned by Done to close. 43 func (e *Event) Fire() bool { 44 ret := false 45 e.o.Do(func() { 46 atomic.StoreInt32(&e.fired, 1) 47 close(e.c) 48 ret = true 49 }) 50 return ret 51 } 52 53 // Done returns a channel that will be closed when Fire is called. 54 func (e *Event) Done() <-chan struct{} { 55 return e.c 56 } 57 58 // HasFired returns true if Fire has been called. 59 func (e *Event) HasFired() bool { 60 return atomic.LoadInt32(&e.fired) == 1 61 } 62 63 // NewEvent returns a new, ready-to-use Event. 64 func NewEvent() *Event { 65 return &Event{c: make(chan struct{})} 66 }