[10185] | 1 | /* $Id: overlappedio.cpp,v 1.21 2003-07-28 11:35:32 sandervl Exp $ */
|
---|
[7549] | 2 |
|
---|
| 3 | /*
|
---|
[7550] | 4 | * Win32 overlapped IO class
|
---|
[7549] | 5 | *
|
---|
[7550] | 6 | * Copyright 2001 Sander van Leeuwen <sandervl@xs4all.nl>
|
---|
[7549] | 7 | *
|
---|
[7550] | 8 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
[7549] | 9 | *
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | #include <os2win.h>
|
---|
| 15 | #include <string.h>
|
---|
| 16 | #include <handlemanager.h>
|
---|
| 17 | #include <heapstring.h>
|
---|
[7631] | 18 | #include <overlappedio.h>
|
---|
[7549] | 19 | #include "oslibdos.h"
|
---|
| 20 |
|
---|
| 21 | #define DBG_LOCALLOG DBG_overlappedio
|
---|
| 22 | #include "dbglocal.h"
|
---|
| 23 |
|
---|
| 24 |
|
---|
[7550] | 25 | //******************************************************************************
|
---|
| 26 | //******************************************************************************
|
---|
[7564] | 27 | OverlappedIOHandler::OverlappedIOHandler(LPOVERLAPPED_HANDLER lpReadHandler,
|
---|
| 28 | LPOVERLAPPED_HANDLER lpWriteHandler,
|
---|
[7598] | 29 | LPOVERLAPPED_HANDLER lpPollHandler,
|
---|
| 30 | BOOL fFullDuplex) :
|
---|
[8839] | 31 | hThreadRead(0), hThreadWrite(0), hThreadPoll(0), refCount(0)
|
---|
[7550] | 32 | {
|
---|
| 33 | OverlappedIOError errcode = OutOfMemory;
|
---|
[7549] | 34 |
|
---|
[7598] | 35 | this->fFullDuplex = fFullDuplex;
|
---|
| 36 |
|
---|
[7632] | 37 | if(lpReadHandler == NULL) {
|
---|
[7552] | 38 | throw(InvalidParameter);
|
---|
| 39 | }
|
---|
[7550] | 40 |
|
---|
[7567] | 41 | pending[ASYNC_INDEX_READ] = pending[ASYNC_INDEX_WRITE] = NULL;
|
---|
| 42 | pending [ASYNC_INDEX_POLL] = pending [ASYNC_INDEX_BUSY] = NULL;
|
---|
[7552] | 43 |
|
---|
| 44 | this->lpReadHandler = lpReadHandler;
|
---|
| 45 | this->lpWriteHandler = lpWriteHandler;
|
---|
| 46 | this->lpPollHandler = lpPollHandler;
|
---|
| 47 |
|
---|
[7550] | 48 | ::InitializeCriticalSection(&critsect);
|
---|
[7552] | 49 | //poll, read & write event semaphores are auto-reset (one thread wakes up
|
---|
| 50 | //after a SetEvent call)
|
---|
| 51 | hEventPoll = ::CreateEventA(NULL, FALSE, FALSE, NULL);
|
---|
| 52 | hEventRead = ::CreateEventA(NULL, FALSE, FALSE, NULL);
|
---|
| 53 | hEventWrite = ::CreateEventA(NULL, FALSE, FALSE, NULL);
|
---|
| 54 |
|
---|
[7598] | 55 | dprintf(("OverlappedIOThread: hEventRead %x hEventWrite %x hEventPoll %x", hEventRead, hEventWrite, hEventPoll));
|
---|
| 56 |
|
---|
[7604] | 57 | //the exit & cancel event semaphores are manual reset, because these events
|
---|
[7552] | 58 | //must be able to wake up multiple threads
|
---|
[7550] | 59 | hEventExit = ::CreateEventA(NULL, TRUE, FALSE, NULL);
|
---|
[7604] | 60 | hEventCancel = ::CreateEventA(NULL, TRUE, FALSE, NULL);
|
---|
| 61 | if(!hEventPoll || !hEventRead || !hEventWrite || !hEventExit || !hEventCancel)
|
---|
[7550] | 62 | {
|
---|
| 63 | DebugInt3();
|
---|
| 64 | errcode = EventCreationFailed;
|
---|
| 65 | goto failed;
|
---|
| 66 | }
|
---|
[7564] | 67 |
|
---|
[7550] | 68 | DWORD dwThreadId;
|
---|
| 69 | LPOVERLAPPED_THREAD_PARAM threadparam;
|
---|
| 70 |
|
---|
[7598] | 71 | dwAsyncType = (lpWriteHandler && fFullDuplex) ? ASYNCIO_READ : ASYNCIO_READWRITE;
|
---|
[7550] | 72 | threadparam = (LPOVERLAPPED_THREAD_PARAM)malloc(sizeof(OVERLAPPED_THREAD_PARAM));
|
---|
| 73 | if(!threadparam) goto outofmem;
|
---|
[7552] | 74 | threadparam->dwOperation = dwAsyncType;
|
---|
[7550] | 75 | threadparam->lpOverlappedObj = this;
|
---|
| 76 | hThreadRead = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
|
---|
[8839] | 77 | if(hThreadRead) {//thread uses this object; keep reference count to avoid premature destruction
|
---|
| 78 | AddRef();
|
---|
| 79 | }
|
---|
[7552] | 80 |
|
---|
[7598] | 81 | if(lpWriteHandler && fFullDuplex) {
|
---|
[7552] | 82 | dwAsyncType |= ASYNCIO_WRITE;
|
---|
| 83 |
|
---|
[7550] | 84 | threadparam = (LPOVERLAPPED_THREAD_PARAM)malloc(sizeof(OVERLAPPED_THREAD_PARAM));
|
---|
| 85 | if(!threadparam) goto outofmem;
|
---|
[7552] | 86 | threadparam->dwOperation = ASYNCIO_WRITE;
|
---|
[7550] | 87 | threadparam->lpOverlappedObj = this;
|
---|
| 88 | hThreadWrite = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
|
---|
[10185] | 89 | SetThreadPriority(hThreadWrite, THREAD_PRIORITY_TIME_CRITICAL);
|
---|
[8839] | 90 | if(hThreadWrite) {//thread uses this object; keep reference count to avoid premature destruction
|
---|
| 91 | AddRef();
|
---|
| 92 | }
|
---|
[7550] | 93 | }
|
---|
| 94 |
|
---|
| 95 | if(lpPollHandler) {
|
---|
[7552] | 96 | dwAsyncType |= ASYNCIO_POLL;
|
---|
| 97 |
|
---|
[7550] | 98 | threadparam = (LPOVERLAPPED_THREAD_PARAM)malloc(sizeof(OVERLAPPED_THREAD_PARAM));
|
---|
| 99 | if(!threadparam) goto outofmem;
|
---|
[7552] | 100 | threadparam->dwOperation = ASYNCIO_POLL;
|
---|
[7550] | 101 | threadparam->lpOverlappedObj = this;
|
---|
| 102 | hThreadPoll = ::CreateThread(NULL, 32*1024, OverlappedIOThread, (LPVOID)threadparam, 0, &dwThreadId);
|
---|
[8647] | 103 | SetThreadPriority(hThreadPoll, THREAD_PRIORITY_TIME_CRITICAL);
|
---|
[8839] | 104 | if(hThreadPoll) {//thread uses this object; keep reference count to avoid premature destruction
|
---|
| 105 | AddRef();
|
---|
| 106 | }
|
---|
[7550] | 107 | }
|
---|
| 108 |
|
---|
[7598] | 109 | if((lpPollHandler && !hThreadPoll) || !hThreadRead || (lpWriteHandler && fFullDuplex && !hThreadWrite))
|
---|
[7550] | 110 | {
|
---|
| 111 | DebugInt3();
|
---|
| 112 | errcode = ThreadCreationFailed;
|
---|
| 113 | goto failed;
|
---|
| 114 | }
|
---|
| 115 | return;
|
---|
| 116 |
|
---|
[7564] | 117 | outofmem:
|
---|
[7550] | 118 | errcode = OutOfMemory;
|
---|
| 119 | //fall through
|
---|
| 120 | failed:
|
---|
[7564] | 121 | //SvL: NOTE: We might not fail gracefully when threads have already been
|
---|
[7550] | 122 | // created. (thread accessing memory that has been freed)
|
---|
| 123 | // Don't feel like wasting time to fix this as this should never
|
---|
| 124 | // happen anyway.
|
---|
| 125 | if(hEventExit) {
|
---|
| 126 | ::SetEvent(hEventExit);
|
---|
| 127 | ::CloseHandle(hEventExit);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | if(hEventRead) ::CloseHandle(hEventRead);
|
---|
| 131 | if(hEventWrite) ::CloseHandle(hEventWrite);
|
---|
| 132 | if(hEventPoll) ::CloseHandle(hEventPoll);
|
---|
| 133 |
|
---|
| 134 | if(hThreadRead) ::CloseHandle(hThreadRead);
|
---|
| 135 | if(hThreadPoll) ::CloseHandle(hThreadPoll);
|
---|
| 136 | if(hThreadWrite) ::CloseHandle(hThreadWrite);
|
---|
| 137 | ::DeleteCriticalSection(&critsect);
|
---|
| 138 |
|
---|
| 139 | throw(errcode);
|
---|
| 140 | }
|
---|
[7549] | 141 | //******************************************************************************
|
---|
| 142 | //******************************************************************************
|
---|
[7550] | 143 | OverlappedIOHandler::~OverlappedIOHandler()
|
---|
| 144 | {
|
---|
[7552] | 145 | dprintf(("~OverlappedIOHandler: signalling overlapped threads"));
|
---|
[7550] | 146 | ::SetEvent(hEventExit);
|
---|
[7549] | 147 |
|
---|
[7550] | 148 | ::CloseHandle(hEventExit);
|
---|
| 149 | ::CloseHandle(hEventRead);
|
---|
| 150 | ::CloseHandle(hEventWrite);
|
---|
| 151 | ::CloseHandle(hEventPoll);
|
---|
[7549] | 152 |
|
---|
[7550] | 153 | ::CloseHandle(hThreadRead);
|
---|
| 154 | if(hThreadPoll) ::CloseHandle(hThreadPoll);
|
---|
| 155 | if(hThreadWrite) ::CloseHandle(hThreadWrite);
|
---|
| 156 |
|
---|
| 157 | DeleteCriticalSection(&critsect);
|
---|
| 158 | }
|
---|
[7549] | 159 | //******************************************************************************
|
---|
| 160 | //******************************************************************************
|
---|
[8839] | 161 | DWORD OverlappedIOHandler::AddRef()
|
---|
| 162 | {
|
---|
| 163 | return InterlockedIncrement(&refCount);
|
---|
| 164 | }
|
---|
| 165 | //******************************************************************************
|
---|
| 166 | //******************************************************************************
|
---|
| 167 | DWORD OverlappedIOHandler::Release(BOOL fSignalExit)
|
---|
| 168 | {
|
---|
| 169 | if(fSignalExit) {
|
---|
| 170 | ::SetEvent(hEventExit);
|
---|
| 171 | }
|
---|
| 172 | if(InterlockedDecrement(&refCount) == 0) {
|
---|
| 173 | dprintf(("OverlappedIOHandler::Release -> delete now"));
|
---|
| 174 | delete this;
|
---|
| 175 | return 0;
|
---|
| 176 | }
|
---|
| 177 | return refCount;
|
---|
| 178 | }
|
---|
| 179 | //******************************************************************************
|
---|
| 180 | //******************************************************************************
|
---|
[21916] | 181 | DWORD CALLBACK OverlappedIOHandler::OverlappedIOThread(LPVOID lpThreadParam)
|
---|
[7550] | 182 | {
|
---|
| 183 | LPOVERLAPPED_THREAD_PARAM threadparam = (LPOVERLAPPED_THREAD_PARAM)lpThreadParam;
|
---|
[7552] | 184 | DWORD dwOperation;
|
---|
[7550] | 185 | OverlappedIOHandler *lpOverlappedObj;
|
---|
| 186 |
|
---|
[8644] | 187 | if(threadparam == NULL || threadparam->lpOverlappedObj == NULL) {
|
---|
[7550] | 188 | DebugInt3();
|
---|
| 189 | return 0;
|
---|
| 190 | }
|
---|
| 191 | lpOverlappedObj = threadparam->lpOverlappedObj;
|
---|
[7552] | 192 | dwOperation = threadparam->dwOperation;
|
---|
[7550] | 193 | //free thread parameter first
|
---|
| 194 | free(threadparam);
|
---|
| 195 |
|
---|
[7552] | 196 | return lpOverlappedObj->threadHandler(dwOperation);
|
---|
[7550] | 197 | }
|
---|
| 198 | //******************************************************************************
|
---|
| 199 | //******************************************************************************
|
---|
[7552] | 200 | DWORD OverlappedIOHandler::threadHandler(DWORD dwOperation)
|
---|
[7550] | 201 | {
|
---|
[7552] | 202 | LPASYNCIOREQUEST lpRequest;
|
---|
[7560] | 203 | LPOVERLAPPED lpOverlapped;
|
---|
[7550] | 204 | HANDLE hEvents[2];
|
---|
[7567] | 205 | HANDLE hEventsWait[2];
|
---|
| 206 | HANDLE hHandle;
|
---|
[7560] | 207 | DWORD ret, dwTimeOut, dwResult;
|
---|
[7552] | 208 | int index;
|
---|
[7550] | 209 |
|
---|
[7552] | 210 | dprintf(("OverlappedIOThread: started for event %d", dwOperation));
|
---|
| 211 | switch(dwOperation) {
|
---|
| 212 | case ASYNCIO_READ:
|
---|
| 213 | case ASYNCIO_READWRITE:
|
---|
[7550] | 214 | hEvents[0] = hEventRead;
|
---|
[7552] | 215 | index = ASYNC_INDEX_READ;
|
---|
[7550] | 216 | break;
|
---|
| 217 |
|
---|
[7552] | 218 | case ASYNCIO_WRITE:
|
---|
[7550] | 219 | hEvents[0] = hEventWrite;
|
---|
[7552] | 220 | index = ASYNC_INDEX_WRITE;
|
---|
[7550] | 221 | break;
|
---|
| 222 |
|
---|
[7552] | 223 | case ASYNCIO_POLL:
|
---|
[7550] | 224 | hEvents[0] = hEventPoll;
|
---|
[7552] | 225 | index = ASYNC_INDEX_POLL;
|
---|
[7550] | 226 | break;
|
---|
| 227 | default:
|
---|
| 228 | DebugInt3();
|
---|
| 229 | }
|
---|
| 230 | hEvents[1] = hEventExit;
|
---|
| 231 |
|
---|
[7603] | 232 | while(TRUE)
|
---|
| 233 | {
|
---|
[7550] | 234 | ret = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
|
---|
| 235 | if(ret == WAIT_FAILED) {
|
---|
| 236 | dprintf(("!WARNING!: WaitForMultipleObjects -> WAIT_FAILED!"));
|
---|
| 237 | break;
|
---|
| 238 | }
|
---|
| 239 | //if hEventExit has been signalled, then we are told to exit
|
---|
| 240 | if(ret == (WAIT_OBJECT_0+1)) {
|
---|
| 241 | dprintf(("end of threadHandler signalled"));
|
---|
| 242 | break;
|
---|
| 243 | }
|
---|
[7603] | 244 | //process all pending jobs
|
---|
| 245 | while(TRUE)
|
---|
| 246 | {
|
---|
| 247 | ::EnterCriticalSection(&critsect);
|
---|
| 248 | if(pending[index] == NULL) {
|
---|
| 249 | ::LeaveCriticalSection(&critsect);
|
---|
| 250 | break;
|
---|
[7598] | 251 | }
|
---|
[7603] | 252 | lpRequest = pending[index];
|
---|
| 253 | pending[index] = lpRequest->next;
|
---|
[7598] | 254 |
|
---|
[8644] | 255 | //add to the head of process list
|
---|
[7603] | 256 | lpRequest->next = pending[ASYNC_INDEX_BUSY];
|
---|
| 257 | pending[ASYNC_INDEX_BUSY] = lpRequest;
|
---|
| 258 | ::LeaveCriticalSection(&critsect);
|
---|
[7552] | 259 |
|
---|
[7603] | 260 | lpOverlapped = lpRequest->lpOverlapped;;
|
---|
| 261 | hHandle = lpRequest->hHandle;
|
---|
[7552] | 262 |
|
---|
[8644] | 263 | #ifdef DEBUG
|
---|
| 264 | switch(lpRequest->dwAsyncType) {
|
---|
| 265 | case ASYNCIO_READ:
|
---|
| 266 | case ASYNCIO_WRITE:
|
---|
| 267 | case ASYNCIO_POLL:
|
---|
| 268 | break;
|
---|
| 269 | default:
|
---|
| 270 | DebugInt3();
|
---|
| 271 | break;
|
---|
| 272 | }
|
---|
| 273 | #endif
|
---|
[7603] | 274 | switch(dwOperation) {
|
---|
| 275 | case ASYNCIO_READ:
|
---|
| 276 | case ASYNCIO_READWRITE:
|
---|
| 277 | case ASYNCIO_WRITE:
|
---|
| 278 | if(lpRequest->dwAsyncType == ASYNCIO_READ || lpWriteHandler == NULL) {
|
---|
| 279 | lpRequest->dwLastError = lpReadHandler(lpRequest, &dwResult, NULL);
|
---|
[7560] | 280 | }
|
---|
[7603] | 281 | else lpRequest->dwLastError = lpWriteHandler(lpRequest, &dwResult, NULL);
|
---|
| 282 |
|
---|
[21916] | 283 | if(!lpRequest->fCancelled)
|
---|
[7631] | 284 | {
|
---|
| 285 | lpOverlapped->Internal = lpRequest->dwLastError;
|
---|
| 286 | lpOverlapped->InternalHigh = dwResult;
|
---|
[8638] | 287 |
|
---|
| 288 | //must NOT store result in specified location!!! (stack corruption)
|
---|
| 289 | //if(lpRequest->lpdwResult) {
|
---|
| 290 | // *lpRequest->lpdwResult = dwResult;
|
---|
| 291 | //}
|
---|
[7603] | 292 | #ifdef DEBUG
|
---|
[7631] | 293 | if(lpRequest->dwAsyncType == ASYNCIO_READ) {
|
---|
| 294 | dprintf(("ASYNCIO_READ %x finished; result %x, last error %d", lpOverlapped, dwResult, lpRequest->dwLastError));
|
---|
| 295 | }
|
---|
| 296 | else dprintf(("ASYNCIO_WRITE %x finished; result %x, last error %d", lpOverlapped, dwResult, lpRequest->dwLastError));
|
---|
| 297 | #endif
|
---|
| 298 | //wake up user thread
|
---|
| 299 | ::SetEvent(lpOverlapped->hEvent);
|
---|
[7603] | 300 | }
|
---|
| 301 | break;
|
---|
| 302 |
|
---|
| 303 | case ASYNCIO_POLL:
|
---|
[7604] | 304 | hEventsWait[0] = hEventCancel;
|
---|
[7603] | 305 | hEventsWait[1] = hEventExit;
|
---|
| 306 | ret = WAIT_TIMEOUT;
|
---|
| 307 | while(TRUE)
|
---|
| 308 | {
|
---|
| 309 | dwTimeOut = 0;
|
---|
| 310 | lpRequest->dwLastError = lpPollHandler(lpRequest, &dwResult, &dwTimeOut);
|
---|
| 311 | if(lpRequest->dwLastError != ERROR_IO_PENDING) {
|
---|
| 312 | break;
|
---|
| 313 | }
|
---|
| 314 | if(dwTimeOut == 0) {
|
---|
| 315 | dprintf(("!ERROR!: lpPollHandler returned timeout 0!!"));
|
---|
| 316 | DebugInt3();
|
---|
| 317 | break;
|
---|
| 318 | }
|
---|
| 319 | //sleep a while to avoid wasting too many cpu cycles; we are woken up when a timeout occurs,
|
---|
| 320 | //when the operation is cancelled or when the process exits
|
---|
| 321 | ret = WaitForMultipleObjects(2, hEventsWait, FALSE, dwTimeOut);
|
---|
| 322 | if(ret != WAIT_TIMEOUT) {
|
---|
| 323 | dprintf(("ASYNCIO_POLL: WaitForSingleObject didn't time out, abort (ret = %x)", ret));
|
---|
| 324 | break;
|
---|
| 325 | }
|
---|
| 326 | }
|
---|
| 327 | //Don't access the overlapped & result memory when CancelIo was used to cancel the operation
|
---|
[7631] | 328 | if(ret == WAIT_TIMEOUT && !lpRequest->fCancelled)
|
---|
[7603] | 329 | {
|
---|
| 330 | dprintf(("ASYNCIO_POLL %x: result %x, last error %d", lpOverlapped, dwResult, lpRequest->dwLastError));
|
---|
| 331 | lpOverlapped->Internal = lpRequest->dwLastError;
|
---|
| 332 | lpOverlapped->InternalHigh = dwResult;
|
---|
| 333 | if(lpRequest->lpdwResult) {
|
---|
| 334 | *lpRequest->lpdwResult = dwResult;
|
---|
| 335 | }
|
---|
| 336 | //wake up user thread
|
---|
| 337 | ::SetEvent(lpOverlapped->hEvent);
|
---|
| 338 | }
|
---|
| 339 | break;
|
---|
[7560] | 340 | }
|
---|
[7603] | 341 | //remove from in-process list and delete async request object
|
---|
[8644] | 342 | removeRequest(ASYNC_INDEX_BUSY, lpRequest);
|
---|
[7603] | 343 | delete lpRequest;
|
---|
| 344 | } //while(TRUE)
|
---|
[7550] | 345 | }
|
---|
[8839] | 346 | Release(); //decrease reference count
|
---|
[7550] | 347 | return 0;
|
---|
| 348 | }
|
---|
| 349 | //******************************************************************************
|
---|
| 350 | //******************************************************************************
|
---|
[7560] | 351 | BOOL OverlappedIOHandler::WriteFile(HANDLE hHandle,
|
---|
[7550] | 352 | LPCVOID lpBuffer,
|
---|
| 353 | DWORD nNumberOfBytesToWrite,
|
---|
| 354 | LPDWORD lpNumberOfBytesWritten,
|
---|
| 355 | LPOVERLAPPED lpOverlapped,
|
---|
[7552] | 356 | LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
|
---|
[7564] | 357 | DWORD dwUserData,
|
---|
| 358 | DWORD dwTimeOut)
|
---|
[7550] | 359 | {
|
---|
[7552] | 360 | LPASYNCIOREQUEST lpRequest, current;
|
---|
| 361 | int index;
|
---|
[7560] | 362 |
|
---|
| 363 | if(!lpOverlapped || lpOverlapped->hEvent == 0) {
|
---|
| 364 | ::SetLastError(ERROR_INVALID_PARAMETER);
|
---|
| 365 | return FALSE;
|
---|
| 366 | }
|
---|
[7564] | 367 |
|
---|
[7552] | 368 | lpRequest = new ASYNCIOREQUEST;
|
---|
| 369 | if(lpRequest == NULL) {
|
---|
| 370 | ::SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
| 371 | return FALSE;
|
---|
| 372 | }
|
---|
| 373 | lpRequest->dwAsyncType = ASYNCIO_WRITE;
|
---|
[7560] | 374 | lpRequest->hHandle = hHandle;
|
---|
[7552] | 375 | lpRequest->lpBuffer = lpBuffer;
|
---|
| 376 | lpRequest->nNumberOfBytes = nNumberOfBytesToWrite;
|
---|
[8638] | 377 | //must NOT store result in specified location!!! (stack corruption)
|
---|
| 378 | //// lpRequest->lpdwResult = lpNumberOfBytesWritten;
|
---|
| 379 | lpRequest->lpdwResult = NULL;
|
---|
[7552] | 380 | lpRequest->lpOverlapped = lpOverlapped;
|
---|
| 381 | lpRequest->lpCompletionRoutine = lpCompletionRoutine;
|
---|
| 382 | lpRequest->dwUserData = dwUserData;
|
---|
[7564] | 383 | lpRequest->dwTimeOut = dwTimeOut;
|
---|
[7552] | 384 | lpRequest->next = NULL;
|
---|
| 385 |
|
---|
[7598] | 386 | if(dwAsyncType & ASYNCIO_READWRITE) {
|
---|
[7552] | 387 | index = ASYNC_INDEX_READ;
|
---|
| 388 | }
|
---|
| 389 | else index = ASYNC_INDEX_WRITE;
|
---|
| 390 |
|
---|
[7604] | 391 | lpOverlapped->Internal = STATUS_PENDING;
|
---|
[7560] | 392 | lpOverlapped->InternalHigh = 0;
|
---|
| 393 | //reset overlapped semaphore to non-signalled
|
---|
| 394 | ::ResetEvent(lpOverlapped->hEvent);
|
---|
| 395 |
|
---|
[9340] | 396 | //Add request to queue; must make sure we do this right before waking the async thread
|
---|
| 397 | addRequest(index, lpRequest);
|
---|
| 398 |
|
---|
[7552] | 399 | //wake up async thread
|
---|
[7598] | 400 | ::SetEvent((dwAsyncType & ASYNCIO_READWRITE) ? hEventRead : hEventWrite);
|
---|
[7552] | 401 |
|
---|
| 402 | ::SetLastError(ERROR_IO_PENDING);
|
---|
[7550] | 403 | return FALSE;
|
---|
| 404 | }
|
---|
| 405 | //******************************************************************************
|
---|
| 406 | //******************************************************************************
|
---|
[7560] | 407 | BOOL OverlappedIOHandler::ReadFile(HANDLE hHandle,
|
---|
[7550] | 408 | LPCVOID lpBuffer,
|
---|
| 409 | DWORD nNumberOfBytesToRead,
|
---|
| 410 | LPDWORD lpNumberOfBytesRead,
|
---|
| 411 | LPOVERLAPPED lpOverlapped,
|
---|
[7552] | 412 | LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
|
---|
[7564] | 413 | DWORD dwUserData,
|
---|
| 414 | DWORD dwTimeOut)
|
---|
[7550] | 415 | {
|
---|
[7552] | 416 | LPASYNCIOREQUEST lpRequest, current;
|
---|
[7560] | 417 |
|
---|
| 418 | if(!lpOverlapped || lpOverlapped->hEvent == 0) {
|
---|
| 419 | ::SetLastError(ERROR_INVALID_PARAMETER);
|
---|
| 420 | return FALSE;
|
---|
| 421 | }
|
---|
[7564] | 422 |
|
---|
[7552] | 423 | lpRequest = new ASYNCIOREQUEST;
|
---|
| 424 | if(lpRequest == NULL) {
|
---|
| 425 | ::SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
| 426 | return FALSE;
|
---|
| 427 | }
|
---|
| 428 | lpRequest->dwAsyncType = ASYNCIO_READ;
|
---|
[7560] | 429 | lpRequest->hHandle = hHandle;
|
---|
[7552] | 430 | lpRequest->lpBuffer = lpBuffer;
|
---|
| 431 | lpRequest->nNumberOfBytes = nNumberOfBytesToRead;
|
---|
[8638] | 432 | //must NOT store result in specified location!!! (stack corruption)
|
---|
| 433 | //// lpRequest->lpdwResult = lpNumberOfBytesRead;
|
---|
| 434 | lpRequest->lpdwResult = NULL;
|
---|
[7552] | 435 | lpRequest->lpOverlapped = lpOverlapped;
|
---|
| 436 | lpRequest->lpCompletionRoutine = lpCompletionRoutine;
|
---|
| 437 | lpRequest->dwUserData = dwUserData;
|
---|
[7564] | 438 | lpRequest->dwTimeOut = dwTimeOut;
|
---|
[7552] | 439 | lpRequest->next = NULL;
|
---|
| 440 |
|
---|
[7604] | 441 | lpOverlapped->Internal = STATUS_PENDING;
|
---|
[7560] | 442 | lpOverlapped->InternalHigh = 0;
|
---|
| 443 | //reset overlapped semaphore to non-signalled
|
---|
| 444 | ::ResetEvent(lpOverlapped->hEvent);
|
---|
| 445 |
|
---|
[9340] | 446 | //Add request to queue; must make sure we do this right before waking the async thread
|
---|
| 447 | addRequest(ASYNC_INDEX_READ, lpRequest);
|
---|
| 448 |
|
---|
[7552] | 449 | //wake up async thread
|
---|
| 450 | ::SetEvent(hEventRead);
|
---|
| 451 | ::SetLastError(ERROR_IO_PENDING);
|
---|
[7550] | 452 | return FALSE;
|
---|
| 453 | }
|
---|
| 454 | //******************************************************************************
|
---|
| 455 | //******************************************************************************
|
---|
[7560] | 456 | BOOL OverlappedIOHandler::WaitForEvent(HANDLE hHandle,
|
---|
[7567] | 457 | DWORD dwEventMask,
|
---|
[7560] | 458 | LPDWORD lpfdwEvtMask,
|
---|
| 459 | LPOVERLAPPED lpOverlapped,
|
---|
| 460 | LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine,
|
---|
[7564] | 461 | DWORD dwUserData,
|
---|
| 462 | DWORD dwTimeOut)
|
---|
[7550] | 463 | {
|
---|
[7560] | 464 | LPASYNCIOREQUEST lpRequest, current;
|
---|
[7596] | 465 | DWORD dwLastError, dwResult;
|
---|
[7560] | 466 |
|
---|
| 467 | if(!lpOverlapped || lpOverlapped->hEvent == 0) {
|
---|
| 468 | ::SetLastError(ERROR_INVALID_PARAMETER);
|
---|
| 469 | return FALSE;
|
---|
| 470 | }
|
---|
[7596] | 471 | if(!lpPollHandler) {
|
---|
| 472 | DebugInt3();
|
---|
| 473 | ::SetLastError(ERROR_INVALID_PARAMETER);
|
---|
| 474 | return FALSE;
|
---|
| 475 | }
|
---|
[7564] | 476 |
|
---|
[7560] | 477 | lpRequest = new ASYNCIOREQUEST;
|
---|
| 478 | if(lpRequest == NULL) {
|
---|
| 479 | ::SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
---|
| 480 | return FALSE;
|
---|
| 481 | }
|
---|
| 482 | lpRequest->dwAsyncType = ASYNCIO_POLL;
|
---|
| 483 | lpRequest->hHandle = hHandle;
|
---|
| 484 | lpRequest->lpBuffer = NULL;
|
---|
| 485 | lpRequest->nNumberOfBytes = 0;
|
---|
[8638] | 486 | //must store result also in specified location
|
---|
[7560] | 487 | lpRequest->lpdwResult = lpfdwEvtMask;
|
---|
| 488 | lpRequest->lpOverlapped = lpOverlapped;
|
---|
| 489 | lpRequest->lpCompletionRoutine = lpCompletionRoutine;
|
---|
| 490 | lpRequest->dwUserData = dwUserData;
|
---|
[7564] | 491 | lpRequest->dwTimeOut = dwTimeOut;
|
---|
[7567] | 492 | lpRequest->dwEventMask = dwEventMask;
|
---|
[7560] | 493 | lpRequest->next = NULL;
|
---|
| 494 |
|
---|
[7604] | 495 | lpOverlapped->Internal = STATUS_PENDING;
|
---|
[7596] | 496 | lpOverlapped->InternalHigh = 0;
|
---|
| 497 | //reset overlapped semaphore to non-signalled
|
---|
| 498 | ::ResetEvent(lpOverlapped->hEvent);
|
---|
| 499 |
|
---|
| 500 | //first check if the event has already occured; if so, return result
|
---|
| 501 | //immediately
|
---|
| 502 | dwLastError = lpPollHandler(lpRequest, &dwResult, &dwTimeOut);
|
---|
[7603] | 503 | if(dwLastError != ERROR_IO_PENDING)
|
---|
[7596] | 504 | {
|
---|
| 505 | dprintf(("OverlappedIOHandler::WaitForEvent %x: result %x, last error %d", lpOverlapped, dwResult, dwLastError));
|
---|
| 506 | lpOverlapped->Internal = dwLastError;
|
---|
| 507 | lpOverlapped->InternalHigh = dwResult;
|
---|
| 508 | if(lpfdwEvtMask) {
|
---|
| 509 | *lpfdwEvtMask = dwResult;
|
---|
| 510 | }
|
---|
| 511 | //wake up user thread
|
---|
| 512 | ::SetEvent(lpOverlapped->hEvent);
|
---|
| 513 |
|
---|
| 514 | delete lpRequest;
|
---|
| 515 | ::SetLastError(dwLastError);
|
---|
| 516 | return (dwLastError == ERROR_SUCCESS);
|
---|
| 517 | }
|
---|
| 518 |
|
---|
[9340] | 519 | //add request to list; must make sure we do this right before waking the async thread
|
---|
[8644] | 520 | addRequest(ASYNC_INDEX_POLL, lpRequest);
|
---|
[7560] | 521 |
|
---|
| 522 | //wake up async thread
|
---|
| 523 | ::SetEvent(hEventPoll);
|
---|
| 524 | ::SetLastError(ERROR_IO_PENDING);
|
---|
[7550] | 525 | return FALSE;
|
---|
| 526 | }
|
---|
| 527 | //******************************************************************************
|
---|
| 528 | //******************************************************************************
|
---|
[7560] | 529 | BOOL OverlappedIOHandler::CancelIo(HANDLE hHandle)
|
---|
| 530 | {
|
---|
| 531 | LPASYNCIOREQUEST lpRequest;
|
---|
| 532 |
|
---|
[7564] | 533 | for(int i=ASYNC_INDEX_READ;i<NR_ASYNC_OPERATIONS;i++)
|
---|
[7560] | 534 | {
|
---|
[21916] | 535 | while(TRUE)
|
---|
[7604] | 536 | {
|
---|
[7560] | 537 | lpRequest = findAndRemoveRequest(i, hHandle);
|
---|
[7567] | 538 |
|
---|
[7560] | 539 | if(lpRequest) {
|
---|
[7604] | 540 | //TODO: This doesn't work if multiple handles share the
|
---|
| 541 | // same OverlappedIOHandler
|
---|
| 542 | lpRequest->fCancelled = TRUE;
|
---|
[8958] | 543 | //GetOverlappedResult must return this error after the request
|
---|
| 544 | //has been cancelled!
|
---|
| 545 | lpRequest->lpOverlapped->Internal = ERROR_OPERATION_ABORTED;
|
---|
[7604] | 546 | ::SetEvent(hEventCancel); //cancel pending operation
|
---|
[7567] | 547 | if(i != ASYNC_INDEX_BUSY) {//thread that handles the request will delete it
|
---|
| 548 | delete lpRequest;
|
---|
| 549 | }
|
---|
[7560] | 550 | }
|
---|
| 551 | else break;
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 | //TODO: return error if there were no pending requests???
|
---|
| 555 | ::SetLastError(ERROR_SUCCESS);
|
---|
| 556 | return TRUE;
|
---|
| 557 | }
|
---|
| 558 | //******************************************************************************
|
---|
| 559 | //******************************************************************************
|
---|
| 560 | BOOL OverlappedIOHandler::GetOverlappedResult(HANDLE hHandle,
|
---|
| 561 | LPOVERLAPPED lpOverlapped,
|
---|
[7550] | 562 | LPDWORD lpcbTransfer,
|
---|
[7564] | 563 | BOOL fWait)
|
---|
[7550] | 564 | {
|
---|
[7560] | 565 | DWORD ret;
|
---|
| 566 |
|
---|
[7564] | 567 | ret = ::WaitForSingleObject(lpOverlapped->hEvent, (fWait) ? INFINITE : 0);
|
---|
[7560] | 568 |
|
---|
| 569 | if(lpcbTransfer)
|
---|
| 570 | *lpcbTransfer = lpOverlapped->InternalHigh;
|
---|
| 571 |
|
---|
| 572 | ::SetLastError(lpOverlapped->Internal);
|
---|
| 573 |
|
---|
[7596] | 574 | dprintf(("GetOverlappedResult %x -> result %d last error %d", hHandle, lpOverlapped->InternalHigh, lpOverlapped->Internal));
|
---|
[7560] | 575 | return (ret == WAIT_OBJECT_0);
|
---|
[7550] | 576 | }
|
---|
| 577 | //******************************************************************************
|
---|
| 578 | //******************************************************************************
|
---|
[7560] | 579 | LPASYNCIOREQUEST OverlappedIOHandler::findAndRemoveRequest(int index, HANDLE hHandle)
|
---|
| 580 | {
|
---|
| 581 | LPASYNCIOREQUEST lpRequest, lpFound = NULL;
|
---|
| 582 |
|
---|
| 583 | ::EnterCriticalSection(&critsect);
|
---|
[7564] | 584 | if(pending[index])
|
---|
[7560] | 585 | {
|
---|
[7564] | 586 | if(pending[index]->hHandle != hHandle)
|
---|
[7560] | 587 | {
|
---|
| 588 | lpRequest = pending[index];
|
---|
| 589 | while(lpRequest->next) {
|
---|
| 590 | if(lpRequest->next->hHandle == hHandle) {
|
---|
| 591 | lpFound = lpRequest->next;
|
---|
| 592 | lpRequest->next = lpFound->next;
|
---|
| 593 | break;
|
---|
| 594 | }
|
---|
| 595 | lpRequest = lpRequest->next;
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 | else {
|
---|
| 599 | lpFound = pending[index];
|
---|
| 600 | pending[index] = lpFound->next;
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
| 603 | ::LeaveCriticalSection(&critsect);
|
---|
| 604 | return lpFound;
|
---|
| 605 | }
|
---|
| 606 | //******************************************************************************
|
---|
| 607 | //******************************************************************************
|
---|
[8644] | 608 | void OverlappedIOHandler::addRequest(int index, LPASYNCIOREQUEST lpRequest)
|
---|
| 609 | {
|
---|
| 610 | LPASYNCIOREQUEST current;
|
---|
[7567] | 611 |
|
---|
[8644] | 612 | ::EnterCriticalSection(&critsect);
|
---|
| 613 | if(pending[index]) {
|
---|
| 614 | current = pending[index];
|
---|
| 615 | while(current->next) {
|
---|
| 616 | current = current->next;
|
---|
| 617 | }
|
---|
| 618 | current->next = lpRequest;
|
---|
| 619 | }
|
---|
| 620 | else pending[index] = lpRequest;
|
---|
| 621 | ::LeaveCriticalSection(&critsect);
|
---|
| 622 | }
|
---|
| 623 | //******************************************************************************
|
---|
| 624 | //******************************************************************************
|
---|
| 625 | void OverlappedIOHandler::removeRequest(int index, LPASYNCIOREQUEST lpRequest)
|
---|
| 626 | {
|
---|
| 627 | LPASYNCIOREQUEST current;
|
---|
| 628 |
|
---|
| 629 | ::EnterCriticalSection(&critsect);
|
---|
| 630 | if(pending[index]) {
|
---|
| 631 | if(pending[index] == lpRequest) {
|
---|
| 632 | pending[index] = lpRequest->next;
|
---|
| 633 | }
|
---|
| 634 | else {
|
---|
| 635 | current = pending[index];
|
---|
| 636 | while(current->next && current->next != lpRequest) {
|
---|
| 637 | current = current->next;
|
---|
| 638 | }
|
---|
| 639 | if(current->next) {
|
---|
| 640 | current->next = lpRequest->next;
|
---|
| 641 | }
|
---|
| 642 | else {
|
---|
| 643 | dprintf(("!ERROR!: request %x not found!!!!!!", lpRequest));
|
---|
| 644 | DebugInt3();
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 | //else removed from list by cancelio
|
---|
| 649 | ::LeaveCriticalSection(&critsect);
|
---|
| 650 | }
|
---|
| 651 | //******************************************************************************
|
---|
| 652 | //******************************************************************************
|
---|
| 653 |
|
---|