BioDynaMo  v1.05.0-137fdb15
spinlock.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_UTIL_SPINLOCK_H_
16 #define CORE_UTIL_SPINLOCK_H_
17 
18 #include <atomic>
19 
20 namespace bdm {
21 
22 class Spinlock {
23  public:
24  Spinlock() = default;
25 
28  Spinlock(const Spinlock& other) {}
29 
30  void lock() { // NOLINT
31  while (flag_.test_and_set()) {
32  // spin
33  ;
34  }
35  }
36 
37  void unlock() { // NOLINT
38  flag_.clear();
39  }
40 
41  private:
42  std::atomic_flag flag_ = ATOMIC_FLAG_INIT;
43 };
44 
45 } // namespace bdm
46 
47 #endif // CORE_UTIL_SPINLOCK_H_
bdm::Spinlock::flag_
std::atomic_flag flag_
Definition: spinlock.h:42
bdm::Spinlock::Spinlock
Spinlock()=default
bdm
Definition: agent.cc:39
bdm::Spinlock::lock
void lock()
Definition: spinlock.h:30
bdm::Spinlock::Spinlock
Spinlock(const Spinlock &other)
Definition: spinlock.h:28
bdm::Spinlock
Definition: spinlock.h:22
bdm::Spinlock::unlock
void unlock()
Definition: spinlock.h:37