Compute Library
 23.08
barrier.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #pragma once
25 
26 #ifndef NO_MULTI_THREADING
27 
28 #include <atomic>
29 
30 namespace arm_gemm {
31 
32 class barrier {
33 private:
34  unsigned int m_threads;
35 
36  std::atomic<unsigned int> m_waiters;
37  std::atomic<unsigned int> m_leavers;
38 
39 public:
40  barrier(unsigned int threads) : m_threads(threads), m_waiters(0), m_leavers(0) { }
41 
42  /* This isn't safe if any thread is waiting... */
43  void set_nthreads(unsigned int nthreads) {
44  m_threads = nthreads;
45  }
46 
47  void arrive_and_wait() {
48  m_waiters++;
49 
50  while (m_waiters != m_threads) {
51  ; /* spin */
52  }
53 
54  unsigned int v = m_leavers.fetch_add(1);
55 
56  if (v == (m_threads - 1)) {
57  m_waiters -= m_threads;
58  m_leavers = 0;
59  } else {
60  while (m_leavers > 0) {
61  ; /* spin */
62  }
63  }
64  }
65 };
66 
67 } // namespace arm_gemm
68 
69 #else
70 
71 namespace arm_gemm {
72 
73 class barrier {
74 public:
75  barrier(unsigned int) { }
76 
77  void arrive_and_wait() { }
78  void set_nthreads(unsigned int ) { }
79 };
80 
81 } // namespace arm_gemm
82 
83 #endif
arm_gemm::barrier::set_nthreads
void set_nthreads(unsigned int nthreads)
Definition: barrier.hpp:43
arm_gemm::barrier::barrier
barrier(unsigned int threads)
Definition: barrier.hpp:40
arm_gemm
Definition: barrier.hpp:30
arm_gemm::barrier::arrive_and_wait
void arrive_and_wait()
Definition: barrier.hpp:47
arm_gemm::barrier
Definition: barrier.hpp:32