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

mutex fixes + added vsemaphore class

File:
1 edited

Legend:

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

    r2802 r3206  
    1 /* $Id: vmutex.cpp,v 1.8 2000-02-16 14:22:47 sandervl Exp $ */
     1/* $Id: vmutex.cpp,v 1.9 2000-03-23 19:23:47 sandervl Exp $ */
    22
    33/*
    44 * Mutex class
    55 *
    6  * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
     6 * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
    77 *
     8 *
     9 * NOTE: When creating a shared mutex object, you must call the ctor, enter &
     10 *       leave with a 2nd parameter (HMTX *). This mutex handle must be located
     11 *       in the local data segment of the dll.
     12 *       This enables other processes than the one that created the mutex object
     13 *       to access it
    814 *
    915 * Project Odin Software License can be found in LICENSE.TXT
    1016 *
    1117 */
    12 #define  INCL_DOSSEMAPHORES
     18#define INCL_DOSSEMAPHORES
     19#define INCL_DOSERRORS
    1320#include <os2wrap.h>    //Odin32 OS/2 api wrappers
    1421#include <vmutex.h>
     
    2128/******************************************************************************/
    2229/******************************************************************************/
    23 VMutex::VMutex(int fShared) : waiting(0)
     30VMutex::VMutex(int fShared, HMTX *phMutex) : waiting(0)
    2431{
    2532 APIRET rc;
    2633
     34  this->fShared = fShared;
    2735  rc = DosCreateMutexSem(NULL, &sem_handle, (fShared == VMUTEX_SHARED) ? DC_SEM_SHARED : 0, FALSE);
    2836  if(rc != 0) {
    2937    dprintf(("Error creating mutex %X\n", rc));
    3038    sem_handle = 0;
     39  }
     40  if(fShared) {
     41        *phMutex = sem_handle;
    3142  }
    3243}
     
    3849
    3950  if(sem_handle) {
    40     for(i=0;i<waiting;i++) {
    41         DosReleaseMutexSem(sem_handle);
    42     }
    43     DosCloseMutexSem(sem_handle);
     51        if(fShared != VMUTEX_SHARED) {
     52                for(i=0;i<waiting;i++) {
     53                        DosReleaseMutexSem(sem_handle);
     54                }
     55        }
     56        DosCloseMutexSem(sem_handle);
    4457  }
    4558}
    4659/******************************************************************************/
    4760/******************************************************************************/
    48 void VMutex::enter(ULONG timeout)
     61void VMutex::enter(ULONG timeout, HMTX *phMutex)
    4962{
     63 APIRET rc;
     64
     65  if(fShared == VMUTEX_SHARED && phMutex == NULL) {
     66        DebugInt3();
     67        return;
     68  }
    5069  if(sem_handle) {
    51     waiting++;
    52     DosRequestMutexSem(sem_handle, timeout);
    53     waiting--;
     70        if(fShared == VMUTEX_SHARED && *phMutex == 0) {
     71                //must open the shared semaphore first (other process created it)
     72                *phMutex = sem_handle;
     73                rc = DosOpenMutexSem(NULL, phMutex);
     74                if(rc) {
     75                        DebugInt3();
     76                }
     77        }
     78        waiting++;
     79        rc = DosRequestMutexSem((fShared == VMUTEX_SHARED) ? *phMutex : sem_handle, timeout);
     80        waiting--;
    5481  }
    5582}
    5683/******************************************************************************/
    5784/******************************************************************************/
    58 void VMutex::leave()
     85void VMutex::leave(HMTX *phMutex)
    5986{
    60   DosReleaseMutexSem(sem_handle);
     87  if((fShared == VMUTEX_SHARED && phMutex == NULL) ||
     88     (fShared == VMUTEX_SHARED && *phMutex == 0)) {
     89        DebugInt3();
     90        //should always call enter first...
     91        return;
     92  }
     93  DosReleaseMutexSem((fShared == VMUTEX_SHARED) ? *phMutex : sem_handle);
    6194}
    6295/******************************************************************************/
Note: See TracChangeset for help on using the changeset viewer.