#include "bees.h"
#include "crucible/error.h"
+#include "crucible/time.h"
+
+#include <algorithm>
using namespace crucible;
using namespace std;
// The Task infrastructure retries via the failing Exclusion's
// rendezvous queue.
auto memo = m_planner_state->locked_set();
+
+ // Canonical acquisition order: all restarting tasks whose
+ // memos overlap contend on their lowest common Exclusion
+ // first, forming one queue on one lock instead of an
+ // A-wants-B / B-wants-A swap cycle that re-wakes both
+ // tasks' waiter herds on every iteration.
+ sort(memo.begin(), memo.end());
+
+ const auto task = Task::current_task();
unique_lock<mutex> lock(s_mutex);
+
+ // Pass 1: side-effect-free peek. A woken task whose memo
+ // is still contended must not acquire (and then release)
+ // any locks on its way to discovering that — the transient
+ // take-and-release perturbs the locks other woken tasks
+ // are about to succeed on, cascading their failures. Only
+ // the failing Exclusion is touched, to establish the
+ // rendezvous for the restart.
for (auto &excl : memo) {
if (!excl) continue;
- auto el = excl->try_lock(Task::current_task());
- if (!el) {
- // Rendezvous established on the failing Exclusion.
- // m_locks destructs on unwind. m_dead not set —
- // this Borrower never became visible and will be
- // discarded as the stack unwinds past its ctor.
+ if (!excl->locked_by_another(task)) continue;
+ auto el = excl->try_lock(task);
+ if (el) {
+ // Freed between peek and try_lock; el releases
+ // here (we just took it, nobody queued behind
+ // it) and pass 2 re-acquires it.
+ continue;
+ }
+ // Rendezvous established on the failing Exclusion.
+ // m_dead not set — this Borrower never became
+ // visible and will be discarded as the stack
+ // unwinds past its ctor. Log rate-limited: at
+ // endgame this fires millions of times against a
+ // handful of hot ref clusters, and the log volume
+ // is itself a cost. The counter is unconditional.
+ BEESCOUNT(borrower_dead_reacquire);
+ static RateLimiter s_reacquire_log_limit(10);
+ if (s_reacquire_log_limit.is_ready()) {
BEESLOGDEBUG("memo reacquire contention ("
<< memo.size() << " locks)");
+ }
+ throw Dead();
+ }
+
+ // Pass 2: acquire. A lock can still be taken by another
+ // thread between the peek and this loop; that follows the
+ // same contention path as before.
+ for (auto &excl : memo) {
+ if (!excl) continue;
+ auto el = excl->try_lock(task);
+ if (!el) {
BEESCOUNT(borrower_dead_reacquire);
+ static RateLimiter s_race_log_limit(10);
+ if (s_race_log_limit.is_ready()) {
+ BEESLOGDEBUG("memo reacquire race ("
+ << memo.size() << " locks)");
+ }
throw Dead();
}
m_locks.push_back({excl, std::move(el)});