summaryrefslogtreecommitdiffstats
path: root/src/include/spinlock.h
blob: 3f12bdc0069e8842c163d865b67bb4cd934cee7e (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2017 SUSE LINUX GmbH
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 * @author Jesse Williamson <jwilliamson@suse.de>
 *
*/

#ifndef CEPH_SPINLOCK_HPP
#define CEPH_SPINLOCK_HPP

#include <atomic>

namespace ceph {
inline namespace version_1_0 {

class spinlock;

inline void spin_lock(std::atomic_flag& lock);
inline void spin_unlock(std::atomic_flag& lock);
inline void spin_lock(ceph::spinlock& lock);
inline void spin_unlock(ceph::spinlock& lock);

/* A pre-packaged spinlock type modelling BasicLockable: */
class spinlock final
{
  std::atomic_flag af = ATOMIC_FLAG_INIT;

  public:
  void lock() {
    ceph::spin_lock(af);
  }
 
  void unlock() noexcept {
    ceph::spin_unlock(af);
  }
};

// Free functions:
inline void spin_lock(std::atomic_flag& lock)
{
 while(lock.test_and_set(std::memory_order_acquire))
  ;
}

inline void spin_unlock(std::atomic_flag& lock)
{
 lock.clear(std::memory_order_release);
}

inline void spin_lock(std::atomic_flag *lock)
{
 spin_lock(*lock);
}

inline void spin_unlock(std::atomic_flag *lock)
{
 spin_unlock(*lock);
}

inline void spin_lock(ceph::spinlock& lock)
{
 lock.lock();
}

inline void spin_unlock(ceph::spinlock& lock)
{
 lock.unlock();
}

inline void spin_lock(ceph::spinlock *lock)
{
 spin_lock(*lock);
}

inline void spin_unlock(ceph::spinlock *lock)
{
 spin_unlock(*lock);
}

} // inline namespace (version)
} // namespace ceph

#endif