Dobby  3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
SpinLock.h
1 /*
2 * If not stated otherwise in this file or this component's LICENSE file the
3 * following copyright and licenses apply:
4 *
5 * Copyright 2015 Sky UK
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19 /*
20  * File: SpinLock.h
21  *
22  */
23 #ifndef SPINLOCK_H
24 #define SPINLOCK_H
25 
26 #include <atomic>
27 
28 namespace AICommon
29 {
30 
41 class Spinlock
42 {
43 public:
44  Spinlock() : mLocked(ATOMIC_FLAG_INIT)
45  { }
46 
47 public:
48  void lock()
49  {
50  while (mLocked.test_and_set(std::memory_order_acquire)) ;
51  }
52  void unlock()
53  {
54  mLocked.clear(std::memory_order_release);
55  }
56  bool try_lock()
57  {
58  return !mLocked.test_and_set(std::memory_order_acquire);
59  }
60 
61 private:
62  std::atomic_flag mLocked;
63 };
64 
65 } // namespace AICommon
66 
67 
68 #endif // !defined(SPINLOCK_H)
69 
Definition: SpinLock.h:42