Advertisement
Ilya_konstantinov

Untitled

Dec 7th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include "mutex.h"
  2. #include <cstdint>
  3.  
  4. /* Implementation of futex syscalls for Linux & MacOS */
  5.  
  6. #if defined(__linux__)
  7.  
  8. #include <unistd.h>
  9. #include <linux/futex.h>
  10. #include <sys/syscall.h>
  11.  
  12. void FutexWait(void *addr, uint64_t expected_value) {
  13.     syscall(SYS_futex, addr, FUTEX_WAIT_PRIVATE, expected_value, nullptr, nullptr, 0);
  14. }
  15.  
  16. void FutexWakeOne(void *addr) {
  17.     syscall(SYS_futex, addr, FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0);
  18. }
  19.  
  20. void FutexWakeAll(void *addr) {
  21.     syscall(SYS_futex, addr, FUTEX_WAKE_PRIVATE, INT_MAX, nullptr, nullptr, 0);
  22. }
  23.  
  24. #elif defined(__APPLE__)
  25.  
  26. // NOLINTBEGIN(readability-identifier-naming)
  27. extern "C" int __ulock_wait(uint32_t operation, void *addr, uint64_t value, uint32_t timeout);
  28. extern "C" int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
  29. // NOLINTEND(readability-identifier-naming)
  30.  
  31. #define UL_COMPARE_AND_WAIT 1
  32. #define ULF_WAKE_ALL        0x00000100
  33.  
  34. void FutexWait(void *addr, uint64_t expected_value) {
  35.     __ulock_wait(UL_COMPARE_AND_WAIT, addr, expected_value, 0);
  36. }
  37.  
  38. void FutexWakeOne(void *addr) {
  39.     __ulock_wake(UL_COMPARE_AND_WAIT, addr, 0);
  40. }
  41.  
  42. void FutexWakeAll(void *addr) {
  43.     __ulock_wake(UL_COMPARE_AND_WAIT | ULF_WAKE_ALL, addr, 0);
  44. }
  45.  
  46. #undef ULF_WAKE_ALL
  47. #undef UL_COMPARE_AND_WAIT
  48.  
  49. #else
  50.  
  51. #error "Only Linux & MacOS platforms are supported!"
  52.  
  53. #endif
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement