| OLD | NEW |
| 1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Fletch 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 #if defined(FLETCH_TARGET_OS_MACOS) | 5 #if defined(FLETCH_TARGET_OS_MACOS) |
| 6 | 6 |
| 7 #include "src/vm/event_handler.h" | 7 #include "src/vm/event_handler.h" |
| 8 | 8 |
| 9 #include <sys/event.h> | 9 #include <sys/event.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <time.h> |
| 11 #include <unistd.h> | 12 #include <unistd.h> |
| 12 | 13 |
| 13 #include "src/vm/thread.h" | 14 #include "src/vm/thread.h" |
| 14 | 15 |
| 15 namespace fletch { | 16 namespace fletch { |
| 16 | 17 |
| 17 int EventHandler::Create() { | 18 int EventHandler::Create() { |
| 18 return kqueue(); | 19 return kqueue(); |
| 19 } | 20 } |
| 20 | 21 |
| 21 void EventHandler::Run() { | 22 void EventHandler::Run() { |
| 22 struct kevent event = {}; | 23 struct kevent event = {}; |
| 23 event.ident = read_fd_; | 24 event.ident = read_fd_; |
| 24 event.flags = EV_ADD; | 25 event.flags = EV_ADD; |
| 25 event.filter = EVFILT_READ; | 26 event.filter = EVFILT_READ; |
| 26 kevent(fd_, &event, 1, NULL, 0, NULL); | 27 kevent(fd_, &event, 1, NULL, 0, NULL); |
| 27 while (true) { | 28 while (true) { |
| 28 int status = kevent(fd_, NULL, 0, &event, 1, NULL); | 29 int64 next_timeout; |
| 30 { |
| 31 ScopedMonitorLock locker(monitor_); |
| 32 next_timeout = next_timeout_; |
| 33 } |
| 34 |
| 35 timespec ts; |
| 36 timespec* interval = NULL; |
| 37 |
| 38 if (next_timeout != INT64_MAX) { |
| 39 next_timeout -= Platform::GetMicroseconds() / 1000; |
| 40 if (next_timeout < 0) next_timeout = 0; |
| 41 ts.tv_sec = next_timeout / 1000; |
| 42 ts.tv_nsec = (next_timeout % 1000) * 1000000; |
| 43 interval = &ts; |
| 44 } |
| 45 |
| 46 int status = kevent(fd_, NULL, 0, &event, 1, interval); |
| 47 |
| 48 HandleTimeouts(); |
| 49 |
| 29 if (status != 1) continue; | 50 if (status != 1) continue; |
| 30 | 51 |
| 31 if (event.ident == static_cast<uintptr_t>(read_fd_)) { | |
| 32 close(read_fd_); | |
| 33 close(fd_); | |
| 34 ScopedMonitorLock locker(monitor_); | |
| 35 fd_ = -1; | |
| 36 monitor_->Notify(); | |
| 37 return; | |
| 38 } | |
| 39 | |
| 40 int filter = event.filter; | 52 int filter = event.filter; |
| 41 int flags = event.flags; | 53 int flags = event.flags; |
| 42 int fflags = event.fflags; | 54 int fflags = event.fflags; |
| 55 |
| 56 if (event.ident == static_cast<uintptr_t>(read_fd_)) { |
| 57 if ((flags & EV_EOF) == 0) { |
| 58 char b; |
| 59 read(read_fd_, &b, 1); |
| 60 continue; |
| 61 } else { |
| 62 close(read_fd_); |
| 63 close(fd_); |
| 64 ScopedMonitorLock locker(monitor_); |
| 65 fd_ = -1; |
| 66 monitor_->Notify(); |
| 67 return; |
| 68 } |
| 69 } |
| 70 |
| 43 word mask = 0; | 71 word mask = 0; |
| 44 if (filter == EVFILT_READ) { | 72 if (filter == EVFILT_READ) { |
| 45 mask = READ_EVENT; | 73 mask = READ_EVENT; |
| 46 if ((flags & EV_EOF) != 0) { | 74 if ((flags & EV_EOF) != 0) { |
| 47 if (fflags != 0) { | 75 if (fflags != 0) { |
| 48 mask = ERROR_EVENT; | 76 mask = ERROR_EVENT; |
| 49 } else { | 77 } else { |
| 50 mask |= CLOSE_EVENT; | 78 mask |= CLOSE_EVENT; |
| 51 } | 79 } |
| 52 } | 80 } |
| 53 } else if (filter == EVFILT_WRITE) { | 81 } else if (filter == EVFILT_WRITE) { |
| 54 if ((flags & EV_EOF) != 0 && fflags != 0) { | 82 if ((flags & EV_EOF) != 0 && fflags != 0) { |
| 55 mask = ERROR_EVENT; | 83 mask = ERROR_EVENT; |
| 56 } else { | 84 } else { |
| 57 mask = WRITE_EVENT; | 85 mask = WRITE_EVENT; |
| 58 } | 86 } |
| 59 } | 87 } |
| 60 | 88 |
| 61 Port* port = reinterpret_cast<Port*>(event.udata); | 89 Port* port = reinterpret_cast<Port*>(event.udata); |
| 62 Send(port, mask); | 90 Send(port, mask); |
| 63 } | 91 } |
| 64 } | 92 } |
| 65 | 93 |
| 66 } // namespace fletch | 94 } // namespace fletch |
| 67 | 95 |
| 68 #endif // defined(FLETCH_TARGET_OS_MACOS) | 96 #endif // defined(FLETCH_TARGET_OS_MACOS) |
| OLD | NEW |