aboutsummaryrefslogtreecommitdiff
path: root/libc/src/__support/threads/linux/barrier.cpp
blob: cf7207b53094bc531fa9fa93b8bf1765c51825ae (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
//===-- Implementation of Barrier class ------------- ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/threads/linux/barrier.h"
#include "hdr/errno_macros.h"
#include "src/__support/threads/CndVar.h"
#include "src/__support/threads/mutex.h"

namespace LIBC_NAMESPACE_DECL {

int Barrier::init(Barrier *b,
                  [[maybe_unused]] const pthread_barrierattr_t *attr,
                  unsigned count) {
  LIBC_ASSERT(attr == nullptr); // TODO implement barrierattr
  if (count == 0)
    return EINVAL;

  b->expected = count;
  b->waiting = 0;
  b->blocking = true;

  int err;
  err = CndVar::init(&b->entering);
  if (err != 0)
    return err;

  err = CndVar::init(&b->exiting);
  if (err != 0)
    return err;

  auto mutex_err = Mutex::init(&b->m, false, false, false, false);
  if (mutex_err != MutexError::NONE)
    return EAGAIN;

  return 0;
}

int Barrier::wait() {
  m.lock();

  // if the barrier is emptying out threads, wait until it finishes
  while (!blocking)
    entering.wait(&m);
  waiting++;

  if (waiting < expected) {
    // block threads until waiting = expected
    while (blocking)
      exiting.wait(&m);
  } else {
    // this is the last thread to call wait(), so lets wake everyone up
    blocking = false;
    exiting.broadcast();
  }
  waiting--;

  if (waiting == 0) {
    // all threads have exited the barrier, let's let the ones waiting to enter
    // continue
    blocking = true;
    entering.broadcast();
    m.unlock();

    // POSIX dictates that the barrier should return a special value to just one
    // thread, so we can arbitrarily choose this thread
    return PTHREAD_BARRIER_SERIAL_THREAD;
  }
  m.unlock();

  return 0;
}

int Barrier::destroy(Barrier *b) {
  CndVar::destroy(&b->entering);
  CndVar::destroy(&b->exiting);
  Mutex::destroy(&b->m);
  return 0;
}

} // namespace LIBC_NAMESPACE_DECL