| 1 | /* $Id: vsemaphore.cpp,v 1.2 2002-07-30 15:34:52 achimha Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Semaphore class
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * NOTE: When creating a shared semaphore object, you must call the ctor, wait &
|
|---|
| 10 | * post with a 2nd parameter (HMTX *). This semaphore handle must be located
|
|---|
| 11 | * in the local data segment of the dll.
|
|---|
| 12 | * This enables other processes than the one that created the semaphore 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 "vsemaphore.h"
|
|---|
| 22 | #include <win32type.h>
|
|---|
| 23 | #include <misc.h>
|
|---|
| 24 |
|
|---|
| 25 | /******************************************************************************/
|
|---|
| 26 | /******************************************************************************/
|
|---|
| 27 | VSemaphore::VSemaphore(int fShared, HEV *phSem)
|
|---|
| 28 | {
|
|---|
| 29 | APIRET rc;
|
|---|
| 30 |
|
|---|
| 31 | this->fShared = fShared;
|
|---|
| 32 | rc = DosCreateEventSem(NULL, &sem_handle, (fShared == VSEMAPHORE_SHARED) ? DC_SEM_SHARED : 0, FALSE);
|
|---|
| 33 | if(rc != 0) {
|
|---|
| 34 | dprintf(("Error creating mutex %X\n", rc));
|
|---|
| 35 | sem_handle = 0;
|
|---|
| 36 | }
|
|---|
| 37 | if(fShared) {
|
|---|
| 38 | *phSem = sem_handle;
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 | /******************************************************************************/
|
|---|
| 42 | /******************************************************************************/
|
|---|
| 43 | VSemaphore::~VSemaphore()
|
|---|
| 44 | {
|
|---|
| 45 | if(sem_handle) {
|
|---|
| 46 | DosCloseEventSem(sem_handle);
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | /******************************************************************************/
|
|---|
| 50 | /******************************************************************************/
|
|---|
| 51 | void VSemaphore::wait(ULONG timeout, HEV *phSem)
|
|---|
| 52 | {
|
|---|
| 53 | APIRET rc;
|
|---|
| 54 |
|
|---|
| 55 | if(fShared == VSEMAPHORE_SHARED && phSem == NULL) {
|
|---|
| 56 | DebugInt3();
|
|---|
| 57 | return;
|
|---|
| 58 | }
|
|---|
| 59 | if(sem_handle) {
|
|---|
| 60 | if(fShared == VSEMAPHORE_SHARED && *phSem == 0) {
|
|---|
| 61 | //must open the shared semaphore first (other process created it)
|
|---|
| 62 | *phSem = sem_handle;
|
|---|
| 63 | rc = DosOpenEventSem(NULL, phSem);
|
|---|
| 64 | if(rc) {
|
|---|
| 65 | DebugInt3();
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | rc = DosWaitEventSem((fShared == VSEMAPHORE_SHARED) ? *phSem : sem_handle, timeout);
|
|---|
| 69 | if(rc) {
|
|---|
| 70 | DebugInt3();
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 | /******************************************************************************/
|
|---|
| 75 | /******************************************************************************/
|
|---|
| 76 | void VSemaphore::reset(HMTX *phSem)
|
|---|
| 77 | {
|
|---|
| 78 | APIRET rc;
|
|---|
| 79 | ULONG count;
|
|---|
| 80 |
|
|---|
| 81 | if(fShared == VSEMAPHORE_SHARED && phSem == NULL) {
|
|---|
| 82 | DebugInt3();
|
|---|
| 83 | return;
|
|---|
| 84 | }
|
|---|
| 85 | if(fShared == VSEMAPHORE_SHARED && *phSem == 0) {
|
|---|
| 86 | //must open the shared semaphore first (other process created it)
|
|---|
| 87 | *phSem = sem_handle;
|
|---|
| 88 | rc = DosOpenEventSem(NULL, phSem);
|
|---|
| 89 | if(rc) {
|
|---|
| 90 | DebugInt3();
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | rc = DosResetEventSem((fShared == VSEMAPHORE_SHARED) ? *phSem : sem_handle, &count);
|
|---|
| 94 | if(rc != NO_ERROR && rc != ERROR_ALREADY_RESET) {
|
|---|
| 95 | DebugInt3();
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | /******************************************************************************/
|
|---|
| 99 | /******************************************************************************/
|
|---|
| 100 | void VSemaphore::post(HMTX *phSem)
|
|---|
| 101 | {
|
|---|
| 102 | APIRET rc;
|
|---|
| 103 |
|
|---|
| 104 | if(fShared == VSEMAPHORE_SHARED && phSem == NULL) {
|
|---|
| 105 | DebugInt3();
|
|---|
| 106 | return;
|
|---|
| 107 | }
|
|---|
| 108 | if(fShared == VSEMAPHORE_SHARED && *phSem == 0) {
|
|---|
| 109 | //must open the shared semaphore first (other process created it)
|
|---|
| 110 | *phSem = sem_handle;
|
|---|
| 111 | rc = DosOpenEventSem(NULL, phSem);
|
|---|
| 112 | if(rc) {
|
|---|
| 113 | DebugInt3();
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | rc = DosPostEventSem((fShared == VSEMAPHORE_SHARED) ? *phSem : sem_handle);
|
|---|
| 117 | if(rc != NO_ERROR && rc != ERROR_ALREADY_POSTED) {
|
|---|
| 118 | DebugInt3();
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | /******************************************************************************/
|
|---|
| 122 | /******************************************************************************/
|
|---|
| 123 |
|
|---|