| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 21:21:57 +02:00
										 |  |  | #ifdef __serenity__
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | #include <AK/Assertions.h>
 | 
					
						
							|  |  |  | #include <AK/Types.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-12 11:17:34 -06:00
										 |  |  | #include <AK/Atomic.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | namespace LibThread { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Lock { | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     Lock() {} | 
					
						
							|  |  |  |     ~Lock() {} | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void lock(); | 
					
						
							|  |  |  |     void unlock(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-04-13 12:29:41 +02:00
										 |  |  |     Atomic<int> m_holder { 0 }; | 
					
						
							| 
									
										
										
										
											2019-07-03 21:17:35 +02:00
										 |  |  |     u32 m_level { 0 }; | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | class Locker { | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-04-30 11:43:25 +02:00
										 |  |  |     ALWAYS_INLINE explicit Locker(Lock& l) | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |         : m_lock(l) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         lock(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-30 11:43:25 +02:00
										 |  |  |     ALWAYS_INLINE ~Locker() { unlock(); } | 
					
						
							|  |  |  |     ALWAYS_INLINE void unlock() { m_lock.unlock(); } | 
					
						
							|  |  |  |     ALWAYS_INLINE void lock() { m_lock.lock(); } | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     Lock& m_lock; | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-30 11:43:25 +02:00
										 |  |  | ALWAYS_INLINE void Lock::lock() | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-01 19:11:08 +02:00
										 |  |  |     int tid = gettid(); | 
					
						
							| 
									
										
										
										
											2020-04-13 12:29:41 +02:00
										 |  |  |     if (m_holder == tid) { | 
					
						
							|  |  |  |         ++m_level; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  |     for (;;) { | 
					
						
							| 
									
										
										
										
											2020-04-13 12:29:41 +02:00
										 |  |  |         int expected = 0; | 
					
						
							|  |  |  |         if (m_holder.compare_exchange_strong(expected, tid, AK::memory_order_acq_rel)) { | 
					
						
							|  |  |  |             m_holder = tid; | 
					
						
							|  |  |  |             m_level = 1; | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-03-25 13:05:24 +01:00
										 |  |  |         donate(m_holder); | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | inline void Lock::unlock() | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-13 12:29:41 +02:00
										 |  |  |     ASSERT(m_holder == gettid()); | 
					
						
							|  |  |  |     ASSERT(m_level); | 
					
						
							|  |  |  |     --m_level; | 
					
						
							|  |  |  |     if (!m_level) | 
					
						
							|  |  |  |         m_holder.store(0, AK::memory_order_release); | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | #define LOCKER(lock) LibThread::Locker locker(lock)
 | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<typename T> | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | class Lockable { | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     Lockable() {} | 
					
						
							|  |  |  |     Lockable(T&& resource) | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |         : m_resource(move(resource)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     Lock& lock() { return m_lock; } | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  |     T& resource() { return m_resource; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     T lock_and_copy() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         LOCKER(m_lock); | 
					
						
							|  |  |  |         return m_resource; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     T m_resource; | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     Lock m_lock; | 
					
						
							| 
									
										
										
										
											2019-03-25 13:03:49 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-06-22 21:21:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 21:21:57 +02:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | namespace LibThread { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Lock { | 
					
						
							| 
									
										
										
										
											2019-06-22 21:21:57 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  |     Lock() { } | 
					
						
							|  |  |  |     ~Lock() { } | 
					
						
							| 
									
										
										
										
											2019-06-22 21:21:57 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 23:51:27 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-22 21:21:57 +02:00
										 |  |  | #define LOCKER(x)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |