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

Function fastlock_lock

src/fastlock.cpp:345–396  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

343
344#ifndef ASM_SPINLOCK
345extern "C" void fastlock_lock(struct fastlock *lock, spin_worker worker)
346{
347 int pidOwner;
348 __atomic_load(&lock->m_pidOwner, &pidOwner, __ATOMIC_ACQUIRE);
349 if (pidOwner == gettid())
350 {
351 ++lock->m_depth;
352 return;
353 }
354
355 int tid = gettid();
356 unsigned myticket = __atomic_fetch_add(&lock->m_ticket.m_avail, 1, __ATOMIC_RELEASE);
357 unsigned cloops = 0;
358 ticket ticketT;
359 int fHighPressure;
360 __atomic_load(&g_fHighCpuPressure, &fHighPressure, __ATOMIC_RELAXED);
361 unsigned loopLimit = fHighPressure ? 0x10000 : 0x100000;
362
363 if (worker != nullptr) {
364 for (;;) {
365 __atomic_load(&lock->m_ticket.u, &ticketT.u, __ATOMIC_ACQUIRE);
366 if ((ticketT.u & 0xffff) == myticket)
367 break;
368 if (!worker())
369 goto LNormalLoop;
370 }
371 } else {
372LNormalLoop:
373 for (;;)
374 {
375 __atomic_load(&lock->m_ticket.u, &ticketT.u, __ATOMIC_ACQUIRE);
376 if ((ticketT.u & 0xffff) == myticket)
377 break;
378
379#if defined(__i386__) || defined(__amd64__)
380 __asm__ __volatile__ ("pause");
381#elif defined(__aarch64__)
382 __asm__ __volatile__ ("yield");
383#endif
384
385 if ((++cloops % loopLimit) == 0)
386 {
387 fastlock_sleep(lock, tid, ticketT.u, myticket);
388 }
389 }
390 }
391
392 lock->m_depth = 1;
393 __atomic_store(&lock->m_pidOwner, &tid, __ATOMIC_RELEASE);
394 ANNOTATE_RWLOCK_ACQUIRED(lock, true);
395 std::atomic_thread_fence(std::memory_order_acquire);
396}
397
398extern "C" int fastlock_trylock(struct fastlock *lock, int fWeak)
399{

Callers 8

pubsubPublishMessageFunction · 0.85
lockMethod · 0.85
clientsCronFunction · 0.85
registerwaitMethod · 0.85
clearwaitMethod · 0.85
fastlock_lock_recursiveFunction · 0.85
createAOFClientFunction · 0.85

Calls 3

gettidFunction · 0.85
workerFunction · 0.85
fastlock_sleepFunction · 0.85

Tested by

no test coverage detected