OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef BIN_EVENTHANDLER_LINUX_H_ | 5 #ifndef BIN_EVENTHANDLER_LINUX_H_ |
6 #define BIN_EVENTHANDLER_LINUX_H_ | 6 #define BIN_EVENTHANDLER_LINUX_H_ |
7 | 7 |
8 #if !defined(BIN_EVENTHANDLER_H_) | 8 #if !defined(BIN_EVENTHANDLER_H_) |
9 #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. | 9 #error Do not include eventhandler_linux.h directly; use eventhandler.h instead. |
10 #endif | 10 #endif |
11 | 11 |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 #include <sys/epoll.h> | 13 #include <sys/epoll.h> |
14 #include <sys/socket.h> | 14 #include <sys/socket.h> |
15 | 15 |
16 #include "platform/hashmap.h" | 16 #include "platform/hashmap.h" |
17 #include "platform/thread.h" | |
18 | 17 |
19 | 18 |
20 namespace dart { | 19 namespace dart { |
21 namespace bin { | 20 namespace bin { |
22 | 21 |
| 22 class InterruptMessage { |
| 23 public: |
| 24 intptr_t id; |
| 25 Dart_Port dart_port; |
| 26 int64_t data; |
| 27 }; |
| 28 |
| 29 |
| 30 class SocketData { |
| 31 public: |
| 32 explicit SocketData(intptr_t fd) : fd_(fd), port_(0), mask_(0), tokens_(8) { |
| 33 ASSERT(fd_ != -1); |
| 34 } |
| 35 |
| 36 intptr_t GetPollEvents(); |
| 37 |
| 38 void Close() { |
| 39 port_ = 0; |
| 40 mask_ = 0; |
| 41 close(fd_); |
| 42 fd_ = -1; |
| 43 } |
| 44 |
| 45 void SetPortAndMask(Dart_Port port, intptr_t mask) { |
| 46 ASSERT(fd_ != -1); |
| 47 port_ = port; |
| 48 mask_ = mask; |
| 49 } |
| 50 |
| 51 intptr_t fd() { return fd_; } |
| 52 Dart_Port port() { return port_; } |
| 53 |
| 54 // Returns true if the last token was taken. |
| 55 bool TakeToken() { |
| 56 ASSERT(tokens_ > 0); |
| 57 tokens_--; |
| 58 return tokens_ == 0; |
| 59 } |
| 60 |
| 61 // Returns true if the tokens was 0 before adding. |
| 62 bool ReturnToken() { |
| 63 ASSERT(tokens_ >= 0); |
| 64 tokens_++; |
| 65 return tokens_ == 1; |
| 66 } |
| 67 |
| 68 private: |
| 69 intptr_t fd_; |
| 70 Dart_Port port_; |
| 71 intptr_t mask_; |
| 72 int tokens_; |
| 73 }; |
| 74 |
| 75 |
23 class EventHandlerImplementation { | 76 class EventHandlerImplementation { |
24 public: | 77 public: |
25 EventHandlerImplementation(); | 78 EventHandlerImplementation(); |
26 ~EventHandlerImplementation(); | 79 ~EventHandlerImplementation(); |
27 | 80 |
28 void Notify(intptr_t id, Dart_Port dart_port, int64_t data); | 81 // Gets the socket data structure for a given file |
| 82 // descriptor. Creates a new one if one is not found. |
| 83 SocketData* GetSocketData(intptr_t fd); |
| 84 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
29 void Start(EventHandler* handler); | 85 void Start(EventHandler* handler); |
30 void Shutdown(); | 86 void Shutdown(); |
31 | 87 |
32 private: | 88 private: |
33 void HandleEvents(struct epoll_event* events, int size); | 89 void HandleEvents(struct epoll_event* events, int size); |
34 static void Poll(uword args); | 90 static void Poll(uword args); |
| 91 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
| 92 void HandleInterruptFd(); |
35 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); | 93 void SetPort(intptr_t fd, Dart_Port dart_port, intptr_t mask); |
36 intptr_t GetPollEvents(intptr_t events); | 94 intptr_t GetPollEvents(intptr_t events, SocketData* sd); |
| 95 static void* GetHashmapKeyFromFd(intptr_t fd); |
| 96 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
37 | 97 |
| 98 HashMap socket_map_; |
38 TimeoutQueue timeout_queue_; | 99 TimeoutQueue timeout_queue_; |
39 bool shutdown_; | 100 bool shutdown_; |
| 101 int interrupt_fds_[2]; |
40 int epoll_fd_; | 102 int epoll_fd_; |
41 int timer_fd_; | 103 int timer_fd_; |
42 Mutex timer_mutex_; | |
43 }; | 104 }; |
44 | 105 |
45 } // namespace bin | 106 } // namespace bin |
46 } // namespace dart | 107 } // namespace dart |
47 | 108 |
48 #endif // BIN_EVENTHANDLER_LINUX_H_ | 109 #endif // BIN_EVENTHANDLER_LINUX_H_ |
OLD | NEW |