1 | /* $Id: vsemaphore.cpp,v 1.1 2000-03-23 19:23:48 sandervl 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 | #define DBG_LOCALLOG DBG_VSemaphore
|
---|
26 | #include "dbglocal.h"
|
---|
27 |
|
---|
28 | /******************************************************************************/
|
---|
29 | /******************************************************************************/
|
---|
30 | VSemaphore::VSemaphore(int fShared, HEV *phSem)
|
---|
31 | {
|
---|
32 | APIRET rc;
|
---|
33 |
|
---|
34 | this->fShared = fShared;
|
---|
35 | rc = DosCreateEventSem(NULL, &sem_handle, (fShared == VSEMAPHORE_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 | *phSem = sem_handle;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | /******************************************************************************/
|
---|
45 | /******************************************************************************/
|
---|
46 | VSemaphore::~VSemaphore()
|
---|
47 | {
|
---|
48 | if(sem_handle) {
|
---|
49 | DosCloseEventSem(sem_handle);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | /******************************************************************************/
|
---|
53 | /******************************************************************************/
|
---|
54 | void VSemaphore::wait(ULONG timeout, HEV *phSem)
|
---|
55 | {
|
---|
56 | APIRET rc;
|
---|
57 |
|
---|
58 | if(fShared == VSEMAPHORE_SHARED && phSem == NULL) {
|
---|
59 | DebugInt3();
|
---|
60 | return;
|
---|
61 | }
|
---|
62 | if(sem_handle) {
|
---|
63 | if(fShared == VSEMAPHORE_SHARED && *phSem == 0) {
|
---|
64 | //must open the shared semaphore first (other process created it)
|
---|
65 | *phSem = sem_handle;
|
---|
66 | rc = DosOpenEventSem(NULL, phSem);
|
---|
67 | if(rc) {
|
---|
68 | DebugInt3();
|
---|
69 | }
|
---|
70 | }
|
---|
71 | rc = DosWaitEventSem((fShared == VSEMAPHORE_SHARED) ? *phSem : sem_handle, timeout);
|
---|
72 | if(rc) {
|
---|
73 | DebugInt3();
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | /******************************************************************************/
|
---|
78 | /******************************************************************************/
|
---|
79 | void VSemaphore::reset(HMTX *phSem)
|
---|
80 | {
|
---|
81 | APIRET rc;
|
---|
82 | ULONG count;
|
---|
83 |
|
---|
84 | if(fShared == VSEMAPHORE_SHARED && phSem == NULL) {
|
---|
85 | DebugInt3();
|
---|
86 | return;
|
---|
87 | }
|
---|
88 | if(fShared == VSEMAPHORE_SHARED && *phSem == 0) {
|
---|
89 | //must open the shared semaphore first (other process created it)
|
---|
90 | *phSem = sem_handle;
|
---|
91 | rc = DosOpenEventSem(NULL, phSem);
|
---|
92 | if(rc) {
|
---|
93 | DebugInt3();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | rc = DosResetEventSem((fShared == VSEMAPHORE_SHARED) ? *phSem : sem_handle, &count);
|
---|
97 | if(rc != NO_ERROR && rc != ERROR_ALREADY_RESET) {
|
---|
98 | DebugInt3();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | /******************************************************************************/
|
---|
102 | /******************************************************************************/
|
---|
103 | void VSemaphore::post(HMTX *phSem)
|
---|
104 | {
|
---|
105 | APIRET rc;
|
---|
106 |
|
---|
107 | if(fShared == VSEMAPHORE_SHARED && phSem == NULL) {
|
---|
108 | DebugInt3();
|
---|
109 | return;
|
---|
110 | }
|
---|
111 | if(fShared == VSEMAPHORE_SHARED && *phSem == 0) {
|
---|
112 | //must open the shared semaphore first (other process created it)
|
---|
113 | *phSem = sem_handle;
|
---|
114 | rc = DosOpenEventSem(NULL, phSem);
|
---|
115 | if(rc) {
|
---|
116 | DebugInt3();
|
---|
117 | }
|
---|
118 | }
|
---|
119 | rc = DosPostEventSem((fShared == VSEMAPHORE_SHARED) ? *phSem : sem_handle);
|
---|
120 | if(rc != NO_ERROR && rc != ERROR_ALREADY_POSTED) {
|
---|
121 | DebugInt3();
|
---|
122 | }
|
---|
123 | }
|
---|
124 | /******************************************************************************/
|
---|
125 | /******************************************************************************/
|
---|
126 |
|
---|