1 /* 2 Copyright (c) 2005-2020 Intel Corporation 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 __TBB_queuing_mutex_H 18 #define __TBB_queuing_mutex_H 19 20 #define __TBB_queuing_mutex_H_include_area 21 #include "internal/_warning_suppress_enable_notice.h" 22 23 #include <cstring> 24 #include "atomic.h" 25 #include "tbb_profiling.h" 26 27 namespace tbb { 28 29 //! Queuing mutex with local-only spinning. 30 /** @ingroup synchronization */ 31 class queuing_mutex : internal::mutex_copy_deprecated_and_disabled { 32 public: 33 //! Construct unacquired mutex. queuing_mutex()34 queuing_mutex() { 35 q_tail = NULL; 36 #if TBB_USE_THREADING_TOOLS 37 internal_construct(); 38 #endif 39 } 40 41 //! The scoped locking pattern 42 /** It helps to avoid the common problem of forgetting to release lock. 43 It also nicely provides the "node" for queuing locks. */ 44 class scoped_lock: internal::no_copy { 45 //! Initialize fields to mean "no lock held". initialize()46 void initialize() { 47 mutex = NULL; 48 going = 0; 49 #if TBB_USE_ASSERT 50 internal::poison_pointer(next); 51 #endif /* TBB_USE_ASSERT */ 52 } 53 54 public: 55 //! Construct lock that has not acquired a mutex. 56 /** Equivalent to zero-initialization of *this. */ scoped_lock()57 scoped_lock() {initialize();} 58 59 //! Acquire lock on given mutex. scoped_lock(queuing_mutex & m)60 scoped_lock( queuing_mutex& m ) { 61 initialize(); 62 acquire(m); 63 } 64 65 //! Release lock (if lock is held). ~scoped_lock()66 ~scoped_lock() { 67 if( mutex ) release(); 68 } 69 70 //! Acquire lock on given mutex. 71 void __TBB_EXPORTED_METHOD acquire( queuing_mutex& m ); 72 73 //! Acquire lock on given mutex if free (i.e. non-blocking) 74 bool __TBB_EXPORTED_METHOD try_acquire( queuing_mutex& m ); 75 76 //! Release lock. 77 void __TBB_EXPORTED_METHOD release(); 78 79 private: 80 //! The pointer to the mutex owned, or NULL if not holding a mutex. 81 queuing_mutex* mutex; 82 83 //! The pointer to the next competitor for a mutex 84 scoped_lock *next; 85 86 //! The local spin-wait variable 87 /** Inverted (0 - blocked, 1 - acquired the mutex) for the sake of 88 zero-initialization. Defining it as an entire word instead of 89 a byte seems to help performance slightly. */ 90 uintptr_t going; 91 }; 92 93 void __TBB_EXPORTED_METHOD internal_construct(); 94 95 // Mutex traits 96 static const bool is_rw_mutex = false; 97 static const bool is_recursive_mutex = false; 98 static const bool is_fair_mutex = true; 99 100 private: 101 //! The last competitor requesting the lock 102 atomic<scoped_lock*> q_tail; 103 104 }; 105 106 __TBB_DEFINE_PROFILING_SET_NAME(queuing_mutex) 107 108 } // namespace tbb 109 110 #include "internal/_warning_suppress_disable_notice.h" 111 #undef __TBB_queuing_mutex_H_include_area 112 113 #endif /* __TBB_queuing_mutex_H */ 114