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 #include "bin/eventhandler.h" | 5 #include "bin/eventhandler.h" |
6 | 6 |
7 #include <process.h> | 7 #include <process.h> |
8 #include <winsock2.h> | 8 #include <winsock2.h> |
9 #include <ws2tcpip.h> | 9 #include <ws2tcpip.h> |
10 #include <mswsock.h> | 10 #include <mswsock.h> |
11 | 11 |
12 #include "bin/builtin.h" | 12 #include "bin/builtin.h" |
13 #include "bin/dartutils.h" | 13 #include "bin/dartutils.h" |
14 #include "bin/socket.h" | 14 #include "bin/socket.h" |
15 #include "platform/thread.h" | 15 #include "platform/thread.h" |
16 | 16 |
17 | 17 |
18 static const int kInfinityTimeout = -1; | 18 static const int kInfinityTimeout = -1; |
| 19 static const int kTimeoutId = -1; |
| 20 static const int kShutdownId = -2; |
19 | 21 |
20 | 22 |
21 int64_t GetCurrentTimeMilliseconds() { | 23 int64_t GetCurrentTimeMilliseconds() { |
22 static const int64_t kTimeEpoc = 116444736000000000LL; | 24 static const int64_t kTimeEpoc = 116444736000000000LL; |
23 | 25 |
24 // Although win32 uses 64-bit integers for representing timestamps, | 26 // Although win32 uses 64-bit integers for representing timestamps, |
25 // these are packed into a FILETIME structure. The FILETIME structure | 27 // these are packed into a FILETIME structure. The FILETIME structure |
26 // is just a struct representing a 64-bit integer. The TimeStamp union | 28 // is just a struct representing a 64-bit integer. The TimeStamp union |
27 // allows access to both a FILETIME and an integer representation of | 29 // allows access to both a FILETIME and an integer representation of |
28 // the timestamp. | 30 // the timestamp. |
(...skipping 579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 } | 610 } |
609 } | 611 } |
610 | 612 |
611 | 613 |
612 bool ClientSocket::IsClosed() { | 614 bool ClientSocket::IsClosed() { |
613 return IsClosing() && !HasPendingRead() && !HasPendingWrite(); | 615 return IsClosing() && !HasPendingRead() && !HasPendingWrite(); |
614 } | 616 } |
615 | 617 |
616 | 618 |
617 void EventHandlerImplementation::HandleInterrupt(InterruptMessage* msg) { | 619 void EventHandlerImplementation::HandleInterrupt(InterruptMessage* msg) { |
618 if (msg->id == -1) { | 620 if (msg->id == kTimeoutId) { |
619 // Change of timeout request. Just set the new timeout and port as the | 621 // Change of timeout request. Just set the new timeout and port as the |
620 // completion thread will use the new timeout value for its next wait. | 622 // completion thread will use the new timeout value for its next wait. |
621 timeout_ = msg->data; | 623 timeout_ = msg->data; |
622 timeout_port_ = msg->dart_port; | 624 timeout_port_ = msg->dart_port; |
| 625 } else if (msg->id == kShutdownId) { |
| 626 shutdown_ = true; |
623 } else { | 627 } else { |
624 bool delete_handle = false; | 628 bool delete_handle = false; |
625 Handle* handle = reinterpret_cast<Handle*>(msg->id); | 629 Handle* handle = reinterpret_cast<Handle*>(msg->id); |
626 ASSERT(handle != NULL); | 630 ASSERT(handle != NULL); |
627 if (handle->is_listen_socket()) { | 631 if (handle->is_listen_socket()) { |
628 ListenSocket* listen_socket = | 632 ListenSocket* listen_socket = |
629 reinterpret_cast<ListenSocket*>(handle); | 633 reinterpret_cast<ListenSocket*>(handle); |
630 listen_socket->EnsureInitialized(this); | 634 listen_socket->EnsureInitialized(this); |
631 listen_socket->SetPortAndMask(msg->dart_port, msg->data); | 635 listen_socket->SetPortAndMask(msg->dart_port, msg->data); |
632 | 636 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 | 824 |
821 EventHandlerImplementation::EventHandlerImplementation() { | 825 EventHandlerImplementation::EventHandlerImplementation() { |
822 intptr_t result; | 826 intptr_t result; |
823 completion_port_ = | 827 completion_port_ = |
824 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1); | 828 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1); |
825 if (completion_port_ == NULL) { | 829 if (completion_port_ == NULL) { |
826 FATAL("Completion port creation failed"); | 830 FATAL("Completion port creation failed"); |
827 } | 831 } |
828 timeout_ = kInfinityTimeout; | 832 timeout_ = kInfinityTimeout; |
829 timeout_port_ = 0; | 833 timeout_port_ = 0; |
| 834 shutdown_ = false; |
830 } | 835 } |
831 | 836 |
832 | 837 |
833 DWORD EventHandlerImplementation::GetTimeout() { | 838 DWORD EventHandlerImplementation::GetTimeout() { |
834 if (timeout_ == kInfinityTimeout) { | 839 if (timeout_ == kInfinityTimeout) { |
835 return kInfinityTimeout; | 840 return kInfinityTimeout; |
836 } | 841 } |
837 intptr_t millis = timeout_ - GetCurrentTimeMilliseconds(); | 842 intptr_t millis = timeout_ - GetCurrentTimeMilliseconds(); |
838 return (millis < 0) ? 0 : millis; | 843 return (millis < 0) ? 0 : millis; |
839 } | 844 } |
840 | 845 |
841 | 846 |
842 void EventHandlerImplementation::SendData(intptr_t id, | 847 void EventHandlerImplementation::SendData(intptr_t id, |
843 Dart_Port dart_port, | 848 Dart_Port dart_port, |
844 intptr_t data) { | 849 intptr_t data) { |
845 InterruptMessage* msg = new InterruptMessage; | 850 InterruptMessage* msg = new InterruptMessage; |
846 msg->id = id; | 851 msg->id = id; |
847 msg->dart_port = dart_port; | 852 msg->dart_port = dart_port; |
848 msg->data = data; | 853 msg->data = data; |
849 BOOL ok = PostQueuedCompletionStatus( | 854 BOOL ok = PostQueuedCompletionStatus( |
850 completion_port_, 0, NULL, reinterpret_cast<OVERLAPPED*>(msg)); | 855 completion_port_, 0, NULL, reinterpret_cast<OVERLAPPED*>(msg)); |
851 if (!ok) { | 856 if (!ok) { |
852 FATAL("PostQueuedCompletionStatus failed"); | 857 FATAL("PostQueuedCompletionStatus failed"); |
853 } | 858 } |
854 } | 859 } |
855 | 860 |
856 | 861 |
857 static void EventHandlerThread(uword args) { | 862 void EventHandlerImplementation::EventHandlerEntry(uword args) { |
858 EventHandlerImplementation* handler = | 863 EventHandlerImplementation* handler = |
859 reinterpret_cast<EventHandlerImplementation*>(args); | 864 reinterpret_cast<EventHandlerImplementation*>(args); |
860 ASSERT(handler != NULL); | 865 ASSERT(handler != NULL); |
861 while (true) { | 866 while (!handler->shutdown_) { |
862 DWORD bytes; | 867 DWORD bytes; |
863 ULONG_PTR key; | 868 ULONG_PTR key; |
864 OVERLAPPED* overlapped; | 869 OVERLAPPED* overlapped; |
865 intptr_t millis = handler->GetTimeout(); | 870 intptr_t millis = handler->GetTimeout(); |
866 BOOL ok = GetQueuedCompletionStatus(handler->completion_port(), | 871 BOOL ok = GetQueuedCompletionStatus(handler->completion_port(), |
867 &bytes, | 872 &bytes, |
868 &key, | 873 &key, |
869 &overlapped, | 874 &overlapped, |
870 millis); | 875 millis); |
871 if (!ok && overlapped == NULL) { | 876 if (!ok && overlapped == NULL) { |
(...skipping 27 matching lines...) Expand all Loading... |
899 InterruptMessage* msg = reinterpret_cast<InterruptMessage*>(overlapped); | 904 InterruptMessage* msg = reinterpret_cast<InterruptMessage*>(overlapped); |
900 handler->HandleInterrupt(msg); | 905 handler->HandleInterrupt(msg); |
901 delete msg; | 906 delete msg; |
902 } else { | 907 } else { |
903 handler->HandleIOCompletion(bytes, key, overlapped); | 908 handler->HandleIOCompletion(bytes, key, overlapped); |
904 } | 909 } |
905 } | 910 } |
906 } | 911 } |
907 | 912 |
908 | 913 |
909 void EventHandlerImplementation::StartEventHandler() { | 914 void EventHandlerImplementation::Start() { |
910 int result = dart::Thread::Start(EventHandlerThread, | 915 int result = dart::Thread::Start(EventHandlerEntry, |
911 reinterpret_cast<uword>(this)); | 916 reinterpret_cast<uword>(this)); |
912 if (result != 0) { | 917 if (result != 0) { |
913 FATAL1("Failed to start event handler thread %d", result); | 918 FATAL1("Failed to start event handler thread %d", result); |
914 } | 919 } |
915 | 920 |
916 // Initialize Winsock32 | 921 // Initialize Winsock32 |
917 if (!Socket::Initialize()) { | 922 if (!Socket::Initialize()) { |
918 FATAL("Failed to initialized Windows sockets"); | 923 FATAL("Failed to initialized Windows sockets"); |
919 } | 924 } |
920 } | 925 } |
| 926 |
| 927 |
| 928 void EventHandlerImplementation::Shutdown() { |
| 929 SendData(kShutdownId, 0, 0); |
| 930 } |
OLD | NEW |