int main() { { typedef std::shared_timed_mutex M; M m0; M m1; std::shared_lock<M> lk0(m0); std::shared_lock<M> lk1(m1); lk1 = std::move(lk0); assert(lk1.mutex() == std::addressof(m0)); assert(lk1.owns_lock() == true); assert(lk0.mutex() == nullptr); assert(lk0.owns_lock() == false); } { typedef nasty_mutex M; M m0; M m1; std::shared_lock<M> lk0(m0); std::shared_lock<M> lk1(m1); lk1 = std::move(lk0); assert(lk1.mutex() == std::addressof(m0)); assert(lk1.owns_lock() == true); assert(lk0.mutex() == nullptr); assert(lk0.owns_lock() == false); } }
int main() { #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES { typedef std::mutex M; M m0; M m1; std::unique_lock<M> lk0(m0); std::unique_lock<M> lk1(m1); lk1 = std::move(lk0); assert(lk1.mutex() == std::addressof(m0)); assert(lk1.owns_lock() == true); assert(lk0.mutex() == nullptr); assert(lk0.owns_lock() == false); } { typedef nasty_mutex M; M m0; M m1; std::unique_lock<M> lk0(m0); std::unique_lock<M> lk1(m1); lk1 = std::move(lk0); assert(lk1.mutex() == std::addressof(m0)); assert(lk1.owns_lock() == true); assert(lk0.mutex() == nullptr); assert(lk0.owns_lock() == false); } #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES }
int main() { { boost::shared_lock<boost::shared_mutex> lk0(m0); boost::shared_lock<boost::shared_mutex> lk1(m1); lk1 = boost::move(lk0); BOOST_TEST(lk1.mutex() == &m0); BOOST_TEST(lk1.owns_lock() == true); BOOST_TEST(lk0.mutex() == 0); BOOST_TEST(lk0.owns_lock() == false); } { boost::shared_lock<boost::shared_mutex> lk1; lk1 = BOOST_THREAD_MAKE_RV_REF(boost::shared_lock<boost::shared_mutex>(m0)); BOOST_TEST(lk1.mutex() == &m0); BOOST_TEST(lk1.owns_lock() == true); } { boost::unique_lock<boost::shared_mutex> lk0(m0); boost::shared_lock<boost::shared_mutex> lk1(m1); lk1 = BOOST_THREAD_MAKE_RV_REF(boost::shared_lock<boost::shared_mutex>(boost::move(lk0))); BOOST_TEST(lk1.mutex() == &m0); BOOST_TEST(lk1.owns_lock() == true); BOOST_TEST(lk0.mutex() == 0); BOOST_TEST(lk0.owns_lock() == false); } { boost::shared_lock<boost::shared_mutex> lk1; lk1 = BOOST_THREAD_MAKE_RV_REF(boost::shared_lock<boost::shared_mutex>(boost::unique_lock<boost::shared_mutex>(m0))); BOOST_TEST(lk1.mutex() == &m0); BOOST_TEST(lk1.owns_lock() == true); } { boost::upgrade_lock<boost::shared_mutex> lk0(m0); boost::shared_lock<boost::shared_mutex> lk1(m1); lk1 = BOOST_THREAD_MAKE_RV_REF(boost::shared_lock<boost::shared_mutex>(boost::move(lk0))); BOOST_TEST(lk1.mutex() == &m0); BOOST_TEST(lk1.owns_lock() == true); BOOST_TEST(lk0.mutex() == 0); BOOST_TEST(lk0.owns_lock() == false); } { boost::shared_lock<boost::shared_mutex> lk1; lk1 = BOOST_THREAD_MAKE_RV_REF(boost::shared_lock<boost::shared_mutex>(boost::upgrade_lock<boost::shared_mutex>(m0))); BOOST_TEST(lk1.mutex() == &m0); BOOST_TEST(lk1.owns_lock() == true); } return boost::report_errors(); }
void WSCoreBoundTaskQueue::executeTask() { //infinite thread loop while (1) { std::shared_ptr<Task> task; //block protected by _threadStatusMutex { std::lock_guard<std::mutex> lk1(_threadStatusMutex); if (_status == TO_STOP) break; } // lock queue to get task std::unique_lock<std::mutex> ul(_queueMutex); // get task and execute if (_runQueue.size() > 0) { // get first task task = _runQueue.front(); _runQueue.pop_front(); ul.unlock(); if (task) { //LOG4CXX_DEBUG(logger, "Started executing task" << std::hex << &task << std::dec << " on core " << _core); // run task (*task)(); //std::cout << "Executed task " << task->vname() << "; hex " << std::hex << &task << std::dec << " on core " << _core<< std::endl; LOG4CXX_DEBUG(logger, "Executed task " << std::hex << &task << std::dec << " on core " << _core); // notify done observers that task is done task->notifyDoneObservers(); } // no task in runQueue -> try to steal task from other queue, otherwise sleep and wait for new tasks } else { // try to steal work ul.unlock(); if (stealTasks() != NULL) continue; ul.lock(); //if queue still empty go to sleep and wait until new tasks have been arrived if (_runQueue.size() < 1) { { //check if queue was suspended { std::lock_guard<std::mutex> lk1(_threadStatusMutex); if (_status != RUN) continue; } //std::cout << "queue " << _core << " sleeping " << std::endl; _condition.wait(ul); } } } } }
void rollbackTransactionFromOplog(BSONObj entry, bool purgeEntry) { bool transactionAlreadyApplied = entry["a"].Bool(); Client::Transaction transaction(DB_SERIALIZABLE); if (transactionAlreadyApplied) { if (entry.hasElement("ref")) { rollbackRefOp(entry); } else if (entry.hasElement("ops")) { rollbackOps(entry["ops"].Array()); } else { verify(0); } } { LOCK_REASON(lockReason, "repl: purging entry from oplog"); Lock::DBRead lk1("local", lockReason); if (purgeEntry) { purgeEntryFromOplog(entry); } else { // set the applied bool to false, to let the oplog know that // this entry has not been applied to collections BSONElementManipulator(entry["a"]).setBool(false); writeEntryToOplog(entry, false); } } transaction.commit(DB_TXN_NOSYNC); }
// takes an entry that was written _logTransactionOps // and applies them to collections // // TODO: possibly improve performance of this. We create and destroy a // context for each operation. Find a way to amortize it out if necessary // void applyTransactionFromOplog(BSONObj entry) { bool transactionAlreadyApplied = entry["a"].Bool(); if (!transactionAlreadyApplied) { Client::Transaction transaction(DB_SERIALIZABLE); if (entry.hasElement("ref")) { applyRefOp(entry); } else if (entry.hasElement("ops")) { applyOps(entry["ops"].Array()); } else { verify(0); } // set the applied bool to true, to let the oplog know that // this entry has been applied to collections BSONElementManipulator(entry["a"]).setBool(true); { LOCK_REASON(lockReason, "repl: setting oplog entry's applied bit"); Lock::DBRead lk1("local", lockReason); writeEntryToOplog(entry, false); } // If this code fails, it is impossible to recover from // because we don't know if the transaction successfully committed // so we might as well crash // There is currently no known way this code can throw an exception try { // we are operating as a secondary. We don't have to fsync transaction.commit(DB_TXN_NOSYNC); } catch (std::exception &e) { log() << "exception during commit of applyTransactionFromOplog, aborting system: " << e.what() << endl; printStackTrace(); logflush(); ::abort(); } } }
int main() { boost::strict_lock<boost::mutex> lk0(m0); boost::strict_lock<boost::mutex> lk1(m1); lk1 = lk0; }
int main() { boost::shared_lock_guard<boost::shared_mutex> lk0(m0); boost::shared_lock_guard<boost::shared_mutex> lk1(m1); lk1 = lk0; }
int main() { boost::lock_guard<boost::mutex> lk0(m0); boost::lock_guard<boost::mutex> lk1(m1); lk1 = lk0; }
void CoreBoundTaskQueue::executeTask() { //infinite thread loop while (1) { //block protected by _threadStatusMutex { std::lock_guard<std::mutex> lk1(_threadStatusMutex); if (_status == TO_STOP) break; } // lock queue to get task std::unique_lock<std::mutex> ul(_queueMutex); // get task and execute if (_runQueue.size() > 0) { std::shared_ptr<Task> task = _runQueue.front(); // get first task _runQueue.pop(); ul.unlock(); if (task) { // set queue to _blocked as we run task; this is a simple mechanism to avoid that further tasks are pushed to this queue if a long running task is executed; check WSSimpleTaskScheduler for task stealing queue _blocked = true; //LOG4CXX_DEBUG(logger, "Started executing task" << std::hex << &task << std::dec << " on core " << _core); // run task //std::cout << "Executed task " << task->vname() << "; hex " << std::hex << &task << std::dec << " on core " << _core<< std::endl; (*task)(); LOG4CXX_DEBUG(logger, "Executed task " << task->vname() << "; hex " << std::hex << &task << std::dec << " on core " << _core); // notify done observers that task is done task->notifyDoneObservers(); _blocked = false; } } // no task in runQueue -> sleep and wait for new tasks else { //if queue still empty go to sleep and wait until new tasks have been arrived if (_runQueue.size() < 1) { // if thread is about to stop, break execution loop { std::lock_guard<std::mutex> lk1(_threadStatusMutex); if (_status != RUN) continue; } //std::cout << "queue " << _core << " sleeping " << std::endl; _condition.wait(ul); } } } }
int main(int, char**) { std::shared_lock<std::shared_timed_mutex> lk0(m0); std::shared_lock<std::shared_timed_mutex> lk1(m1); lk1 = lk0; return 0; }
int main() { boost::upgrade_lock<boost::shared_mutex> lk0(m0); boost::upgrade_lock<boost::shared_mutex> lk1(lk0); BOOST_TEST(lk1.mutex() == &m1); BOOST_TEST(lk1.owns_lock() == true); BOOST_TEST(lk0.mutex() == 0); BOOST_TEST(lk0.owns_lock() == false); }
int main() { std::unique_lock<std::mutex> lk0; assert(lk0.mutex() == nullptr); std::unique_lock<std::mutex> lk1(m); assert(lk1.mutex() == &m); lk1.unlock(); assert(lk1.mutex() == &m); }
TEST(unique_lock, swap) { lockable_mock m1, m2; ASSERT_FALSE(m1.locked()); ASSERT_FALSE(m2.locked()); { unique_lock lk1(m1); ASSERT_TRUE(lk1.locked()); ASSERT_TRUE(m1.locked()); { unique_lock lk2; ASSERT_FALSE(lk2.locked()); swap(lk1, lk2); // NOLINT EXPECT_FALSE(lk1.locked()); EXPECT_TRUE(lk2.locked()); EXPECT_TRUE(m1.locked()); } EXPECT_FALSE(lk1.locked()); EXPECT_FALSE(m1.locked()); } { unique_lock lk1(m1); ASSERT_TRUE(lk1.locked()); ASSERT_TRUE(m1.locked()); { unique_lock lk2(m2); ASSERT_TRUE(lk2.locked()); swap(lk1, lk2); // NOLINT EXPECT_TRUE(lk1.locked()); EXPECT_TRUE(lk2.locked()); EXPECT_TRUE(m1.locked()); EXPECT_TRUE(m2.locked()); } EXPECT_TRUE(lk1.locked()); EXPECT_FALSE(m1.locked()); EXPECT_TRUE(m2.locked()); } }
void logTransactionOpsRef(GTID gtid, uint64_t timestamp, uint64_t hash, OID& oid) { Lock::DBRead lk1("local"); BSONObjBuilder b; addGTIDToBSON("_id", gtid, b); b.appendDate("ts", timestamp); b.append("h", (long long)hash); b.append("a", true); b.append("ref", oid); BSONObj bb = b.done(); writeEntryToOplog(bb, true); }
int main() { boost::unique_lock<boost::mutex> lk0(m0); boost::unique_lock<boost::mutex> lk1(m1); lk1 = lk0; BOOST_TEST(lk1.mutex() == &m0); BOOST_TEST(lk1.owns_lock() == true); BOOST_TEST(lk0.mutex() == 0); BOOST_TEST(lk0.owns_lock() == false); }
int main(int, char**) { std::unique_lock<std::mutex> lk0; assert(static_cast<bool>(lk0) == false); std::unique_lock<std::mutex> lk1(m); assert(static_cast<bool>(lk1) == true); lk1.unlock(); assert(static_cast<bool>(lk1) == false); return 0; }
int tc_libcxx_thread_thread_lock_unique_obs_op_bool(void) { std::unique_lock<std::mutex> lk0; TC_ASSERT_EXPR(static_cast<bool>(lk0) == false); std::unique_lock<std::mutex> lk1(m); TC_ASSERT_EXPR(static_cast<bool>(lk1) == true); lk1.unlock(); TC_ASSERT_EXPR(static_cast<bool>(lk1) == false); TC_SUCCESS_RESULT(); return 0; }
int main() { boost::shared_lock < boost::shared_mutex > lk0; BOOST_TEST(bool(lk0) == false); boost::shared_lock < boost::shared_mutex > lk1(m); BOOST_TEST(bool(lk1) == true); lk1.unlock(); BOOST_TEST(bool(lk1) == false); return boost::report_errors(); }
int main() { boost::upgrade_lock<boost::shared_mutex> lk0; BOOST_TEST(lk0.mutex() == 0); boost::upgrade_lock<boost::shared_mutex> lk1(m); BOOST_TEST(lk1.mutex() == &m); lk1.unlock(); BOOST_TEST(lk1.mutex() == &m); return boost::report_errors(); }
int main() { #if _LIBCPP_STD_VER > 11 std::shared_lock<std::shared_timed_mutex> lk0; assert(lk0.owns_lock() == false); std::shared_lock<std::shared_timed_mutex> lk1(m); assert(lk1.owns_lock() == true); lk1.unlock(); assert(lk1.owns_lock() == false); static_assert(noexcept(lk0.owns_lock()), "owns_lock must be noexcept"); #endif // _LIBCPP_STD_VER > 11 }
int main() { boost::shared_lock<shared_mutex> lk1(m); boost::shared_lock<shared_mutex> lk2; lk1.swap(lk2); BOOST_TEST(lk1.mutex() == 0); BOOST_TEST(lk1.owns_lock() == false); BOOST_TEST(lk2.mutex() == &m); BOOST_TEST(lk2.owns_lock() == true); return boost::report_errors(); }
int main() { boost::unique_lock<boost::mutex> lk0; BOOST_TEST(lk0.owns_lock() == false); boost::unique_lock<boost::mutex> lk1(m); BOOST_TEST(lk1.owns_lock() == true); lk1.unlock(); BOOST_TEST(lk1.owns_lock() == false); return boost::report_errors(); }
int main(int, char**) { std::shared_lock<std::shared_timed_mutex> lk0; assert(lk0.mutex() == nullptr); std::shared_lock<std::shared_timed_mutex> lk1(m); assert(lk1.mutex() == &m); lk1.unlock(); assert(lk1.mutex() == &m); static_assert(noexcept(lk0.mutex()), "mutex() must be noexcept"); return 0; }
int main() { #if _LIBCPP_STD_VER > 11 std::shared_lock<std::shared_mutex> lk0(m0); std::shared_lock<std::shared_mutex> lk1(m1); lk1 = std::move(lk0); assert(lk1.mutex() == &m0); assert(lk1.owns_lock() == true); assert(lk0.mutex() == nullptr); assert(lk0.owns_lock() == false); #endif // _LIBCPP_STD_VER > 11 }
int main() { { boost::unique_lock<boost::mutex> lk0; BOOST_TEST(bool(lk0) == false); boost::unique_lock<boost::mutex> lk1(m); BOOST_TEST(bool(lk1) == true); lk1.unlock(); BOOST_TEST(bool(lk1) == false); } { boost::unique_lock<boost::mutex> lk0; if (lk0) BOOST_TEST(false); boost::unique_lock<boost::mutex> lk1(m); if (!lk1) BOOST_TEST(false); lk1.unlock(); if (lk1) BOOST_TEST(false); } return boost::report_errors(); }
int main() { boost::mutex m0; boost::mutex m1; boost::unique_lock<boost::mutex> lk0(m0); boost::unique_lock<boost::mutex> lk1(m1); { boost::reverse_lock<boost::unique_lock<boost::mutex> > lg0(lk0); boost::reverse_lock<boost::unique_lock<boost::mutex> > lg1(lk1); lg1 = lg0; } }
void swap() { boost::fibers::mutex mtx1, mtx2; boost::unique_lock< boost::fibers::mutex > lk1( mtx1), lk2( mtx2); BOOST_CHECK_EQUAL( lk1.mutex(), & mtx1); BOOST_CHECK_EQUAL( lk2.mutex(), & mtx2); lk1.swap( lk2); BOOST_CHECK_EQUAL( lk1.mutex(), & mtx2); BOOST_CHECK_EQUAL( lk2.mutex(), & mtx1); }
static void _logTransactionOps(GTID gtid, uint64_t timestamp, uint64_t hash, BSONArray& opInfo) { Lock::DBRead lk1("local"); BSONObjBuilder b; addGTIDToBSON("_id", gtid, b); b.appendDate("ts", timestamp); b.append("h", (long long)hash); b.append("a", true); b.append("ops", opInfo); BSONObj bb = b.done(); // write it to oplog LOG(3) << "writing " << bb.toString(false, true) << " to master " << endl; writeEntryToOplog(bb, true); }
void try_lock_concept() { dummy_mutex mtx1, mtx2; mtx2.lock(); boost::unique_lock< dummy_mutex > lk1( mtx1, boost::defer_lock), lk2( mtx2, boost::defer_lock); int res = boost::try_lock( lk1, lk2); BOOST_CHECK( res == 1); BOOST_CHECK( ! mtx1.is_locked); BOOST_CHECK( mtx2.is_locked); BOOST_CHECK( ! lk1.owns_lock() ); BOOST_CHECK( ! lk2.owns_lock() ); }