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; }
/****************************************************************\ * Routine to really stop semaphore example. * *--------------------------------------------------------------* * * * Name: StopSemaphore( PVOID pvArg ) * * * * Purpose: Waits for threads to complete, * * Sends message to message thread to indicate this * * has occurred, and exits. * * * * Usage: Exec'd from BeginStop when user selects Stop from * * Semaphore menu. * * * * Method: Turns off Auto mode, if present by posting auto * * semaphore. Then stop event is posted. Waits * * for threads to die. * * Returns: * * * \****************************************************************/ VOID StopSemaphore( PVOID pvArg ) { ULONG rc,usCount, i; PULONG pfAutoMode = (PULONG)pvArg; if (*pfAutoMode) { rc = DosWaitThread(&tidAutoThread,0L); if (rc && (rc != ERROR_INVALID_THREADID)) { SemError("DosWaitThread",rc); } *pfAutoMode = FALSE; } /* Wait for usConsumer threads to die. Order of death not important. */ for (usCount = 0; usCount < usConsumerThreadsCreated; usCount++) { rc = DosWaitThread(&thrConsumers[usCount].tid,0L); /* rc is ERROR_INVALID_THREADID the thread is already dead.*\ \* This is OK and not a error. */ if (rc && (rc != ERROR_INVALID_THREADID)) { SemError("DosWaitThread",rc); } } /* Threads dead so we don't need semaphores any more. */ DosCloseEventSem(hevStopAuto); DosCloseEventSem(hevStop); DosCloseMutexSem(hmtxOwnResource); for (i = 0; i < MAXRESOURCES; i++) { DosCloseEventSem(aSquares[i].hev); } DosCloseMuxWaitSem (hmuxResource); WinPostMsg (hwndMain, WM_COMMAND, (MPARAM)IDM_STOPFINISHED, (MPARAM)NULL); DosExit (EXIT_THREAD, 0); return; }
void TThreads::suspend() { shutDownFlag=1; TID localTID=mouseThreadID; if (localTID != 0xFFFF) DosWaitThread(&localTID,DCWW_WAIT); // cerr << " MouseThread has ended\n"; ULONG count; assert(! DosQueryEventSem(TThreads::hevKeyboard2,&count)); if (!count) assert(! DosPostEventSem(TThreads::hevKeyboard2)); // Make sure the thread is running. localTID=keyboardThreadID; DosWaitThread(&localTID,DCWW_WAIT); // cerr << " KeyboardThread has ended\n"; assert(!DosCloseMutexSem(hmtxMouse1)); assert(!DosCloseEventSem(hevKeyboard2)); assert(!DosCloseEventSem(hevKeyboard1)); assert(!DosCloseEventSem(hevMouse2)); assert(!DosCloseEventSem(hevMouse1)); assert(!DosCloseMuxWaitSem(hmuxMaster)); if (tiled->mouseHandle != 0xFFFF) MouClose(tiled->mouseHandle); // This doesn't work, the mouseThread uses the handle. assert(! DosFreeMem(tiled)); // Better not, dito }
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; }
BOOL OMuxWaitSem::closeMuxWaitSem() { muxwaitState = DosCloseMuxWaitSem(hmux); return(muxwaitState == 0); }