MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / registerwait

Method registerwait

src/fastlock.cpp:213–276  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

211
212public:
213 void registerwait(fastlock *lock, pid_t thispid)
214 {
215 static volatile bool fInDeadlock = false;
216
217 if (lock == &m_lock || g_fInCrash)
218 return;
219 fastlock_lock(&m_lock);
220
221 if (fInDeadlock)
222 {
223 printTrace();
224 fastlock_unlock(&m_lock);
225 return;
226 }
227
228 m_mapwait.insert(std::make_pair(thispid, lock));
229
230 // Detect cycles
231 pid_t pidCheck = thispid;
232 size_t cchecks = 0;
233 for (;;)
234 {
235 auto itr = m_mapwait.find(pidCheck);
236 if (itr == m_mapwait.end())
237 break;
238
239 __atomic_load(&itr->second->m_pidOwner, &pidCheck, __ATOMIC_RELAXED);
240 if (pidCheck == thispid)
241 {
242 // Deadlock detected, printout some debugging info and crash
243 serverLog(3 /*LL_WARNING*/, "\n\n");
244 serverLog(3 /*LL_WARNING*/, "!!! ERROR: Deadlock detected !!!");
245 pidCheck = thispid;
246 for (;;)
247 {
248 auto itr = m_mapwait.find(pidCheck);
249 serverLog(3 /* LL_WARNING */, "\t%d: (%p) %s", pidCheck, itr->second, itr->second->szName);
250 __atomic_load(&itr->second->m_pidOwner, &pidCheck, __ATOMIC_RELAXED);
251 if (pidCheck == thispid)
252 break;
253 }
254 // Wake All sleeping threads so they can print their callstacks
255#ifdef HAVE_BACKTRACE
256#ifdef __linux__
257 int mask = -1;
258 fInDeadlock = true;
259 fastlock_unlock(&m_lock);
260 futex(&lock->m_ticket.u, FUTEX_WAKE_BITSET_PRIVATE, INT_MAX, nullptr, mask);
261 futex(&itr->second->m_ticket.u, FUTEX_WAKE_BITSET_PRIVATE, INT_MAX, nullptr, mask);
262 sleep(2);
263 fastlock_lock(&m_lock);
264 printTrace();
265#endif
266#endif
267 serverLog(3 /*LL_WARNING*/, "!!! KeyDB Will Now Crash !!!");
268 _serverPanic(__FILE__, __LINE__, "Deadlock detected");
269 }
270

Callers 1

fastlock_sleepFunction · 0.80

Calls 10

fastlock_lockFunction · 0.85
printTraceFunction · 0.85
fastlock_unlockFunction · 0.85
serverLogFunction · 0.85
futexFunction · 0.85
_serverPanicFunction · 0.70
insertMethod · 0.45
findMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected