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