2019-02-21 00:16:47 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, John Sully <john at eqalpha dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-02-10 20:24:11 -05:00
|
|
|
#include "fastlock.h"
|
2019-02-10 22:00:19 -05:00
|
|
|
#include <unistd.h>
|
2019-02-16 14:25:14 -05:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sched.h>
|
2019-02-20 01:20:26 -05:00
|
|
|
#include <atomic>
|
2019-02-22 21:00:14 -05:00
|
|
|
#include <assert.h>
|
2019-02-10 20:24:11 -05:00
|
|
|
|
2019-02-21 00:16:47 -05:00
|
|
|
/****************************************************
|
|
|
|
*
|
|
|
|
* Implementation of a fair spinlock. To promote fairness we
|
|
|
|
* use a ticket lock instead of a raw spinlock
|
|
|
|
*
|
|
|
|
****************************************************/
|
|
|
|
|
2019-02-16 14:25:14 -05:00
|
|
|
static_assert(sizeof(pid_t) <= sizeof(fastlock::m_pidOwner), "fastlock::m_pidOwner not large enough");
|
|
|
|
|
2019-02-25 18:21:27 -05:00
|
|
|
extern "C" pid_t gettid()
|
2019-02-16 14:25:14 -05:00
|
|
|
{
|
|
|
|
static thread_local int pidCache = -1;
|
|
|
|
if (pidCache == -1)
|
|
|
|
pidCache = syscall(SYS_gettid);
|
|
|
|
return pidCache;
|
|
|
|
}
|
2019-02-15 14:11:05 -05:00
|
|
|
|
2019-02-10 20:24:11 -05:00
|
|
|
extern "C" void fastlock_init(struct fastlock *lock)
|
|
|
|
{
|
2019-02-22 01:23:31 -05:00
|
|
|
lock->m_ticket.m_active = 0;
|
|
|
|
lock->m_ticket.m_avail = 0;
|
2019-02-15 14:11:05 -05:00
|
|
|
lock->m_depth = 0;
|
2019-02-22 21:00:14 -05:00
|
|
|
lock->m_pidOwner = -1;
|
2019-02-10 20:24:11 -05:00
|
|
|
}
|
|
|
|
|
2019-02-25 18:21:27 -05:00
|
|
|
#ifndef ASM_SPINLOCK
|
2019-02-10 20:24:11 -05:00
|
|
|
extern "C" void fastlock_lock(struct fastlock *lock)
|
|
|
|
{
|
2019-02-21 00:16:47 -05:00
|
|
|
if ((int)__atomic_load_4(&lock->m_pidOwner, __ATOMIC_ACQUIRE) == gettid())
|
2019-02-16 14:25:14 -05:00
|
|
|
{
|
2019-02-21 00:16:47 -05:00
|
|
|
++lock->m_depth;
|
|
|
|
return;
|
|
|
|
}
|
2019-02-16 14:25:14 -05:00
|
|
|
|
2019-02-22 15:49:22 -05:00
|
|
|
unsigned myticket = __atomic_fetch_add(&lock->m_ticket.m_avail, 1, __ATOMIC_RELEASE);
|
2019-02-21 00:16:47 -05:00
|
|
|
|
2019-02-22 15:49:22 -05:00
|
|
|
int cloops = 0;
|
|
|
|
while (__atomic_load_2(&lock->m_ticket.m_active, __ATOMIC_ACQUIRE) != myticket)
|
2019-02-21 00:16:47 -05:00
|
|
|
{
|
2019-02-22 15:49:22 -05:00
|
|
|
if ((++cloops % 1024*1024) == 0)
|
|
|
|
sched_yield();
|
2019-02-25 18:21:27 -05:00
|
|
|
#if defined(__i386__) || defined(__amd64__)
|
|
|
|
__asm__ ("pause");
|
|
|
|
#endif
|
2019-02-10 22:00:19 -05:00
|
|
|
}
|
2019-02-20 01:20:26 -05:00
|
|
|
|
2019-02-15 14:11:05 -05:00
|
|
|
lock->m_depth = 1;
|
2019-02-21 00:16:47 -05:00
|
|
|
__atomic_store_4(&lock->m_pidOwner, gettid(), __ATOMIC_RELEASE);
|
2019-02-22 15:49:22 -05:00
|
|
|
std::atomic_thread_fence(std::memory_order_acquire);
|
2019-02-10 20:24:11 -05:00
|
|
|
}
|
|
|
|
|
2019-02-22 01:23:31 -05:00
|
|
|
extern "C" int fastlock_trylock(struct fastlock *lock)
|
|
|
|
{
|
|
|
|
if ((int)__atomic_load_4(&lock->m_pidOwner, __ATOMIC_ACQUIRE) == gettid())
|
|
|
|
{
|
|
|
|
++lock->m_depth;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cheap test
|
|
|
|
if (lock->m_ticket.m_active != lock->m_ticket.m_avail)
|
|
|
|
return false;
|
|
|
|
|
2019-02-22 15:49:22 -05:00
|
|
|
uint16_t active = __atomic_load_2(&lock->m_ticket.m_active, __ATOMIC_RELAXED);
|
2019-02-22 01:23:31 -05:00
|
|
|
uint16_t next = active + 1;
|
|
|
|
|
|
|
|
struct ticket ticket_expect { active, active };
|
|
|
|
struct ticket ticket_setiflocked { active, next };
|
|
|
|
if (__atomic_compare_exchange(&lock->m_ticket, &ticket_expect, &ticket_setiflocked, true /*strong*/, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE))
|
|
|
|
{
|
|
|
|
lock->m_depth = 1;
|
|
|
|
__atomic_store_4(&lock->m_pidOwner, gettid(), __ATOMIC_RELEASE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-10 20:24:11 -05:00
|
|
|
extern "C" void fastlock_unlock(struct fastlock *lock)
|
|
|
|
{
|
2019-02-15 14:11:05 -05:00
|
|
|
--lock->m_depth;
|
|
|
|
if (lock->m_depth == 0)
|
|
|
|
{
|
2019-02-22 21:00:14 -05:00
|
|
|
assert((int)__atomic_load_4(&lock->m_pidOwner, __ATOMIC_RELAXED) >= 0); // unlock after free
|
2019-02-15 14:11:05 -05:00
|
|
|
lock->m_pidOwner = -1;
|
2019-02-22 15:49:22 -05:00
|
|
|
std::atomic_thread_fence(std::memory_order_acquire);
|
2019-03-02 16:47:27 -05:00
|
|
|
__atomic_fetch_add(&lock->m_ticket.m_active, 1, __ATOMIC_ACQ_REL); // on x86 the atomic is not required here, but ASM handles that case
|
2019-02-15 14:11:05 -05:00
|
|
|
}
|
2019-02-10 20:24:11 -05:00
|
|
|
}
|
2019-03-02 16:47:27 -05:00
|
|
|
#endif
|
2019-02-10 22:00:19 -05:00
|
|
|
|
2019-02-10 20:24:11 -05:00
|
|
|
extern "C" void fastlock_free(struct fastlock *lock)
|
|
|
|
{
|
|
|
|
// NOP
|
2019-02-22 21:00:14 -05:00
|
|
|
assert((lock->m_ticket.m_active == lock->m_ticket.m_avail) // Asser the lock is unlocked
|
|
|
|
|| (lock->m_pidOwner == gettid() && (lock->m_ticket.m_active == lock->m_ticket.m_avail-1))); // OR we own the lock and nobody else is waiting
|
|
|
|
lock->m_pidOwner = -2; // sentinal value indicating free
|
2019-02-15 14:11:05 -05:00
|
|
|
}
|
2019-02-18 22:25:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
bool fastlock::fOwnLock()
|
|
|
|
{
|
|
|
|
return gettid() == m_pidOwner;
|
|
|
|
}
|