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

handlemanager support for thread handles + WaitForSingleObject (thread) fix

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/HandleManager.cpp

    r2802 r3128  
    1 /* $Id: HandleManager.cpp,v 1.35 2000-02-16 14:23:57 sandervl Exp $ */
     1/* $Id: HandleManager.cpp,v 1.36 2000-03-16 19:20:36 sandervl Exp $ */
    22
    33/*
     
    5757#include "HMComm.h"
    5858#include "HMToken.h"
    59 #include <winconst.h>
     59#include "HMThread.h"
     60#include <vmutex.h>
    6061
    6162#define DBG_LOCALLOG    DBG_handlemanager
     
    109110            /* the device name is repeated here to enable device alias names */
    110111static PHMDEVICE TabWin32Devices = NULL;
     112
    111113static HMHANDLE TabWin32Handles[MAX_OS2_HMHANDLES];   /* static handle table */
    112 
     114VMutex          handleMutex;
    113115
    114116struct _HMGlobals
     
    128130  HMDeviceHandler        *pHMComm;                   /* serial communication */
    129131  HMDeviceHandler        *pHMToken;         /* security tokens */
     132  HMDeviceHandler        *pHMThread;
    130133
    131134  ULONG         ulHandleLast;                   /* index of last used handle */
     
    195198  register ULONG ulLoop;
    196199
     200  handleMutex.enter();
     201
    197202  for (ulLoop = 1; // @@@PH Note, this is an experimental change
    198203                   // 0L as hHandle is sometimes considered invalid!
     
    205210        TabWin32Handles[ulLoop].hmHandleData.dwUserData     = 0;
    206211        TabWin32Handles[ulLoop].hmHandleData.dwInternalType = HMTYPE_UNKNOWN;
     212        handleMutex.leave();
    207213        return (ulLoop);                    /* OK, then return it to the caller */
    208214    }
    209215  }
    210216
     217  handleMutex.leave();
    211218  return (INVALID_HANDLE_VALUE);             /* haven't found any free handle */
    212219}
     
    326333    HMGlobals.fIsInitialized = TRUE;                             /* OK, done */
    327334
     335    handleMutex.enter();
    328336    // fill handle table
    329     for (ulIndex = 0;
    330          ulIndex < MAX_OS2_HMHANDLES;
    331          ulIndex++)
    332       TabWin32Handles[ulIndex].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
     337    for(ulIndex = 0; ulIndex < MAX_OS2_HMHANDLES; ulIndex++) {
     338        TabWin32Handles[ulIndex].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
     339    }
     340    handleMutex.leave();
    333341
    334342    dprintf(("KERNEL32:HandleManager:HMInitialize() storing handles.\n"));
     
    338346           sizeof(HMGlobals));
    339347
    340                        /* copy standard handles from OS/2's Open32 Subsystem */
     348    /* copy standard handles from OS/2's Open32 Subsystem */
    341349    HMSetStdHandle(STD_INPUT_HANDLE,  GetStdHandle(STD_INPUT_HANDLE));
    342350    HMSetStdHandle(STD_OUTPUT_HANDLE, GetStdHandle(STD_OUTPUT_HANDLE));
     
    351359    HMGlobals.pHMComm       = new HMDeviceCommClass("\\\\COM\\");
    352360    HMGlobals.pHMToken      = new HMDeviceTokenClass("\\\\TOKEN\\");
     361    HMGlobals.pHMThread     = new HMDeviceThreadClass("\\\\THREAD\\");
    353362  }
    354363  return (NO_ERROR);
     
    408417 *****************************************************************************/
    409418
    410 DWORD  HMHandleAllocate (PULONG phHandle16,
    411                          ULONG  hHandleOS2)
     419DWORD HMHandleAllocate (PULONG phHandle16,
     420                        ULONG  hHandleOS2)
    412421{
    413422  register ULONG ulHandle;
     
    421430  ulHandle = HMGlobals.ulHandleLast;                      /* get free handle */
    422431
     432  handleMutex.enter();
     433
    423434  if(ulHandle == 0) {
    424435        ulHandle = 1; //SvL: Start searching from index 1
     
    426437  do
    427438  {
    428                                                   /* check if handle is free */
     439    /* check if handle is free */
    429440    if (TabWin32Handles[ulHandle].hmHandleData.hHMHandle == INVALID_HANDLE_VALUE)
    430441    {
    431       *phHandle16 = ulHandle;
    432       TabWin32Handles[ulHandle].hmHandleData.hHMHandle = hHandleOS2;
    433       HMGlobals.ulHandleLast = ulHandle;          /* to shorten search times */
    434 
    435       return (NO_ERROR);                                               /* OK */
     442        *phHandle16 = ulHandle;
     443        TabWin32Handles[ulHandle].hmHandleData.hHMHandle = hHandleOS2;
     444        HMGlobals.ulHandleLast = ulHandle;          /* to shorten search times */
     445
     446        handleMutex.leave();
     447        return (NO_ERROR);                                               /* OK */
    436448    }
    437449
     
    442454  }
    443455  while (ulHandle != HMGlobals.ulHandleLast);
     456
     457  handleMutex.leave();
    444458
    445459  return (ERROR_TOO_MANY_OPEN_FILES);                      /* OK, we're done */
     
    29102924  return STATUS_SUCCESS;
    29112925}
    2912 
     2926/*****************************************************************************
     2927 * Name      : HMCreateThread
     2928 * Purpose   : router function for CreateThread
     2929 * Parameters:
     2930 * Variables :
     2931 * Result    :
     2932 * Remark    :
     2933 * Status    :
     2934 *
     2935 * Author    : SvL
     2936 *****************************************************************************/
     2937HANDLE HMCreateThread(LPSECURITY_ATTRIBUTES  lpsa,
     2938                      DWORD                  cbStack,
     2939                      LPTHREAD_START_ROUTINE lpStartAddr,
     2940                      LPVOID                 lpvThreadParm,
     2941                      DWORD                  fdwCreate,
     2942                      LPDWORD                lpIDThread)
     2943{
     2944  int             iIndex;                     /* index into the handle table */
     2945  int             iIndexNew;                  /* index into the handle table */
     2946  HMDeviceHandler *pDeviceHandler;         /* device handler for this handle */
     2947  PHMHANDLEDATA   pHMHandleData;
     2948  HANDLE          rc;                                     /* API return code */
     2949
     2950  SetLastError(ERROR_SUCCESS);
     2951
     2952  pDeviceHandler = HMGlobals.pHMThread;         /* device is predefined */
     2953
     2954  iIndexNew = _HMHandleGetFree();                         /* get free handle */
     2955  if (-1 == iIndexNew)                            /* oops, no free handles ! */
     2956  {
     2957    SetLastError(ERROR_NOT_ENOUGH_MEMORY);      /* use this as error message */
     2958    return 0;
     2959  }
     2960
     2961  /* initialize the complete HMHANDLEDATA structure */
     2962  pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
     2963  pHMHandleData->dwType     = FILE_TYPE_UNKNOWN;      /* unknown handle type */
     2964  pHMHandleData->dwAccess   = 0;
     2965  pHMHandleData->dwShare    = 0;
     2966  pHMHandleData->dwCreation = 0;
     2967  pHMHandleData->dwFlags    = 0;
     2968  pHMHandleData->lpHandlerData = NULL;
     2969
     2970
     2971  /* we've got to mark the handle as occupied here, since another device */
     2972  /* could be created within the device handler -> deadlock */
     2973
     2974  /* write appropriate entry into the handle table if open succeeded */
     2975  TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
     2976  TabWin32Handles[iIndexNew].pDeviceHandler         = pDeviceHandler;
     2977
     2978                                                  /* call the device handler */
     2979
     2980  // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
     2981  //              a valid HandleManager-internal handle!
     2982  rc = pDeviceHandler->CreateThread(&TabWin32Handles[iIndexNew].hmHandleData,
     2983                                    lpsa,  cbStack, lpStartAddr,
     2984                                    lpvThreadParm, fdwCreate, lpIDThread);
     2985
     2986  if (rc == 0)     /* oops, creation failed within the device handler */
     2987  {
     2988        TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
     2989        return 0;                                           /* signal error */
     2990  }
     2991
     2992  return iIndexNew;
     2993}
     2994/*****************************************************************************
     2995 * Name      : HMGetThreadPriority
     2996 * Purpose   : router function for GetThreadPriority
     2997 * Parameters:
     2998 * Variables :
     2999 * Result    :
     3000 * Remark    :
     3001 * Status    :
     3002 *
     3003 * Author    : SvL
     3004 *****************************************************************************/
     3005INT HMGetThreadPriority(HANDLE hThread)
     3006{
     3007  int       iIndex;                           /* index into the handle table */
     3008  INT       lpResult;                /* result from the device handler's API */
     3009  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3010
     3011  SetLastError(ERROR_SUCCESS);
     3012                                                          /* validate handle */
     3013  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3014  if (-1 == iIndex)                                               /* error ? */
     3015  {
     3016    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3017    return (-1);                                         /* signal failure */
     3018  }
     3019
     3020  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3021  lpResult = pHMHandle->pDeviceHandler->GetThreadPriority(&TabWin32Handles[iIndex].hmHandleData);
     3022
     3023  return (lpResult);                                  /* deliver return code */
     3024}
     3025/*****************************************************************************
     3026 * Name      : HMSuspendThread
     3027 * Purpose   : router function for SuspendThread
     3028 * Parameters:
     3029 * Variables :
     3030 * Result    :
     3031 * Remark    :
     3032 * Status    :
     3033 *
     3034 * Author    : SvL
     3035 *****************************************************************************/
     3036DWORD HMSuspendThread(HANDLE hThread)
     3037{
     3038  int       iIndex;                           /* index into the handle table */
     3039  HANDLE    lpResult;                /* result from the device handler's API */
     3040  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3041
     3042  SetLastError(ERROR_SUCCESS);
     3043                                                         /* validate handle */
     3044  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3045  if (-1 == iIndex)                                               /* error ? */
     3046  {
     3047    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3048    return -1;                                         /* signal failure */
     3049  }
     3050
     3051  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3052  lpResult = pHMHandle->pDeviceHandler->SuspendThread(&TabWin32Handles[iIndex].hmHandleData);
     3053
     3054  return (lpResult);                                  /* deliver return code */
     3055}
     3056/*****************************************************************************
     3057 * Name      : HMSetThreadPriority
     3058 * Purpose   : router function for SetThreadPriority
     3059 * Parameters:
     3060 * Variables :
     3061 * Result    :
     3062 * Remark    :
     3063 * Status    :
     3064 *
     3065 * Author    : SvL
     3066 *****************************************************************************/
     3067BOOL HMSetThreadPriority(HANDLE hThread, int priority)
     3068{
     3069  int       iIndex;                           /* index into the handle table */
     3070  BOOL      lpResult;                   /* result from the device handler's API */
     3071  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3072
     3073  SetLastError(ERROR_SUCCESS);
     3074                                                          /* validate handle */
     3075  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3076  if (-1 == iIndex)                                               /* error ? */
     3077  {
     3078    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3079    return FALSE;                                         /* signal failure */
     3080  }
     3081
     3082  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3083  lpResult = pHMHandle->pDeviceHandler->SetThreadPriority(&TabWin32Handles[iIndex].hmHandleData, priority);
     3084
     3085  return (lpResult);                                  /* deliver return code */
     3086}
     3087/*****************************************************************************
     3088 * Name      : HMGetThreadContext
     3089 * Purpose   : router function for GetThreadContext
     3090 * Parameters:
     3091 * Variables :
     3092 * Result    :
     3093 * Remark    :
     3094 * Status    :
     3095 *
     3096 * Author    : SvL
     3097 *****************************************************************************/
     3098BOOL HMGetThreadContext(HANDLE hThread, CONTEXT *lpContext)
     3099{
     3100  int       iIndex;                           /* index into the handle table */
     3101  BOOL      lpResult;                /* result from the device handler's API */
     3102  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3103
     3104  SetLastError(ERROR_SUCCESS);
     3105                                                          /* validate handle */
     3106  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3107  if (-1 == iIndex)                                               /* error ? */
     3108  {
     3109    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3110    return FALSE;                                         /* signal failure */
     3111  }
     3112
     3113  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3114  lpResult = pHMHandle->pDeviceHandler->GetThreadContext(&TabWin32Handles[iIndex].hmHandleData, lpContext);
     3115
     3116  return (lpResult);                                  /* deliver return code */
     3117}
     3118/*****************************************************************************
     3119 * Name      : HMSetThreadContext
     3120 * Purpose   : router function for SetThreadContext
     3121 * Parameters:
     3122 * Variables :
     3123 * Result    :
     3124 * Remark    :
     3125 * Status    :
     3126 *
     3127 * Author    : SvL
     3128 *****************************************************************************/
     3129BOOL HMSetThreadContext(HANDLE hThread, const CONTEXT *lpContext)
     3130{
     3131  int       iIndex;                           /* index into the handle table */
     3132  BOOL      lpResult;                /* result from the device handler's API */
     3133  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3134
     3135  SetLastError(ERROR_SUCCESS);
     3136                                                          /* validate handle */
     3137  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3138  if (-1 == iIndex)                                               /* error ? */
     3139  {
     3140    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3141    return FALSE;                                         /* signal failure */
     3142  }
     3143
     3144  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3145  lpResult = pHMHandle->pDeviceHandler->SetThreadContext(&TabWin32Handles[iIndex].hmHandleData, lpContext);
     3146
     3147  return (lpResult);                                  /* deliver return code */
     3148}
     3149/*****************************************************************************
     3150 * Name      : HMTerminateThread
     3151 * Purpose   : router function for TerminateThread
     3152 * Parameters:
     3153 * Variables :
     3154 * Result    :
     3155 * Remark    :
     3156 * Status    :
     3157 *
     3158 * Author    : SvL
     3159 *****************************************************************************/
     3160BOOL HMTerminateThread(HANDLE hThread, DWORD exitcode)
     3161{
     3162  int       iIndex;                           /* index into the handle table */
     3163  BOOL      lpResult;                /* result from the device handler's API */
     3164  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3165
     3166  SetLastError(ERROR_SUCCESS);
     3167                                                          /* validate handle */
     3168  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3169  if (-1 == iIndex)                                               /* error ? */
     3170  {
     3171    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3172    return FALSE;                                         /* signal failure */
     3173  }
     3174
     3175  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3176  lpResult = pHMHandle->pDeviceHandler->TerminateThread(&TabWin32Handles[iIndex].hmHandleData, exitcode);
     3177
     3178  return (lpResult);                                  /* deliver return code */
     3179}
     3180/*****************************************************************************
     3181 * Name      : HMResumeThread
     3182 * Purpose   : router function for ResumeThread
     3183 * Parameters:
     3184 * Variables :
     3185 * Result    :
     3186 * Remark    :
     3187 * Status    :
     3188 *
     3189 * Author    : SvL
     3190 *****************************************************************************/
     3191DWORD HMResumeThread(HANDLE hThread)
     3192{
     3193  int       iIndex;                           /* index into the handle table */
     3194  DWORD     lpResult;                /* result from the device handler's API */
     3195  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3196
     3197  SetLastError(ERROR_SUCCESS);
     3198                                                 /* validate handle */
     3199  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3200  if (-1 == iIndex)                                               /* error ? */
     3201  {
     3202    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3203    return -1;                                         /* signal failure */
     3204  }
     3205
     3206  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3207  lpResult = pHMHandle->pDeviceHandler->ResumeThread(&TabWin32Handles[iIndex].hmHandleData);
     3208
     3209  return (lpResult);                                  /* deliver return code */
     3210}
     3211
     3212/*****************************************************************************
     3213 * Name      : HMGetExitCodeThread
     3214 * Purpose   : router function for GetExitCodeThread
     3215 * Parameters:
     3216 * Variables :
     3217 * Result    :
     3218 * Remark    :
     3219 * Status    :
     3220 *
     3221 * Author    : SvL
     3222 *****************************************************************************/
     3223BOOL HMGetExitCodeThread(HANDLE hThread, LPDWORD lpExitCode)
     3224{
     3225  int       iIndex;                           /* index into the handle table */
     3226  BOOL      lpResult;                /* result from the device handler's API */
     3227  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3228
     3229  SetLastError(ERROR_SUCCESS);
     3230                                                          /* validate handle */
     3231  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3232  if (-1 == iIndex)                                               /* error ? */
     3233  {
     3234    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3235    return FALSE;                                         /* signal failure */
     3236  }
     3237
     3238  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3239  lpResult = pHMHandle->pDeviceHandler->GetExitCodeThread(&TabWin32Handles[iIndex].hmHandleData, lpExitCode);
     3240
     3241  return (lpResult);                                  /* deliver return code */
     3242}
     3243/*****************************************************************************
     3244 * Name      : HMSetThreadTerminated
     3245 * Purpose   :
     3246 * Parameters:
     3247 * Variables :
     3248 * Result    :
     3249 * Remark    :
     3250 * Status    :
     3251 *
     3252 * Author    : SvL
     3253 *****************************************************************************/
     3254BOOL HMSetThreadTerminated(HANDLE hThread)
     3255{
     3256  int       iIndex;                           /* index into the handle table */
     3257  BOOL      lpResult;                /* result from the device handler's API */
     3258  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3259
     3260  SetLastError(ERROR_SUCCESS);
     3261                                                          /* validate handle */
     3262  iIndex = _HMHandleQuery(hThread);              /* get the index */
     3263  if (-1 == iIndex)                                               /* error ? */
     3264  {
     3265    SetLastError(ERROR_INVALID_HANDLE);       /* set win32 error information */
     3266    return FALSE;                                         /* signal failure */
     3267  }
     3268
     3269  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3270  lpResult = pHMHandle->pDeviceHandler->SetThreadTerminated(&TabWin32Handles[iIndex].hmHandleData);
     3271
     3272  return (lpResult);                                  /* deliver return code */
     3273}
Note: See TracChangeset for help on using the changeset viewer.