void InitializeDoseInternalFromApp() { auto sem = SharedMemoryObject::GetSharedMemory().find_or_construct<boost::interprocess::interprocess_semaphore> ("InitializationGateKeeper")(0); lllog(1) << "Waiting for dose_main to initialize dose_internal" << std::endl; for(;;) { if (sem->try_wait()) { break; } //sleep_for contains an interruption point, which makes it possible to interrupt the thread //if it is hanging in here. Useful in dobexplorer, for example. boost::this_thread::sleep_for(boost::chrono::milliseconds(100)); } sem->post(); lllog(1) << "Connecting to dose_internal from app" << std::endl; Connections::Initialize(false,0); ContextSharedTable::Initialize(); MessageTypes::Initialize(false); EndStates::Initialize(); ServiceTypes::Initialize(false,0); InjectionKindTable::Initialize(); EntityTypes::Initialize(false,0); }
bool SSemaphore::post_and_wait() { // TODO: merge these functions? Probably has a race condition. if (try_wait()) return false; post(); wait(); return true; }
void Baton::wait(TimeoutHandler& timeoutHandler) { auto timeoutFunc = [this, &timeoutHandler] { if (!try_wait()) { postHelper(TIMEOUT); } timeoutHandler.timeoutPtr_ = 0; }; timeoutHandler.timeoutFunc_ = std::ref(timeoutFunc); timeoutHandler.fiberManager_ = FiberManager::getFiberManagerUnsafe(); wait(); timeoutHandler.cancelTimeout(); }
bool Baton::spinWaitForEarlyPost() { static_assert(PreBlockAttempts > 0, "isn't this assert clearer than an uninitialized variable warning?"); for (int i = 0; i < PreBlockAttempts; ++i) { if (try_wait()) { // hooray! return true; } // The pause instruction is the polite way to spin, but it doesn't // actually affect correctness to omit it if we don't have it. // Pausing donates the full capabilities of the current core to // its other hyperthreads for a dozen cycles or so asm_volatile_pause(); } return false; }
int main(void *arg) { int sem, i; (void)arg; assert(screate(-2) == -1); assert((sem = screate(2)) >= 0); assert(signaln(sem, -4) < 0); assert(sreset(sem, -3) == -1); assert(scount(sem) == 2); assert(signaln(sem, 32760) == 0); assert(signaln(sem, 6) == -2); assert(scount(sem) == 32762); assert(wait(sem) == 0); assert(scount(sem) == 32761); assert(signaln(sem, 30000) == -2); assert(scount(sem) == 32761); assert(wait(sem) == 0); assert(scount(sem) == 32760); assert(signaln(sem, -2) < 0); assert(scount(sem) == 32760); assert(wait(sem) == 0); assert(scount(sem) == 32759); assert(signaln(sem, 8) == 0); assert(scount(sem) == 32767); assert(signaln(sem, 1) == -2); assert(scount(sem) == 32767); assert(signal(sem) == -2); assert(scount(sem) == 32767); for (i=0; i<32767; i++) { assert(wait(sem) == 0); } assert(try_wait(sem) == -3); assert(scount(sem) == 0); assert(sdelete(sem) == 0); printf("ok.\n"); }
void exec_wait() { int i = -1; int exit_reason; /* reason why a command completed */ /* Wait for a command to complete, while snarfing up any output. */ while ( 1 ) { /* Check for a complete command, briefly. */ i = try_wait( 500 ); /* Read in the output of all running commands. */ read_output(); /* Close out pending debug style dialogs. */ close_alerts(); /* Process the completed command we found. */ if ( i >= 0 ) { exit_reason = EXIT_OK; break; } /* Check if a command ran out of time. */ i = try_kill_one(); if ( i >= 0 ) { exit_reason = EXIT_TIMEOUT; break; } } /* We have a command... process it. */ { DWORD exit_code; timing_info time; int rstat; /* The time data for the command. */ record_times( cmdtab[ i ].pi.hProcess, &time ); /* Removed the used temporary command file. */ if ( cmdtab[ i ].command_file->size ) unlink( cmdtab[ i ].command_file->value ); /* Find out the process exit code. */ GetExitCodeProcess( cmdtab[ i ].pi.hProcess, &exit_code ); /* The dispossition of the command. */ if ( interrupted() ) rstat = EXEC_CMD_INTR; else if ( exit_code ) rstat = EXEC_CMD_FAIL; else rstat = EXEC_CMD_OK; /* Call the callback, may call back to jam rule land. */ (*cmdtab[ i ].func)( cmdtab[ i ].closure, rstat, &time, cmdtab[ i ].buffer_out->value, cmdtab[ i ].buffer_err->value, exit_reason ); /* Clean up our child process tracking data. No need to clear the * temporary command file name as it gets reused. */ closeWinHandle( &cmdtab[ i ].pi.hProcess ); closeWinHandle( &cmdtab[ i ].pi.hThread ); closeWinHandle( &cmdtab[ i ].pipe_out[ EXECCMD_PIPE_READ ] ); closeWinHandle( &cmdtab[ i ].pipe_out[ EXECCMD_PIPE_WRITE ] ); closeWinHandle( &cmdtab[ i ].pipe_err[ EXECCMD_PIPE_READ ] ); closeWinHandle( &cmdtab[ i ].pipe_err[ EXECCMD_PIPE_WRITE ] ); string_renew( cmdtab[ i ].buffer_out ); string_renew( cmdtab[ i ].buffer_err ); } }
int exec_wait() { int i = -1; /* Handle naive make1() which does not know if cmds are running. */ if ( !cmdsrunning ) return 0; /* Wait for a command to complete, while snarfing up any output. */ do { /* Check for a complete command, briefly. */ i = try_wait(500); /* Read in the output of all running commands. */ read_output(); /* Close out pending debug style dialogs. */ close_alerts(); /* Check if a command ran out of time. */ if ( i < 0 ) i = try_kill_one(); } while ( i < 0 ); /* We have a command... process it. */ --cmdsrunning; { timing_info time; int rstat; /* The time data for the command. */ record_times( cmdtab[ i ].pi.hProcess, &time ); /* Clear the temp file. */ if ( cmdtab[ i ].tempfile_bat ) { unlink( cmdtab[ i ].tempfile_bat ); BJAM_FREE( cmdtab[ i ].tempfile_bat ); cmdtab[ i ].tempfile_bat = NULL; } /* Find out the process exit code. */ GetExitCodeProcess( cmdtab[ i ].pi.hProcess, &cmdtab[ i ].exit_code ); /* The dispossition of the command. */ if ( intr ) rstat = EXEC_CMD_INTR; else if ( cmdtab[ i ].exit_code != 0 ) rstat = EXEC_CMD_FAIL; else rstat = EXEC_CMD_OK; /* Output the action block. */ out_action( cmdtab[ i ].action.size > 0 ? cmdtab[ i ].action.value : 0, cmdtab[ i ].target.size > 0 ? cmdtab[ i ].target.value : 0, cmdtab[ i ].command.size > 0 ? cmdtab[ i ].command.value : 0, cmdtab[ i ].buffer_out.size > 0 ? cmdtab[ i ].buffer_out.value : 0, cmdtab[ i ].buffer_err.size > 0 ? cmdtab[ i ].buffer_err.value : 0, cmdtab[ i ].exit_reason ); /* Call the callback, may call back to jam rule land. Assume -p0 in * effect so only pass buffer containing merged output. */ (*cmdtab[ i ].func)( cmdtab[ i ].closure, rstat, &time, cmdtab[ i ].command.value, cmdtab[ i ].buffer_out.value ); /* Clean up the command data, process, etc. */ string_free( &cmdtab[ i ].action ); string_new( &cmdtab[ i ].action ); string_free( &cmdtab[ i ].target ); string_new( &cmdtab[ i ].target ); string_free( &cmdtab[ i ].command ); string_new( &cmdtab[ i ].command ); if ( cmdtab[ i ].pi.hProcess ) { CloseHandle( cmdtab[ i ].pi.hProcess ); cmdtab[ i ].pi.hProcess = 0; } if ( cmdtab[ i ].pi.hThread ) { CloseHandle( cmdtab[ i ].pi.hThread ); cmdtab[ i ].pi.hThread = 0; } if ( cmdtab[ i ].pipe_out[ 0 ] ) { CloseHandle( cmdtab[ i ].pipe_out[ 0 ] ); cmdtab[ i ].pipe_out[ 0 ] = 0; } if ( cmdtab[ i ].pipe_out[ 1 ] ) { CloseHandle( cmdtab[ i ].pipe_out[ 1 ] ); cmdtab[ i ].pipe_out[ 1 ] = 0; } if ( cmdtab[ i ].pipe_err[ 0 ] ) { CloseHandle( cmdtab[ i ].pipe_err[ 0 ] ); cmdtab[ i ].pipe_err[ 0 ] = 0; } if ( cmdtab[ i ].pipe_err[ 1 ] ) { CloseHandle( cmdtab[ i ].pipe_err[ 1 ] ); cmdtab[ i ].pipe_err[ 1 ] = 0; } string_free( &cmdtab[ i ].buffer_out ); string_new( &cmdtab[ i ].buffer_out ); string_free( &cmdtab[ i ].buffer_err ); string_new( &cmdtab[ i ].buffer_err ); cmdtab[ i ].exit_code = 0; cmdtab[ i ].exit_reason = EXIT_OK; } return 1; }
typename enable_if< detail::is_time_tag_of< TimePoint, detail::time_point_tag >, bool >::type wait_until(TimePoint const& abs_time) { if (try_wait()) return true; return m_sem.timed_wait(sync::detail::time_traits< TimePoint >::to_sync_unit(abs_time)); }
typename enable_if< detail::is_time_tag_of< Duration, detail::time_duration_tag >, bool >::type wait_for(Duration const& duration) { if (try_wait()) return true; return m_sem.timed_wait(sync::detail::time_traits< Duration >::to_sync_unit(duration)); }
typename enable_if_c< sync::detail::time_traits< Time >::is_specialized, bool >::type timed_wait(Time const& t) { if (try_wait()) return true; return m_sem.timed_wait(sync::detail::time_traits< Time >::to_sync_unit(t)); }
// Register a callback with the event. If the event is already // signaled, invoke the callback (and leave the event in the // signaled state), otherwise put the event to the waited state. // // Pre: state != waited void wait(waiter * w) { if (!try_wait(w)) w->signal(event_ptr(this)); }