| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  rw_lock.h                                                            */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2021-01-01 20:13:46 +01:00
										 |  |  | /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */ | 
					
						
							| 
									
										
										
										
											2017-03-05 15:47:28 +01:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:10:34 +01:00
										 |  |  | #ifndef RW_LOCK_H
 | 
					
						
							|  |  |  | #define RW_LOCK_H
 | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-07 19:33:38 -03:00
										 |  |  | #include "core/error/error_list.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | #if !defined(NO_THREADS)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <shared_mutex>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | class RWLock { | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	mutable std::shared_timed_mutex mutex; | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	// Lock the rwlock, block if locked by someone else
 | 
					
						
							|  |  |  | 	void read_lock() const { | 
					
						
							|  |  |  | 		mutex.lock_shared(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	// Unlock the rwlock, let other threads continue
 | 
					
						
							|  |  |  | 	void read_unlock() const { | 
					
						
							|  |  |  | 		mutex.unlock_shared(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	// Attempt to lock the rwlock, OK on success, ERR_BUSY means it can't lock.
 | 
					
						
							|  |  |  | 	Error read_try_lock() const { | 
					
						
							|  |  |  | 		return mutex.try_lock_shared() ? OK : ERR_BUSY; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Lock the rwlock, block if locked by someone else
 | 
					
						
							|  |  |  | 	void write_lock() { | 
					
						
							|  |  |  | 		mutex.lock(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Unlock the rwlock, let other thwrites continue
 | 
					
						
							|  |  |  | 	void write_unlock() { | 
					
						
							|  |  |  | 		mutex.unlock(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	// Attempt to lock the rwlock, OK on success, ERR_BUSY means it can't lock.
 | 
					
						
							|  |  |  | 	Error write_try_lock() { | 
					
						
							|  |  |  | 		return mutex.try_lock() ? OK : ERR_BUSY; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RWLock { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	void read_lock() const {} | 
					
						
							|  |  |  | 	void read_unlock() const {} | 
					
						
							|  |  |  | 	Error read_try_lock() const { return OK; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void write_lock() {} | 
					
						
							|  |  |  | 	void write_unlock() {} | 
					
						
							|  |  |  | 	Error write_try_lock() { return OK; } | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | class RWLockRead { | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	const RWLock &lock; | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	RWLockRead(const RWLock &p_lock) : | 
					
						
							|  |  |  | 			lock(p_lock) { | 
					
						
							|  |  |  | 		lock.read_lock(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	~RWLockRead() { | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 		lock.read_unlock(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RWLockWrite { | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	RWLock &lock; | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 	RWLockWrite(RWLock &p_lock) : | 
					
						
							|  |  |  | 			lock(p_lock) { | 
					
						
							|  |  |  | 		lock.write_lock(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	~RWLockWrite() { | 
					
						
							| 
									
										
										
										
											2021-01-18 14:01:38 +01:00
										 |  |  | 		lock.write_unlock(); | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-01-07 18:25:37 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 11:10:34 +01:00
										 |  |  | #endif // RW_LOCK_H
 |