• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CORE_LIB_IOMGR_LOCKFREE_EVENT_H
20 #define GRPC_CORE_LIB_IOMGR_LOCKFREE_EVENT_H
21 
22 /* Lock free event notification for file descriptors */
23 
24 #include <grpc/support/port_platform.h>
25 
26 #include <grpc/support/atm.h>
27 
28 #include "src/core/lib/iomgr/closure.h"
29 
30 namespace grpc_core {
31 
32 class LockfreeEvent {
33  public:
34   LockfreeEvent();
35 
36   LockfreeEvent(const LockfreeEvent&) = delete;
37   LockfreeEvent& operator=(const LockfreeEvent&) = delete;
38 
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.
42   void InitEvent();
43   void DestroyEvent();
44 
45   // Returns true if fd has been shutdown, false otherwise.
IsShutdown()46   bool IsShutdown() const {
47     return (gpr_atm_no_barrier_load(&state_) & kShutdownBit) != 0;
48   }
49 
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);
56 
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);
60 
61   // Signals that the event has been received.
62   void SetReady();
63 
64  private:
65   enum State { kClosureNotReady = 0, kClosureReady = 2, kShutdownBit = 1 };
66 
67   gpr_atm state_;
68 };
69 
70 }  // namespace grpc_core
71 
72 #endif /* GRPC_CORE_LIB_IOMGR_LOCKFREE_EVENT_H */
73