void output_(FILE* f) { IO_TYPE ioStream(f,"w"); uint64_t count = 0; uint64_t nextStart = 0; while (count< count_) { boost::mutex::scoped_lock lock(out_buf_mtx_); while (out_buf_size_ == 0) out_buf_con_.wait(lock); IASSERT(fwrite(&out_buf_size_, sizeof(uint32_t), 1, f)==1); IASSERT(fwrite(&out_buf_num_, sizeof(uint32_t), 1, f)==1); //IASSERT(fwrite(&max_record_len_of_this_run_, sizeof(uint32_t), 1, f)==1); //TODO uint64_t nextStartPos = ftell(f); IASSERT(fwrite(&nextStart, sizeof(uint64_t), 1, f)==1); //IASSERT(fwrite(out_buf_, out_buf_size_, 1, f)==1); ioStream.write(out_buf_, out_buf_size_); nextStart = ftell(f); fseek(f, nextStartPos, SEEK_SET); IASSERT(fwrite(&nextStart, sizeof(uint64_t), 1, f)==1); fseek(f, nextStart, SEEK_SET); IASSERT(t_check_sort_()); count += out_buf_num_; out_buf_size_ = out_buf_num_ = 0; out_buf_con_.notify_one(); } std::cout<<"Outputting is over...\n"; }
void player(int active) { boost::unique_lock<boost::mutex> lock(mutex); int other = active == PLAYER_A ? PLAYER_B : PLAYER_A; while (state < GAME_OVER) { //std::cout << player_name(active) << ": Play." << std::endl; state = other; cond.notify_all(); do { cond.wait(lock); if (state == other) { std::cout << "---" << player_name(active) << ": Spurious wakeup!" << std::endl; } } while (state == other); } ++state; std::cout << player_name(active) << ": Gone." << std::endl; cond.notify_all(); }
virtual bool run(OperationContext* txn, const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) { if (txn->lockState()->isLocked()) { errmsg = "fsync: Cannot execute fsync command from contexts that hold a data lock"; return false; } bool sync = !cmdObj["async"].trueValue(); // async means do an fsync, but return immediately bool lock = cmdObj["lock"].trueValue(); log() << "CMD fsync: sync:" << sync << " lock:" << lock << endl; if( lock ) { if ( ! sync ) { errmsg = "fsync: sync option must be true when using lock"; return false; } SimpleMutex::scoped_lock lk(m); err = ""; (new FSyncLockThread())->go(); while ( ! locked && err.size() == 0 ) { _threadSync.wait( m ); } if ( err.size() ){ errmsg = err; return false; } log() << "db is now locked, no writes allowed. db.fsyncUnlock() to unlock" << endl; log() << " For more info see " << FSyncCommand::url() << endl; result.append("info", "now locked against writes, use db.fsyncUnlock() to unlock"); result.append("seeAlso", FSyncCommand::url()); } else { // the simple fsync command case if (sync) { // can this be GlobalRead? and if it can, it should be nongreedy. ScopedTransaction transaction(txn, MODE_X); Lock::GlobalWrite w(txn->lockState()); getDur().commitNow(txn); // No WriteUnitOfWork needed, as this does no writes of its own. } // Take a global IS lock to ensure the storage engine is not shutdown Lock::GlobalLock global(txn->lockState(), MODE_IS, UINT_MAX); StorageEngine* storageEngine = getGlobalServiceContext()->getGlobalStorageEngine(); result.append( "numFiles" , storageEngine->flushAllFiles( sync ) ); } return 1; }
void interpreter_lock_impl::unlock_thread() { boost::mutex::scoped_lock lock(mutex_); while (thread_lock_count_ > 0) { condition_.wait(lock); } assert(NULL != thread_state_); PyEval_RestoreThread(thread_state_); thread_lock_count_ = -1; thread_state_ = NULL; condition_.notify_all(); }
// this function is currently not exception-safe: // if the wait calls throw, m_pendingWriter can be left in an inconsistent // state void acquire_write_lock() { boost::mutex::scoped_lock lock(mtx); // ensure subsequent readers block ++num_pending_writers; // ensure all reader locks are released while(num_readers > 0) { no_readers.wait(lock); } // only continue when the current writer has finished // and another writer has not been woken first while(is_current_writer) { writer_finished.wait(lock); } --num_pending_writers; is_current_writer = true; }
void acquire_read_lock() { boost::mutex::scoped_lock lock(mtx); // require a while loop here, since when the writerFinished condition is // notified, we should not allow readers to lock if there is a writer // waiting. if there is a writer waiting, we continue waiting while(num_pending_writers != 0 || is_current_writer) { writer_finished.wait(lock); } ++num_readers; }
void interpreter_lock_impl::lock() { boost::mutex::scoped_lock lock(mutex_); while (NULL != thread_state_) { if (!thread_lock_count_) { break; } condition_.wait(lock); } if (NULL == thread_state_) { throw std::runtime_error("Can not acquire python interpreter lock"); } PyEval_RestoreThread(thread_state_); thread_lock_count_++; }
virtual bool run(OperationContext* txn, const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) { if (txn->lockState()->isLocked()) { errmsg = "fsync: Cannot execute fsync command from contexts that hold a data lock"; return false; } bool sync = !cmdObj["async"].trueValue(); // async means do an fsync, but return immediately bool lock = cmdObj["lock"].trueValue(); log() << "CMD fsync: sync:" << sync << " lock:" << lock << endl; if( lock ) { if ( ! sync ) { errmsg = "fsync: sync option must be true when using lock"; return false; } SimpleMutex::scoped_lock lk(m); err = ""; (new FSyncLockThread())->go(); while ( ! locked && err.size() == 0 ) { _threadSync.wait( m ); } if ( err.size() ){ errmsg = err; return false; } log() << "db is now locked for snapshotting, no writes allowed. db.fsyncUnlock() to unlock" << endl; log() << " For more info see " << FSyncCommand::url() << endl; result.append("info", "now locked against writes, use db.fsyncUnlock() to unlock"); result.append("seeAlso", FSyncCommand::url()); } else { // the simple fsync command case if (sync) { // can this be GlobalRead? and if it can, it should be nongreedy. Lock::GlobalWrite w(txn->lockState()); getDur().commitNow(); } // question : is it ok this is not in the dblock? i think so but this is a change from past behavior, // please advise. result.append( "numFiles" , MemoryMappedFile::flushAll( sync ) ); } return 1; }
int WServer::waitForShutdown(const char *restartWatchFile) { #if !defined(WIN32) if (!CatchSignals) { for(;;) sleep(0x1<<16); } #endif // WIN32 #ifdef WT_THREADED #if !defined(_WIN32) sigset_t wait_mask; sigemptyset(&wait_mask); sigaddset(&wait_mask, SIGHUP); sigaddset(&wait_mask, SIGINT); sigaddset(&wait_mask, SIGQUIT); sigaddset(&wait_mask, SIGTERM); pthread_sigmask(SIG_BLOCK, &wait_mask, 0); for (;;) { int sig; sigwait(&wait_mask, &sig); if (sig != -1) { if (sig == SIGHUP) { if (instance()) instance()->configuration().rereadConfiguration(); } else return sig; } } #else // WIN32 boost::mutex::scoped_lock terminationLock(terminationMutex); SetConsoleCtrlHandler(console_ctrl_handler, TRUE); while (!terminationRequested) terminationCondition.wait(terminationLock); SetConsoleCtrlHandler(console_ctrl_handler, FALSE); return 0; #endif // WIN32 #else return 0; #endif // WT_THREADED }
void yield(result_ref_t result) // in the same thread as 'work()'. { boost::mutex::scoped_lock lock(m_mutex); // 'result' is alive until next increment, // as far as 'value' can go across thread-boundary. m_presult = boost::addressof(result); m_status.reset(block_incrementing::value); m_cond.notify_one(); while (!m_status.test(block_incrementing::value) && !m_status.test(block_interrupted::value)) m_cond.wait(lock); if (m_status.test(block_interrupted::value)) throw yield_break_exception(); }
virtual bool run(const string& dbname, BSONObj& cmdObj, int, string& errmsg, BSONObjBuilder& result, bool fromRepl) { if (Lock::isLocked()) { errmsg = "fsync: Cannot execute fsync command from contexts that hold a data lock"; return false; } bool sync = !cmdObj["async"].trueValue(); // async means do an fsync, but return immediately bool lock = cmdObj["lock"].trueValue(); if( lock ) { if ( ! sync ) { errmsg = "fsync: sync option must be true when using lock"; return false; } SimpleMutex::scoped_lock lk(m); err = ""; (new FSyncLockThread())->go(); while ( ! locked && err.size() == 0 ) { _threadSync.wait( m ); } if ( err.size() ){ errmsg = err; return false; } log() << "db is now locked for snapshotting, no writes allowed. db.fsyncUnlock() to unlock" << endl; log() << " For more info see " << FSyncCommand::url() << endl; result.append("info", "now locked against writes, use db.fsyncUnlock() to unlock"); result.append("seeAlso", FSyncCommand::url()); } else { // the simple fsync command case if (sync) { Lock::GlobalWrite w; // can this be GlobalRead? and if it can, it should be nongreedy. DEV LOG(0) << "in fsync: flushAll/commitNow not implemented, doing nothing!" << endl; } // question : is it ok this is not in the dblock? i think so but this is a change from past behavior, // please advise. DEV LOG(0) << "in fsync: number of files flushed not known, arbitrarily reporting 1!" << endl; result.append( "numFiles" , 1 ); } return 1; }
int main(int argc, char* argv[]) { boost::mutex::scoped_lock lock(g_close_mutex); io_service ios; boost::shared_ptr<player> player_sptr = player::create(ios); std::string ipport = "127.0.0.1:9906"; player_sptr->start(endpoint_from_string<endpoint>(ipport)); ios.run(); g_close_condition.wait(lock); return 0; }
// // blocking pop: return a TPtr if available // T pop() { boost::mutex::scoped_lock data_available_lock(data_available_mtx); while(!data_available) data_available_cond.wait(data_available_lock); T p; { boost::mutex::scoped_lock lock(q_mutex); assert(q_.size() > 0); p = q_.back(); q_.pop(); data_available = q_.size() > 0; } return p; };
bool dequeue( msg_type & msg) { typename boost::mutex::scoped_lock lock( mtx_); if ( active_ == false && empty_() ) return false; while (empty_() ){ not_empty_cond_.wait( lock, boost::bind( & Queue< T, Q >::consumers_activate_, this) ); } dequeue_( msg); if ( active_ == true && queue_.size() <= low_water_mark_) { not_full_cond_.notify_one(); } return msg ? true : false; }
bool enqueue( msg_type const& msg) { typename boost::mutex::scoped_lock lock( mtx_); if ( active_ == false) return false; not_full_cond_.wait( lock, boost::bind( & Queue< T, Q >::suppliers_activate_, this) ); if ( active_ != false) { enqueue_( msg); not_empty_cond_.notify_one(); return true; } else return false; }
/** start */ void start_and_wait(std::string name) { is_running_ = true; if (signal(SIGTERM, unix_service<TBase>::handleSigTerm) == SIG_ERR) handle_error(__LINE__, __FILE__, "Failed to hook SIGTERM!"); if (signal(SIGINT, unix_service<TBase>::handleSigInt) == SIG_ERR) handle_error(__LINE__, __FILE__, "Failed to hook SIGTERM!"); TBase::handle_startup("TODO"); print_debug("Service started waiting for termination event..."); { boost::unique_lock<boost::mutex> lock(stop_mutex_); while(is_running_) shutdown_condition_.wait(lock); } print_debug("Shutting down..."); TBase::handle_shutdown("TODO"); print_debug("Shutting down (down)..."); }
int main() { state = START; boost::thread thrda(&player, PLAYER_A); boost::thread thrdb(&player, PLAYER_B); boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC_); xt.sec += 1; boost::thread::sleep(xt); { boost::unique_lock<boost::mutex> lock(mutex); std::cout << "---Noise ON..." << std::endl; } for (int i = 0; i < 10; ++i) cond.notify_all(); { boost::unique_lock<boost::mutex> lock(mutex); std::cout << "---Noise OFF..." << std::endl; state = GAME_OVER; cond.notify_all(); do { cond.wait(lock); } while (state != BOTH_PLAYERS_GONE); } std::cout << "GAME OVER" << std::endl; thrda.join(); thrdb.join(); return 0; }
int main(int argc, char* argv[]) { state = START; boost::thread thrda(thread_adapter(&player, (void*)PLAYER_A)); boost::thread thrdb(thread_adapter(&player, (void*)PLAYER_B)); boost::xtime xt; boost::xtime_get(&xt, boost::TIME_UTC); xt.sec += 1; boost::thread::sleep(xt); { boost::mutex::scoped_lock lock(mutex); std::cout << "---Noise ON..." << std::endl; } for (int i = 0; i < 1000000; ++i) cond.notify_all(); { boost::mutex::scoped_lock lock(mutex); std::cout << "---Noise OFF..." << std::endl; state = GAME_OVER; cond.notify_all(); do { cond.wait(lock); } while (state != BOTH_PLAYERS_GONE); } std::cout << "GAME OVER" << std::endl; thrda.join(); thrdb.join(); return 0; }
void prefetch_(FILE* f) { IO_TYPE ioStream(f); const uint64_t FILE_LEN = ioStream.length(); run_num_ = 0; uint64_t pos = sizeof(uint64_t); std::cout<<std::endl; ioStream.seek(pos); while(pos < FILE_LEN) { std::cout<<"\rA runner is processing "<<pos*1./FILE_LEN<<std::flush; ++run_num_; boost::mutex::scoped_lock lock(pre_buf_mtx_); while (pre_buf_size_!=0) pre_buf_con_.wait(lock); //uint32_t s = (uint32_t)(FILE_LEN-pos>RUN_BUF_SIZE_? RUN_BUF_SIZE_: FILE_LEN-pos); uint32_t s; if(!ioStream.isCompression()) s = (uint32_t)(FILE_LEN-pos>RUN_BUF_SIZE_? RUN_BUF_SIZE_: FILE_LEN-pos); else s = RUN_BUF_SIZE_; //std::cout<<std::endl<<pos<<"-"<<FILE_LEN<<"-"<<RUN_BUF_SIZE_<<"-"<<s<<std::endl; if(!ioStream.isCompression()) ioStream.seek(pos); //IASSERT(fread(pre_buf_, s, 1, f)==1); s = ioStream.read(pre_buf_, s); if(!ioStream.isCompression()) pos += (uint64_t)s; else pos = ioStream.tell(); //check the position of the last record pre_buf_size_ = 0; pre_buf_num_ = 0; for(; pre_buf_size_<s; ++pre_buf_num_) { if (pre_buf_size_+*(LEN_TYPE*)(pre_buf_+pre_buf_size_)+sizeof(LEN_TYPE)>s) break; pre_buf_size_ += *(LEN_TYPE*)(pre_buf_+pre_buf_size_)+sizeof(LEN_TYPE); } pos -= (uint64_t)(s- pre_buf_size_); //std::cout<<"pre_buf_size_ "<<pre_buf_size_<<" pre_buf_num_ "<<pre_buf_num_<<" ret "<<s<<" pos "<<pos<<std::endl; if (pre_buf_num_ == 0) { std::cout<<"\n[Warning]: A record is too long, and has been ignored!\n"; //pos += *(LEN_TYPE*)(pre_buf_+pre_buf_size_) + sizeof(LEN_TYPE); --count_; RUN_BUF_SIZE_ = (uint32_t)((*(LEN_TYPE*)(pre_buf_+pre_buf_size_) + sizeof(LEN_TYPE))*1.1); pre_buf_ = (char*)realloc(pre_buf_, RUN_BUF_SIZE_); continue; } //IASSERT(pre_buf_size_ <= RUN_BUF_SIZE_); pre_buf_con_.notify_one(); } std::cout<<"Prefetching is over...\n"; }
void sort_() { uint64_t count = 0; while (count< count_) { uint32_t pre_buf_size = 0; uint32_t pre_buf_num = 0; { boost::mutex::scoped_lock lock(pre_buf_mtx_); while (pre_buf_size_==0) pre_buf_con_.wait(lock); assert(pre_buf_size_ <= RUN_BUF_SIZE_); memcpy(run_buf_, pre_buf_, pre_buf_size_); pre_buf_size = pre_buf_size_; pre_buf_num = pre_buf_num_; count += pre_buf_num_; pre_buf_num_ = pre_buf_size_ = 0; pre_buf_con_.notify_one(); } key_buf_ = (struct KEY_PTR*)realloc(key_buf_, pre_buf_num*sizeof(struct KEY_PTR)); uint32_t pos = 0; for (uint32_t i = 0; i<pre_buf_num; ++i) { key_buf_[i] = KEY_PTR(pos); assert(pos <= RUN_BUF_SIZE_); pos += *(LEN_TYPE*)(run_buf_ + pos)+sizeof(LEN_TYPE); IASSERT(pos<=pre_buf_size); } IASSERT(pos==pre_buf_size); quick_sort_(0, pre_buf_num-1, pre_buf_num); boost::mutex::scoped_lock lock(out_buf_mtx_); while (out_buf_size_ != 0) out_buf_con_.wait(lock); out_buf_size_ = 0; out_buf_num_ = 0; LEN_TYPE max_len_of_this_run = (LEN_TYPE)0; for (uint32_t i=0; i<pre_buf_num; ++i, ++out_buf_num_) { assert(key_buf_[i].pos <= RUN_BUF_SIZE_); assert(out_buf_size_+key_buf_[i].LEN(run_buf_)+ sizeof(LEN_TYPE) <= RUN_BUF_SIZE_); memcpy(out_buf_+out_buf_size_, run_buf_+ key_buf_[i].pos, key_buf_[i].LEN(run_buf_)+ sizeof(LEN_TYPE)); LEN_TYPE len = key_buf_[i].LEN(run_buf_) + sizeof(LEN_TYPE); out_buf_size_ += len; if(len > max_len_of_this_run) max_len_of_this_run = len; } max_record_len_of_this_run_ = max_len_of_this_run; min_run_buff_size_for_merger_ += max_record_len_of_this_run_; if(max_len_of_this_run > max_record_len_) max_record_len_ = (uint32_t)max_len_of_this_run; IASSERT(out_buf_num_ == pre_buf_num); IASSERT(out_buf_size_ == pre_buf_size); out_buf_con_.notify_one(); } std::cout<<"Sorting is over...\n"; }
int main(int argc, char **argv) { // Signal handlers signal(SIGTERM, Handler); signal(SIGINT, Handler); signal(SIGQUIT, Handler); signal(SIGABRT, Handler); signal(SIGPIPE, Handler); boost::mutex guard_mutex; // Setup readline history_filename = GetUserHomeDirectory() + "/.atomic_history"; read_history(history_filename.data()); rl_attempted_completion_function = AutoComplete; // Save command line state tcgetattr(fileno(stdin), &original_flags); // Parse commandline boost::program_options::options_description command_line; boost::program_options::variables_map variable_map; command_line.add_options() ("help,h", "produce help message") ("command,c", boost::program_options::value<std::string>()->default_value(""), "command") ("server,s", boost::program_options::value<std::string>()->default_value("localhost"), "server address") ("port,p", boost::program_options::value<unsigned int>()->default_value(1202), "server port"); try { boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(command_line).run(), variable_map); } catch (boost::program_options::unknown_option e) { std::cerr << e.what() << std::endl; std::cout << command_line << std::endl; CleanUp(); return EXIT_FAILURE; } catch (boost::program_options::invalid_syntax e) { std::cerr << e.what() << std::endl; std::cout << command_line << std::endl; CleanUp(); return EXIT_FAILURE; } if (variable_map.count("help") != 0) { std::cout << command_line << std::endl; CleanUp(); return EXIT_SUCCESS; } net::Manager::Create(); if (variable_map["command"].as<std::string>() == "") { std::cout << "\033[29;1mAtom Interactive Console, version " + std::string(VERSION) + " starting...\033[0m" << std::endl; std::cout << "\033[29;1mReleased under " + std::string(LICENSE) + ".\033[0m" << std::endl; std::cout << "Written by Mattias Runge 2010." << std::endl; std::cout << "Connecting to " << variable_map["server"].as<std::string>().data() << ":" << variable_map["port"].as<unsigned int>() << "..." << std::endl; } try { cc = ConsoleClient::Pointer(new ConsoleClient(variable_map["server"].as<std::string>(), variable_map["port"].as<unsigned int>())); } catch (std::runtime_error& e) { std::cerr << "Connection error: " << e.what() << std::endl; CleanUp(); return EXIT_FAILURE; } if (variable_map["command"].as<std::string>() != "") { boost::mutex::scoped_lock guard(guard_mutex); on_message_condition.wait(guard); if (!finish) { cc->SendResponse(variable_map["command"].as<std::string>()); if (!finish) { on_message_condition.wait(guard); } } } else { while (true) { boost::mutex::scoped_lock guard(guard_mutex); on_message_condition.wait(guard); if (finish) { break; } while ((buffer = readline(cc->GetPrompt().data())) != NULL) { if (finish) { break; } if (strlen(buffer) == 0) { continue; } break; } if (finish) { break; } cc->SendResponse(buffer); add_history(buffer); } } CleanUp(); return EXIT_SUCCESS; }
int WServer::waitForShutdown(const char *restartWatchFile) { #if !defined(WT_WIN32) if (!CatchSignals) { for(;;) sleep(0x1<<16); } #endif // WIN32 #ifdef WT_THREADED #if !defined(WT_WIN32) sigset_t wait_mask; sigemptyset(&wait_mask); // Block the signals which interest us sigaddset(&wait_mask, SIGHUP); sigaddset(&wait_mask, SIGINT); sigaddset(&wait_mask, SIGQUIT); sigaddset(&wait_mask, SIGTERM); pthread_sigmask(SIG_BLOCK, &wait_mask, 0); for (;;) { int rc, sig= -1; // Wait for a signal to be raised rc= sigwait(&wait_mask, &sig); // branch based on return value of sigwait(). switch (rc) { case 0: // rc indicates one of the blocked signals was raised. // branch based on the signal which was raised. switch(sig) { case SIGHUP: // SIGHUP means re-read the configuration. if (instance()) instance()->configuration().rereadConfiguration(); break; default: // Any other blocked signal means time to quit. return sig; } break; case EINTR: // rc indicates an unblocked signal was raised, so we'll go // around again. break; default: // report the error and return an obviously illegitimate signal value. throw WServer::Exception(std::string("sigwait() error: ") + strerror(rc)); return -1; } } #else // WIN32 boost::mutex::scoped_lock terminationLock(terminationMutex); SetConsoleCtrlHandler(console_ctrl_handler, TRUE); while (!terminationRequested) terminationCondition.wait(terminationLock); SetConsoleCtrlHandler(console_ctrl_handler, FALSE); return 0; #endif // WIN32 #else return 0; #endif // WT_THREADED }
inline void boost_threaded_monitor::wait() { cond_.wait(lock_); }