Blender V4.3
semaphore.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifndef __UTIL_SEMAPHORE_H__
6#define __UTIL_SEMAPHORE_H__
7
8#include "util/thread.h"
9
11
12/* Counting Semaphore
13 *
14 * To restrict concurrent access to a resource to a specified number
15 * of threads. Similar to std::counting_semaphore from C++20. */
16
18 public:
19 explicit thread_counting_semaphore(const int count) : count(count) {}
20
22
23 void acquire()
24 {
26 while (count == 0) {
27 condition.wait(lock);
28 }
29 count--;
30 }
31
32 void release()
33 {
35 count++;
36 condition.notify_one();
37 }
38
39 protected:
42 int count;
43};
44
46
47#endif /* __UTIL_SEMAPHORE_H__ */
volatile int lock
thread_counting_semaphore(const thread_counting_semaphore &)=delete
thread_condition_variable condition
Definition semaphore.h:41
thread_counting_semaphore(const int count)
Definition semaphore.h:19
#define CCL_NAMESPACE_END
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:30
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition thread.h:29
std::condition_variable thread_condition_variable
Definition thread.h:31