2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* ThreadPrimitives.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2022-03-22 04:36:23 +08:00
|
|
|
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_THREADPRIMITIVES_H
|
|
|
|
#define FLOW_THREADPRIMITIVES_H
|
|
|
|
#pragma once
|
|
|
|
|
2020-04-25 08:07:50 +08:00
|
|
|
#include <atomic>
|
2020-07-14 01:38:00 +08:00
|
|
|
#include <array>
|
2020-04-25 08:07:50 +08:00
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "flow/Error.h"
|
|
|
|
#include "flow/Trace.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-02-02 05:23:53 +08:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD__)
|
2017-05-26 04:48:44 +08:00
|
|
|
#include <semaphore.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <mach/mach_init.h>
|
|
|
|
#include <mach/task.h>
|
|
|
|
#include <mach/semaphore.h>
|
|
|
|
#include <mach/sync_policy.h>
|
|
|
|
#include <mach/mach_error.h>
|
|
|
|
#include <mach/clock_types.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if VALGRIND
|
|
|
|
#include <drd.h>
|
|
|
|
#endif
|
|
|
|
|
2020-07-14 01:38:00 +08:00
|
|
|
// TODO: We should make this dependent on the CPU. Maybe cmake
|
|
|
|
// can set this variable properly?
|
2021-01-04 21:13:06 +08:00
|
|
|
constexpr size_t MAX_CACHE_LINE_SIZE = 64;
|
2020-07-14 01:38:00 +08:00
|
|
|
|
2021-01-04 21:13:06 +08:00
|
|
|
class alignas(MAX_CACHE_LINE_SIZE) ThreadSpinLock {
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
// #ifdef _WIN32
|
2020-04-25 08:07:50 +08:00
|
|
|
ThreadSpinLock() {
|
2017-05-26 04:48:44 +08:00
|
|
|
#if VALGRIND
|
|
|
|
ANNOTATE_RWLOCK_CREATE(this);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
~ThreadSpinLock() {
|
|
|
|
#if VALGRIND
|
|
|
|
ANNOTATE_RWLOCK_DESTROY(this);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void enter() {
|
2020-04-15 10:22:37 +08:00
|
|
|
while (isLocked.test_and_set(std::memory_order_acquire))
|
2022-02-11 14:17:15 +08:00
|
|
|
#if defined(__aarch64__)
|
2022-02-11 18:24:56 +08:00
|
|
|
__asm__ volatile("isb");
|
2022-02-11 14:17:15 +08:00
|
|
|
#elif defined(__powerpc64__)
|
2022-02-11 20:11:50 +08:00
|
|
|
__asm__ volatile("or 27,27,27" ::: "memory");
|
2020-04-15 10:22:37 +08:00
|
|
|
#else
|
2022-02-11 18:24:56 +08:00
|
|
|
_mm_pause();
|
2020-04-15 10:22:37 +08:00
|
|
|
#endif
|
2017-05-26 04:48:44 +08:00
|
|
|
#if VALGRIND
|
|
|
|
ANNOTATE_RWLOCK_ACQUIRED(this, true);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void leave() {
|
2020-04-25 08:07:50 +08:00
|
|
|
isLocked.clear(std::memory_order_release);
|
2017-05-26 04:48:44 +08:00
|
|
|
#if VALGRIND
|
|
|
|
ANNOTATE_RWLOCK_RELEASED(this, true);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void assertNotEntered() {
|
2020-04-28 01:57:04 +08:00
|
|
|
ASSERT(!isLocked.test_and_set(std::memory_order_acquire));
|
|
|
|
isLocked.clear(std::memory_order_release);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2020-04-25 08:07:50 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
|
|
|
ThreadSpinLock(const ThreadSpinLock&);
|
|
|
|
void operator=(const ThreadSpinLock&);
|
2020-04-25 08:07:50 +08:00
|
|
|
std::atomic_flag isLocked = ATOMIC_FLAG_INIT;
|
2020-07-14 01:38:00 +08:00
|
|
|
// We want a spin lock to occupy a cache line in order to
|
|
|
|
// prevent false sharing.
|
2021-01-04 21:13:06 +08:00
|
|
|
std::array<uint8_t, MAX_CACHE_LINE_SIZE - sizeof(isLocked)> padding;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadSpinLockHolder {
|
|
|
|
ThreadSpinLock& lock;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
ThreadSpinLockHolder(ThreadSpinLock& lock) : lock(lock) { lock.enter(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
~ThreadSpinLockHolder() { lock.leave(); }
|
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
class ThreadUnsafeSpinLock {
|
|
|
|
public:
|
|
|
|
void enter(){};
|
|
|
|
void leave(){};
|
|
|
|
void assertNotEntered(){};
|
|
|
|
};
|
|
|
|
class ThreadUnsafeSpinLockHolder {
|
|
|
|
public:
|
|
|
|
ThreadUnsafeSpinLockHolder(ThreadUnsafeSpinLock&){};
|
|
|
|
};
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
#if FLOW_THREAD_SAFE
|
|
|
|
|
|
|
|
typedef ThreadSpinLock SpinLock;
|
|
|
|
typedef ThreadSpinLockHolder SpinLockHolder;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
typedef ThreadUnsafeSpinLock SpinLock;
|
|
|
|
typedef ThreadUnsafeSpinLockHolder SpinLockHolder;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class Event {
|
|
|
|
public:
|
|
|
|
Event();
|
|
|
|
~Event();
|
|
|
|
void set();
|
|
|
|
void block();
|
|
|
|
|
|
|
|
private:
|
|
|
|
#ifdef _WIN32
|
|
|
|
void* ev;
|
2020-02-02 05:23:53 +08:00
|
|
|
#elif defined(__linux__) || defined(__FreeBSD__)
|
2017-05-26 04:48:44 +08:00
|
|
|
sem_t sem;
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
mach_port_t self;
|
|
|
|
semaphore_t sem;
|
|
|
|
#else
|
|
|
|
#error Port me!
|
2021-03-11 02:06:03 +08:00
|
|
|
#endif
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
class Mutex {
|
2017-05-26 04:48:44 +08:00
|
|
|
// A re-entrant process-local blocking lock (e.g. CRITICAL_SECTION on Windows)
|
|
|
|
// Thread safe even if !FLOW_THREAD_SAFE
|
|
|
|
public:
|
|
|
|
Mutex();
|
|
|
|
~Mutex();
|
|
|
|
void enter();
|
|
|
|
void leave();
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
|
|
|
void* impl;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MutexHolder {
|
|
|
|
Mutex& lock;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
MutexHolder(Mutex& lock) : lock(lock) { lock.enter(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
~MutexHolder() { lock.leave(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|