dispenso 1.4.1
A library for task parallelism
Loading...
Searching...
No Matches
latch.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
15#pragma once
16
17#include <dispenso/platform.h>
18
19#include <dispenso/detail/completion_event_impl.h>
20
21namespace dispenso {
22
27class Latch {
28 public:
34 explicit Latch(uint32_t threadGroupCount) noexcept : impl_(threadGroupCount) {}
35
39 void count_down(uint32_t n = 1) noexcept {
40 if (impl_.intrusiveStatus().fetch_sub(n, std::memory_order_acq_rel) == 1) {
41 impl_.notify(0);
42 }
43 }
44
53 bool try_wait() const noexcept {
54 return impl_.intrusiveStatus().load(std::memory_order_acquire) == 0;
55 }
56
60 void wait() const noexcept {
61 impl_.wait(0);
62 }
63
67 void arrive_and_wait() noexcept {
68 if (impl_.intrusiveStatus().fetch_sub(1, std::memory_order_acq_rel) > 1) {
69 impl_.wait(0);
70 } else {
71 impl_.notify(0);
72 }
73 }
74
75 private:
76 detail::CompletionEventImpl impl_;
77};
78
79} // namespace dispenso
void arrive_and_wait() noexcept
Definition latch.h:67
void count_down(uint32_t n=1) noexcept
Definition latch.h:39
Latch(uint32_t threadGroupCount) noexcept
Definition latch.h:34
bool try_wait() const noexcept
Definition latch.h:53
void wait() const noexcept
Definition latch.h:60