ULONG OMuxWaitSem::waitMuxWaitSem(ULONG timeout) { ULONG posted = 0; muxwaitState = DosWaitMuxWaitSem(hmux, timeout, &posted); if (muxwaitState) return(0); return(posted); }
/****************************************************************\ * Routine for consumer threads. * *--------------------------------------------------------------* * * * Name: ThreadConsumer(PVOID) * * * * Purpose: There are NUMUSERS copies of this thread to act * * as consumers of the resource. The thread waits for* * exclusive access to the resource and colors it. * * * * Usage: Threads created by StartSemExample. * * * * Method: Waits for mutex to gain ownership of resource. * * Releases resource when user event. Dies when * * Stop event. * * Returns: * * * \****************************************************************/ VOID ThreadConsumer( PVOID pvMyID ) { ULONG ulPostCnt; ULONG ulUser=0L; ULONG rc; HAB habb; HMQ hmqq; /* Need a message queue for any thread that wants to print messages. */ habb = WinInitialize(0); hmqq = WinCreateMsgQueue(habb,0); /* while the user has not selected stop ... */ while ((DosWaitEventSem(hevStop,SEM_IMMEDIATE_RETURN)) == ERROR_TIMEOUT) { /* Wait for exclusive ownership of resource */ DosRequestMutexSem(hmtxOwnResource,SEM_INDEFINITE_WAIT); /* the following check is necessary because the stop semaphore * may have been posted while we were waiting on the mutex */ if (DosWaitEventSem(hevStop, SEM_IMMEDIATE_RETURN) == ERROR_TIMEOUT) { /*********************************************************************\ * an item is ready, which one? don't wait forever because there is * * a possibility that the stop semaphore was posted and that no more * * resource is forthcoming. we wait twice as long as we think is * * necessary. * \*********************************************************************/ if (!DosWaitMuxWaitSem (hmuxResource, max (ulTimeout << 1, TIMEOUTPERIOD) , &ulUser)) { MyMove ((ULONG) pvMyID, ulUser); DosResetEventSem(aSquares[ulUser].hev, &ulPostCnt); } } /* Let some other thread have resource. */ rc = DosReleaseMutexSem(hmtxOwnResource); if (rc) { SemError("DosReleaseMutexSem",rc); } } /* hevStop was posted, kill this thread */ WinDestroyMsgQueue (hmqq); WinTerminate (habb); DosExit(EXIT_THREAD, 0); return; }
static ULONG vlc_DosWaitEventSemEx( HEV hev, ULONG ulTimeout, BOOL fCancelable ) { HMUX hmux; SEMRECORD asr[ 2 ]; ULONG ulUser; int n; ULONG rc; struct vlc_thread *th = vlc_threadvar_get( thread_key ); if( th == NULL || !fCancelable ) { /* Main thread - cannot be cancelled anyway * Alien thread - out of our control */ if( hev != NULLHANDLE ) return DosWaitEventSem( hev, ulTimeout ); return DosSleep( ulTimeout ); } n = 0; if( hev != NULLHANDLE ) { asr[ n ].hsemCur = ( HSEM )hev; asr[ n ].ulUser = 0; n++; } asr[ n ].hsemCur = ( HSEM )th->cancel_event; asr[ n ].ulUser = 0xFFFF; n++; DosCreateMuxWaitSem( NULL, &hmux, n, asr, DCMW_WAIT_ANY ); rc = DosWaitMuxWaitSem( hmux, ulTimeout, &ulUser ); DosCloseMuxWaitSem( hmux ); if( rc ) return rc; if( ulUser == 0xFFFF ) { vlc_cancel_self( th ); return ERROR_INTERRUPT; } return NO_ERROR; }
int main(VOID) { HMUX hmuxHandAny = NULLHANDLE; /* Muxwaithandle */ HEV hev[5] = {0}; /* Event semaphores */ SEMRECORD apsr[5] = {{0}}; /* Semaphore records */ APIRET rc = NO_ERROR; /* Return code */ ULONG ulLoop = 0; /* Loop count */ ULONG ulSem = 0; for (ulLoop = 0; ulLoop < 5; ulLoop++) { rc = DosCreateEventSem((PSZ) NULL, &hev[ulLoop], 0, FALSE); if (rc != NO_ERROR) { printf("DosCreateEventSem error: return code = %u\n", rc); return 1; } apsr[ulLoop].hsemCur = (HSEM) hev[ulLoop], apsr[ulLoop].ulUser = 0; } /* endfor */ rc = DosCreateMuxWaitSem((PSZ) NULL, &hmuxHandAny, 5L, /* Number of semaphores in list */ apsr, /* Semaphore list */ DCMW_WAIT_ANY); /* Wait for any semaphore */ if (rc != NO_ERROR) { printf("DosCreateMuxWaitSem error: return code = %u\n", rc); return 1; } rc = DosWaitMuxWaitSem(hmuxHandAny, SEM_IMMEDIATE_RETURN, &ulSem); /* No semaphores have been posted, so we should see a timeout below... */ if (rc != ERROR_TIMEOUT) { printf("DosWaitMuxWaitSem error: return code = %u\n", rc); return 1; } rc = DosDeleteMuxWaitSem(hmuxHandAny, apsr[0].hsemCur); if (rc != NO_ERROR) { printf("DosDeleteMuxWaitSem error: return code = %u\n", rc); return 1; } rc = DosCloseMuxWaitSem(hmuxHandAny); if (rc != NO_ERROR) { printf("DosCloseMuxWaitSem error: return code = %u\n", rc); return 1; } return NO_ERROR; }
void TProgram::getEvent(TEvent& event) { if( pending.what != evNothing ) { event = pending; pending.what = evNothing; } else { #ifdef __MSDOS__ event.getMouseEvent(); if( event.what == evNothing ) { event.getKeyEvent(); if( event.what == evNothing ) idle(); } #else // __MSDOS__ #ifdef __OS2__ ULONG which; int delay = TEventQueue::lastMouse.buttons != 0 ? TEventQueue::autoDelay : event_delay; APIRET rc = DosWaitMuxWaitSem(TThreads::hmuxMaster,delay,&which); if (rc==0 || TEventQueue::lastMouse.buttons != 0) { if (which==0 || TEventQueue::lastMouse.buttons != 0) { event.getMouseEvent(); } else if (which==1) { event.getKeyEvent(); } } else { event.what = evNothing; idle(); } #else #ifdef __NT__ DWORD rc; int delay = TEventQueue::lastMouse.buttons != 0 ? TEventQueue::autoDelay : event_delay; rc = TThreads::ispending() ? WAIT_OBJECT_0 : WaitForSingleObject(TThreads::chandle[0],delay); switch ( rc ) { default: case WAIT_OBJECT_0: event.getMouseEvent(); if ( event.what == evNothing ) { event.getKeyEvent(); if ( event.what == evNothing ) idle(); } break; case WAIT_TIMEOUT: event.what = evNothing; if ( TEventQueue::lastMouse.buttons != 0 ) event.getMouseEvent(); if ( event.what == evNothing ) idle(); break; } #else #error Unknown platform! #endif // __NT__ #endif // __OS2__ #endif // __MSDOS__ } if( statusLine != 0 ) { if( (event.what & evKeyDown) != 0 || ( (event.what & evMouseDown) != 0 && firstThat( hasMouse, &event ) == statusLine ) ) statusLine->handleEvent( event ); } }