blob: 8fbcddfef829a2a6ee7e39e9d75444db79871825 [file] [log] [blame]
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01001/*
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +01002 * Copyright (c) 2019-2020 Arm Limited.
Georgios Pinitascfa2bba2019-06-27 17:00:52 +01003 *
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
30namespace arm_gemm {
31
32class barrier {
33private:
34 unsigned int m_threads;
35
36 std::atomic<unsigned int> m_waiters;
37 std::atomic<unsigned int> m_leavers;
38
39public:
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
71namespace arm_gemm {
72
73class barrier {
74public:
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