Ignore:
Timestamp:
Mar 26, 2003, 5:02:33 PM (22 years ago)
Author:
sandervl
Message:

WaitForSingleObject/(Msg)WaitForMultipleObject fixes to prevent thread priorities from being accidentally boosted to time critical

File:
1 edited

Legend:

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

    r9911 r9938  
    1 /* $Id: HandleManager.cpp,v 1.95 2003-03-06 10:44:32 sandervl Exp $ */
     1/* $Id: HandleManager.cpp,v 1.96 2003-03-26 16:02:33 sandervl Exp $ */
    22
    33/*
     
    20692069  //To avoid this problem, we temporarily switch to time critical priority.
    20702070  HANDLE hThread          = GetCurrentThread();
    2071   DWORD  dwThreadPriority = GetThreadPriority(hThread);
    2072 
    2073   if(dwTimeout && dwTimeout < 20 && dwThreadPriority != THREAD_PRIORITY_TIME_CRITICAL) {
    2074       dprintf(("Temporarily change priority to THREAD_PRIORITY_TIME_CRITICAL for better timing"));
    2075       SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
    2076       //round to 8 ms units to get more precise timeouts
    2077       if(dwTimeout > 8)
    2078           dwTimeout = (dwTimeout/8)*8;
    2079   }
    2080   else dwThreadPriority = -1;
     2071  BOOL   fChangePriority  = FALSE;
     2072  DWORD  dwThreadPriority;
     2073
     2074  if(dwTimeout && dwTimeout < 20) {
     2075      dwThreadPriority = GetThreadPriority(hThread);
     2076      if(dwThreadPriority != THREAD_PRIORITY_TIME_CRITICAL)
     2077      {
     2078          dprintf(("Temporarily change priority to THREAD_PRIORITY_TIME_CRITICAL for better timing"));
     2079          SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
     2080          //round to 8 ms units to get more precise timeouts
     2081          if(dwTimeout > 8)
     2082              dwTimeout = (dwTimeout/8)*8;
     2083          fChangePriority = TRUE;
     2084      }
     2085  }
    20812086
    20822087  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     
    20852090
    20862091  //Restore thread priority if we previously changed it
    2087   if(dwThreadPriority != -1) {
     2092  if(fChangePriority) {
    20882093      SetThreadPriority(hThread, dwThreadPriority);
    20892094  }
     
    30063011                                DWORD   dwTimeout)
    30073012{
    3008 #ifdef USE_OS2SEMAPHORES
    3009   int       iIndex;                           /* index into the handle table */
    3010   DWORD     dwResult;                /* result from the device handler's API */
    3011   PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
    3012 
    3013   if(cObjects == 1) {
    3014       return HMWaitForSingleObject(*lphObjects, dwTimeout);
    3015   }
    3016 
    3017   if(cObjects > MAXIMUM_WAIT_OBJECTS) {
    3018       dprintf(("KERNEL32: HMWaitForMultipleObjects: Too many objects (%d)", cObjects));
    3019       SetLastError(ERROR_INVALID_PARAMETER);
    3020       return WAIT_FAILED;
    3021   }
    3022 
    3023                                                           /* validate handle */
    3024   iIndex = _HMHandleQuery(*lphObjects);                   /* get the index */
    3025   if (-1 == iIndex)                                       /* error ? */
    3026   {//oh, oh. possible problem here
    3027    //TODO: rewrite handling of other handles; don't forward to open32
    3028       dprintf(("WANRING: HMWaitForMultipleObjects: unknown handle passed on to Open32 -> will not work if other handles are semaphores"));
    3029       return O32_WaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout);
    3030   }
    3031 
    3032   pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
    3033   dwResult = pHMHandle->pDeviceHandler->WaitForMultipleObjects(&pHMHandle->hmHandleData,
    3034                                                                cObjects, lphObjects, fWaitAll,
    3035                                                                dwTimeout);
    3036 
    3037   return (dwResult);                                  /* deliver return code */
    3038 #else
    30393013  ULONG   ulIndex;
    30403014  PHANDLE pArrayOfHandles;
     
    30893063  //To avoid this problem, we temporarily switch to time critical priority.
    30903064  HANDLE hThread          = GetCurrentThread();
    3091   DWORD  dwThreadPriority = GetThreadPriority(hThread);
    3092 
    3093   if(dwTimeout && dwTimeout < 20 && dwThreadPriority != THREAD_PRIORITY_TIME_CRITICAL) {
    3094       dprintf(("Temporarily change priority to THREAD_PRIORITY_TIME_CRITICAL for better timing"));
    3095       SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
    3096       //round to 8 ms units to get more precise timeouts
    3097       if(dwTimeout > 8)
    3098           dwTimeout = (dwTimeout/8)*8;
    3099   }
    3100   else dwThreadPriority = -1;
     3065  BOOL   fChangePriority  = FALSE;
     3066  DWORD  dwThreadPriority;
     3067
     3068  if(dwTimeout && dwTimeout < 20) {
     3069      dwThreadPriority = GetThreadPriority(hThread);
     3070      if(dwThreadPriority != THREAD_PRIORITY_TIME_CRITICAL)
     3071      {
     3072          dprintf(("Temporarily change priority to THREAD_PRIORITY_TIME_CRITICAL for better timing"));
     3073          SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
     3074          //round to 8 ms units to get more precise timeouts
     3075          if(dwTimeout > 8)
     3076              dwTimeout = (dwTimeout/8)*8;
     3077          fChangePriority = TRUE;
     3078      }
     3079  }
    31013080
    31023081  // OK, now forward to Open32.
     
    31093088
    31103089  //Restore old thread priority if we changed it before
    3111   if(dwThreadPriority != -1) {
     3090  if(fChangePriority) {
    31123091      SetThreadPriority(hThread, dwThreadPriority);
    31133092  }
    31143093
    31153094  return (rc);                            // OK, done
    3116 #endif
    31173095}
    31183096
     
    31613139                                     DWORD      dwWakeMask)
    31623140{
    3163 #ifdef USE_OS2SEMAPHORES
    3164   int       iIndex;                           /* index into the handle table */
    3165   DWORD     dwResult;                /* result from the device handler's API */
    3166   PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
    3167 
    3168 
    3169   if(dwWakeMask == 0) {
    3170       dprintf(("WARNING: wakemask == 0 -> calling WaitForMultipleObjects"));
    3171       return HMWaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout);
    3172   }
    3173                                                           /* validate handle */
    3174   iIndex = _HMHandleQuery(*lphObjects);                   /* get the index */
    3175   if (-1 == iIndex)                                       /* error ? */
    3176   {//oh, oh. possible problem here
    3177    //TODO: rewrite handling of other handles; don't forward to open32
    3178       dprintf(("WANRING: HMWaitForMultipleObjects: unknown handle passed on to Open32 -> will not work if other handles are semaphores"));
    3179       return O32_MsgWaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout, dwWakeMask);
    3180   }
    3181 
    3182   if(cObjects > MAXIMUM_WAIT_OBJECTS) {
    3183       dprintf(("KERNEL32: HMMsgWaitForMultipleObjects: Too many objects (%d)", cObjects));
    3184       SetLastError(ERROR_INVALID_PARAMETER);
    3185       return WAIT_FAILED;
    3186   }
    3187 
    3188   pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
    3189   dwResult = pHMHandle->pDeviceHandler->MsgWaitForMultipleObjects(&pHMHandle->hmHandleData,
    3190                                                                   cObjects, lphObjects, fWaitAll,
    3191                                                                   dwTimeout, dwWakeMask);
    3192 
    3193   return (dwResult);                                  /* deliver return code */
    3194 #else
    31953141  ULONG   ulIndex;
    31963142  PHANDLE pArrayOfHandles;
     
    32403186  //To avoid this problem, we temporarily switch to time critical priority.
    32413187  HANDLE hThread          = GetCurrentThread();
    3242   DWORD  dwThreadPriority = GetThreadPriority(hThread);
    3243 
    3244   if(dwTimeout && dwTimeout < 20 && dwThreadPriority != THREAD_PRIORITY_TIME_CRITICAL) {
    3245       dprintf(("Temporarily change priority to THREAD_PRIORITY_TIME_CRITICAL for better timing"));
    3246       SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
    3247       //round to 8 ms units to get more precise timeouts
    3248       if(dwTimeout > 8)
    3249           dwTimeout = (dwTimeout/8)*8;
    3250   }
    3251   else dwThreadPriority = -1;
     3188  BOOL   fChangePriority  = FALSE;
     3189  DWORD  dwThreadPriority;
     3190
     3191  if(dwTimeout && dwTimeout < 20) {
     3192      dwThreadPriority = GetThreadPriority(hThread);
     3193      if(dwThreadPriority != THREAD_PRIORITY_TIME_CRITICAL)
     3194      {
     3195          dprintf(("Temporarily change priority to THREAD_PRIORITY_TIME_CRITICAL for better timing"));
     3196          SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
     3197          //round to 8 ms units to get more precise timeouts
     3198          if(dwTimeout > 8)
     3199              dwTimeout = (dwTimeout/8)*8;
     3200          fChangePriority = TRUE;
     3201      }
     3202  }
    32523203
    32533204  // OK, now forward to Open32.
     
    32603211
    32613212  //Restore old thread priority if we changed it before
    3262   if(dwThreadPriority != -1) {
     3213  if(fChangePriority) {
    32633214      SetThreadPriority(hThread, dwThreadPriority);
    32643215  }
     
    32663217  dprintf2(("MsgWaitForMultipleObjects returned %d", rc));
    32673218  return (rc);                            // OK, done
    3268 #endif
    32693219}
    32703220/*****************************************************************************
Note: See TracChangeset for help on using the changeset viewer.