| 1 | /* $Id: asyncthread.cpp,v 1.15 2002-02-23 16:39:09 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Async thread help functions | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * TODO: Not everything is 100% thread safe  (i.e. async parameter updates) | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 | #define INCL_BASE | 
|---|
| 14 | #include <os2wrap.h> | 
|---|
| 15 | #include <os2sel.h> | 
|---|
| 16 | #include <stdlib.h> | 
|---|
| 17 | #include <wprocess.h> | 
|---|
| 18 | #include <win32api.h> | 
|---|
| 19 | #include <misc.h> | 
|---|
| 20 | #include <string.h> | 
|---|
| 21 | #include <vmutex.h> | 
|---|
| 22 | #include "wsock32.h" | 
|---|
| 23 | #include "asyncthread.h" | 
|---|
| 24 |  | 
|---|
| 25 | static PASYNCTHREADPARM threadList = NULL; | 
|---|
| 26 | VMutex asyncThreadMutex; | 
|---|
| 27 |  | 
|---|
| 28 | static void AddToQueue(PASYNCTHREADPARM pThreadParm); | 
|---|
| 29 |  | 
|---|
| 30 | //****************************************************************************** | 
|---|
| 31 | //****************************************************************************** | 
|---|
| 32 | static void _Optlink AsyncThread(void *arg) | 
|---|
| 33 | { | 
|---|
| 34 | PASYNCTHREADPARM pThreadParm = (PASYNCTHREADPARM)arg; | 
|---|
| 35 | #ifdef DEBUG | 
|---|
| 36 | ULONG ulSocket    = pThreadParm->u.asyncselect.s; | 
|---|
| 37 | ULONG hTaskHandle = pThreadParm->hAsyncTaskHandle; | 
|---|
| 38 | #endif | 
|---|
| 39 |  | 
|---|
| 40 | pThreadParm->asyncProc((PVOID)arg); | 
|---|
| 41 |  | 
|---|
| 42 | //only for blocking hooks (currently not implemented) | 
|---|
| 43 | ////  if(pThreadParm->request == ASYNC_BLOCKHOOK) | 
|---|
| 44 | ////    WSASetBlocking(FALSE, pThreadParm->hThread); | 
|---|
| 45 |  | 
|---|
| 46 | dprintf(("AsyncThread %x socket %x exit", hTaskHandle, ulSocket)); | 
|---|
| 47 | free((PVOID)pThreadParm); | 
|---|
| 48 | } | 
|---|
| 49 | //****************************************************************************** | 
|---|
| 50 | //****************************************************************************** | 
|---|
| 51 | ULONG QueueAsyncJob(ASYNCTHREADPROC asyncproc, PASYNCTHREADPARM pThreadParm, BOOL fSetBlocking) | 
|---|
| 52 | { | 
|---|
| 53 | TID tid; | 
|---|
| 54 |  | 
|---|
| 55 | dprintf(("QueueAsyncJob for socket %x", pThreadParm->u.asyncselect.s)); | 
|---|
| 56 |  | 
|---|
| 57 | pThreadParm->asyncProc         = asyncproc; | 
|---|
| 58 | pThreadParm->fActive           = TRUE; | 
|---|
| 59 | pThreadParm->fCancelled        = FALSE; | 
|---|
| 60 | pThreadParm->next              = NULL; | 
|---|
| 61 | pThreadParm->hAsyncTaskHandle  = 0; | 
|---|
| 62 |  | 
|---|
| 63 | pThreadParm->hThread           = GetCurrentThread(); | 
|---|
| 64 | AddToQueue(pThreadParm); | 
|---|
| 65 |  | 
|---|
| 66 | if(fSetBlocking) WSASetBlocking(TRUE); | 
|---|
| 67 |  | 
|---|
| 68 | USHORT sel = GetFS(); | 
|---|
| 69 | tid = _beginthread(AsyncThread, NULL, 16384, (PVOID)pThreadParm); | 
|---|
| 70 | SetFS(sel); | 
|---|
| 71 | if (tid == -1) { | 
|---|
| 72 | dprintf(("QueueAsyncJob: _beginthread failed")); | 
|---|
| 73 | if(fSetBlocking) WSASetBlocking(FALSE); | 
|---|
| 74 | RemoveFromQueue(pThreadParm); | 
|---|
| 75 | WSASetLastError(WSAEFAULT); | 
|---|
| 76 | return 0; | 
|---|
| 77 | } | 
|---|
| 78 | pThreadParm->hAsyncTaskHandle = tid; | 
|---|
| 79 | WSASetLastError(NO_ERROR); | 
|---|
| 80 | return pThreadParm->hAsyncTaskHandle; | 
|---|
| 81 | } | 
|---|
| 82 | //****************************************************************************** | 
|---|
| 83 | //****************************************************************************** | 
|---|
| 84 | void AddToQueue(PASYNCTHREADPARM pThreadParm) | 
|---|
| 85 | { | 
|---|
| 86 | asyncThreadMutex.enter(); | 
|---|
| 87 | pThreadParm->next = threadList; | 
|---|
| 88 | threadList        = pThreadParm; | 
|---|
| 89 | asyncThreadMutex.leave(); | 
|---|
| 90 | } | 
|---|
| 91 | //****************************************************************************** | 
|---|
| 92 | //****************************************************************************** | 
|---|
| 93 | void RemoveFromQueue(PASYNCTHREADPARM pThreadParm) | 
|---|
| 94 | { | 
|---|
| 95 | PASYNCTHREADPARM pThreadInfo; | 
|---|
| 96 |  | 
|---|
| 97 | asyncThreadMutex.enter(); | 
|---|
| 98 | pThreadInfo = threadList; | 
|---|
| 99 |  | 
|---|
| 100 | if(pThreadInfo == pThreadParm) { | 
|---|
| 101 | threadList = pThreadParm->next; | 
|---|
| 102 | } | 
|---|
| 103 | else { | 
|---|
| 104 | while(pThreadInfo->next) { | 
|---|
| 105 | if(pThreadInfo->next == pThreadParm) { | 
|---|
| 106 | pThreadInfo->next = pThreadParm->next; | 
|---|
| 107 | break; | 
|---|
| 108 | } | 
|---|
| 109 | pThreadInfo = pThreadInfo->next; | 
|---|
| 110 | } | 
|---|
| 111 | if(pThreadInfo == NULL) { | 
|---|
| 112 | dprintf(("RemoveFromQueue: parm %x not found!!", pThreadParm)); | 
|---|
| 113 | DebugInt3(); | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 | memset(pThreadParm, 0, sizeof(*pThreadParm)); | 
|---|
| 117 | asyncThreadMutex.leave(); | 
|---|
| 118 | } | 
|---|
| 119 | //****************************************************************************** | 
|---|
| 120 | //****************************************************************************** | 
|---|
| 121 | void WSACancelAllAsyncRequests() | 
|---|
| 122 | { | 
|---|
| 123 | PASYNCTHREADPARM pThreadInfo; | 
|---|
| 124 | BOOL             found = FALSE; | 
|---|
| 125 |  | 
|---|
| 126 | dprintf(("WSACancelAllAsyncRequests")); | 
|---|
| 127 | asyncThreadMutex.enter(); | 
|---|
| 128 | pThreadInfo = threadList; | 
|---|
| 129 |  | 
|---|
| 130 | while(pThreadInfo) { | 
|---|
| 131 | dprintf(("WSACancelAllAsyncRequests %x", pThreadInfo->hAsyncTaskHandle)); | 
|---|
| 132 | pThreadInfo->fCancelled = TRUE; | 
|---|
| 133 | pThreadInfo->u.asyncselect.asyncSem->post(); | 
|---|
| 134 | pThreadInfo = pThreadInfo->next; | 
|---|
| 135 | } | 
|---|
| 136 | asyncThreadMutex.leave(); | 
|---|
| 137 | //TODO: not the right way to wait for the async threads to die | 
|---|
| 138 | DosSleep(250); | 
|---|
| 139 | } | 
|---|
| 140 | //****************************************************************************** | 
|---|
| 141 | //****************************************************************************** | 
|---|
| 142 | int WIN32API WSACancelAsyncRequest(LHANDLE hAsyncTaskHandle) | 
|---|
| 143 | { | 
|---|
| 144 | PASYNCTHREADPARM pThreadInfo; | 
|---|
| 145 | BOOL             found = FALSE; | 
|---|
| 146 |  | 
|---|
| 147 | dprintf(("WSACancelAsyncRequest: cancel task %x", hAsyncTaskHandle)); | 
|---|
| 148 | asyncThreadMutex.enter(); | 
|---|
| 149 | pThreadInfo = threadList; | 
|---|
| 150 |  | 
|---|
| 151 | while(pThreadInfo) { | 
|---|
| 152 | if(pThreadInfo->hAsyncTaskHandle == hAsyncTaskHandle) { | 
|---|
| 153 | pThreadInfo->fCancelled = TRUE; | 
|---|
| 154 | found = TRUE; | 
|---|
| 155 | break; | 
|---|
| 156 | } | 
|---|
| 157 | pThreadInfo = pThreadInfo->next; | 
|---|
| 158 | } | 
|---|
| 159 | asyncThreadMutex.leave(); | 
|---|
| 160 | if(found == FALSE) { | 
|---|
| 161 | WSASetLastError(WSAEINVAL); | 
|---|
| 162 | dprintf(("WSACancelAsyncRequest: task not found!!")); | 
|---|
| 163 | } | 
|---|
| 164 | return (found) ? NO_ERROR : SOCKET_ERROR; | 
|---|
| 165 | } | 
|---|
| 166 | //****************************************************************************** | 
|---|
| 167 | //Only to cancel blocking hooks | 
|---|
| 168 | //****************************************************************************** | 
|---|
| 169 | int WIN32API WSACancelBlockingCall() | 
|---|
| 170 | { | 
|---|
| 171 | HANDLE hThread = GetCurrentThread(); | 
|---|
| 172 |  | 
|---|
| 173 | dprintf(("WSACancelBlockingCall")); | 
|---|
| 174 | #if 0 | 
|---|
| 175 | asyncThreadMutex.enter(); | 
|---|
| 176 | pThreadInfo = threadList; | 
|---|
| 177 |  | 
|---|
| 178 | while(pThreadInfo) { | 
|---|
| 179 | if(pThreadInfo->hThread == hThread) { | 
|---|
| 180 | pThreadInfo->fCancelled = TRUE; | 
|---|
| 181 |  | 
|---|
| 182 | if(pThreadInfo->request == ASYNC_BLOCKHOOK) { | 
|---|
| 183 | ret = so_cancel(pThreadInfo->blockedsocket); | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | found = TRUE; | 
|---|
| 187 | break; | 
|---|
| 188 | } | 
|---|
| 189 | pThreadInfo = pThreadInfo->next; | 
|---|
| 190 | } | 
|---|
| 191 | asyncThreadMutex.leave(); | 
|---|
| 192 | #endif | 
|---|
| 193 | return SOCKET_ERROR; | 
|---|
| 194 | } | 
|---|
| 195 | //****************************************************************************** | 
|---|
| 196 | //dump queue | 
|---|
| 197 | //****************************************************************************** | 
|---|
| 198 | void DumpQueue(void) | 
|---|
| 199 | { | 
|---|
| 200 | HANDLE hThread = GetCurrentThread(); | 
|---|
| 201 | PASYNCTHREADPARM pThreadInfo; | 
|---|
| 202 |  | 
|---|
| 203 | dprintf(("DumpQueue")); | 
|---|
| 204 |  | 
|---|
| 205 | asyncThreadMutex.enter(); | 
|---|
| 206 | pThreadInfo = threadList; | 
|---|
| 207 |  | 
|---|
| 208 | while(pThreadInfo) { | 
|---|
| 209 | dprintf(( "SOCKET %08xh thread#%d events %xh pending %xh, select %d", | 
|---|
| 210 | pThreadInfo->u.asyncselect.s, | 
|---|
| 211 | pThreadInfo->hAsyncTaskHandle, | 
|---|
| 212 | pThreadInfo->u.asyncselect.lEvents, | 
|---|
| 213 | pThreadInfo->u.asyncselect.lEventsPending, | 
|---|
| 214 | pThreadInfo->fWaitSelect)); | 
|---|
| 215 | pThreadInfo = pThreadInfo->next; | 
|---|
| 216 | } | 
|---|
| 217 | asyncThreadMutex.leave(); | 
|---|
| 218 | dprintf(("DumpQueue done")); | 
|---|
| 219 | } | 
|---|
| 220 | //****************************************************************************** | 
|---|
| 221 | //Assumes caller owns async thread mutex! | 
|---|
| 222 | //****************************************************************************** | 
|---|
| 223 | PASYNCTHREADPARM FindAsyncEvent(SOCKET s) | 
|---|
| 224 | { | 
|---|
| 225 | PASYNCTHREADPARM pThreadInfo; | 
|---|
| 226 |  | 
|---|
| 227 | pThreadInfo = threadList; | 
|---|
| 228 | while(pThreadInfo) { | 
|---|
| 229 | if(pThreadInfo->u.asyncselect.s == s && !pThreadInfo->fRemoved) { | 
|---|
| 230 | return pThreadInfo; | 
|---|
| 231 | } | 
|---|
| 232 | pThreadInfo = pThreadInfo->next; | 
|---|
| 233 | } | 
|---|
| 234 | return NULL; | 
|---|
| 235 | } | 
|---|
| 236 | //****************************************************************************** | 
|---|
| 237 | //****************************************************************************** | 
|---|
| 238 | BOOL FindAndSetAsyncEvent(SOCKET s, int mode, int notifyHandle, int notifyData, ULONG lEventMask) | 
|---|
| 239 | { | 
|---|
| 240 | PASYNCTHREADPARM pThreadInfo; | 
|---|
| 241 |  | 
|---|
| 242 | asyncThreadMutex.enter(); | 
|---|
| 243 | pThreadInfo = FindAsyncEvent(s); | 
|---|
| 244 | if(pThreadInfo) | 
|---|
| 245 | { | 
|---|
| 246 | int size, state, tmp; | 
|---|
| 247 | state = ioctl(s, FIOBSTATUS, (char *)&tmp, sizeof(tmp)); | 
|---|
| 248 | dprintf(("FindAndSetAsyncEvent: state %x", state)); | 
|---|
| 249 |  | 
|---|
| 250 | //Don't send FD_CONNECT is socket was already connected (accept returns connected socket) | 
|---|
| 251 | if(state & SS_ISCONNECTED) { | 
|---|
| 252 | pThreadInfo->fConnected = TRUE; | 
|---|
| 253 | } | 
|---|
| 254 | else pThreadInfo->fConnected = FALSE; | 
|---|
| 255 |  | 
|---|
| 256 | pThreadInfo->u.asyncselect.mode           = mode; | 
|---|
| 257 | pThreadInfo->u.asyncselect.lEvents        = lEventMask; | 
|---|
| 258 | pThreadInfo->u.asyncselect.lEventsPending = lEventMask; | 
|---|
| 259 | pThreadInfo->notifyHandle                 = notifyHandle; | 
|---|
| 260 | pThreadInfo->notifyData                   = notifyData; | 
|---|
| 261 | if(lEventMask == 0) { | 
|---|
| 262 | //make sure this thread isn't used anymore | 
|---|
| 263 | pThreadInfo->fRemoved = TRUE; | 
|---|
| 264 | } | 
|---|
| 265 | if(pThreadInfo->fWaitSelect) { | 
|---|
| 266 | //cancel pending select in async select thread (if any) | 
|---|
| 267 | so_cancel(s); | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | //unblock async thread if it was waiting | 
|---|
| 271 | pThreadInfo->u.asyncselect.asyncSem->post(); | 
|---|
| 272 | } | 
|---|
| 273 | asyncThreadMutex.leave(); | 
|---|
| 274 | return(pThreadInfo != NULL); | 
|---|
| 275 | } | 
|---|
| 276 | //****************************************************************************** | 
|---|
| 277 | //****************************************************************************** | 
|---|
| 278 | void EnableAsyncEvent(SOCKET s, ULONG flags) | 
|---|
| 279 | { | 
|---|
| 280 | PASYNCTHREADPARM pThreadInfo; | 
|---|
| 281 |  | 
|---|
| 282 | asyncThreadMutex.enter(); | 
|---|
| 283 | pThreadInfo = FindAsyncEvent(s); | 
|---|
| 284 | if(pThreadInfo) { | 
|---|
| 285 | pThreadInfo->u.asyncselect.lEventsPending |= (pThreadInfo->u.asyncselect.lEvents & flags); | 
|---|
| 286 | //unblock async thread if it was waiting | 
|---|
| 287 | pThreadInfo->u.asyncselect.asyncSem->post(); | 
|---|
| 288 |  | 
|---|
| 289 | //cancel pending select in async select thread (if any) | 
|---|
| 290 | if(pThreadInfo->fWaitSelect) { | 
|---|
| 291 | so_cancel(s); | 
|---|
| 292 | } | 
|---|
| 293 | } | 
|---|
| 294 | asyncThreadMutex.leave(); | 
|---|
| 295 | } | 
|---|
| 296 | //****************************************************************************** | 
|---|
| 297 | //****************************************************************************** | 
|---|
| 298 | BOOL QueryAsyncEvent(SOCKET s, int *pMode, ULONG *pNofityHandle, ULONG *pNofityData, | 
|---|
| 299 | ULONG *plEvent) | 
|---|
| 300 | { | 
|---|
| 301 | PASYNCTHREADPARM pThreadInfo; | 
|---|
| 302 |  | 
|---|
| 303 | asyncThreadMutex.enter(); | 
|---|
| 304 | pThreadInfo = FindAsyncEvent(s); | 
|---|
| 305 | if(pThreadInfo) { | 
|---|
| 306 | *pMode         = pThreadInfo->u.asyncselect.mode; | 
|---|
| 307 | *pNofityHandle = pThreadInfo->notifyHandle; | 
|---|
| 308 | *pNofityData   = pThreadInfo->notifyData; | 
|---|
| 309 | *plEvent       = pThreadInfo->u.asyncselect.lEvents; | 
|---|
| 310 | } | 
|---|
| 311 | asyncThreadMutex.leave(); | 
|---|
| 312 | return(pThreadInfo != NULL); | 
|---|
| 313 | } | 
|---|
| 314 | //****************************************************************************** | 
|---|
| 315 | //****************************************************************************** | 
|---|