Ignore:
Timestamp:
Mar 23, 2000, 8:21:56 PM (25 years ago)
Author:
sandervl
Message:

wsock32\new update

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wsock32/new/asyncthread.cpp

    r3198 r3205  
    1 /* $Id: asyncthread.cpp,v 1.1 2000-03-22 20:01:04 sandervl Exp $ */
     1/* $Id: asyncthread.cpp,v 1.2 2000-03-23 19:21:53 sandervl Exp $ */
    22
    33/*
     
    66 * Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl)
    77 *
    8  *
    9  * Not everything is thread safe
     8 * TODO: Not everything is 100% thread safe  (i.e. async parameter updates)
    109 *
    1110 * Project Odin Software License can be found in LICENSE.TXT
     11 *
    1212 */
    1313#define INCL_BASE
    1414#include <os2wrap.h>
    1515#include <os2sel.h>
     16#include <stdlib.h>
    1617#include <wprocess.h>
    1718#include <win32api.h>
     
    3536  pThreadParm->asyncProc((PVOID)arg);
    3637
    37   WSASetBlocking(TRUE, pThreadParm->hThread);
     38//only for blocking hooks (currently not implemented
     39////  if(pThreadParm->request == ASYNC_BLOCKHOOK)
     40////    WSASetBlocking(FALSE, pThreadParm->hThread);
     41
     42  pThreadParm->fActive = FALSE;
     43  RemoveFromQueue(pThreadParm);
     44  free((PVOID)pThreadParm);
    3845
    3946  DosExit(EXIT_THREAD, 0);
     
    4148//******************************************************************************
    4249//******************************************************************************
    43 ULONG QueueAsyncJob(ASYNCTHREADPROC asyncproc, PASYNCTHREADPARM pThreadParm)
     50ULONG QueueAsyncJob(ASYNCTHREADPROC asyncproc, PASYNCTHREADPARM pThreadParm, BOOL fSetBlocking)
    4451{
    4552 APIRET rc;
     
    5562   AddToQueue(pThreadParm);
    5663
    57    WSASetBlocking(TRUE);
     64   if(fSetBlocking) WSASetBlocking(TRUE);
    5865
    5966   rc = DosCreateThread(&tid, AsyncThread, (ULONG)pThreadParm, CREATE_READY, 16384);
     
    6168   {
    6269        dprintf(("QueueAsyncJob: DosCreateThread failed with error %x", rc));
    63         WSASetBlocking(FALSE);
     70        if(fSetBlocking) WSASetBlocking(FALSE);
    6471        RemoveFromQueue(pThreadParm);
    6572        WSASetLastError(WSAEFAULT);
     
    8390void RemoveFromQueue(PASYNCTHREADPARM pThreadParm)
    8491{
    85  PASYNCTHREADPARM parm;
    86 
    87    asyncThreadMutex.enter();
    88    parm = threadList;
    89 
    90    if(parm == pThreadParm) {
     92 PASYNCTHREADPARM pThreadInfo;
     93
     94   asyncThreadMutex.enter();
     95   pThreadInfo = threadList;
     96
     97   if(pThreadInfo == pThreadParm) {
    9198        threadList = pThreadParm->next;
    9299   }
    93100   else {
    94         while(parm->next) {
    95                 if(parm->next == pThreadParm) {
    96                         parm->next = pThreadParm->next;
     101        while(pThreadInfo->next) {
     102                if(pThreadInfo->next == pThreadParm) {
     103                        pThreadInfo->next = pThreadParm->next;
    97104                        break;
    98105                }
    99                 parm = parm->next;
    100         }
    101         if(parm == NULL) {
     106                pThreadInfo = pThreadInfo->next;
     107        }
     108        if(pThreadInfo == NULL) {
    102109                dprintf(("RemoveFromQueue: parm %x not found!!", pThreadParm));
    103110                DebugInt3();
     
    111118int WIN32API WSACancelAsyncRequest(LHANDLE hAsyncTaskHandle)
    112119{
    113  PASYNCTHREADPARM parm;
     120 PASYNCTHREADPARM pThreadInfo;
    114121 BOOL             found = FALSE;
    115122
    116123   dprintf(("WSACancelAsyncRequest: cancel task %x", hAsyncTaskHandle));
    117124   asyncThreadMutex.enter();
    118    parm = threadList;
    119 
    120    while(parm) {
    121         if(parm->hAsyncTaskHandle == hAsyncTaskHandle) {
    122                 parm->fCancelled = TRUE;
     125   pThreadInfo = threadList;
     126
     127   while(pThreadInfo) {
     128        if(pThreadInfo->hAsyncTaskHandle == hAsyncTaskHandle) {
     129                pThreadInfo->fCancelled = TRUE;
    123130                found = TRUE;
    124131                break;
    125132        }
    126         parm = parm->next;
     133        pThreadInfo = pThreadInfo->next;
    127134   }
    128135   asyncThreadMutex.leave();
     
    134141}
    135142//******************************************************************************
     143//Only to cancel blocking hooks
     144//******************************************************************************
     145int WIN32API WSACancelBlockingCall()
     146{
     147 HANDLE hThread = GetCurrentThread();
     148
     149   dprintf(("WSACancelBlockingCall"));
     150#if 0
     151   asyncThreadMutex.enter();
     152   pThreadInfo = threadList;
     153
     154   while(pThreadInfo) {
     155        if(pThreadInfo->hThread == hThread) {
     156                pThreadInfo->fCancelled = TRUE;
     157
     158                if(pThreadInfo->request == ASYNC_BLOCKHOOK) {
     159                        ret = so_cancel(pThreadInfo->blockedsocket);
     160                }
     161
     162                found = TRUE;
     163                break;
     164        }
     165        pThreadInfo = pThreadInfo->next;
     166   }
     167   asyncThreadMutex.leave();
     168#endif
     169   return SOCKET_ERROR;
     170}
     171//******************************************************************************
     172//Assumes caller owns async thread mutex!
     173//******************************************************************************
     174static PASYNCTHREADPARM FindAsyncEvent(SOCKET s)
     175{
     176 PASYNCTHREADPARM pThreadInfo;
     177
     178   pThreadInfo = threadList;
     179   while(pThreadInfo) {
     180        if(pThreadInfo->u.asyncselect.s == s) {
     181                return pThreadInfo;
     182        }
     183        pThreadInfo = pThreadInfo->next;
     184   }
     185   return NULL;
     186}
     187//******************************************************************************
     188//******************************************************************************
     189BOOL FindAndSetAsyncEvent(SOCKET s, HWND hwnd, int msg, ULONG lEvent)
     190{
     191 PASYNCTHREADPARM pThreadInfo;
     192 
     193   asyncThreadMutex.enter();
     194   pThreadInfo = FindAsyncEvent(s);
     195   if(pThreadInfo) {
     196        pThreadInfo->u.asyncselect.lEvents = lEvent;
     197        pThreadInfo->hwnd                  = hwnd;
     198        pThreadInfo->msg                   = msg;
     199        //cancel pending select in async select thread (if any)
     200        so_cancel(s);
     201
     202        //unblock async thread if it was waiting
     203        pThreadInfo->u.asyncselect.asyncSem->post();
     204   }
     205   asyncThreadMutex.leave();
     206   return(pThreadInfo != NULL);
     207}
     208//******************************************************************************
    136209//******************************************************************************
    137210void EnableAsyncEvent(SOCKET s, ULONG flags)
    138211{
     212 PASYNCTHREADPARM pThreadInfo;
     213
     214   asyncThreadMutex.enter();
     215   pThreadInfo = FindAsyncEvent(s);
     216   if(pThreadInfo) {
     217        pThreadInfo->u.asyncselect.lEventsPending |= (pThreadInfo->u.asyncselect.lEvents & flags);
     218        //cancel pending select in async select thread (if any)
     219        so_cancel(s);
     220
     221        //unblock async thread if it was waiting
     222        pThreadInfo->u.asyncselect.asyncSem->post();
     223   }
     224   asyncThreadMutex.leave();
    139225}
    140226//******************************************************************************
     
    142228BOOL QueryAsyncEvent(SOCKET s, HWND *pHwnd, int *pMsg, ULONG *plEvent)
    143229{
    144    return FALSE;
    145 }
    146 //******************************************************************************
    147 //******************************************************************************
     230 PASYNCTHREADPARM pThreadInfo;
     231
     232   asyncThreadMutex.enter();
     233   pThreadInfo = FindAsyncEvent(s);
     234   if(pThreadInfo) {
     235        *pHwnd   = pThreadInfo->hwnd;
     236        *pMsg    = pThreadInfo->msg;
     237        *plEvent = pThreadInfo->u.asyncselect.lEvents;
     238   }
     239   asyncThreadMutex.leave();
     240   return(pThreadInfo != NULL);
     241}
     242//******************************************************************************
     243//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.