libfoedus-core
FOEDUS Core Library
spin_until_impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_ASSORTED_SPIN_UNTIL_IMPL_HPP_
19 #define FOEDUS_ASSORTED_SPIN_UNTIL_IMPL_HPP_
20 
23 
24 namespace foedus {
25 namespace assorted {
26 
32 
60 template <typename COND>
61 inline uint64_t spin_until(COND spin_until_cond) {
63 
64  const bool on_valgrind = is_running_on_valgrind();
65  while (!spin_until_cond()) {
66  // Valgrind never switches context without this.
67  // This if should have a negligible overhead.
68  if (on_valgrind) {
70  }
71  }
72 
73  watch.stop();
74  return watch.elapsed();
75 }
76 
81 inline void yield_if_valgrind() {
82  const bool on_valgrind = is_running_on_valgrind();
83  if (on_valgrind) {
85  }
86 }
87 
88 } // namespace assorted
89 } // namespace foedus
90 
91 #endif // FOEDUS_ASSORTED_SPIN_UNTIL_IMPL_HPP_
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
uint64_t spin_until(COND spin_until_cond)
Spin locally until the given condition returns true.
uint64_t elapsed() const __attribute__((always_inline))
Definition: rdtsc_watch.hpp:52
void spinlock_yield()
Invoke _mm_pause(), x86 PAUSE instruction, or something equivalent in the env.
A RDTSC-based low-overhead stop watch.
Definition: rdtsc_watch.hpp:37
uint64_t stop() __attribute__((always_inline))
Take another current time tick.
Definition: rdtsc_watch.hpp:47
void yield_if_valgrind()
Use this in your while as a stop-gap before switching to spin_until().