When a process creates an event semaphore, all of the threads that belong to the process have immediate access to the semaphore.
Threads in other processes must open the semaphore by calling DosOpenEventSem before they can use the semaphore in any other event semaphore function.
The following code fragment shows how processes can open an event semaphore that was created in a different process and then wait for the event to be posted:
#define INCL_DOSSEMAPHORES /* Semaphore values */ #include <os2.h> HEV hevEventHandle = 0; /* Must be 0 because we are opening */ /* the semaphore by name */ DosOpenEventSem("\\sem32\\wrtevent", &hevEventHandle); DosWaitEventSem(hevEventHandle, SEM_INDEFINITE_WAIT); /* Waits until event is posted */ . . /* Read from file when event is posted. */ .
Applications can open an event semaphore by name or by handle. If the name is used to open the semaphore, as in the code fragment above, the handle parameter must be 0. If the handle is used to open the semaphore, the name parameter must be NULL.
Access to semaphores is on a per-process basis. Therefore, a semaphore that has been opened by one thread in a process is open to all other threads in that process as well.
DosOpenEventSem merely provides access to an event semaphore. In order to wait for an event semaphore to be posted, a thread must call DosWaitEventSem. In order to post or reset an open event semaphore, a thread uses DosPostEventSem or DosResetEventSem respectively.
When a process no longer requires access to an event semaphore, it closes the semaphore by calling DosCloseEventSem. If a process ends without closing an open semaphore, the semaphore is closed by OS/2.
Each call to DosOpenEventSem increments the usage count of the semaphore. This count is initialized to 1 when the semaphore is created and is decremented by each call to DosCloseEventSem. When the usage count reaches 0, the semaphore is deleted by OS/2.
Calls to DosOpenEventSem and DosCloseEventSem can be nested, but the usage count for a semaphore cannot exceed 65535. If an attempt is made to exceed this number, ERROR_TOO_MANY_OPENS is returned.