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 "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(TARGET_OS_WINDOWS) | 6 #if defined(TARGET_OS_WINDOWS) |
7 | 7 |
8 #include "bin/eventhandler.h" | 8 #include "bin/eventhandler.h" |
9 | 9 |
10 #include <process.h> // NOLINT | 10 #include <process.h> // NOLINT |
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
882 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1); | 882 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, 1); |
883 if (completion_port_ == NULL) { | 883 if (completion_port_ == NULL) { |
884 FATAL("Completion port creation failed"); | 884 FATAL("Completion port creation failed"); |
885 } | 885 } |
886 timeout_ = kInfinityTimeout; | 886 timeout_ = kInfinityTimeout; |
887 timeout_port_ = 0; | 887 timeout_port_ = 0; |
888 shutdown_ = false; | 888 shutdown_ = false; |
889 } | 889 } |
890 | 890 |
891 | 891 |
| 892 EventHandlerImplementation::~EventHandlerImplementation() { |
| 893 CloseHandle(completion_port_); |
| 894 } |
| 895 |
| 896 |
892 DWORD EventHandlerImplementation::GetTimeout() { | 897 DWORD EventHandlerImplementation::GetTimeout() { |
893 if (timeout_ == kInfinityTimeout) { | 898 if (timeout_ == kInfinityTimeout) { |
894 return kInfinityTimeout; | 899 return kInfinityTimeout; |
895 } | 900 } |
896 intptr_t millis = timeout_ - TimerUtils::GetCurrentTimeMilliseconds(); | 901 intptr_t millis = timeout_ - TimerUtils::GetCurrentTimeMilliseconds(); |
897 return (millis < 0) ? 0 : millis; | 902 return (millis < 0) ? 0 : millis; |
898 } | 903 } |
899 | 904 |
900 | 905 |
901 void EventHandlerImplementation::SendData(intptr_t id, | 906 void EventHandlerImplementation::SendData(intptr_t id, |
902 Dart_Port dart_port, | 907 Dart_Port dart_port, |
903 int64_t data) { | 908 int64_t data) { |
904 InterruptMessage* msg = new InterruptMessage; | 909 InterruptMessage* msg = new InterruptMessage; |
905 msg->id = id; | 910 msg->id = id; |
906 msg->dart_port = dart_port; | 911 msg->dart_port = dart_port; |
907 msg->data = data; | 912 msg->data = data; |
908 BOOL ok = PostQueuedCompletionStatus( | 913 BOOL ok = PostQueuedCompletionStatus( |
909 completion_port_, 0, NULL, reinterpret_cast<OVERLAPPED*>(msg)); | 914 completion_port_, 0, NULL, reinterpret_cast<OVERLAPPED*>(msg)); |
910 if (!ok) { | 915 if (!ok) { |
911 FATAL("PostQueuedCompletionStatus failed"); | 916 FATAL("PostQueuedCompletionStatus failed"); |
912 } | 917 } |
913 } | 918 } |
914 | 919 |
915 | 920 |
916 void EventHandlerImplementation::EventHandlerEntry(uword args) { | 921 void EventHandlerImplementation::EventHandlerEntry(uword args) { |
917 EventHandlerImplementation* handler = | 922 EventHandler* handler = reinterpret_cast<EventHandler*>(args); |
918 reinterpret_cast<EventHandlerImplementation*>(args); | 923 EventHandlerImplementation* handler_impl = &handler->delegate_; |
919 ASSERT(handler != NULL); | 924 ASSERT(handler_impl != NULL); |
920 while (!handler->shutdown_) { | 925 while (!handler_impl->shutdown_) { |
921 DWORD bytes; | 926 DWORD bytes; |
922 ULONG_PTR key; | 927 ULONG_PTR key; |
923 OVERLAPPED* overlapped; | 928 OVERLAPPED* overlapped; |
924 intptr_t millis = handler->GetTimeout(); | 929 intptr_t millis = handler_impl->GetTimeout(); |
925 BOOL ok = GetQueuedCompletionStatus(handler->completion_port(), | 930 BOOL ok = GetQueuedCompletionStatus(handler_impl->completion_port(), |
926 &bytes, | 931 &bytes, |
927 &key, | 932 &key, |
928 &overlapped, | 933 &overlapped, |
929 millis); | 934 millis); |
930 if (!ok && overlapped == NULL) { | 935 if (!ok && overlapped == NULL) { |
931 if (GetLastError() == ERROR_ABANDONED_WAIT_0) { | 936 if (GetLastError() == ERROR_ABANDONED_WAIT_0) { |
932 // The completion port should never be closed. | 937 // The completion port should never be closed. |
933 Log::Print("Completion port closed\n"); | 938 Log::Print("Completion port closed\n"); |
934 UNREACHABLE(); | 939 UNREACHABLE(); |
935 } else { | 940 } else { |
936 // Timeout is signalled by false result and NULL in overlapped. | 941 // Timeout is signalled by false result and NULL in overlapped. |
937 handler->HandleTimeout(); | 942 handler_impl->HandleTimeout(); |
938 } | 943 } |
939 } else if (!ok) { | 944 } else if (!ok) { |
940 // Treat ERROR_CONNECTION_ABORTED as connection closed. | 945 // Treat ERROR_CONNECTION_ABORTED as connection closed. |
941 // The error ERROR_OPERATION_ABORTED is set for pending | 946 // The error ERROR_OPERATION_ABORTED is set for pending |
942 // accept requests for a listen socket which is closed. | 947 // accept requests for a listen socket which is closed. |
943 // ERROR_NETNAME_DELETED occurs when the client closes | 948 // ERROR_NETNAME_DELETED occurs when the client closes |
944 // the socket it is reading from. | 949 // the socket it is reading from. |
945 DWORD last_error = GetLastError(); | 950 DWORD last_error = GetLastError(); |
946 if (last_error == ERROR_CONNECTION_ABORTED || | 951 if (last_error == ERROR_CONNECTION_ABORTED || |
947 last_error == ERROR_OPERATION_ABORTED || | 952 last_error == ERROR_OPERATION_ABORTED || |
948 last_error == ERROR_NETNAME_DELETED || | 953 last_error == ERROR_NETNAME_DELETED || |
949 last_error == ERROR_BROKEN_PIPE) { | 954 last_error == ERROR_BROKEN_PIPE) { |
950 ASSERT(bytes == 0); | 955 ASSERT(bytes == 0); |
951 handler->HandleIOCompletion(bytes, key, overlapped); | 956 handler_impl->HandleIOCompletion(bytes, key, overlapped); |
952 } else { | 957 } else { |
953 ASSERT(bytes == 0); | 958 ASSERT(bytes == 0); |
954 handler->HandleIOCompletion(-1, key, overlapped); | 959 handler_impl->HandleIOCompletion(-1, key, overlapped); |
955 } | 960 } |
956 } else if (key == NULL) { | 961 } else if (key == NULL) { |
957 // A key of NULL signals an interrupt message. | 962 // A key of NULL signals an interrupt message. |
958 InterruptMessage* msg = reinterpret_cast<InterruptMessage*>(overlapped); | 963 InterruptMessage* msg = reinterpret_cast<InterruptMessage*>(overlapped); |
959 handler->HandleInterrupt(msg); | 964 handler_impl->HandleInterrupt(msg); |
960 delete msg; | 965 delete msg; |
961 } else { | 966 } else { |
962 handler->HandleIOCompletion(bytes, key, overlapped); | 967 handler_impl->HandleIOCompletion(bytes, key, overlapped); |
963 } | 968 } |
964 } | 969 } |
| 970 delete handler; |
965 } | 971 } |
966 | 972 |
967 | 973 |
968 void EventHandlerImplementation::Start() { | 974 void EventHandlerImplementation::Start(EventHandler* handler) { |
969 int result = dart::Thread::Start(EventHandlerEntry, | 975 int result = dart::Thread::Start(EventHandlerEntry, |
970 reinterpret_cast<uword>(this)); | 976 reinterpret_cast<uword>(handler)); |
971 if (result != 0) { | 977 if (result != 0) { |
972 FATAL1("Failed to start event handler thread %d", result); | 978 FATAL1("Failed to start event handler thread %d", result); |
973 } | 979 } |
974 | 980 |
975 // Initialize Winsock32 | 981 // Initialize Winsock32 |
976 if (!Socket::Initialize()) { | 982 if (!Socket::Initialize()) { |
977 FATAL("Failed to initialized Windows sockets"); | 983 FATAL("Failed to initialized Windows sockets"); |
978 } | 984 } |
979 } | 985 } |
980 | 986 |
981 | 987 |
982 void EventHandlerImplementation::Shutdown() { | 988 void EventHandlerImplementation::Shutdown() { |
983 SendData(kShutdownId, 0, 0); | 989 SendData(kShutdownId, 0, 0); |
984 } | 990 } |
985 | 991 |
986 #endif // defined(TARGET_OS_WINDOWS) | 992 #endif // defined(TARGET_OS_WINDOWS) |
OLD | NEW |