Ignore:
Timestamp:
Feb 4, 2003, 12:29:03 PM (23 years ago)
Author:
sandervl
Message:

Support DuplicateHandle for threads; cleaned up semaphore code

File:
1 edited

Legend:

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

    r6084 r9748  
    1 /* $Id: hmevent.cpp,v 1.8 2001-06-23 16:59:27 sandervl Exp $ */
     1/* $Id: hmevent.cpp,v 1.9 2003-02-04 11:28:57 sandervl Exp $ */
    22
    33/*
     
    3030 *****************************************************************************/
    3131
    32 #ifdef USE_OS2SEMAPHORES
    33 #define INCL_DOSSEMAPHORES
    34 #define INCL_DOSERRORS
    35 #include <os2wrap.h>
    36 #include <win32type.h>
    37 #include <win32api.h>
    38 #include <winconst.h>
    39 #else
    4032#include <os2win.h>
    41 #endif
    4233
    4334#include <stdlib.h>
     
    5344#define DBG_LOCALLOG    DBG_hmevent
    5445#include "dbglocal.h"
    55 
    56 #ifndef DCE_AUTORESET
    57 #define DCE_AUTORESET   0x1000  /* DosCreateEventSem option to auto-reset  */
    58                                 /* event semaphore on post.                */
    59 #define DCE_POSTONE     0x0800  /* DosCreateEventSem option to post only   */
    60                                 /* waiter and auto-reset the semaphore when*/
    61                                 /* there are multiple waiters.             */
    62 #endif
    6346
    6447/*****************************************************************************
     
    9376                                      LPCTSTR               lpszEventName)
    9477{
    95 #ifdef USE_OS2SEMAPHORES
    96   APIRET rc;
    97   HEV hev;
    98   char szSemName[CCHMAXPATH];
    99 
    100   dprintf(("KERNEL32: HandleManager::Event::CreateEvent(%08xh,%08xh,%08xh,%08xh,%s)\n",
    101            pHMHandleData,
    102            lpsa,
    103            fManualReset,
    104            fInitialState,
    105            lpszEventName));
    106 
    107   if(lpszEventName) {
    108       strcpy(szSemName, "\\SEM32\\");
    109       strcat(szSemName, lpszEventName);
    110       lpszEventName = szSemName;
    111       FixSemName((char *)lpszEventName);
    112   }
    113   //Manual reset means all threads waiting on the event semaphore will be
    114   //unblocked and the app must manually reset the event semaphore
    115   //Automatic reset -> only one waiting thread unblocked & state reset
    116   rc = DosCreateEventSem(lpszEventName, &hev, (fManualReset) ? 0 : (DCE_POSTONE|DCE_AUTORESET), fInitialState);
    117 
    118   if(rc) {
    119       dprintf(("DosCreateEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
    120       pHMHandleData->hHMHandle = 0;
    121       return error2WinError(rc);
    122   }
    123   pHMHandleData->dwAccess  = EVENT_ALL_ACCESS_W;
    124   pHMHandleData->dwFlags   = fManualReset;
    125   pHMHandleData->hHMHandle = hev;
    126   pHMHandleData->dwInternalType = HMTYPE_EVENTSEM;
    127   return ERROR_SUCCESS_W;
    128 #else
    12978  HANDLE hOpen32;
    13079
     
    14796  }
    14897  else
    149     return (O32_GetLastError());
    150 #endif
     98    return (GetLastError());
    15199}
    152100
     
    168116                                    LPCTSTR               lpszEventName)
    169117{
    170 #ifdef USE_OS2SEMAPHORES
    171   HEV    hev;
    172   APIRET rc;
    173   char szSemName[CCHMAXPATH];
    174 
    175   dprintf(("KERNEL32: HandleManager::Event::OpenEvent(%08xh,%08xh,%s)\n",
    176            pHMHandleData,
    177            fInheritHandle,
    178            lpszEventName));
    179 
    180   if(lpszEventName == NULL) {
    181       pHMHandleData->hHMHandle = 0;
    182       return ERROR_INVALID_PARAMETER_W;
    183   }
    184 
    185   strcpy(szSemName, "\\SEM32\\");
    186   strcat(szSemName, lpszEventName);
    187   FixSemName(szSemName);
    188   rc = DosOpenEventSem(szSemName, &hev);
    189   if(rc) {
    190       dprintf(("DosOpenEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
    191       pHMHandleData->hHMHandle = 0;
    192       return error2WinError(rc);
    193   }
    194   pHMHandleData->dwInternalType = HMTYPE_EVENTSEM;
    195   pHMHandleData->hHMHandle = hev;
    196   return ERROR_SUCCESS_W;
    197 #else
    198118  HANDLE hOpen32;
    199119
     
    213133  }
    214134  else
    215     return (O32_GetLastError());
    216 #endif
     135    return (GetLastError());
    217136}
    218 
    219 /*****************************************************************************
    220  * Name      : HMDeviceEventClass::CloseHandle
    221  * Purpose   : close the handle
    222  * Parameters: PHMHANDLEDATA pHMHandleData
    223  * Variables :
    224  * Result    : API returncode
    225  * Remark    :
    226  * Status    :
    227  *
    228  * Author    :
    229  *****************************************************************************/
    230 
    231 #ifdef USE_OS2SEMAPHORES
    232 BOOL HMDeviceEventClass::CloseHandle(PHMHANDLEDATA pHMHandleData)
    233 {
    234   APIRET rc;
    235 
    236   if(pHMHandleData->hHMHandle) {
    237       rc = DosCloseEventSem((HEV)pHMHandleData->hHMHandle);
    238       if(rc) {
    239           dprintf(("DosCloseEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
    240           SetLastError(error2WinError(rc));
    241           return FALSE;
    242       }
    243   }
    244   return TRUE;
    245 }
    246 #endif
    247 
    248 /*****************************************************************************
    249  * Name      : HMDeviceEventClass::DuplicateHandle
    250  * Purpose   :
    251  * Parameters:
    252  *             various parameters as required
    253  * Variables :
    254  * Result    :
    255  * Remark    : the standard behaviour is to return an error code for non-
    256  *             existant request codes
    257  * Status    :
    258  *
    259  * Author    :
    260  *****************************************************************************/
    261 #ifdef USE_OS2SEMAPHORES
    262 BOOL HMDeviceEventClass::DuplicateHandle(PHMHANDLEDATA pHMHandleData, HANDLE  srcprocess,
    263                                PHMHANDLEDATA pHMSrcHandle,
    264                                HANDLE  destprocess,
    265                                PHANDLE desthandle,
    266                                DWORD   fdwAccess,
    267                                BOOL    fInherit,
    268                                DWORD   fdwOptions,
    269                                DWORD   fdwOdinOptions)
    270 {
    271   APIRET rc;
    272   HEV hev;
    273  
    274   dprintf(("KERNEL32:HandleManager::DuplicateHandle %s(%08x,%08x,%08x,%08x,%08x) - NOT IMPLEMENTED!!!!!!!!\n",
    275            lpHMDeviceName,
    276            pHMHandleData,
    277            srcprocess, pHMSrcHandle, destprocess, desthandle));
    278 
    279   if(srcprocess != destprocess) {
    280       DebugInt3();
    281       SetLastError(ERROR_ACCESS_DENIED_W);
    282       return FALSE;
    283   }
    284   hev = (HEV)pHMSrcHandle->hHMHandle;
    285   rc = DosOpenEventSem(NULL, &hev);
    286   if(rc) {
    287       dprintf(("DosOpenEventSem %x failed with rc %d", pHMSrcHandle->hHMHandle, rc));
    288       pHMHandleData->hHMHandle = 0;
    289       SetLastError(error2WinError(rc));
    290       return FALSE;
    291   }
    292   pHMHandleData->dwAccess  = fdwAccess;
    293   pHMHandleData->dwFlags   = pHMSrcHandle->dwFlags; //fManualReset
    294   pHMHandleData->hHMHandle = hev;
    295   pHMHandleData->dwInternalType = HMTYPE_EVENTSEM;
    296   SetLastError(ERROR_SUCCESS_W);
    297   return TRUE;
    298 }
    299 #endif
    300 
    301 #ifdef USE_OS2SEMAPHORES
    302 /*****************************************************************************
    303  * Name      : DWORD HMDeviceEventClass::WaitForSingleObject
    304  * Purpose   : object synchronization
    305  * Parameters: PHMHANDLEDATA pHMHandleData
    306  *             DWORD dwTimeout
    307  * Variables :
    308  * Result    : API returncode
    309  * Remark    :
    310  * Status    :
    311  *
    312  * Author    : SvL
    313  *****************************************************************************/
    314 
    315 DWORD HMDeviceEventClass::WaitForSingleObject(PHMHANDLEDATA pHMHandleData,
    316                                               DWORD         dwTimeout)
    317 {
    318  DWORD rc;
    319 
    320   dprintf2(("KERNEL32: HMDeviceEventClass::WaitForSingleObject(%08xh %08xh)",
    321             pHMHandleData->hHMHandle, dwTimeout));
    322 
    323   if(!(pHMHandleData->dwAccess & SYNCHRONIZE_W) )
    324   {
    325       dprintf(("ERROR: Access denied!!"));
    326       SetLastError(ERROR_ACCESS_DENIED_W);
    327       return WAIT_FAILED_W;
    328   }
    329 
    330   rc = DosWaitEventSem(pHMHandleData->hHMHandle, dwTimeout);
    331   if(rc && rc != ERROR_INTERRUPT && rc != ERROR_TIMEOUT && rc != ERROR_SEM_OWNER_DIED) {
    332       dprintf(("DosWaitEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
    333       SetLastError(error2WinError(rc));
    334       return WAIT_FAILED_W;
    335   }
    336   SetLastError(ERROR_SUCCESS_W);
    337   if(rc == ERROR_INTERRUPT || rc == ERROR_SEM_OWNER_DIED) {
    338       return WAIT_ABANDONED_W;
    339   }
    340   else
    341   if(rc == ERROR_TIMEOUT) {
    342       return WAIT_TIMEOUT_W;
    343   }
    344   return WAIT_OBJECT_0_W;
    345 }
    346 #endif
    347 
    348 #ifdef USE_OS2SEMAPHORES
    349 /*****************************************************************************
    350  * Name      : DWORD HMDeviceEventClass::WaitForSingleObjectEx
    351  * Purpose   : object synchronization
    352  * Parameters: PHMHANDLEDATA pHMHandleData
    353  *             DWORD dwTimeout
    354  *             BOOL  fAlertable
    355  * Variables :
    356  * Result    : API returncode
    357  * Remark    :
    358  * Status    :
    359  *
    360  * Author    : SvL
    361  *****************************************************************************/
    362 
    363 DWORD HMDeviceEventClass::WaitForSingleObjectEx(PHMHANDLEDATA pHMHandleData,
    364                                                 DWORD         dwTimeout,
    365                                                 BOOL          fAlertable)
    366 {
    367   dprintf2(("KERNEL32: HMDeviceEventClass::WaitForSingleObjectEx(%08xh,%08h,%08xh) not implemented correctly.\n",
    368             pHMHandleData->hHMHandle, dwTimeout, fAlertable));
    369 
    370   if(!(pHMHandleData->dwAccess & SYNCHRONIZE_W) )
    371   {
    372       dprintf(("ERROR: Access denied!!"));
    373       SetLastError(ERROR_ACCESS_DENIED_W);
    374       return WAIT_FAILED_W;
    375   }
    376 
    377   return WaitForSingleObject(pHMHandleData, dwTimeout);
    378 }
    379 #endif
    380 
    381 #ifdef USE_OS2SEMAPHORES
    382 /*****************************************************************************
    383  * Name      : BOOL HMDeviceEventClass::MsgWaitForMultipleObjects
    384  * Purpose   :
    385  * Variables :
    386  * Result    :
    387  * Remark    :
    388  * Status    :
    389  *
    390  * Author    : SvL
    391  *****************************************************************************/
    392 DWORD HMDeviceEventClass::MsgWaitForMultipleObjects(PHMHANDLEDATA pHMHandleData,
    393                                                     DWORD      nCount,
    394                                                     PHANDLE    pHandles,
    395                                                     BOOL       fWaitAll,
    396                                                     DWORD      dwMilliseconds,
    397                                                     DWORD      dwWakeMask)
    398 {
    399     return HMSemMsgWaitForMultipleObjects(nCount, pHandles, fWaitAll, dwMilliseconds, dwWakeMask);
    400 }
    401 #endif
    402 
    403 #ifdef USE_OS2SEMAPHORES
    404 /*****************************************************************************
    405  * Name      : BOOL HMDeviceHandler::WaitForMultipleObjects
    406  * Purpose   :
    407  * Variables :
    408  * Result    :
    409  * Remark    :
    410  * Status    :
    411  *
    412  * Author    : SvL
    413  *****************************************************************************/
    414 DWORD HMDeviceEventClass::WaitForMultipleObjects(PHMHANDLEDATA pHMHandleData,
    415                                                  DWORD   cObjects,
    416                                                  PHANDLE lphObjects,
    417                                                  BOOL    fWaitAll,
    418                                                  DWORD   dwTimeout)
    419 {
    420     return HMSemWaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout);
    421 }
    422 #endif
    423137
    424138/*****************************************************************************
     
    436150BOOL HMDeviceEventClass::SetEvent(PHMHANDLEDATA pHMHandleData)
    437151{
    438 #ifdef USE_OS2SEMAPHORES
    439   APIRET rc;
    440 
    441   dprintf(("KERNEL32: HandleManager::Event::SetEvent(%08xh)\n",
    442             pHMHandleData->hHMHandle));
    443 
    444   if(!(pHMHandleData->dwAccess & EVENT_MODIFY_STATE_W) )
    445   {
    446       dprintf(("ERROR: Access denied!!"));
    447       SetLastError(ERROR_ACCESS_DENIED_W);
    448       return FALSE;
    449   }
    450 
    451   rc = DosPostEventSem(pHMHandleData->hHMHandle);
    452   if(rc && rc != ERROR_ALREADY_POSTED) {
    453       dprintf(("DosPostEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
    454       SetLastError(error2WinError(rc));
    455       return FALSE;
    456   }
    457   SetLastError(ERROR_SUCCESS_W);
    458   return TRUE;
    459 #else
    460152  dprintf(("KERNEL32: HandleManager::Event::SetEvent(%08xh)\n",
    461153            pHMHandleData->hHMHandle));
    462154
    463155  return (O32_SetEvent(pHMHandleData->hHMHandle));
    464 #endif
    465156}
    466157
     
    480171BOOL HMDeviceEventClass::PulseEvent(PHMHANDLEDATA pHMHandleData)
    481172{
    482 #ifdef USE_OS2SEMAPHORES
    483   APIRET rc;
    484   ULONG count; 
    485 
    486   dprintf2(("KERNEL32: HandleManager::Event::PulseEvent(%08xh)\n",
    487             pHMHandleData->hHMHandle));
    488 
    489   if(!(pHMHandleData->dwAccess & EVENT_MODIFY_STATE_W) )
    490   {
    491       dprintf(("ERROR: Access denied!!"));
    492       SetLastError(ERROR_ACCESS_DENIED_W);
    493       return FALSE;
    494   }
    495 
    496   rc = DosPostEventSem(pHMHandleData->hHMHandle);
    497   if(rc && rc != ERROR_ALREADY_POSTED) {
    498       dprintf(("DosPostEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
    499       SetLastError(error2WinError(rc));
    500       return FALSE;
    501   }
    502   if(pHMHandleData->dwFlags == TRUE) {//fManualReset
    503       rc = DosResetEventSem(pHMHandleData->hHMHandle, &count);
    504       if(rc && rc != ERROR_ALREADY_RESET) {
    505           dprintf(("DosResetEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
    506           SetLastError(error2WinError(rc));
    507           return FALSE;
    508       }
    509   }
    510   SetLastError(ERROR_SUCCESS_W);
    511   return TRUE;
    512 #else
    513173  dprintf2(("KERNEL32: HandleManager::Event::PulseEvent(%08xh)\n",
    514174            pHMHandleData->hHMHandle));
    515175
    516176  return (O32_PulseEvent(pHMHandleData->hHMHandle));
    517 #endif
    518177}
    519178
     
    533192BOOL HMDeviceEventClass::ResetEvent(PHMHANDLEDATA pHMHandleData)
    534193{
    535 #ifdef USE_OS2SEMAPHORES
    536   APIRET rc;
    537   ULONG count; 
    538  
    539   dprintf2(("KERNEL32: HandleManager::Event::ResetEvent(%08xh)\n",
    540             pHMHandleData->hHMHandle));
    541 
    542   if(!(pHMHandleData->dwAccess & EVENT_MODIFY_STATE_W) )
    543   {
    544       dprintf(("ERROR: Access denied!!"));
    545       SetLastError(ERROR_ACCESS_DENIED_W);
    546       return FALSE;
    547   }
    548 
    549   rc = DosResetEventSem(pHMHandleData->hHMHandle, &count);
    550   if(rc && rc != ERROR_ALREADY_RESET) {
    551       dprintf(("DosResetEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
    552       SetLastError(error2WinError(rc));
    553       return FALSE;
    554   }
    555   SetLastError(ERROR_SUCCESS_W);
    556   return TRUE;
    557 #else
    558194  dprintf2(("KERNEL32: HandleManager::Event::ResetEvent(%08xh)\n",
    559195            pHMHandleData->hHMHandle));
    560196
    561197  return (O32_ResetEvent(pHMHandleData->hHMHandle));
    562 #endif
    563198}
    564199
Note: See TracChangeset for help on using the changeset viewer.