Changeset 6069 for trunk/src


Ignore:
Timestamp:
Jun 22, 2001, 9:40:28 PM (24 years ago)
Author:
sandervl
Message:

semaphore update

Location:
trunk/src/kernel32
Files:
5 edited

Legend:

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

    r6049 r6069  
    1 /* $Id: HandleManager.cpp,v 1.65 2001-06-19 10:50:23 sandervl Exp $ */
     1/* $Id: HandleManager.cpp,v 1.66 2001-06-22 19:40:27 sandervl Exp $ */
    22
    33/*
     
    19131913
    19141914          while(TRUE) {
    1915               dwResult = O32_MsgWaitForMultipleObjects(1, &hObject, FALSE,
    1916                                                        INFINITE, QS_ALLINPUT);
     1915              dwResult = HMMsgWaitForMultipleObjects(1, &hObject, FALSE,
     1916                                                     INFINITE, QS_ALLINPUT);
    19171917              if(dwResult == WAIT_OBJECT_0 + 1) {
    19181918                  MSG msg ;
     
    19481948  }
    19491949
    1950   // @@@PH Problem: wrong class (base class) is called instead of
    1951   //                open32 class ?! Why ?!
    19521950  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
    19531951  dwResult = pHMHandle->pDeviceHandler->WaitForSingleObject(&pHMHandle->hmHandleData,
     
    28772875                                DWORD   dwTimeout)
    28782876{
     2877#ifdef USE_OS2SEMAPHORES
     2878  int       iIndex;                           /* index into the handle table */
     2879  DWORD     dwResult;                /* result from the device handler's API */
     2880  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     2881
     2882  if(cObjects == 1) {
     2883      return HMWaitForSingleObject(*lphObjects, dwTimeout);
     2884  }
     2885                                                          /* validate handle */
     2886  iIndex = _HMHandleQuery(*lphObjects);                   /* get the index */
     2887  if (-1 == iIndex)                                       /* error ? */
     2888  {//oh, oh. possible problem here
     2889   //TODO: rewrite handling of other handles; don't forward to open32
     2890      dprintf(("WANRING: HMWaitForMultipleObjects: unknown handle passed on to Open32 -> will not work if other handles are semaphores"));
     2891      return O32_WaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout);
     2892  }
     2893
     2894  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     2895  dwResult = pHMHandle->pDeviceHandler->WaitForMultipleObjects(&pHMHandle->hmHandleData,
     2896                                                               cObjects, lphObjects, fWaitAll,
     2897                                                               dwTimeout);
     2898
     2899  return (dwResult);                                  /* deliver return code */
     2900#else
    28792901  ULONG   ulIndex;
    28802902  PHANDLE pArrayOfHandles;
     
    29332955
    29342956  return (rc);                            // OK, done
     2957#endif
    29352958}
    29362959
     
    29732996 *****************************************************************************/
    29742997
    2975 DWORD  HMMsgWaitForMultipleObjects  (DWORD      nCount,
    2976                                      LPHANDLE       pHandles,
     2998DWORD  HMMsgWaitForMultipleObjects  (DWORD      cObjects,
     2999                                     LPHANDLE   lphObjects,
    29773000                                     BOOL       fWaitAll,
    2978                                      DWORD      dwMilliseconds,
     3001                                     DWORD      dwTimeout,
    29793002                                     DWORD      dwWakeMask)
    29803003{
     3004#ifdef USE_OS2SEMAPHORES
     3005  int       iIndex;                           /* index into the handle table */
     3006  DWORD     dwResult;                /* result from the device handler's API */
     3007  PHMHANDLE pHMHandle;       /* pointer to the handle structure in the table */
     3008
     3009                                                          /* validate handle */
     3010  iIndex = _HMHandleQuery(*lphObjects);                   /* get the index */
     3011  if (-1 == iIndex)                                       /* error ? */
     3012  {//oh, oh. possible problem here
     3013   //TODO: rewrite handling of other handles; don't forward to open32
     3014      dprintf(("WANRING: HMWaitForMultipleObjects: unknown handle passed on to Open32 -> will not work if other handles are semaphores"));
     3015      return O32_MsgWaitForMultipleObjects(cObjects, lphObjects, fWaitAll, dwTimeout, dwWakeMask);
     3016  }
     3017
     3018  pHMHandle = &TabWin32Handles[iIndex];               /* call device handler */
     3019  dwResult = pHMHandle->pDeviceHandler->MsgWaitForMultipleObjects(&pHMHandle->hmHandleData,
     3020                                                                  cObjects, lphObjects, fWaitAll,
     3021                                                                  dwTimeout, dwWakeMask);
     3022
     3023  return (dwResult);                                  /* deliver return code */
     3024#else
    29813025  ULONG   ulIndex;
    29823026  PHANDLE pArrayOfHandles;
     
    30303074
    30313075  return (rc);                            // OK, done
     3076#endif
    30323077}
    30333078/*****************************************************************************
  • trunk/src/kernel32/hmdevice.h

    r6060 r6069  
    1 /* $Id: hmdevice.h,v 1.27 2001-06-21 21:07:53 sandervl Exp $ */
     1/* $Id: hmdevice.h,v 1.28 2001-06-22 19:40:27 sandervl Exp $ */
    22
    33/*
     
    2525 *****************************************************************************/
    2626
    27 #define HMTYPE_UNKNOWN    0
    28 #define HMTYPE_MEMMAP   1
    29 #define HMTYPE_DEVICE     2
    30 #define HMTYPE_PROCESSTOKEN   3
    31 #define HMTYPE_THREADTOKEN  4
     27#define HMTYPE_UNKNOWN          0
     28#define HMTYPE_MEMMAP           1
     29#define HMTYPE_DEVICE           2
     30#define HMTYPE_PROCESSTOKEN     3
     31#define HMTYPE_THREADTOKEN      4
    3232#define HMTYPE_THREAD           5
    3333#define HMTYPE_PIPE             6
     34#define HMTYPE_EVENTSEM         7
     35#define HMTYPE_MUTEXSEM         8
     36#define HMTYPE_SEMAPHORE        9
    3437//.....
    3538
  • trunk/src/kernel32/hmevent.cpp

    r6060 r6069  
    1 /* $Id: hmevent.cpp,v 1.6 2001-06-21 21:07:53 sandervl Exp $ */
     1/* $Id: hmevent.cpp,v 1.7 2001-06-22 19:40:28 sandervl Exp $ */
    22
    33/*
     
    122122  pHMHandleData->dwFlags   = fManualReset;
    123123  pHMHandleData->hHMHandle = hev;
     124  pHMHandleData->dwInternalType = HMTYPE_EVENTSEM;
    124125  return ERROR_SUCCESS_W;
    125126#else
     
    188189      return error2WinError(rc);
    189190  }
     191  pHMHandleData->dwInternalType = HMTYPE_EVENTSEM;
    190192  pHMHandleData->hHMHandle = hev;
    191193  return ERROR_SUCCESS_W;
     
    288290  pHMHandleData->dwFlags   = pHMSrcHandle->dwFlags; //fManualReset
    289291  pHMHandleData->hHMHandle = hev;
     292  pHMHandleData->dwInternalType = HMTYPE_EVENTSEM;
    290293  SetLastError(ERROR_SUCCESS_W);
    291294  return TRUE;
  • trunk/src/kernel32/hmmutex.cpp

    r6060 r6069  
    1 /* $Id: hmmutex.cpp,v 1.5 2001-06-21 21:07:53 sandervl Exp $ */
     1/* $Id: hmmutex.cpp,v 1.6 2001-06-22 19:40:28 sandervl Exp $ */
    22
    33/*
     
    106106  pHMHandleData->dwAccess  = MUTEX_ALL_ACCESS_W;
    107107  pHMHandleData->hHMHandle = htmx;
     108  pHMHandleData->dwInternalType = HMTYPE_MUTEXSEM;
    108109  return ERROR_SUCCESS_W;
    109110#else
     
    171172  }
    172173  pHMHandleData->hHMHandle = hmtx;
     174  pHMHandleData->dwInternalType = HMTYPE_MUTEXSEM;
    173175  return ERROR_SUCCESS_W;
    174176#else
     
    269271  pHMHandleData->dwAccess  = fdwAccess;
    270272  pHMHandleData->hHMHandle = hmtx;
     273  pHMHandleData->dwInternalType = HMTYPE_MUTEXSEM;
    271274  SetLastError(ERROR_SUCCESS_W);
    272275  return TRUE;
  • trunk/src/kernel32/hmsemaphore.cpp

    r6060 r6069  
    1 /* $Id: hmsemaphore.cpp,v 1.5 2001-06-21 21:07:54 sandervl Exp $ */
     1/* $Id: hmsemaphore.cpp,v 1.6 2001-06-22 19:40:28 sandervl Exp $ */
    22
    33/*
     
    88 * TODO: No inheritance when CreateSemaphore is called for existing named event semaphore?
    99 *       (see HMCreateSemaphore in handlemanager.cpp)
    10  * TODO: OpenSemaphore does not work right now! initialcount/maximumcount)
     10 * TODO: OpenSemaphore does not work. (get SEM_INFO pointer)
    1111 * TODO: Name collisions with files & mutex not allowed. Check if this can happen in OS/2
    12  * TODO: Does NOT work for sharing semaphores between processes!!
    1312 *
    1413 * Project Odin Software License can be found in LICENSE.TXT
     
    4443#include <stdlib.h>
    4544#include <string.h>
     45#include <heapshared.h>
    4646#include "unicode.h"
    4747#include "misc.h"
     
    6262#endif
    6363
     64typedef struct {
     65    LONG currentCount;
     66    LONG maximumCount;
     67    LONG refCount;
     68    HEV  hev;
     69} SEM_INFO, *PSEM_INFO;
    6470
    6571/*****************************************************************************
     
    125131  }
    126132  pHMHandleData->dwAccess  = SEMAPHORE_ALL_ACCESS_W;
    127   pHMHandleData->dwFlags   = lMaximumCount;
    128   pHMHandleData->dwCreation= lInitialCount;
    129   pHMHandleData->hHMHandle = hev;
     133  PSEM_INFO pSemInfo       = (PSEM_INFO)_smalloc(sizeof(SEM_INFO));
     134  pSemInfo->refCount       = 1;
     135  pSemInfo->hev            = hev;
     136  pSemInfo->maximumCount   = lMaximumCount;
     137  pSemInfo->currentCount   = lInitialCount;
     138  pHMHandleData->hHMHandle = (DWORD)pSemInfo;
     139  pHMHandleData->dwInternalType = HMTYPE_SEMAPHORE;
    130140  return ERROR_SUCCESS_W;
    131141#else
     
    196206  }
    197207  pHMHandleData->hHMHandle = hev;
     208  pHMHandleData->dwInternalType = HMTYPE_SEMAPHORE;
    198209  return ERROR_SUCCESS_W;
    199210#else
     
    235246{
    236247  APIRET rc;
    237 
    238   if(pHMHandleData->hHMHandle) {
    239       rc = DosCloseEventSem((HEV)pHMHandleData->hHMHandle);
     248  PSEM_INFO pSemInfo  = (PSEM_INFO)pHMHandleData->hHMHandle;
     249
     250  if(pSemInfo) {
     251      rc = DosCloseEventSem(pSemInfo->hev);
    240252      if(rc) {
    241           dprintf(("DosCloseEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
     253          dprintf(("DosCloseEventSem %x failed with rc %d", pSemInfo->hev, rc));
    242254          SetLastError(error2WinError(rc));
    243255          return FALSE;
     256      }
     257      if(InterlockedDecrement(&pSemInfo->refCount) == 0) {
     258          free(pSemInfo);
    244259      }
    245260  }
     
    273288{
    274289  APIRET rc;
    275   HEV hev;
     290  HEV    hev;
     291  PSEM_INFO pSemInfo  = (PSEM_INFO)pHMSrcHandle->hHMHandle;
    276292 
    277293  dprintf(("KERNEL32:HandleManager::DuplicateHandle %s(%08x,%08x,%08x,%08x,%08x)",
     
    285301      return FALSE;
    286302  }
    287   hev = (HEV)pHMSrcHandle->hHMHandle;
    288   rc = DosOpenEventSem(NULL, &hev);
    289   if(rc) {
    290       dprintf(("DosOpenEventSem %x failed with rc %d", pHMSrcHandle->hHMHandle, rc));
    291       pHMHandleData->hHMHandle = 0;
    292       SetLastError(error2WinError(rc));
    293       return FALSE;
    294   }
     303  InterlockedIncrement(&pSemInfo->refCount);
    295304  pHMHandleData->dwAccess  = fdwAccess;
    296   pHMHandleData->dwFlags   = pHMSrcHandle->dwFlags;    //lMaximumCount;
    297   pHMHandleData->dwCreation= pHMSrcHandle->dwCreation; //lInitialCount;
    298   pHMHandleData->hHMHandle = hev;
     305  pHMHandleData->hHMHandle = (DWORD)pSemInfo;
     306  pHMHandleData->dwInternalType = HMTYPE_SEMAPHORE;
    299307  SetLastError(ERROR_SUCCESS_W);
    300308  return TRUE;
     
    317325
    318326DWORD HMDeviceSemaphoreClass::WaitForSingleObject(PHMHANDLEDATA pHMHandleData,
    319                                                DWORD         dwTimeout)
     327                                                  DWORD         dwTimeout)
    320328{
    321329 DWORD rc;
     
    331339  }
    332340
    333   rc = DosWaitEventSem(pHMHandleData->hHMHandle, dwTimeout);
     341  PSEM_INFO pSemInfo  = (PSEM_INFO)pHMHandleData->hHMHandle;
     342
     343  if(InterlockedDecrement(&pSemInfo->currentCount) >= 0) {
     344      SetLastError(ERROR_SUCCESS_W);
     345      return WAIT_OBJECT_0_W; 
     346  }
     347
     348  rc = DosWaitEventSem(pSemInfo->hev, dwTimeout);
    334349  if(rc && rc != ERROR_INTERRUPT && rc != ERROR_TIMEOUT && rc != ERROR_SEM_OWNER_DIED) {
    335       dprintf(("DosWaitEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
     350      dprintf(("DosWaitEventSem %x failed with rc %d", pSemInfo->hev, rc));
    336351      SetLastError(error2WinError(rc));
    337352      return WAIT_FAILED_W;
     
    477492  }
    478493
    479   rc = DosResetEventSem(pHMHandleData->hHMHandle, &count);
     494  PSEM_INFO pSemInfo  = (PSEM_INFO)pHMHandleData->hHMHandle;
     495
     496  if(InterlockedIncrement(&pSemInfo->currentCount) > 0) {
     497      //TODO: this is NOT thread safe:
     498      if(pSemInfo->currentCount > pSemInfo->maximumCount) {
     499          pSemInfo->currentCount = pSemInfo->maximumCount;
     500      }
     501      SetLastError(ERROR_SUCCESS_W);
     502      return TRUE; 
     503  }
     504
     505  rc = DosResetEventSem(pSemInfo->hev, &count);
    480506  if(rc) {
    481       dprintf(("DosResetEventSem %x failed with rc %d", pHMHandleData->hHMHandle, rc));
     507      dprintf(("DosResetEventSem %x failed with rc %d", pSemInfo->hev, rc));
    482508      SetLastError(error2WinError(rc));
    483509      return FALSE;
Note: See TracChangeset for help on using the changeset viewer.