OLD | NEW |
| (Empty) |
1 // Copyright 2007-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // reactor.{h, cc} implements the reactor design pattern. | |
17 // | |
18 // For each signaled handle the reactor calls back the event handler only once. | |
19 // To get further notifications, the handle must be registered again with the | |
20 // reactor. This may sound like a lot of work but this is the only way to | |
21 // guarantee that the event handler is only called once. | |
22 // UnregisterHandle can't be called when handling a callback or it results in | |
23 // a deadlock. When handling a callback, the only possible operation is | |
24 // registering back a handle using Register. | |
25 | |
26 #ifndef OMAHA_COMMON_REACTOR_H__ | |
27 #define OMAHA_COMMON_REACTOR_H__ | |
28 | |
29 #include <windows.h> | |
30 #include <vector> | |
31 #include "base/basictypes.h" | |
32 | |
33 namespace omaha { | |
34 | |
35 class EventHandler; | |
36 | |
37 class Reactor { | |
38 public: | |
39 Reactor(); | |
40 ~Reactor(); | |
41 | |
42 // Starts demultiplexing and dispatching events. | |
43 HRESULT HandleEvents(); | |
44 | |
45 // Registers an event handler for a handle. The reactor does not own the | |
46 // handle. Registering the same handle twice results in undefined behavior. | |
47 // The flags parameter can be one of the WT* thread pool values or 0 for | |
48 // a reasonable default. | |
49 HRESULT RegisterHandle(HANDLE handle, | |
50 EventHandler* event_handler, | |
51 uint32 flags); | |
52 | |
53 // Registers the handle again. This method can be called from the callback. | |
54 HRESULT RegisterHandle(HANDLE handle); | |
55 | |
56 // Unregisters the handle. The method blocks and waits for any callback to | |
57 // complete if an event dispatching is in progress. | |
58 HRESULT UnregisterHandle(HANDLE handle); | |
59 | |
60 private: | |
61 struct RegistrationState { | |
62 RegistrationState() | |
63 : reactor(NULL), | |
64 event_handler(NULL), | |
65 handle(NULL), | |
66 wait_handle(NULL), | |
67 flags(0) {} | |
68 | |
69 Reactor* reactor; | |
70 EventHandler* event_handler; | |
71 HANDLE handle; | |
72 HANDLE wait_handle; | |
73 uint32 flags; | |
74 }; | |
75 | |
76 static void __stdcall Callback(void* param, BOOLEAN timer_or_wait); | |
77 void DoCallback(RegistrationState* registration_state); | |
78 | |
79 HRESULT DoRegisterHandle(HANDLE handle); | |
80 | |
81 // Releases the ownership of the registration state corresponding to a handle. | |
82 RegistrationState* ReleaseHandlerState(HANDLE handle); | |
83 | |
84 CRITICAL_SECTION cs_; | |
85 std::vector<RegistrationState*> handlers_; | |
86 | |
87 DISALLOW_EVIL_CONSTRUCTORS(Reactor); | |
88 }; | |
89 | |
90 } // namespace omaha | |
91 | |
92 #endif // OMAHA_COMMON_REACTOR_H__ | |
OLD | NEW |