File: atomic_wait_helper.h

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (92 lines) | stat: -rw-r--r-- 2,425 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef TEST_BENCHMARK_ATOMIC_WAIT_HELPER_H
#define TEST_BENCHMARK_ATOMIC_WAIT_HELPER_H

#include <atomic>
#include <chrono>
#include <exception>
#include <stop_token>
#include <thread>

struct HighPrioTask {
  sched_param param;
  pthread_attr_t attr_t;
  pthread_t thread;
  std::atomic_bool stopped{false};

  HighPrioTask(const HighPrioTask&) = delete;

  HighPrioTask() {
    pthread_attr_init(&attr_t);
    pthread_attr_setschedpolicy(&attr_t, SCHED_FIFO);
    param.sched_priority = sched_get_priority_max(SCHED_FIFO);
    pthread_attr_setschedparam(&attr_t, &param);
    pthread_attr_setinheritsched(&attr_t, PTHREAD_EXPLICIT_SCHED);

    auto thread_fun = [](void* arg) -> void* {
      auto* stop = reinterpret_cast<std::atomic_bool*>(arg);
      while (!stop->load(std::memory_order_relaxed)) {
        // spin
      }
      return nullptr;
    };

    if (pthread_create(&thread, &attr_t, thread_fun, &stopped) != 0) {
      throw std::runtime_error("failed to create thread");
    }
  }

  ~HighPrioTask() {
    stopped = true;
    pthread_attr_destroy(&attr_t);
    pthread_join(thread, nullptr);
  }
};

template <std::size_t N>
struct NumHighPrioTasks {
  static constexpr auto value = N;
};

template <std::size_t N>
struct NumWaitingThreads {
  static constexpr auto value = N;
};

template <std::size_t N>
struct NumberOfAtomics {
  static constexpr auto value = N;
};

struct KeepNotifying {
  template <class Atomic>
  static void notify(Atomic& a, std::stop_token st) {
    while (!st.stop_requested()) {
      a.fetch_add(1, std::memory_order_relaxed);
      a.notify_all();
    }
  }
};

template <std::size_t N>
struct NotifyEveryNus {
  template <class Atomic>
  static void notify(Atomic& a, std::stop_token st) {
    while (!st.stop_requested()) {
      auto start = std::chrono::system_clock::now();
      a.fetch_add(1, std::memory_order_relaxed);
      a.notify_all();
      while (std::chrono::system_clock::now() - start < std::chrono::microseconds{N}) {
      }
    }
  }
};

#endif // TEST_BENCHMARK_ATOMIC_WAIT_HELPER_H