Lines 67-73
Link Here
|
67 |
static int max_num_completion_contexts = 0; |
67 |
static int max_num_completion_contexts = 0; |
68 |
static HANDLE ThreadDispatchIOCP = NULL; |
68 |
static HANDLE ThreadDispatchIOCP = NULL; |
69 |
static HANDLE qwait_event = NULL; |
69 |
static HANDLE qwait_event = NULL; |
|
|
70 |
void HandlePendingContext(PCOMP_CONTEXT pContext, ap_sb_handle_t *pSbh); |
70 |
|
71 |
|
|
|
72 |
/* LTAC: Store the actual handle of the IO completion port that the worker |
73 |
* thread is supposed to work on. Stored in Thread local storage area so |
74 |
* that each thread can be associated to differnt IOCP Queue efficiently. |
75 |
* This way we make specific thread to work on specific completion ports |
76 |
* (Connection acceptence or Async Callback IOCP port) |
77 |
*/ |
78 |
__declspec(thread) static HANDLE tls_hIOCP = 0; |
79 |
|
80 |
/* If "DedicatedThreadsForAsyncCallbacks" experimental httpd configuration |
81 |
* item is set, AsyncDispatchIOCP will store the IOCP handle specifically |
82 |
* meant for Async callback notifications. |
83 |
*/ |
84 |
static HANDLE AsyncDispatchIOCP = NULL; |
85 |
|
86 |
/* Holds the count of dedicated threads initialized for Async |
87 |
* completion notification processing. |
88 |
*/ |
89 |
static int s_nAsyncHandlingThreadsInit = 0; |
71 |
|
90 |
|
72 |
void mpm_recycle_completion_context(PCOMP_CONTEXT context) |
91 |
void mpm_recycle_completion_context(PCOMP_CONTEXT context) |
73 |
{ |
92 |
{ |
Lines 95-100
Link Here
|
95 |
} |
114 |
} |
96 |
} |
115 |
} |
97 |
|
116 |
|
|
|
117 |
|
118 |
|
119 |
void mpm_recycle_iocp_completion_context(PCOMP_CONTEXT context) |
120 |
{ |
121 |
|
122 |
/* LTAC: We use Async framework in AcceptEx mode - where one thread |
123 |
* is not always associated to one context/connection - since a |
124 |
* context can result in a Async context/connection which has to be |
125 |
* queued seperately for later usage and cleanup. |
126 |
* Therefore in Async / AcceptEx mode - we cleanup the context once |
127 |
* we are done with the sync/async connection |
128 |
|
129 |
* LTAC TODO OPTIMIZATION: |
130 |
* We can try to optimize by truly recycling the context rather than |
131 |
* freeing and creating contexts every time - this saves us some CPU |
132 |
* cycles, however - when we recycle, memory used within the context |
133 |
* (transaction pool to be precise) doesn't get cleaned up until the |
134 |
* child restarts. This is something we may need to fix it in core |
135 |
* Apache if we really want to recycle completion contexts for performance |
136 |
* reasons |
137 |
* |
138 |
* Note: |
139 |
* context->accept_socket may be in a disconnected but reusable |
140 |
* state so -don't- close it. |
141 |
*/ |
142 |
ap_assert(use_acceptex); |
143 |
ap_assert(ThreadDispatchIOCP != NULL); |
144 |
|
145 |
if (context) { |
146 |
if(context->pAsyncConRec != NULL){ |
147 |
if (context->pAsyncConRec->opState != CONN_OPSTATE_ASYNC_DONE ){ |
148 |
/* LTAC: This is an Async context don't clean up |
149 |
*/ |
150 |
AP_LOG_CERROR(APLOG_MARK,APLOG_WARNING, 0, context->pAsyncConRec, |
151 |
"Thread(%04d) Connection(%05d,%s) Not Destroying async context since state is pending: %08x", |
152 |
GetCurrentThreadId(), context->pAsyncConRec->id, "", context); |
153 |
return; |
154 |
} |
155 |
else{ |
156 |
AP_LOG_CERROR(APLOG_MARK,APLOG_DEBUG, 0, context->pAsyncConRec, |
157 |
"Thread(%04d) Connection(%05d,%s) Destroying async context: %08x", |
158 |
GetCurrentThreadId(), context->pAsyncConRec->id, "", context); |
159 |
} |
160 |
} |
161 |
else{ |
162 |
AP_LOG_ERROR(APLOG_MARK,APLOG_DEBUG, 0, ap_server_conf, |
163 |
"Thread(%04d) Connection(%05d,%s) Destroying context: %08x", |
164 |
GetCurrentThreadId(), 0, "", context); |
165 |
} |
166 |
|
167 |
/* Acquire child lock |
168 |
*/ |
169 |
apr_thread_mutex_lock(child_lock); |
170 |
|
171 |
/* By now this event should not exist but do a sanity cleanup |
172 |
* TODO: remove this later |
173 |
*/ |
174 |
if (context->Overlapped.hEvent != NULL){ |
175 |
CloseHandle(context->Overlapped.hEvent); |
176 |
context->Overlapped.hEvent = NULL; |
177 |
} |
178 |
|
179 |
apr_pool_destroy(context->ptrans); |
180 |
apr_atomic_dec32(&num_completion_contexts); |
181 |
free(context); |
182 |
|
183 |
/* Release lock */ |
184 |
apr_thread_mutex_unlock(child_lock); |
185 |
|
186 |
context = NULL; |
187 |
} |
188 |
} |
189 |
|
190 |
|
98 |
PCOMP_CONTEXT mpm_get_completion_context(void) |
191 |
PCOMP_CONTEXT mpm_get_completion_context(void) |
99 |
{ |
192 |
{ |
100 |
apr_status_t rv; |
193 |
apr_status_t rv; |
Lines 178-184
Link Here
|
178 |
context->accept_socket = INVALID_SOCKET; |
271 |
context->accept_socket = INVALID_SOCKET; |
179 |
context->ba = apr_bucket_alloc_create(context->ptrans); |
272 |
context->ba = apr_bucket_alloc_create(context->ptrans); |
180 |
apr_atomic_inc32(&num_completion_contexts); |
273 |
apr_atomic_inc32(&num_completion_contexts); |
181 |
|
|
|
182 |
apr_thread_mutex_unlock(child_lock); |
274 |
apr_thread_mutex_unlock(child_lock); |
183 |
break; |
275 |
break; |
184 |
} |
276 |
} |
Lines 191-196
Link Here
|
191 |
return context; |
283 |
return context; |
192 |
} |
284 |
} |
193 |
|
285 |
|
|
|
286 |
|
287 |
|
288 |
PCOMP_CONTEXT mpm_get_Iocp_completion_context(void) |
289 |
{ |
290 |
apr_status_t rv; |
291 |
PCOMP_CONTEXT context = NULL; |
292 |
|
293 |
int max_num_contexts = g_MaxConnectionsToQueuePerChild; |
294 |
|
295 |
ap_assert(use_acceptex); |
296 |
ap_assert(ThreadDispatchIOCP != NULL); |
297 |
|
298 |
/* LTAC: Throttle connection accept based on number of completion/ |
299 |
* connection contexts |
300 |
* We account only for Sync completion context - since these are the |
301 |
* connections that are actually eating httpd.exe - CPU time slice, |
302 |
* hence we deduct the Async contexts |
303 |
*/ |
304 |
if (num_completion_contexts - g_AsyncContextCount >= max_num_contexts ){ |
305 |
static DWORD dwLastTime = 0; |
306 |
|
307 |
/* Log about this resource contention very first time or |
308 |
* every 10 minutes |
309 |
*/ |
310 |
if (dwLastTime == 0 || |
311 |
GetTickCount() - dwLastTime > 10*60*1000 /*10 minutes*/){ |
312 |
AP_LOG_ERROR(APLOG_MARK,APLOG_WARNING, 0, ap_server_conf, |
313 |
"Thread(%04d) Contexts(Total: %d Sync: %d Async: %d " |
314 |
"Max: %d) returning null context", GetCurrentThreadId(), |
315 |
num_completion_contexts, num_completion_contexts - |
316 |
g_AsyncContextCount, g_AsyncContextCount, max_num_contexts); |
317 |
|
318 |
dwLastTime = GetTickCount(); |
319 |
} |
320 |
|
321 |
/* Wait for a second for accept thread to free CPU |
322 |
* TODO: Make the sleep interval configurable if accept thread is |
323 |
* eating more CPU? |
324 |
*/ |
325 |
Sleep(1000); |
326 |
return NULL; |
327 |
} |
328 |
|
329 |
while (1) { |
330 |
apr_allocator_t *allocator; |
331 |
|
332 |
apr_thread_mutex_lock(child_lock); |
333 |
|
334 |
/* LTAC: Dont use APR to allocate contexts, doing so will spike memory |
335 |
* usage, this is because garbage collection of free'd APR memory |
336 |
* happens only when child httpd process die's |
337 |
*/ |
338 |
context = (PCOMP_CONTEXT) malloc(sizeof(COMP_CONTEXT)); |
339 |
memset(context, 0, sizeof(COMP_CONTEXT)); |
340 |
|
341 |
/* This event is not really required if we are using Apache single |
342 |
* process mode - refer win_acceptIocp() function for more info |
343 |
*/ |
344 |
context->Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
345 |
if (context->Overlapped.hEvent == NULL) { |
346 |
/* Hopefully this is a temporary condition ... */ |
347 |
ap_log_error(APLOG_MARK,APLOG_WARNING, apr_get_os_error(), ap_server_conf, |
348 |
"mpm_get_Iocp_completion_context: CreateEvent failed."); |
349 |
free(context); |
350 |
apr_thread_mutex_unlock(child_lock); |
351 |
return NULL; |
352 |
} |
353 |
|
354 |
/* Create the tranaction pool */ |
355 |
apr_allocator_create(&allocator); |
356 |
apr_allocator_max_free_set(allocator, ap_max_mem_free); |
357 |
rv = apr_pool_create_ex(&context->ptrans, pchild, NULL, allocator); |
358 |
if (rv != APR_SUCCESS) { |
359 |
ap_log_error(APLOG_MARK,APLOG_WARNING, rv, ap_server_conf, |
360 |
"mpm_get_Iocp_completion_context: Failed to create the transaction pool."); |
361 |
CloseHandle(context->Overlapped.hEvent); |
362 |
free(context); |
363 |
apr_thread_mutex_unlock(child_lock); |
364 |
return NULL; |
365 |
} |
366 |
apr_allocator_owner_set(allocator, context->ptrans); |
367 |
apr_pool_tag(context->ptrans, "IocpTransaction"); |
368 |
|
369 |
context->accept_socket = INVALID_SOCKET; |
370 |
context->ba = apr_bucket_alloc_create(context->ptrans); |
371 |
|
372 |
/* LTAC: Setup new Async entities within context |
373 |
*/ |
374 |
context->pAsyncConRec = NULL; |
375 |
context->next = NULL; |
376 |
apr_atomic_inc32(&num_completion_contexts); |
377 |
|
378 |
apr_thread_mutex_unlock(child_lock); |
379 |
break; |
380 |
} |
381 |
|
382 |
return context; |
383 |
} |
384 |
|
194 |
apr_status_t mpm_post_completion_context(PCOMP_CONTEXT context, |
385 |
apr_status_t mpm_post_completion_context(PCOMP_CONTEXT context, |
195 |
io_state_e state) |
386 |
io_state_e state) |
196 |
{ |
387 |
{ |
Lines 681-698
Link Here
|
681 |
} |
872 |
} |
682 |
|
873 |
|
683 |
|
874 |
|
|
|
875 |
/* Windows NT/2000 specific code... |
876 |
* This function is modified version of of winnt_accept(). |
877 |
* Accept processing for on Windows NT uses a producer/consumer queue |
878 |
* An accept thread accepts connections off the network using IO compeletion |
879 |
* ports then issues PostQueuedCompletionStatus() to awake a thread blocked |
880 |
* on the ThreadDispatch IOCompletionPort (IOCP) |
881 |
* |
882 |
* winnt_acceptIocp() |
883 |
* One or more accept threads run in this function, each of which accepts |
884 |
* connections off the network using Completion ports and calls |
885 |
* PostQueuedCompletionStatus() to queue an io completion packet to the |
886 |
* ThreadDispatch IOCompletionPort. |
887 |
* winnt_get_connection() |
888 |
* Worker threads block on the ThreadDispatch IOCompletionPort awaiting |
889 |
* connections to service. |
890 |
*/ |
891 |
static unsigned int __stdcall winnt_acceptIocp(void *lr_) |
892 |
{ |
893 |
ap_listen_rec *lr = (ap_listen_rec *)lr_; |
894 |
apr_os_sock_info_t sockinfo; |
895 |
PCOMP_CONTEXT context = NULL; |
896 |
DWORD BytesRead = 0; |
897 |
SOCKET nlsd; |
898 |
int rv, err_count = 0; |
899 |
|
900 |
DWORD dwCompKey = IOCP_CONNECTION_ACCEPTED; |
901 |
LPOVERLAPPED pOvl = 0; |
902 |
HANDLE hTemp = 0; |
903 |
int nRetry = 0; |
904 |
|
905 |
#if APR_HAVE_IPV6 |
906 |
SOCKADDR_STORAGE ss_listen; |
907 |
int namelen = sizeof(ss_listen); |
908 |
#endif |
909 |
|
910 |
|
911 |
apr_os_sock_get(&nlsd, lr->sd); |
912 |
|
913 |
|
914 |
#if APR_HAVE_IPV6 |
915 |
if (getsockname(nlsd, (struct sockaddr *)&ss_listen, &namelen) == SOCKET_ERROR) { |
916 |
ap_log_error(APLOG_MARK,APLOG_ERR, apr_get_netos_error(), ap_server_conf, |
917 |
"winnt_accept: getsockname error on listening socket, is IPv6 available?"); |
918 |
return 1; |
919 |
} |
920 |
#endif |
921 |
|
922 |
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf, |
923 |
"Child %d Thread %d: Starting thread to listen on port %d.", |
924 |
my_pid, GetCurrentThreadId(),lr->bind_addr->port); |
925 |
while (!shutdown_in_progress) { |
926 |
/* LTAC: Clean up the old contexts before using a new one |
927 |
* For Async framework - the number of contexts are limited to the new |
928 |
* config item - "MaxConnections", and we do not queue all contexts |
929 |
* (a.k.a connections) for reuse as in Stock apache since each |
930 |
* connection is holding socket + file resources (handles) and this |
931 |
* would result in instability of apache during long runs |
932 |
*/ |
933 |
if (context){ |
934 |
mpm_recycle_iocp_completion_context(context); |
935 |
context = NULL; |
936 |
} |
937 |
|
938 |
context = mpm_get_Iocp_completion_context(); |
939 |
if (!context) { |
940 |
/* Temporary resource constraint? */ |
941 |
/* LTAC: Max connections could have been hit? |
942 |
*/ |
943 |
Sleep(0); |
944 |
continue; |
945 |
} |
946 |
|
947 |
|
948 |
/* Create and initialize the accept socket */ |
949 |
#if APR_HAVE_IPV6 |
950 |
if (context->accept_socket == INVALID_SOCKET) { |
951 |
context->accept_socket = socket(ss_listen.ss_family, SOCK_STREAM, IPPROTO_TCP); |
952 |
context->socket_family = ss_listen.ss_family; |
953 |
} |
954 |
else if (context->socket_family != ss_listen.ss_family) { |
955 |
closesocket(context->accept_socket); |
956 |
context->accept_socket = socket(ss_listen.ss_family, SOCK_STREAM, IPPROTO_TCP); |
957 |
context->socket_family = ss_listen.ss_family; |
958 |
} |
959 |
|
960 |
if (context->accept_socket == INVALID_SOCKET) { |
961 |
ap_log_error(APLOG_MARK,APLOG_WARNING, apr_get_netos_error(), ap_server_conf, |
962 |
"winnt_accept: Failed to allocate an accept socket. " |
963 |
"Temporary resource constraint? Try again."); |
964 |
Sleep(100); |
965 |
continue; |
966 |
} |
967 |
#else |
968 |
if (context->accept_socket == INVALID_SOCKET) { |
969 |
context->accept_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
970 |
if (context->accept_socket == INVALID_SOCKET) { |
971 |
/* Another temporary condition? */ |
972 |
ap_log_error(APLOG_MARK,APLOG_WARNING, apr_get_netos_error(), ap_server_conf, |
973 |
"winnt_acceptIocp: Failed to allocate an accept socket. " |
974 |
"Temporary resource constraint? Try again."); |
975 |
Sleep(100); |
976 |
continue; |
977 |
} |
978 |
} |
979 |
#endif |
980 |
/* AcceptEx on the completion context. The completion context will be |
981 |
* signaled when a connection is accepted. |
982 |
*/ |
983 |
if (!AcceptEx(nlsd, context->accept_socket, |
984 |
context->buff, |
985 |
0, |
986 |
PADDED_ADDR_SIZE, |
987 |
PADDED_ADDR_SIZE, |
988 |
&BytesRead, |
989 |
&context->Overlapped)) { |
990 |
rv = apr_get_netos_error(); |
991 |
if ((rv == APR_FROM_OS_ERROR(WSAEINVAL)) || |
992 |
(rv == APR_FROM_OS_ERROR(WSAENOTSOCK))) { |
993 |
/* We can get here when: |
994 |
* 1) the client disconnects early |
995 |
* 2) TransmitFile does not properly recycle the accept socket (typically |
996 |
* because the client disconnected) |
997 |
* 3) there is VPN or Firewall software installed with buggy AcceptEx implementation |
998 |
* 4) the webserver is using a dynamic address that has changed |
999 |
*/ |
1000 |
++err_count; |
1001 |
closesocket(context->accept_socket); |
1002 |
context->accept_socket = INVALID_SOCKET; |
1003 |
if (err_count > MAX_ACCEPTEX_ERR_COUNT) { |
1004 |
ap_log_error(APLOG_MARK, APLOG_ERR, rv, ap_server_conf, |
1005 |
"Child %d: Encountered too many errors accepting client connections. " |
1006 |
"Possible causes: dynamic address renewal, or incompatible VPN or firewall software. " |
1007 |
"Try using the Win32DisableAcceptEx directive.", my_pid); |
1008 |
err_count = 0; |
1009 |
} |
1010 |
continue; |
1011 |
} |
1012 |
else if ((rv != APR_FROM_OS_ERROR(ERROR_IO_PENDING)) && |
1013 |
(rv != APR_FROM_OS_ERROR(WSA_IO_PENDING))) { |
1014 |
++err_count; |
1015 |
if (err_count > MAX_ACCEPTEX_ERR_COUNT) { |
1016 |
ap_log_error(APLOG_MARK,APLOG_ERR, rv, ap_server_conf, |
1017 |
"Child %d: Encountered too many errors accepting client connections. " |
1018 |
"Possible causes: Unknown. " |
1019 |
"Try using the Win32DisableAcceptEx directive.", my_pid); |
1020 |
err_count = 0; |
1021 |
} |
1022 |
closesocket(context->accept_socket); |
1023 |
context->accept_socket = INVALID_SOCKET; |
1024 |
continue; |
1025 |
} |
1026 |
err_count = 0; |
1027 |
|
1028 |
/* Wait for pending i/o. |
1029 |
* Wake up once per second to check for shutdown . |
1030 |
* XXX: We should be waiting on exit_event instead of polling |
1031 |
*/ |
1032 |
while (1) { |
1033 |
rv = WaitForSingleObject(context->Overlapped.hEvent, 1000); |
1034 |
if (rv == WAIT_OBJECT_0) { |
1035 |
if (context->accept_socket == INVALID_SOCKET) { |
1036 |
/* socket already closed */ |
1037 |
break; |
1038 |
} |
1039 |
if (!GetOverlappedResult((HANDLE)context->accept_socket, |
1040 |
&context->Overlapped, |
1041 |
&BytesRead, FALSE)) { |
1042 |
ap_log_error(APLOG_MARK, APLOG_WARNING, |
1043 |
apr_get_os_error(), ap_server_conf, |
1044 |
"winnt_acceptIocp: Asynchronous AcceptEx failed."); |
1045 |
closesocket(context->accept_socket); |
1046 |
context->accept_socket = INVALID_SOCKET; |
1047 |
} |
1048 |
break; |
1049 |
} |
1050 |
|
1051 |
|
1052 |
/* WAIT_TIMEOUT */ |
1053 |
if (shutdown_in_progress) { |
1054 |
closesocket(context->accept_socket); |
1055 |
context->accept_socket = INVALID_SOCKET; |
1056 |
break; |
1057 |
} |
1058 |
} |
1059 |
if (context->accept_socket == INVALID_SOCKET) { |
1060 |
continue; |
1061 |
} |
1062 |
} |
1063 |
|
1064 |
err_count = 0; |
1065 |
/* Inherit the listen socket settings. Required for |
1066 |
* shutdown() to work |
1067 |
*/ |
1068 |
/* LTAC: LEAK! LEAK! LEAK! |
1069 |
* Setsockopt - results in handle leaks, internally WinSock module |
1070 |
* sets up a timerhandle when the following call is made to inherit |
1071 |
* lisenting socket properties (running in parent httpd.exe process |
1072 |
* or service context)to associate this socket with the service |
1073 |
* shutdown event. I don't see this essential since we close the |
1074 |
* the open sockets during shutdown gracefully in at end of |
1075 |
* woker_main() |
1076 |
* |
1077 |
*if (setsockopt(context->accept_socket, SOL_SOCKET, |
1078 |
* SO_UPDATE_ACCEPT_CONTEXT, (char *)&nlsd, |
1079 |
* sizeof(nlsd))) { |
1080 |
* ap_log_error(APLOG_MARK, APLOG_WARNING, apr_get_netos_error(), ap_server_conf, |
1081 |
* "setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed."); |
1082 |
* * Not a failure condition. Keep running. * |
1083 |
*} |
1084 |
*/ |
1085 |
/* Get the local & remote address */ |
1086 |
GetAcceptExSockaddrs(context->buff, |
1087 |
0, |
1088 |
PADDED_ADDR_SIZE, |
1089 |
PADDED_ADDR_SIZE, |
1090 |
&context->sa_server, |
1091 |
&context->sa_server_len, |
1092 |
&context->sa_client, |
1093 |
&context->sa_client_len); |
1094 |
|
1095 |
sockinfo.os_sock = &context->accept_socket; |
1096 |
sockinfo.local = context->sa_server; |
1097 |
sockinfo.remote = context->sa_client; |
1098 |
sockinfo.family = context->sa_server->sa_family; |
1099 |
sockinfo.type = SOCK_STREAM; |
1100 |
apr_os_sock_make(&context->sock, &sockinfo, context->ptrans); |
1101 |
|
1102 |
/* LTAC: The event handle is no longer required since we will utilize |
1103 |
* IOCP to fire events - this way we reduce the number of handles used |
1104 |
* by Apache |
1105 |
*/ |
1106 |
CloseHandle(context->Overlapped.hEvent); |
1107 |
context->Overlapped.hEvent=NULL; |
1108 |
|
1109 |
/* When a connection is received, send an io completion notification to |
1110 |
* the ThreadDispatchIOCP. This function could be replaced by |
1111 |
* mpm_post_completion_context(), but why do an extra function call... |
1112 |
*/ |
1113 |
PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, IOCP_CONNECTION_ACCEPTED, |
1114 |
&context->Overlapped); |
1115 |
context = NULL; |
1116 |
} |
1117 |
if (!shutdown_in_progress) { |
1118 |
/* Yow, hit an irrecoverable error! Tell the child to die. */ |
1119 |
SetEvent(exit_event); |
1120 |
} |
1121 |
|
1122 |
AP_LOG_ERROR(APLOG_MARK, APLOG_INFO, APR_SUCCESS, ap_server_conf, |
1123 |
"Child %d: Thread: %d Accept thread exiting.", my_pid, |
1124 |
GetCurrentThreadId()); |
1125 |
return 0; |
1126 |
} |
684 |
static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context) |
1127 |
static PCOMP_CONTEXT winnt_get_connection(PCOMP_CONTEXT context) |
685 |
{ |
1128 |
{ |
686 |
int rc; |
1129 |
int rc; |
687 |
DWORD BytesRead; |
1130 |
DWORD BytesRead = 0; |
688 |
LPOVERLAPPED pol; |
1131 |
LPOVERLAPPED pol = NULL; |
689 |
#ifdef _WIN64 |
1132 |
#ifdef _WIN64 |
690 |
ULONG_PTR CompKey; |
1133 |
ULONG_PTR CompKey; |
691 |
#else |
1134 |
#else |
692 |
DWORD CompKey; |
1135 |
DWORD CompKey; |
693 |
#endif |
1136 |
#endif |
694 |
|
1137 |
|
695 |
mpm_recycle_completion_context(context); |
1138 |
mpm_recycle_iocp_completion_context(context); |
696 |
|
1139 |
|
697 |
apr_atomic_inc32(&g_blocked_threads); |
1140 |
apr_atomic_inc32(&g_blocked_threads); |
698 |
while (1) { |
1141 |
while (1) { |
Lines 700-712
Link Here
|
700 |
apr_atomic_dec32(&g_blocked_threads); |
1143 |
apr_atomic_dec32(&g_blocked_threads); |
701 |
return NULL; |
1144 |
return NULL; |
702 |
} |
1145 |
} |
703 |
rc = GetQueuedCompletionStatus(ThreadDispatchIOCP, &BytesRead, &CompKey, |
1146 |
|
704 |
&pol, INFINITE); |
1147 |
rc = GetQueuedCompletionStatus(tls_hIOCP, &BytesRead, &CompKey, &pol, |
|
|
1148 |
INFINITE); |
705 |
if (!rc) { |
1149 |
if (!rc) { |
706 |
rc = apr_get_os_error(); |
1150 |
rc = apr_get_os_error(); |
707 |
ap_log_error(APLOG_MARK,APLOG_DEBUG, rc, ap_server_conf, |
1151 |
AP_LOG_ERROR(APLOG_MARK,APLOG_INFO, rc, ap_server_conf, |
708 |
"Child %d: GetQueuedComplationStatus returned %d", my_pid, rc); |
1152 |
"Thread(%04d) GetQueuedComplationStatus returned %d", |
709 |
continue; |
1153 |
GetCurrentThreadId(), rc); |
|
|
1154 |
/* Continue processing the error condition further below, there can |
1155 |
* be Transmit File errors. E.g. 720064 -> The specified network name |
1156 |
* is no longer available |
1157 |
*/ |
710 |
} |
1158 |
} |
711 |
|
1159 |
|
712 |
switch (CompKey) { |
1160 |
switch (CompKey) { |
Lines 716-722
Link Here
|
716 |
case IOCP_SHUTDOWN: |
1164 |
case IOCP_SHUTDOWN: |
717 |
apr_atomic_dec32(&g_blocked_threads); |
1165 |
apr_atomic_dec32(&g_blocked_threads); |
718 |
return NULL; |
1166 |
return NULL; |
|
|
1167 |
|
1168 |
case IOCP_CONNECTION_PENDING_DONE: |
1169 |
{ |
1170 |
// LTAC: Move Async processing to a dedicated thread if child |
1171 |
// threads are too busy ? |
1172 |
// Once we get real data from PQA tests we can make this call |
1173 |
// |
1174 |
context = CONTAINING_RECORD(pol, COMP_CONTEXT, Overlapped); |
1175 |
break; |
1176 |
} |
1177 |
|
1178 |
|
1179 |
case IOCP_TRANSMITFILE_DONE: |
1180 |
{ |
1181 |
/* LTAC: TF I/O operation is done or have error'd */ |
1182 |
context = CONTAINING_RECORD(pol, COMP_CONTEXT, Overlapped); |
1183 |
|
1184 |
if (!context && !context->pAsyncConRec){ |
1185 |
ap_log_error(APLOG_MARK, APLOG_ERR, 0, ap_server_conf, |
1186 |
"TransmitFile operation done - context/connection" |
1187 |
"is invalid"); |
1188 |
/* Cleanup the memory associated to context */ |
1189 |
mpm_recycle_iocp_completion_context(context); |
1190 |
context = NULL; |
1191 |
} |
1192 |
break; |
1193 |
} |
1194 |
|
719 |
default: |
1195 |
default: |
|
|
1196 |
AP_LOG_ERROR(APLOG_MARK,APLOG_ERR, 0, ap_server_conf, |
1197 |
"Thread(%04d) Unknown completion key: %d", |
1198 |
GetCurrentThreadId(), CompKey); |
720 |
apr_atomic_dec32(&g_blocked_threads); |
1199 |
apr_atomic_dec32(&g_blocked_threads); |
721 |
return NULL; |
1200 |
return NULL; |
722 |
} |
1201 |
} |
Lines 740-745
Link Here
|
740 |
int thread_num = (int)thread_num_val; |
1219 |
int thread_num = (int)thread_num_val; |
741 |
ap_sb_handle_t *sbh; |
1220 |
ap_sb_handle_t *sbh; |
742 |
|
1221 |
|
|
|
1222 |
/* LTAC: Initialize the TLS IOCP handle varibale during thread startup */ |
1223 |
if (use_acceptex && tls_hIOCP == 0) |
1224 |
{ |
1225 |
/* using child lock for thread synchronization */ |
1226 |
apr_thread_mutex_lock(child_lock); |
1227 |
if (s_nAsyncHandlingThreadsInit >= g_DedicatedThreadsForAsyncCallbacks){ |
1228 |
tls_hIOCP = ThreadDispatchIOCP; |
1229 |
} |
1230 |
else{ |
1231 |
tls_hIOCP = AsyncDispatchIOCP; |
1232 |
s_nAsyncHandlingThreadsInit++; |
1233 |
AP_LOG_ERROR(APLOG_MARK,APLOG_NOTICE, 0, ap_server_conf, |
1234 |
"Thread %04d dedicated for Async completion callbacks.", |
1235 |
GetCurrentThreadId()); |
1236 |
} |
1237 |
apr_thread_mutex_unlock(child_lock); |
1238 |
} |
1239 |
|
1240 |
/* Start the worker thread loop */ |
743 |
while (1) { |
1241 |
while (1) { |
744 |
conn_rec *c; |
1242 |
conn_rec *c; |
745 |
apr_int32_t disconnected; |
1243 |
apr_int32_t disconnected; |
Lines 768-795
Link Here
|
768 |
} |
1266 |
} |
769 |
|
1267 |
|
770 |
ap_create_sb_handle(&sbh, context->ptrans, 0, thread_num); |
1268 |
ap_create_sb_handle(&sbh, context->ptrans, 0, thread_num); |
|
|
1269 |
|
1270 |
/* LTAC: If Async context was retrieved process it. |
1271 |
*/ |
1272 |
if (context->pAsyncConRec != NULL){ |
1273 |
HandlePendingContext(context, sbh); |
1274 |
continue; |
1275 |
} |
1276 |
|
771 |
c = ap_run_create_connection(context->ptrans, ap_server_conf, |
1277 |
c = ap_run_create_connection(context->ptrans, ap_server_conf, |
772 |
context->sock, thread_num, sbh, |
1278 |
context->sock, thread_num, sbh, |
773 |
context->ba); |
1279 |
context->ba); |
774 |
|
1280 |
|
775 |
if (c) { |
1281 |
if (c) { |
|
|
1282 |
/* LTAC: Setup IOCP details before processing the connection |
1283 |
*/ |
1284 |
c->hAsyncDispatchIOCP = AsyncDispatchIOCP; |
1285 |
c->pOverlapped = &context->Overlapped; |
1286 |
c->pContext = context; |
1287 |
c->pRequest = NULL; |
1288 |
context->pAsyncConRec = c; |
1289 |
c->nUsageCount = 0; |
1290 |
|
776 |
ap_process_connection(c, context->sock); |
1291 |
ap_process_connection(c, context->sock); |
|
|
1292 |
|
1293 |
|
1294 |
/* LTAC: If connection is set to any of the pending states |
1295 |
* it represents Async or Long operation, hence continue |
1296 |
* processing the rest of the connections by not blocking |
1297 |
* the current thread, once the Async / long operation is |
1298 |
* done, winnt_get_connection will get the context and we |
1299 |
* can clean it up here |
1300 |
*/ |
1301 |
if (use_acceptex && |
1302 |
c->opState == CONN_OPSTATE_ASYNC_PRE_READY){ |
1303 |
/* Async operation was spwan by current thread, do the |
1304 |
* necessary book keeping tasks and then release the |
1305 |
* context lock so that other threads that would process |
1306 |
* the Async completion notification can cleanup the |
1307 |
* connection context safely |
1308 |
*/ |
1309 |
|
1310 |
ap_assert(c->pRequest->the_request != NULL); |
1311 |
AP_LOG_CERROR(APLOG_MARK,APLOG_INFO, 0, c, |
1312 |
"Thread(%04d) Connection(%05d, sock: %05d) Async Initiated!" |
1313 |
"URI: %s", GetCurrentThreadId(), c->id, context-> |
1314 |
accept_socket, c->pRequest->the_request); |
1315 |
|
1316 |
async_context_add(context); |
1317 |
|
1318 |
/* Released the connection conext, and set the lock state as |
1319 |
* ready for other threads to process/cleanup |
1320 |
*/ |
1321 |
context = NULL; |
1322 |
apr_atomic_set32((volatile apr_uint32_t*) &c->opState, |
1323 |
CONN_OPSTATE_ASYNC_READY); |
1324 |
continue; |
1325 |
} |
1326 |
|
1327 |
/* LTAC: Connection is not Async, so reset the context state |
1328 |
*/ |
1329 |
context->pAsyncConRec = NULL; |
777 |
apr_socket_opt_get(context->sock, APR_SO_DISCONNECTED, |
1330 |
apr_socket_opt_get(context->sock, APR_SO_DISCONNECTED, |
778 |
&disconnected); |
1331 |
&disconnected); |
779 |
if (!disconnected) { |
1332 |
if (!disconnected) { |
780 |
context->accept_socket = INVALID_SOCKET; |
1333 |
context->accept_socket = INVALID_SOCKET; |
781 |
ap_lingering_close(c); |
1334 |
/* LTAC: Do not call socket lingering close while using |
782 |
} |
1335 |
* AcceptEx + Keep alive OFF, because the connection and socket |
783 |
else if (!use_acceptex) { |
1336 |
* would get closed any moment and calling lingering close will |
784 |
/* If the socket is disconnected but we are not using acceptex, |
1337 |
* result in crash |
785 |
* we cannot reuse the socket. Disconnected sockets are removed |
1338 |
*/ |
786 |
* from the apr_socket_t struct by apr_sendfile() to prevent the |
1339 |
if (!use_acceptex || |
787 |
* socket descriptor from being inadvertently closed by a call |
1340 |
c->keepalive == AP_CONN_KEEPALIVE){ |
788 |
* to apr_socket_close(), so close it directly. |
1341 |
/* Call socket lingering close only when Keep alive is ON, |
|
|
1342 |
* because the connection object would be dead otherwise |
1343 |
* and would result in crash while calling lingering close |
1344 |
*/ |
1345 |
ap_lingering_close(c); |
1346 |
} |
1347 |
/* If socket is connected and uses Acceptex mode, socket gets |
1348 |
* closed during context cleanup |
789 |
*/ |
1349 |
*/ |
790 |
closesocket(context->accept_socket); |
|
|
791 |
context->accept_socket = INVALID_SOCKET; |
792 |
} |
1350 |
} |
|
|
1351 |
else { |
1352 |
/* If the socket is disconnected (by TransmitFile API call) |
1353 |
* irrespective of AcceptEx mode or not we should close the |
1354 |
* socket handle otherwise it would lead to socket handle leak. |
1355 |
* Disconnected sockets are removed from the apr_socket_t |
1356 |
* struct by apr_sendfile() to prevent the socket descriptor |
1357 |
* from being inadvertently closed by a call to |
1358 |
* apr_socket_close(), so close it directly. |
1359 |
*/ |
1360 |
closesocket(context->accept_socket); |
1361 |
context->accept_socket = INVALID_SOCKET; |
1362 |
} |
793 |
} |
1363 |
} |
794 |
else { |
1364 |
else { |
795 |
/* ap_run_create_connection closes the socket on failure */ |
1365 |
/* ap_run_create_connection closes the socket on failure */ |
Lines 850-856
Link Here
|
850 |
/* Now start a thread per listener */ |
1420 |
/* Now start a thread per listener */ |
851 |
for (lr = ap_listeners; lr; lr = lr->next) { |
1421 |
for (lr = ap_listeners; lr; lr = lr->next) { |
852 |
if (lr->sd != NULL) { |
1422 |
if (lr->sd != NULL) { |
853 |
_beginthreadex(NULL, 1000, winnt_accept, |
1423 |
_beginthreadex(NULL, 1000, winnt_acceptIocp, |
854 |
(void *) lr, 0, &tid); |
1424 |
(void *) lr, 0, &tid); |
855 |
} |
1425 |
} |
856 |
} |
1426 |
} |
Lines 913-923
Link Here
|
913 |
* on Windows NT/2000 |
1483 |
* on Windows NT/2000 |
914 |
*/ |
1484 |
*/ |
915 |
if (use_acceptex) { |
1485 |
if (use_acceptex) { |
|
|
1486 |
/* LTAC: The following variable represents number of CPUs present in |
1487 |
* the system, i.e., max number of threads that can be truly executed |
1488 |
* in concurrent fashion. I believe this should be number of Physical |
1489 |
* CPUs rather than number of Logical CPUs (HyperThreading is dumb). |
1490 |
* TODO: For now I'm setting this value as 4 and has scaled well, we |
1491 |
* can fine tune this value using Win32 API or NUM_OF_PROCESSORS |
1492 |
* env variable |
1493 |
*/ |
1494 |
int nAvailableProcessors = 4; |
1495 |
|
916 |
/* Create the worker thread dispatch IOCP */ |
1496 |
/* Create the worker thread dispatch IOCP */ |
917 |
ThreadDispatchIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, |
1497 |
ThreadDispatchIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, |
918 |
NULL, |
1498 |
NULL, |
919 |
0, |
1499 |
0, |
920 |
0); /* CONCURRENT ACTIVE THREADS */ |
1500 |
nAvailableProcessors); /* CONCURRENT ACTIVE THREADS */ |
|
|
1501 |
|
1502 |
|
1503 |
/* If dedicated Async threads are required - then create seperate IO completion port*/ |
1504 |
if (g_DedicatedThreadsForAsyncCallbacks > 0){ |
1505 |
AsyncDispatchIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, |
1506 |
NULL, |
1507 |
0, |
1508 |
nAvailableProcessors); /* CONCURRENT ACTIVE THREADS */ |
1509 |
} |
1510 |
else{ |
1511 |
AsyncDispatchIOCP = ThreadDispatchIOCP; |
1512 |
} |
1513 |
|
1514 |
AP_LOG_ERROR(APLOG_MARK, APLOG_NOTICE, 0, ap_server_conf, |
1515 |
"Child %d: Completion Ports - ConnectionRequest: %08x" |
1516 |
" Async completion callbacks: %08x", my_pid, ThreadDispatchIOCP, |
1517 |
AsyncDispatchIOCP); |
1518 |
|
1519 |
|
1520 |
|
921 |
apr_thread_mutex_create(&qlock, APR_THREAD_MUTEX_DEFAULT, pchild); |
1521 |
apr_thread_mutex_create(&qlock, APR_THREAD_MUTEX_DEFAULT, pchild); |
922 |
qwait_event = CreateEvent(NULL, TRUE, FALSE, NULL); |
1522 |
qwait_event = CreateEvent(NULL, TRUE, FALSE, NULL); |
923 |
if (!qwait_event) { |
1523 |
if (!qwait_event) { |
Lines 1094-1100
Link Here
|
1094 |
else { /* Windows NT/2000 */ |
1694 |
else { /* Windows NT/2000 */ |
1095 |
/* Post worker threads blocked on the ThreadDispatch IOCompletion port */ |
1695 |
/* Post worker threads blocked on the ThreadDispatch IOCompletion port */ |
1096 |
while (g_blocked_threads > 0) { |
1696 |
while (g_blocked_threads > 0) { |
1097 |
ap_log_error(APLOG_MARK,APLOG_INFO, APR_SUCCESS, ap_server_conf, |
1697 |
AP_LOG_ERROR(APLOG_MARK,APLOG_INFO, APR_SUCCESS, ap_server_conf, |
1098 |
"Child %d: %d threads blocked on the completion port", my_pid, g_blocked_threads); |
1698 |
"Child %d: %d threads blocked on the completion port", my_pid, g_blocked_threads); |
1099 |
for (i=g_blocked_threads; i > 0; i--) { |
1699 |
for (i=g_blocked_threads; i > 0; i--) { |
1100 |
PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, IOCP_SHUTDOWN, NULL); |
1700 |
PostQueuedCompletionStatus(ThreadDispatchIOCP, 0, IOCP_SHUTDOWN, NULL); |
Lines 1103-1113
Link Here
|
1103 |
} |
1703 |
} |
1104 |
/* Empty the accept queue of completion contexts */ |
1704 |
/* Empty the accept queue of completion contexts */ |
1105 |
apr_thread_mutex_lock(qlock); |
1705 |
apr_thread_mutex_lock(qlock); |
|
|
1706 |
if (!use_acceptex) { |
1106 |
while (qhead) { |
1707 |
while (qhead) { |
1107 |
CloseHandle(qhead->Overlapped.hEvent); |
1708 |
CloseHandle(qhead->Overlapped.hEvent); |
1108 |
closesocket(qhead->accept_socket); |
1709 |
closesocket(qhead->accept_socket); |
1109 |
qhead = qhead->next; |
1710 |
qhead = qhead->next; |
1110 |
} |
1711 |
} |
|
|
1712 |
} |
1713 |
else { |
1714 |
// LTAC: Cleanup async contexts |
1715 |
// |
1716 |
while (async_context_head) |
1717 |
{ |
1718 |
closesocket(async_context_head->accept_socket); |
1719 |
async_context_head = async_context_head->next; |
1720 |
} |
1721 |
} |
1111 |
apr_thread_mutex_unlock(qlock); |
1722 |
apr_thread_mutex_unlock(qlock); |
1112 |
} |
1723 |
} |
1113 |
|
1724 |
|
Lines 1200-1212
Link Here
|
1200 |
apr_thread_mutex_destroy(allowed_globals.jobmutex); |
1811 |
apr_thread_mutex_destroy(allowed_globals.jobmutex); |
1201 |
apr_thread_mutex_destroy(child_lock); |
1812 |
apr_thread_mutex_destroy(child_lock); |
1202 |
|
1813 |
|
1203 |
if (use_acceptex) { |
1814 |
/** LTAC: We use qlock for Async context queue */ |
|
|
1815 |
if (use_acceptex) |
1816 |
{ |
1204 |
apr_thread_mutex_destroy(qlock); |
1817 |
apr_thread_mutex_destroy(qlock); |
1205 |
CloseHandle(qwait_event); |
1818 |
CloseHandle(qwait_event); |
|
|
1819 |
|
1820 |
/* Close completion port created for dedicated Async thread |
1821 |
* handlers |
1822 |
*/ |
1823 |
if (g_DedicatedThreadsForAsyncCallbacks > 0){ |
1824 |
CloseHandle(AsyncDispatchIOCP); |
1825 |
} |
1206 |
} |
1826 |
} |
1207 |
|
1827 |
|
1208 |
apr_pool_destroy(pchild); |
1828 |
apr_pool_destroy(pchild); |
1209 |
CloseHandle(exit_event); |
1829 |
CloseHandle(exit_event); |
1210 |
} |
1830 |
} |
1211 |
|
1831 |
|
|
|
1832 |
extern PASYNC_CONTEXT async_context_head = NULL; |
1833 |
extern PASYNC_CONTEXT async_context_tail = NULL; |
1834 |
extern volatile int g_AsyncContextCount = 0; |
1835 |
|
1836 |
/* async_context_add() |
1837 |
* Adds the Context object to Async queue |
1838 |
*/ |
1839 |
PASYNC_CONTEXT async_context_add(const PASYNC_CONTEXT pAsyncCntxt) |
1840 |
{ |
1841 |
apr_thread_mutex_lock(qlock); |
1842 |
|
1843 |
if (async_context_tail != NULL && |
1844 |
async_context_head != NULL){ |
1845 |
async_context_tail->next = pAsyncCntxt; |
1846 |
async_context_tail = pAsyncCntxt; |
1847 |
} |
1848 |
else if (async_context_head == NULL){ |
1849 |
async_context_head = pAsyncCntxt; |
1850 |
async_context_tail = pAsyncCntxt; |
1851 |
} |
1852 |
async_context_tail->next = NULL; |
1853 |
apr_thread_mutex_unlock(qlock); |
1854 |
apr_atomic_inc32(&g_AsyncContextCount); |
1855 |
|
1856 |
AP_LOG_ERROR(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf, |
1857 |
"Thread(%04d) Connection(%05d,%s) AsyncCntxt count: %d", |
1858 |
GetCurrentThreadId(), 0, 0, g_AsyncContextCount); |
1859 |
|
1860 |
return async_context_head; |
1861 |
} |
1862 |
|
1863 |
/* async_context_remove |
1864 |
* Removes the Context object from Async queue, note - we do not free memory |
1865 |
* here, but free it in mpm_recycle_iocp_completion_context() |
1866 |
*/ |
1867 |
PASYNC_CONTEXT async_context_remove(const PASYNC_CONTEXT pAsyncCntxt) |
1868 |
{ |
1869 |
PASYNC_CONTEXT pCur = NULL; |
1870 |
PASYNC_CONTEXT pPrev = NULL; |
1871 |
volatile int bIsRemoved = 0; |
1872 |
|
1873 |
apr_thread_mutex_lock(qlock); |
1874 |
|
1875 |
pCur = async_context_head; |
1876 |
while(pCur != NULL){ |
1877 |
if (pCur == pAsyncCntxt){ |
1878 |
/* found async context */ |
1879 |
|
1880 |
if (pPrev == NULL){ |
1881 |
/* pCur is head node */ |
1882 |
async_context_head = pCur->next; |
1883 |
if (async_context_head == NULL){ |
1884 |
/* there are no nodes left in queue */ |
1885 |
async_context_tail = NULL; |
1886 |
} |
1887 |
bIsRemoved = 1; |
1888 |
break; |
1889 |
} |
1890 |
else { |
1891 |
pPrev->next = pCur->next; |
1892 |
/* if we have removed the tail, reset tail */ |
1893 |
if (async_context_tail == pCur){ |
1894 |
async_context_tail = pPrev; |
1895 |
} |
1896 |
bIsRemoved = 1; |
1897 |
break; |
1898 |
} |
1899 |
} |
1900 |
pPrev = pCur; |
1901 |
pCur = pCur->next; |
1902 |
} |
1903 |
|
1904 |
apr_thread_mutex_unlock(qlock); |
1905 |
if (bIsRemoved == 1){ |
1906 |
apr_atomic_dec32(&g_AsyncContextCount); |
1907 |
} |
1908 |
|
1909 |
AP_LOG_ERROR(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf, |
1910 |
"Thread(%04d) Connection(%05d,%s) " |
1911 |
" NtAsyncIoContext_Remove() %08x - AsyncCntxt count: %d", |
1912 |
GetCurrentThreadId(), 0,"", pAsyncCntxt, g_AsyncContextCount); |
1913 |
|
1914 |
|
1915 |
return async_context_head; |
1916 |
} |
1917 |
|
1918 |
|
1919 |
/* HandlePendingContext |
1920 |
* |
1921 |
* Handles Async or long pending operation completion signal. |
1922 |
* For Push connections: we close the socket if it is not already closed, and |
1923 |
* cleanup the connection record. |
1924 |
* |
1925 |
* TODO items: |
1926 |
* |
1927 |
* For Async Read : Provide the status of read operation? |
1928 |
*/ |
1929 |
void HandlePendingContext(PCOMP_CONTEXT pContext, ap_sb_handle_t *pSbh) |
1930 |
{ |
1931 |
int i=0; |
1932 |
apr_int32_t disconnected; |
1933 |
conn_rec* pAsyncConRec = pContext->pAsyncConRec; |
1934 |
char* pszRequestUri = "unknown"; |
1935 |
|
1936 |
ap_assert(pAsyncConRec != NULL); |
1937 |
|
1938 |
/* Store the request URI */ |
1939 |
if (pAsyncConRec->pRequest && |
1940 |
pAsyncConRec->pRequest->the_request){ |
1941 |
pszRequestUri = pAsyncConRec->pRequest->the_request; |
1942 |
} |
1943 |
|
1944 |
/* LTAC: Update the scoreboard handle in Async context since the current |
1945 |
* thread will be different from the thread that created the context |
1946 |
*/ |
1947 |
pAsyncConRec->sbh = pSbh; |
1948 |
/* TODO: Optimize later, wait for not more than 3 seconds */ |
1949 |
for(;i < 3 && pAsyncConRec->opState != CONN_OPSTATE_ASYNC_READY;i++){ |
1950 |
Sleep(1000); |
1951 |
if (i == 2){ |
1952 |
ap_log_cerror(APLOG_MARK, APLOG_NOTICE, 0, pContext->pAsyncConRec, |
1953 |
"Thread(%04d) Connection(%05d, socket: %05d) Context not " |
1954 |
"released by Async spawner thread!) ", GetCurrentThreadId(), |
1955 |
pAsyncConRec->id, pContext->accept_socket); |
1956 |
} |
1957 |
} |
1958 |
|
1959 |
/* At this point we expect the Async operation spwaner thread to have |
1960 |
* relinquised the context object and set its state to READY, I believe |
1961 |
* 3 seconds should be more than enough for relinquishing, if not we will |
1962 |
* assert (logs error and restarts Apache). |
1963 |
*/ |
1964 |
ap_assert(pAsyncConRec->opState == CONN_OPSTATE_ASYNC_READY); |
1965 |
ap_assert(pAsyncConRec->opState != CONN_OPSTATE_ASYNC_DONE); |
1966 |
|
1967 |
/* TODO: Update the scoreboard status - SEPM does not support this |
1968 |
* module for Amber release |
1969 |
*/ |
1970 |
/* |
1971 |
check_pipeline_flush(r); |
1972 |
ap_update_child_status(r->connection->sbh, SERVER_BUSY_LOG, r); |
1973 |
ap_run_log_transaction(r); |
1974 |
if (ap_extended_status) |
1975 |
ap_time_process_request(r->connection->sbh, STOP_PREQUEST); |
1976 |
*/ |
1977 |
|
1978 |
|
1979 |
/* Transmit File ? |
1980 |
*/ |
1981 |
if (pAsyncConRec->AsyncOperation.bAsyncSendFile) |
1982 |
{ |
1983 |
DWORD dwBytesWritten = 0; |
1984 |
|
1985 |
if (!GetOverlappedResult((HANDLE)pContext->accept_socket, &pContext->Overlapped, |
1986 |
&dwBytesWritten, TRUE)) { |
1987 |
AP_LOG_CERROR(APLOG_MARK, APLOG_ERR, apr_get_os_error(), pAsyncConRec, |
1988 |
"Thread(%04d) TransmitFile failed, socket: %05d, " |
1989 |
"Threads ready: %d, URI: %s", GetCurrentThreadId(), |
1990 |
pContext->accept_socket, g_blocked_threads, pszRequestUri); |
1991 |
} |
1992 |
else{ |
1993 |
AP_LOG_CERROR(APLOG_MARK,APLOG_INFO, 0, pAsyncConRec, |
1994 |
"Thread(%04d) TransmitFile done, socket: %05d, bytes xfer: %d," |
1995 |
"Threads ready: %d, url: %s", GetCurrentThreadId(), |
1996 |
pContext->accept_socket, dwBytesWritten, g_blocked_threads, |
1997 |
pszRequestUri); |
1998 |
} |
1999 |
} |
2000 |
/* End of Transmit File */ |
2001 |
|
2002 |
apr_socket_opt_get(pContext->sock, APR_SO_DISCONNECTED, |
2003 |
&disconnected); |
2004 |
if (!disconnected) { |
2005 |
AP_LOG_CERROR(APLOG_MARK, APLOG_INFO, 0, pAsyncConRec, |
2006 |
"Thread(%04d) Async done, closing socket!: %05d, URI: %s", |
2007 |
GetCurrentThreadId(), pContext->accept_socket, pszRequestUri); |
2008 |
pContext->accept_socket = INVALID_SOCKET; |
2009 |
ap_lingering_close(pContext->pAsyncConRec); |
2010 |
} |
2011 |
else { |
2012 |
AP_LOG_CERROR(APLOG_MARK, APLOG_NOTICE, 0, pAsyncConRec, |
2013 |
"Thread(%04d) Async done, socket already closed!: %05d, URI: %s", |
2014 |
GetCurrentThreadId(), pContext->accept_socket, pszRequestUri); |
2015 |
} |
2016 |
|
2017 |
/* LTAC: If socket is disconnected are we leaking connection objects ? |
2018 |
* I doubt this since connection objects are associated to |
2019 |
* context objects transaction pool which is getting destroyed |
2020 |
* during mpm_recycle_iocp_completion_context() invocation |
2021 |
*/ |
2022 |
async_context_remove(pContext); |
2023 |
apr_atomic_set32((volatile apr_uint32_t*) &pAsyncConRec->opState, |
2024 |
CONN_OPSTATE_ASYNC_DONE); |
2025 |
} |
1212 |
#endif /* def WIN32 */ |
2026 |
#endif /* def WIN32 */ |