Initializes owner_thread_id_ and critical_section_ in static mutexes.
| 10119 | |
| 10120 | // Initializes owner_thread_id_ and critical_section_ in static mutexes. |
| 10121 | void Mutex::ThreadSafeLazyInit() { |
| 10122 | // Dynamic mutexes are initialized in the constructor. |
| 10123 | if (type_ == kStatic) { |
| 10124 | switch ( |
| 10125 | ::InterlockedCompareExchange(&critical_section_init_phase_, 1L, 0L)) { |
| 10126 | case 0: |
| 10127 | // If critical_section_init_phase_ was 0 before the exchange, we |
| 10128 | // are the first to test it and need to perform the initialization. |
| 10129 | owner_thread_id_ = 0; |
| 10130 | { |
| 10131 | // Use RAII to flag that following mem alloc is never deallocated. |
| 10132 | #ifdef _MSC_VER |
| 10133 | MemoryIsNotDeallocated memory_is_not_deallocated; |
| 10134 | #endif // _MSC_VER |
| 10135 | critical_section_ = new CRITICAL_SECTION; |
| 10136 | } |
| 10137 | ::InitializeCriticalSection(critical_section_); |
| 10138 | // Updates the critical_section_init_phase_ to 2 to signal |
| 10139 | // initialization complete. |
| 10140 | GTEST_CHECK_(::InterlockedCompareExchange( |
| 10141 | &critical_section_init_phase_, 2L, 1L) == |
| 10142 | 1L); |
| 10143 | break; |
| 10144 | case 1: |
| 10145 | // Somebody else is already initializing the mutex; spin until they |
| 10146 | // are done. |
| 10147 | while (::InterlockedCompareExchange(&critical_section_init_phase_, |
| 10148 | 2L, |
| 10149 | 2L) != 2L) { |
| 10150 | // Possibly yields the rest of the thread's time slice to other |
| 10151 | // threads. |
| 10152 | ::Sleep(0); |
| 10153 | } |
| 10154 | break; |
| 10155 | |
| 10156 | case 2: |
| 10157 | break; // The mutex is already initialized and ready for use. |
| 10158 | |
| 10159 | default: |
| 10160 | GTEST_CHECK_(false) |
| 10161 | << "Unexpected value of critical_section_init_phase_ " |
| 10162 | << "while initializing a static mutex."; |
| 10163 | } |
| 10164 | } |
| 10165 | } |
| 10166 | |
| 10167 | namespace { |
| 10168 |
nothing calls this directly
no outgoing calls
no test coverage detected