[3226] | 1 | /* $Id: asyncthread.cpp,v 1.3 2000-03-24 19:22:51 sandervl Exp $ */
|
---|
[3198] | 2 |
|
---|
| 3 | /*
|
---|
| 4 | * Async thread help functions
|
---|
| 5 | *
|
---|
| 6 | * Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
| 7 | *
|
---|
[3205] | 8 | * TODO: Not everything is 100% thread safe (i.e. async parameter updates)
|
---|
[3198] | 9 | *
|
---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
[3205] | 11 | *
|
---|
[3198] | 12 | */
|
---|
| 13 | #define INCL_BASE
|
---|
| 14 | #include <os2wrap.h>
|
---|
| 15 | #include <os2sel.h>
|
---|
[3205] | 16 | #include <stdlib.h>
|
---|
[3198] | 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 | static VMutex asyncThreadMutex;
|
---|
| 27 |
|
---|
| 28 | static void AddToQueue(PASYNCTHREADPARM pThreadParm);
|
---|
| 29 |
|
---|
| 30 | //******************************************************************************
|
---|
| 31 | //******************************************************************************
|
---|
| 32 | static void APIENTRY AsyncThread(ULONG arg)
|
---|
| 33 | {
|
---|
| 34 | PASYNCTHREADPARM pThreadParm = (PASYNCTHREADPARM)arg;
|
---|
| 35 |
|
---|
| 36 | pThreadParm->asyncProc((PVOID)arg);
|
---|
| 37 |
|
---|
[3226] | 38 | //only for blocking hooks (currently not implemented)
|
---|
[3205] | 39 | //// if(pThreadParm->request == ASYNC_BLOCKHOOK)
|
---|
| 40 | //// WSASetBlocking(FALSE, pThreadParm->hThread);
|
---|
[3198] | 41 |
|
---|
[3205] | 42 | free((PVOID)pThreadParm);
|
---|
| 43 |
|
---|
[3198] | 44 | DosExit(EXIT_THREAD, 0);
|
---|
| 45 | }
|
---|
| 46 | //******************************************************************************
|
---|
| 47 | //******************************************************************************
|
---|
[3205] | 48 | ULONG QueueAsyncJob(ASYNCTHREADPROC asyncproc, PASYNCTHREADPARM pThreadParm, BOOL fSetBlocking)
|
---|
[3198] | 49 | {
|
---|
| 50 | APIRET rc;
|
---|
| 51 | TID tid;
|
---|
| 52 |
|
---|
| 53 | pThreadParm->asyncProc = asyncproc;
|
---|
| 54 | pThreadParm->fActive = TRUE;
|
---|
| 55 | pThreadParm->fCancelled = FALSE;
|
---|
| 56 | pThreadParm->next = NULL;
|
---|
| 57 | pThreadParm->hAsyncTaskHandle = 0;
|
---|
| 58 |
|
---|
| 59 | pThreadParm->hThread = GetCurrentThread();
|
---|
| 60 | AddToQueue(pThreadParm);
|
---|
| 61 |
|
---|
[3205] | 62 | if(fSetBlocking) WSASetBlocking(TRUE);
|
---|
[3198] | 63 |
|
---|
| 64 | rc = DosCreateThread(&tid, AsyncThread, (ULONG)pThreadParm, CREATE_READY, 16384);
|
---|
| 65 | if(rc)
|
---|
| 66 | {
|
---|
| 67 | dprintf(("QueueAsyncJob: DosCreateThread failed with error %x", rc));
|
---|
[3205] | 68 | if(fSetBlocking) WSASetBlocking(FALSE);
|
---|
[3198] | 69 | RemoveFromQueue(pThreadParm);
|
---|
| 70 | WSASetLastError(WSAEFAULT);
|
---|
| 71 | return 0;
|
---|
| 72 | }
|
---|
| 73 | pThreadParm->hAsyncTaskHandle = tid;
|
---|
| 74 | WSASetLastError(NO_ERROR);
|
---|
| 75 | return pThreadParm->hAsyncTaskHandle;
|
---|
| 76 | }
|
---|
| 77 | //******************************************************************************
|
---|
| 78 | //******************************************************************************
|
---|
| 79 | void AddToQueue(PASYNCTHREADPARM pThreadParm)
|
---|
| 80 | {
|
---|
| 81 | asyncThreadMutex.enter();
|
---|
| 82 | pThreadParm->next = threadList;
|
---|
| 83 | threadList = pThreadParm;
|
---|
| 84 | asyncThreadMutex.leave();
|
---|
| 85 | }
|
---|
| 86 | //******************************************************************************
|
---|
| 87 | //******************************************************************************
|
---|
| 88 | void RemoveFromQueue(PASYNCTHREADPARM pThreadParm)
|
---|
| 89 | {
|
---|
[3205] | 90 | PASYNCTHREADPARM pThreadInfo;
|
---|
[3198] | 91 |
|
---|
| 92 | asyncThreadMutex.enter();
|
---|
[3205] | 93 | pThreadInfo = threadList;
|
---|
[3198] | 94 |
|
---|
[3205] | 95 | if(pThreadInfo == pThreadParm) {
|
---|
[3198] | 96 | threadList = pThreadParm->next;
|
---|
| 97 | }
|
---|
| 98 | else {
|
---|
[3205] | 99 | while(pThreadInfo->next) {
|
---|
| 100 | if(pThreadInfo->next == pThreadParm) {
|
---|
| 101 | pThreadInfo->next = pThreadParm->next;
|
---|
[3198] | 102 | break;
|
---|
| 103 | }
|
---|
[3205] | 104 | pThreadInfo = pThreadInfo->next;
|
---|
[3198] | 105 | }
|
---|
[3205] | 106 | if(pThreadInfo == NULL) {
|
---|
[3198] | 107 | dprintf(("RemoveFromQueue: parm %x not found!!", pThreadParm));
|
---|
| 108 | DebugInt3();
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | memset(pThreadParm, 0, sizeof(*pThreadParm));
|
---|
| 112 | asyncThreadMutex.leave();
|
---|
| 113 | }
|
---|
| 114 | //******************************************************************************
|
---|
| 115 | //******************************************************************************
|
---|
| 116 | int WIN32API WSACancelAsyncRequest(LHANDLE hAsyncTaskHandle)
|
---|
| 117 | {
|
---|
[3205] | 118 | PASYNCTHREADPARM pThreadInfo;
|
---|
[3198] | 119 | BOOL found = FALSE;
|
---|
| 120 |
|
---|
| 121 | dprintf(("WSACancelAsyncRequest: cancel task %x", hAsyncTaskHandle));
|
---|
| 122 | asyncThreadMutex.enter();
|
---|
[3205] | 123 | pThreadInfo = threadList;
|
---|
[3198] | 124 |
|
---|
[3205] | 125 | while(pThreadInfo) {
|
---|
| 126 | if(pThreadInfo->hAsyncTaskHandle == hAsyncTaskHandle) {
|
---|
| 127 | pThreadInfo->fCancelled = TRUE;
|
---|
[3198] | 128 | found = TRUE;
|
---|
| 129 | break;
|
---|
| 130 | }
|
---|
[3205] | 131 | pThreadInfo = pThreadInfo->next;
|
---|
[3198] | 132 | }
|
---|
| 133 | asyncThreadMutex.leave();
|
---|
| 134 | if(found == FALSE) {
|
---|
| 135 | WSASetLastError(WSAEINVAL);
|
---|
| 136 | dprintf(("WSACancelAsyncRequest: task not found!!"));
|
---|
| 137 | }
|
---|
| 138 | return (found) ? NO_ERROR : SOCKET_ERROR;
|
---|
| 139 | }
|
---|
| 140 | //******************************************************************************
|
---|
[3205] | 141 | //Only to cancel blocking hooks
|
---|
[3198] | 142 | //******************************************************************************
|
---|
[3205] | 143 | int WIN32API WSACancelBlockingCall()
|
---|
| 144 | {
|
---|
| 145 | HANDLE hThread = GetCurrentThread();
|
---|
| 146 |
|
---|
| 147 | dprintf(("WSACancelBlockingCall"));
|
---|
| 148 | #if 0
|
---|
| 149 | asyncThreadMutex.enter();
|
---|
| 150 | pThreadInfo = threadList;
|
---|
| 151 |
|
---|
| 152 | while(pThreadInfo) {
|
---|
| 153 | if(pThreadInfo->hThread == hThread) {
|
---|
| 154 | pThreadInfo->fCancelled = TRUE;
|
---|
| 155 |
|
---|
| 156 | if(pThreadInfo->request == ASYNC_BLOCKHOOK) {
|
---|
| 157 | ret = so_cancel(pThreadInfo->blockedsocket);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | found = TRUE;
|
---|
| 161 | break;
|
---|
| 162 | }
|
---|
| 163 | pThreadInfo = pThreadInfo->next;
|
---|
| 164 | }
|
---|
| 165 | asyncThreadMutex.leave();
|
---|
| 166 | #endif
|
---|
| 167 | return SOCKET_ERROR;
|
---|
| 168 | }
|
---|
| 169 | //******************************************************************************
|
---|
| 170 | //Assumes caller owns async thread mutex!
|
---|
| 171 | //******************************************************************************
|
---|
| 172 | static PASYNCTHREADPARM FindAsyncEvent(SOCKET s)
|
---|
| 173 | {
|
---|
| 174 | PASYNCTHREADPARM pThreadInfo;
|
---|
| 175 |
|
---|
| 176 | pThreadInfo = threadList;
|
---|
| 177 | while(pThreadInfo) {
|
---|
| 178 | if(pThreadInfo->u.asyncselect.s == s) {
|
---|
| 179 | return pThreadInfo;
|
---|
| 180 | }
|
---|
| 181 | pThreadInfo = pThreadInfo->next;
|
---|
| 182 | }
|
---|
| 183 | return NULL;
|
---|
| 184 | }
|
---|
| 185 | //******************************************************************************
|
---|
| 186 | //******************************************************************************
|
---|
| 187 | BOOL FindAndSetAsyncEvent(SOCKET s, HWND hwnd, int msg, ULONG lEvent)
|
---|
| 188 | {
|
---|
| 189 | PASYNCTHREADPARM pThreadInfo;
|
---|
| 190 |
|
---|
| 191 | asyncThreadMutex.enter();
|
---|
| 192 | pThreadInfo = FindAsyncEvent(s);
|
---|
| 193 | if(pThreadInfo) {
|
---|
| 194 | pThreadInfo->u.asyncselect.lEvents = lEvent;
|
---|
| 195 | pThreadInfo->hwnd = hwnd;
|
---|
| 196 | pThreadInfo->msg = msg;
|
---|
| 197 | //cancel pending select in async select thread (if any)
|
---|
| 198 | so_cancel(s);
|
---|
| 199 |
|
---|
| 200 | //unblock async thread if it was waiting
|
---|
| 201 | pThreadInfo->u.asyncselect.asyncSem->post();
|
---|
| 202 | }
|
---|
| 203 | asyncThreadMutex.leave();
|
---|
| 204 | return(pThreadInfo != NULL);
|
---|
| 205 | }
|
---|
| 206 | //******************************************************************************
|
---|
| 207 | //******************************************************************************
|
---|
[3198] | 208 | void EnableAsyncEvent(SOCKET s, ULONG flags)
|
---|
| 209 | {
|
---|
[3205] | 210 | PASYNCTHREADPARM pThreadInfo;
|
---|
| 211 |
|
---|
| 212 | asyncThreadMutex.enter();
|
---|
| 213 | pThreadInfo = FindAsyncEvent(s);
|
---|
| 214 | if(pThreadInfo) {
|
---|
| 215 | pThreadInfo->u.asyncselect.lEventsPending |= (pThreadInfo->u.asyncselect.lEvents & flags);
|
---|
[3226] | 216 | //unblock async thread if it was waiting
|
---|
| 217 | pThreadInfo->u.asyncselect.asyncSem->post();
|
---|
| 218 |
|
---|
[3205] | 219 | //cancel pending select in async select thread (if any)
|
---|
| 220 | so_cancel(s);
|
---|
| 221 | }
|
---|
| 222 | asyncThreadMutex.leave();
|
---|
[3198] | 223 | }
|
---|
| 224 | //******************************************************************************
|
---|
| 225 | //******************************************************************************
|
---|
| 226 | BOOL QueryAsyncEvent(SOCKET s, HWND *pHwnd, int *pMsg, ULONG *plEvent)
|
---|
| 227 | {
|
---|
[3205] | 228 | PASYNCTHREADPARM pThreadInfo;
|
---|
| 229 |
|
---|
| 230 | asyncThreadMutex.enter();
|
---|
| 231 | pThreadInfo = FindAsyncEvent(s);
|
---|
| 232 | if(pThreadInfo) {
|
---|
| 233 | *pHwnd = pThreadInfo->hwnd;
|
---|
| 234 | *pMsg = pThreadInfo->msg;
|
---|
| 235 | *plEvent = pThreadInfo->u.asyncselect.lEvents;
|
---|
| 236 | }
|
---|
| 237 | asyncThreadMutex.leave();
|
---|
| 238 | return(pThreadInfo != NULL);
|
---|
[3198] | 239 | }
|
---|
| 240 | //******************************************************************************
|
---|
| 241 | //******************************************************************************
|
---|