SeqAn3
The Modern C++ library for sequence analysis.
spin_delay.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2019, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2019, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <thread>
16 #include <xmmintrin.h>
17 
18 #include <seqan3/core/platform.hpp>
19 
20 namespace seqan3::detail
21 {
33 class spin_delay
34 {
35 public:
39  constexpr spin_delay() noexcept = default;
40  constexpr spin_delay(spin_delay const &) noexcept = default;
41  constexpr spin_delay(spin_delay &&) noexcept = default;
42  constexpr spin_delay & operator=(spin_delay const &) noexcept = default;
43  constexpr spin_delay & operator=(spin_delay &&) noexcept = default;
44  ~spin_delay() noexcept = default;
45 
54  void wait()
55  {
56  if (current <= max_repetitions) // Start active spinning phase
57  {
58  for (int_fast32_t i = 0; i < current; ++i)
59  pause_processor();
60  current <<= 1; // double the amount of active CPU waiting cycles.
61  }
62  else // Start passive spinning phase
63  {
65  }
66  }
67 
68 private:
69 
71  void pause_processor()
72  {
73  #if defined(__SSE2__) // AMD and Intel
74  _mm_pause();
75  #elif defined(__ia64__) // IA64
76  __asm__ __volatile__ ("hint @pause");
77  #else // everything else.
78  asm volatile ("nop" ::: "memory"); // default operation - does nothing => Might lead to passive spinning.
79  #endif
80  }
81 
83  static constexpr int_fast32_t max_repetitions{16};
85  int_fast32_t current{1};
86 };
87 
88 } // namespace seqan3::detail
Provides platform and dependency checks.
T yield(T... args)
Definition: aligned_sequence_concept.hpp:35