Files
test/intern/cycles/util/semaphore.h
Brecht Van Lommel dd51c8660b Refactor: Cycles: Add const keyword where possible, using clang-tidy
Check was misc-const-correctness, combined with readability-isolate-declaration
as suggested by the docs.

Temporarily clang-format "QualifierAlignment: Left" was used to get consistency
with the prevailing order of keywords.

Pull Request: https://projects.blender.org/blender/blender/pulls/132361
2025-01-03 10:23:20 +01:00

45 lines
864 B
C++

/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#pragma once
#include "util/thread.h"
CCL_NAMESPACE_BEGIN
/* Counting Semaphore
*
* To restrict concurrent access to a resource to a specified number
* of threads. Similar to std::counting_semaphore from C++20. */
class thread_counting_semaphore {
public:
explicit thread_counting_semaphore(const int count) : count(count) {}
thread_counting_semaphore(const thread_counting_semaphore &) = delete;
void acquire()
{
thread_scoped_lock lock(mutex);
while (count == 0) {
condition.wait(lock);
}
count--;
}
void release()
{
const thread_scoped_lock lock(mutex);
count++;
condition.notify_one();
}
protected:
thread_mutex mutex;
thread_condition_variable condition;
int count;
};
CCL_NAMESPACE_END