OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ | 5 #ifndef RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ |
6 #define RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ | 6 #define RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ |
7 | 7 |
8 #if !defined(RUNTIME_BIN_EVENTHANDLER_H_) | 8 #if !defined(RUNTIME_BIN_EVENTHANDLER_H_) |
9 #error Do not include eventhandler_fuchsia.h directly; use eventhandler.h. | 9 #error Do not include eventhandler_fuchsia.h directly; use eventhandler.h. |
10 #endif | 10 #endif |
11 | 11 |
12 #include <errno.h> | 12 #include <errno.h> |
13 #include <magenta/syscalls.h> | 13 #include <sys/epoll.h> |
| 14 #include <sys/socket.h> |
| 15 #include <unistd.h> |
14 | 16 |
15 #include "platform/signal_blocker.h" | 17 #include "platform/signal_blocker.h" |
16 | 18 |
17 namespace dart { | 19 namespace dart { |
18 namespace bin { | 20 namespace bin { |
19 | 21 |
20 class DescriptorInfo : public DescriptorInfoBase { | 22 class DescriptorInfo : public DescriptorInfoBase { |
21 public: | 23 public: |
22 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) {} | 24 explicit DescriptorInfo(intptr_t fd) : DescriptorInfoBase(fd) {} |
23 | 25 |
24 virtual ~DescriptorInfo() {} | 26 virtual ~DescriptorInfo() {} |
25 | 27 |
| 28 intptr_t GetPollEvents(); |
| 29 |
26 virtual void Close() { | 30 virtual void Close() { |
27 VOID_TEMP_FAILURE_RETRY(close(fd_)); | 31 // Should be VOID_TEMP_FAILURE_RETRY |
| 32 VOID_NO_RETRY_EXPECTED(close(fd_)); |
28 fd_ = -1; | 33 fd_ = -1; |
29 } | 34 } |
30 | 35 |
31 private: | 36 private: |
32 DISALLOW_COPY_AND_ASSIGN(DescriptorInfo); | 37 DISALLOW_COPY_AND_ASSIGN(DescriptorInfo); |
33 }; | 38 }; |
34 | 39 |
35 class DescriptorInfoSingle : public DescriptorInfoSingleMixin<DescriptorInfo> { | 40 class DescriptorInfoSingle : public DescriptorInfoSingleMixin<DescriptorInfo> { |
36 public: | 41 public: |
37 explicit DescriptorInfoSingle(intptr_t fd) | 42 explicit DescriptorInfoSingle(intptr_t fd) |
38 : DescriptorInfoSingleMixin(fd, false) {} | 43 : DescriptorInfoSingleMixin(fd, false) {} |
39 virtual ~DescriptorInfoSingle() {} | 44 virtual ~DescriptorInfoSingle() {} |
40 | 45 |
41 private: | 46 private: |
42 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoSingle); | 47 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoSingle); |
43 }; | 48 }; |
44 | 49 |
45 class DescriptorInfoMultiple | 50 class DescriptorInfoMultiple |
46 : public DescriptorInfoMultipleMixin<DescriptorInfo> { | 51 : public DescriptorInfoMultipleMixin<DescriptorInfo> { |
47 public: | 52 public: |
48 explicit DescriptorInfoMultiple(intptr_t fd) | 53 explicit DescriptorInfoMultiple(intptr_t fd) |
49 : DescriptorInfoMultipleMixin(fd, false) {} | 54 : DescriptorInfoMultipleMixin(fd, false) {} |
50 virtual ~DescriptorInfoMultiple() {} | 55 virtual ~DescriptorInfoMultiple() {} |
51 | 56 |
52 private: | 57 private: |
53 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoMultiple); | 58 DISALLOW_COPY_AND_ASSIGN(DescriptorInfoMultiple); |
54 }; | 59 }; |
55 | 60 |
56 // Information needed to call mx_handle_wait_many(), and to handle events. | |
57 class MagentaWaitManyInfo { | |
58 public: | |
59 MagentaWaitManyInfo(); | |
60 ~MagentaWaitManyInfo(); | |
61 | |
62 intptr_t capacity() const { return capacity_; } | |
63 intptr_t size() const { return size_; } | |
64 DescriptorInfo** descriptor_infos() const { return descriptor_infos_; } | |
65 mx_wait_item_t* items() const { return items_; } | |
66 | |
67 void AddHandle(mx_handle_t handle, mx_signals_t signals, DescriptorInfo* di); | |
68 void RemoveHandle(mx_handle_t handle); | |
69 | |
70 private: | |
71 static const intptr_t kInitialCapacity = 32; | |
72 | |
73 void GrowArraysIfNeeded(intptr_t desired_size); | |
74 | |
75 intptr_t capacity_; | |
76 intptr_t size_; | |
77 DescriptorInfo** descriptor_infos_; | |
78 mx_wait_item_t* items_; | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(MagentaWaitManyInfo); | |
81 }; | |
82 | |
83 class EventHandlerImplementation { | 61 class EventHandlerImplementation { |
84 public: | 62 public: |
85 EventHandlerImplementation(); | 63 EventHandlerImplementation(); |
86 ~EventHandlerImplementation(); | 64 ~EventHandlerImplementation(); |
87 | 65 |
| 66 void UpdateEpollInstance(intptr_t old_mask, DescriptorInfo* di); |
| 67 |
| 68 // Gets the socket data structure for a given file |
| 69 // descriptor. Creates a new one if one is not found. |
| 70 DescriptorInfo* GetDescriptorInfo(intptr_t fd, bool is_listening); |
88 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); | 71 void SendData(intptr_t id, Dart_Port dart_port, int64_t data); |
89 void Start(EventHandler* handler); | 72 void Start(EventHandler* handler); |
90 void Shutdown(); | 73 void Shutdown(); |
91 | 74 |
92 const MagentaWaitManyInfo& info() const { return info_; } | 75 private: |
| 76 static void Poll(uword args); |
| 77 static void* GetHashmapKeyFromFd(intptr_t fd); |
| 78 static uint32_t GetHashmapHashFromFd(intptr_t fd); |
93 | 79 |
94 private: | |
95 int64_t GetTimeout() const; | 80 int64_t GetTimeout() const; |
96 void HandleEvents(); | 81 void HandleEvents(struct epoll_event* events, int size); |
97 void HandleTimeout(); | 82 void HandleTimeout(); |
98 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); | 83 void WakeupHandler(intptr_t id, Dart_Port dart_port, int64_t data); |
| 84 intptr_t GetPollEvents(intptr_t events, DescriptorInfo* di); |
99 void HandleInterruptFd(); | 85 void HandleInterruptFd(); |
100 static void Poll(uword args); | |
101 | 86 |
| 87 HashMap socket_map_; |
102 TimeoutQueue timeout_queue_; | 88 TimeoutQueue timeout_queue_; |
103 bool shutdown_; | 89 bool shutdown_; |
104 mx_handle_t interrupt_handles_[2]; | 90 int interrupt_fds_[2]; |
105 | 91 int epoll_fd_; |
106 MagentaWaitManyInfo info_; | |
107 | 92 |
108 DISALLOW_COPY_AND_ASSIGN(EventHandlerImplementation); | 93 DISALLOW_COPY_AND_ASSIGN(EventHandlerImplementation); |
109 }; | 94 }; |
110 | 95 |
111 } // namespace bin | 96 } // namespace bin |
112 } // namespace dart | 97 } // namespace dart |
113 | 98 |
114 #endif // RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ | 99 #endif // RUNTIME_BIN_EVENTHANDLER_FUCHSIA_H_ |
OLD | NEW |