| 1 | /* $Id: vmutex.cpp,v 1.9 2000-03-23 19:23:47 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Mutex class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | *
|
|---|
| 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
|
|---|
| 14 | *
|
|---|
| 15 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 16 | *
|
|---|
| 17 | */
|
|---|
| 18 | #define INCL_DOSSEMAPHORES
|
|---|
| 19 | #define INCL_DOSERRORS
|
|---|
| 20 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 21 | #include <vmutex.h>
|
|---|
| 22 | #include <win32type.h>
|
|---|
| 23 | #include <misc.h>
|
|---|
| 24 |
|
|---|
| 25 | #define DBG_LOCALLOG DBG_vmutex
|
|---|
| 26 | #include "dbglocal.h"
|
|---|
| 27 |
|
|---|
| 28 | /******************************************************************************/
|
|---|
| 29 | /******************************************************************************/
|
|---|
| 30 | VMutex::VMutex(int fShared, HMTX *phMutex) : waiting(0)
|
|---|
| 31 | {
|
|---|
| 32 | APIRET rc;
|
|---|
| 33 |
|
|---|
| 34 | this->fShared = fShared;
|
|---|
| 35 | rc = DosCreateMutexSem(NULL, &sem_handle, (fShared == VMUTEX_SHARED) ? DC_SEM_SHARED : 0, FALSE);
|
|---|
| 36 | if(rc != 0) {
|
|---|
| 37 | dprintf(("Error creating mutex %X\n", rc));
|
|---|
| 38 | sem_handle = 0;
|
|---|
| 39 | }
|
|---|
| 40 | if(fShared) {
|
|---|
| 41 | *phMutex = sem_handle;
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | /******************************************************************************/
|
|---|
| 45 | /******************************************************************************/
|
|---|
| 46 | VMutex::~VMutex()
|
|---|
| 47 | {
|
|---|
| 48 | int i;
|
|---|
| 49 |
|
|---|
| 50 | if(sem_handle) {
|
|---|
| 51 | if(fShared != VMUTEX_SHARED) {
|
|---|
| 52 | for(i=0;i<waiting;i++) {
|
|---|
| 53 | DosReleaseMutexSem(sem_handle);
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | DosCloseMutexSem(sem_handle);
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | /******************************************************************************/
|
|---|
| 60 | /******************************************************************************/
|
|---|
| 61 | void VMutex::enter(ULONG timeout, HMTX *phMutex)
|
|---|
| 62 | {
|
|---|
| 63 | APIRET rc;
|
|---|
| 64 |
|
|---|
| 65 | if(fShared == VMUTEX_SHARED && phMutex == NULL) {
|
|---|
| 66 | DebugInt3();
|
|---|
| 67 | return;
|
|---|
| 68 | }
|
|---|
| 69 | if(sem_handle) {
|
|---|
| 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--;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | /******************************************************************************/
|
|---|
| 84 | /******************************************************************************/
|
|---|
| 85 | void VMutex::leave(HMTX *phMutex)
|
|---|
| 86 | {
|
|---|
| 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);
|
|---|
| 94 | }
|
|---|
| 95 | /******************************************************************************/
|
|---|
| 96 | /******************************************************************************/
|
|---|
| 97 |
|
|---|