Blender  V2.93
util_semaphore.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2020 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __UTIL_SEMAPHORE_H__
18 #define __UTIL_SEMAPHORE_H__
19 
20 #include "util/util_thread.h"
21 
23 
24 /* Counting Semaphore
25  *
26  * To restrict concurrent access to a resource to a specified number
27  * of threads. Similar to std::counting_semaphore from C++20. */
28 
30  public:
31  explicit thread_counting_semaphore(const int count) : count(count)
32  {
33  }
34 
36 
37  void acquire()
38  {
40  while (count == 0) {
41  condition.wait(lock);
42  }
43  count--;
44  }
45 
46  void release()
47  {
49  count++;
50  condition.notify_one();
51  }
52 
53  protected:
56  int count;
57 };
58 
60 
61 #endif /* __UTIL_SEMAPHORE_H__ */
thread_counting_semaphore(const thread_counting_semaphore &)=delete
thread_condition_variable condition
thread_counting_semaphore(const int count)
#define CCL_NAMESPACE_END
std::unique_lock< std::mutex > thread_scoped_lock
Definition: util_thread.h:41
CCL_NAMESPACE_BEGIN typedef std::mutex thread_mutex
Definition: util_thread.h:40
std::condition_variable thread_condition_variable
Definition: util_thread.h:42