3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_IOMGR_LOCKFREE_EVENT_H
20 #define GRPC_CORE_LIB_IOMGR_LOCKFREE_EVENT_H
22 /* Lock free event notification for file descriptors */
24 #include <grpc/support/port_platform.h>
26 #include <grpc/support/atm.h>
28 #include "src/core/lib/iomgr/closure.h"
36 LockfreeEvent(const LockfreeEvent&) = delete;
37 LockfreeEvent& operator=(const LockfreeEvent&) = delete;
39 // These methods are used to initialize and destroy the internal state. These
40 // cannot be done in constructor and destructor because SetReady may be called
41 // when the event is destroyed and put in a freelist.
45 // Returns true if fd has been shutdown, false otherwise.
46 bool IsShutdown() const {
47 return (gpr_atm_no_barrier_load(&state_) & kShutdownBit) != 0;
50 // Schedules \a closure when the event is received (see SetReady()) or the
51 // shutdown state has been set. Note that the event may have already been
52 // received, in which case the closure would be scheduled immediately.
53 // If the shutdown state has already been set, then \a closure is scheduled
54 // with the shutdown error.
55 void NotifyOn(grpc_closure* closure);
57 // Sets the shutdown state. If a closure had been provided by NotifyOn and has
58 // not yet been scheduled, it will be scheduled with \a error.
59 bool SetShutdown(grpc_error* error);
61 // Signals that the event has been received.
65 enum State { kClosureNotReady = 0, kClosureReady = 2, kShutdownBit = 1 };
70 } // namespace grpc_core
72 #endif /* GRPC_CORE_LIB_IOMGR_LOCKFREE_EVENT_H */