1 // 2 // 3 // Copyright 2017 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // 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 #ifndef GRPC_SRC_CORE_LIB_IOMGR_LOCKFREE_EVENT_H 20 #define GRPC_SRC_CORE_LIB_IOMGR_LOCKFREE_EVENT_H 21 22 // Lock free event notification for file descriptors 23 24 #include <grpc/support/atm.h> 25 #include <grpc/support/port_platform.h> 26 27 #include "src/core/lib/iomgr/closure.h" 28 29 namespace grpc_core { 30 31 class LockfreeEvent { 32 public: 33 LockfreeEvent(); 34 35 LockfreeEvent(const LockfreeEvent&) = delete; 36 LockfreeEvent& operator=(const LockfreeEvent&) = delete; 37 38 // These methods are used to initialize and destroy the internal state. These 39 // cannot be done in constructor and destructor because SetReady may be called 40 // when the event is destroyed and put in a freelist. 41 void InitEvent(); 42 void DestroyEvent(); 43 44 // Returns true if fd has been shutdown, false otherwise. IsShutdown()45 bool IsShutdown() const { 46 return (gpr_atm_no_barrier_load(&state_) & kShutdownBit) != 0; 47 } 48 49 // Schedules \a closure when the event is received (see SetReady()) or the 50 // shutdown state has been set. Note that the event may have already been 51 // received, in which case the closure would be scheduled immediately. 52 // If the shutdown state has already been set, then \a closure is scheduled 53 // with the shutdown error. 54 void NotifyOn(grpc_closure* closure); 55 56 // Sets the shutdown state. If a closure had been provided by NotifyOn and has 57 // not yet been scheduled, it will be scheduled with \a shutdown_error. 58 bool SetShutdown(grpc_error_handle shutdown_error); 59 60 // Signals that the event has been received. 61 void SetReady(); 62 63 private: 64 enum State { kClosureNotReady = 0, kClosureReady = 2, kShutdownBit = 1 }; 65 66 gpr_atm state_; 67 }; 68 69 } // namespace grpc_core 70 71 #endif // GRPC_SRC_CORE_LIB_IOMGR_LOCKFREE_EVENT_H 72