1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-25 04:02:42 +01:00
rpcs3/Utilities/sync.h

157 lines
3.6 KiB
C
Raw Normal View History

2016-05-13 16:01:48 +02:00
#pragma once
/* For internal use. Don't include. */
2020-12-12 13:01:29 +01:00
#include "util/types.hpp"
#include "util/atomic.hpp"
#include "util/dyn_lib.hpp"
2016-05-13 16:01:48 +02:00
#ifdef _WIN32
#define NOMINMAX
2016-05-13 16:01:48 +02:00
#include <Windows.h>
#include <time.h>
#elif __linux__
#include <errno.h>
#include <sys/syscall.h>
#include <linux/futex.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#endif
#include <algorithm>
#include <ctime>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include <unordered_map>
2016-05-13 16:01:48 +02:00
#ifdef _WIN32
DYNAMIC_IMPORT("ntdll.dll", NtWaitForKeyedEvent, NTSTATUS(HANDLE, PVOID Key, BOOLEAN Alertable, PLARGE_INTEGER Timeout));
DYNAMIC_IMPORT("ntdll.dll", NtReleaseKeyedEvent, NTSTATUS(HANDLE, PVOID Key, BOOLEAN Alertable, PLARGE_INTEGER Timeout));
DYNAMIC_IMPORT("ntdll.dll", NtWaitForSingleObject, NTSTATUS(HANDLE Handle, BOOLEAN Alertable, PLARGE_INTEGER Timeout));
DYNAMIC_IMPORT("ntdll.dll", NtDelayExecution, NTSTATUS(BOOLEAN Alertable, PLARGE_INTEGER DelayInterval));
DYNAMIC_IMPORT("ntdll.dll", NtWaitForAlertByThreadId, NTSTATUS(PVOID Address, PLARGE_INTEGER Timeout));
DYNAMIC_IMPORT("ntdll.dll", NtAlertThreadByThreadId, NTSTATUS(DWORD_PTR ThreadId));
constexpr NTSTATUS NTSTATUS_SUCCESS = 0;
constexpr NTSTATUS NTSTATUS_ALERTED = 0x101;
constexpr NTSTATUS NTSTATUS_TIMEOUT = 0x102;
#endif
2016-05-13 16:01:48 +02:00
#ifndef __linux__
enum
{
FUTEX_PRIVATE_FLAG = 0,
FUTEX_WAIT = 0,
FUTEX_WAIT_PRIVATE = FUTEX_WAIT,
FUTEX_WAKE = 1,
FUTEX_WAKE_PRIVATE = FUTEX_WAKE,
FUTEX_BITSET = 2,
FUTEX_WAIT_BITSET = FUTEX_WAIT | FUTEX_BITSET,
FUTEX_WAIT_BITSET_PRIVATE = FUTEX_WAIT_BITSET,
FUTEX_WAKE_BITSET = FUTEX_WAKE | FUTEX_BITSET,
FUTEX_WAKE_BITSET_PRIVATE = FUTEX_WAKE_BITSET,
};
#endif
2016-05-13 16:01:48 +02:00
inline int futex(volatile void* uaddr, int futex_op, uint val, const timespec* timeout = nullptr, uint mask = 0)
2016-05-13 16:01:48 +02:00
{
#ifdef __linux__
return syscall(SYS_futex, uaddr, futex_op, static_cast<int>(val), timeout, nullptr, static_cast<int>(mask));
#else
static struct futex_manager
2016-05-13 16:01:48 +02:00
{
struct waiter
{
uint val;
uint mask;
std::condition_variable cv;
};
2016-05-13 16:01:48 +02:00
std::mutex mutex;
std::unordered_multimap<volatile void*, waiter*, pointer_hash<volatile void, alignof(int)>> map;
2016-05-13 16:01:48 +02:00
int operator()(volatile void* uaddr, int futex_op, uint val, const timespec* timeout, uint mask)
2016-05-13 16:01:48 +02:00
{
std::unique_lock lock(mutex);
2016-05-13 16:01:48 +02:00
switch (futex_op)
2016-05-13 16:01:48 +02:00
{
case FUTEX_WAIT_PRIVATE:
{
mask = -1;
[[fallthrough]];
}
case FUTEX_WAIT_BITSET_PRIVATE:
{
if (*reinterpret_cast<volatile uint*>(uaddr) != val)
2016-05-13 16:01:48 +02:00
{
errno = EAGAIN;
return -1;
2016-05-13 16:01:48 +02:00
}
waiter rec;
rec.val = val;
rec.mask = mask;
const auto& ref = *map.emplace(uaddr, &rec);
2016-05-13 16:01:48 +02:00
int res = 0;
2016-05-13 16:01:48 +02:00
if (!timeout)
{
rec.cv.wait(lock, [&] { return !rec.mask; });
}
else if (futex_op == FUTEX_WAIT)
{
const auto nsec = std::chrono::nanoseconds(timeout->tv_nsec + timeout->tv_sec * 1000000000ull);
2016-05-13 16:01:48 +02:00
if (!rec.cv.wait_for(lock, nsec, [&] { return !rec.mask; }))
{
res = -1;
errno = ETIMEDOUT;
}
}
else
{
// TODO
}
2018-05-17 16:40:29 +02:00
map.erase(std::find(map.find(uaddr), map.end(), ref));
return res;
}
2016-05-13 16:01:48 +02:00
case FUTEX_WAKE_PRIVATE:
{
mask = -1;
[[fallthrough]];
}
case FUTEX_WAKE_BITSET_PRIVATE:
{
int res = 0;
2016-05-13 16:01:48 +02:00
for (auto range = map.equal_range(uaddr); val && range.first != range.second; range.first++)
{
auto& entry = *range.first->second;
2018-05-17 16:40:29 +02:00
if (entry.mask & mask)
{
entry.cv.notify_one();
entry.mask = 0;
res++;
val--;
}
}
2016-05-13 16:01:48 +02:00
return res;
}
}
2016-05-13 16:01:48 +02:00
errno = EINVAL;
return -1;
2016-05-13 16:01:48 +02:00
}
} g_futex;
2016-05-13 16:01:48 +02:00
return g_futex(uaddr, futex_op, val, timeout, mask);
2016-05-13 16:01:48 +02:00
#endif
}