| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  semaphore.h                                                           */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #ifndef SEMAPHORE_H
 | 
					
						
							|  |  |  | #define SEMAPHORE_H
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/error/error_list.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | #include "core/typedefs.h"
 | 
					
						
							| 
									
										
										
										
											2023-01-28 13:34:06 +01:00
										 |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							|  |  |  | #include "core/error/error_macros.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <condition_variable>
 | 
					
						
							|  |  |  | #include <mutex>
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Semaphore { | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2023-01-27 12:36:01 +01:00
										 |  |  | 	mutable std::mutex mutex; | 
					
						
							|  |  |  | 	mutable std::condition_variable condition; | 
					
						
							|  |  |  | 	mutable uint32_t count = 0; // Initialized as locked.
 | 
					
						
							| 
									
										
										
										
											2023-01-28 13:34:06 +01:00
										 |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							|  |  |  | 	mutable uint32_t awaiters = 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-03-03 09:26:42 +01:00
										 |  |  | 	_ALWAYS_INLINE_ void post() const { | 
					
						
							| 
									
										
										
										
											2023-01-27 12:36:01 +01:00
										 |  |  | 		std::lock_guard lock(mutex); | 
					
						
							|  |  |  | 		count++; | 
					
						
							|  |  |  | 		condition.notify_one(); | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-03 09:26:42 +01:00
										 |  |  | 	_ALWAYS_INLINE_ void wait() const { | 
					
						
							| 
									
										
										
										
											2023-01-27 12:36:01 +01:00
										 |  |  | 		std::unique_lock lock(mutex); | 
					
						
							| 
									
										
										
										
											2023-01-28 13:34:06 +01:00
										 |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							|  |  |  | 		++awaiters; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-01-27 12:36:01 +01:00
										 |  |  | 		while (!count) { // Handle spurious wake-ups.
 | 
					
						
							|  |  |  | 			condition.wait(lock); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-01-28 13:34:06 +01:00
										 |  |  | 		--count; | 
					
						
							|  |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							|  |  |  | 		--awaiters; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-03 09:26:42 +01:00
										 |  |  | 	_ALWAYS_INLINE_ bool try_wait() const { | 
					
						
							| 
									
										
										
										
											2023-01-27 12:36:01 +01:00
										 |  |  | 		std::lock_guard lock(mutex); | 
					
						
							|  |  |  | 		if (count) { | 
					
						
							|  |  |  | 			count--; | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2023-01-27 12:36:01 +01:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-28 13:34:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef DEBUG_ENABLED
 | 
					
						
							|  |  |  | 	~Semaphore() { | 
					
						
							|  |  |  | 		// Destroying an std::condition_variable when not all threads waiting on it have been notified
 | 
					
						
							|  |  |  | 		// invokes undefined behavior (e.g., it may be nicely destroyed or it may be awaited forever.)
 | 
					
						
							|  |  |  | 		// That means other threads could still be running the body of std::condition_variable::wait()
 | 
					
						
							|  |  |  | 		// but already past the safety checkpoint. That's the case for instance if that function is already
 | 
					
						
							|  |  |  | 		// waiting to lock again.
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		// We will make the rule a bit more restrictive and simpler to understand at the same time: there
 | 
					
						
							|  |  |  | 		// should not be any threads at any stage of the waiting by the time the semaphore is destroyed.
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		// We do so because of the following reasons:
 | 
					
						
							|  |  |  | 		// - We have the guideline that threads must be awaited (i.e., completed), so the waiting thread
 | 
					
						
							|  |  |  | 		//   must be completely done by the time the thread controlling it finally destroys the semaphore.
 | 
					
						
							|  |  |  | 		//   Therefore, only a coding mistake could make the program run into such a attempt at premature
 | 
					
						
							|  |  |  | 		//   destruction of the semaphore.
 | 
					
						
							|  |  |  | 		// - In scripting, given that Semaphores are wrapped by RefCounted classes, in general it can't
 | 
					
						
							|  |  |  | 		//   happen that a thread is trying to destroy a Semaphore while another is still doing whatever with
 | 
					
						
							|  |  |  | 		//   it, so the simplification is mostly transparent to script writers.
 | 
					
						
							|  |  |  | 		// - The redefined rule can be checked for failure to meet it, which is what this implementation does.
 | 
					
						
							|  |  |  | 		//   This is useful to detect a few cases of potential misuse; namely:
 | 
					
						
							|  |  |  | 		//   a) In scripting:
 | 
					
						
							|  |  |  | 		//      * The coder is naughtily dealing with the reference count causing a semaphore to die prematurely.
 | 
					
						
							|  |  |  | 		//      * The coder is letting the project reach its termination without having cleanly finished threads
 | 
					
						
							|  |  |  | 		//        that await on semaphores (or at least, let the usual semaphore-controlled loop exit).
 | 
					
						
							|  |  |  | 		//   b) In the native side, where Semaphore is not a ref-counted beast and certain coding mistakes can
 | 
					
						
							|  |  |  | 		//      lead to its premature destruction as well.
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		// Let's let users know they are doing it wrong, but apply a, somewhat hacky, countermeasure against UB
 | 
					
						
							|  |  |  | 		// in debug builds.
 | 
					
						
							|  |  |  | 		std::lock_guard lock(mutex); | 
					
						
							|  |  |  | 		if (awaiters) { | 
					
						
							|  |  |  | 			WARN_PRINT( | 
					
						
							|  |  |  | 					"A Semaphore object is being destroyed while one or more threads are still waiting on it.\n" | 
					
						
							|  |  |  | 					"Please call post() on it as necessary to prevent such a situation and so ensure correct cleanup."); | 
					
						
							|  |  |  | 			// And now, the hacky countermeasure (i.e., leak the condition variable).
 | 
					
						
							|  |  |  | 			new (&condition) std::condition_variable(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-07-29 12:59:18 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:10:34 +01:00
										 |  |  | #endif // SEMAPHORE_H
 |