Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 17 | #pragma once |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 18 | #if defined(_WIN32) |
| 19 | |
| 20 | #include <windows.h> |
| 21 | |
| 22 | #include <android-base/macros.h> |
| 23 | |
| 24 | #include "adb.h" |
| 25 | |
| 26 | // The prebuilt version of mingw we use doesn't support mutex or recursive_mutex. |
| 27 | // Therefore, implement our own using the Windows primitives. |
| 28 | // Put them directly into the std namespace, so that when they're actually available, the build |
| 29 | // breaks until they're removed. |
| 30 | |
| 31 | #include <mutex> |
| 32 | namespace std { |
| 33 | |
| 34 | // CRITICAL_SECTION is recursive, so just wrap it in a Mutex-compatible class. |
| 35 | class recursive_mutex { |
| 36 | public: |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 37 | typedef CRITICAL_SECTION* native_handle_type; |
| 38 | |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 39 | recursive_mutex() { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 40 | InitializeCriticalSection(&cs_); |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ~recursive_mutex() { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 44 | DeleteCriticalSection(&cs_); |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void lock() { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 48 | EnterCriticalSection(&cs_); |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool try_lock() { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 52 | return TryEnterCriticalSection(&cs_); |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void unlock() { |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 56 | LeaveCriticalSection(&cs_); |
| 57 | } |
| 58 | |
| 59 | native_handle_type native_handle() { |
| 60 | return &cs_; |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | private: |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 64 | CRITICAL_SECTION cs_; |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(recursive_mutex); |
| 67 | }; |
| 68 | |
| 69 | class mutex { |
| 70 | public: |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 71 | typedef CRITICAL_SECTION* native_handle_type; |
| 72 | |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 73 | mutex() { |
| 74 | } |
| 75 | |
| 76 | ~mutex() { |
| 77 | } |
| 78 | |
| 79 | void lock() { |
| 80 | mutex_.lock(); |
| 81 | if (++lock_count_ != 1) { |
| 82 | fatal("non-recursive mutex locked reentrantly"); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void unlock() { |
| 87 | if (--lock_count_ != 0) { |
| 88 | fatal("non-recursive mutex unlock resulted in unexpected lock count: %d", lock_count_); |
| 89 | } |
| 90 | mutex_.unlock(); |
| 91 | } |
| 92 | |
| 93 | bool try_lock() { |
| 94 | if (!mutex_.try_lock()) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | if (lock_count_ != 0) { |
| 99 | mutex_.unlock(); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | ++lock_count_; |
| 104 | return true; |
| 105 | } |
| 106 | |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 107 | native_handle_type native_handle() { |
| 108 | return mutex_.native_handle(); |
| 109 | } |
| 110 | |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 111 | private: |
| 112 | recursive_mutex mutex_; |
| 113 | size_t lock_count_ = 0; |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 114 | |
| 115 | friend class condition_variable; |
Josh Gao | 903b749 | 2016-05-17 19:23:39 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } |
| 119 | |
Yabin Cui | b74c649 | 2016-04-29 16:53:52 -0700 | [diff] [blame] | 120 | #endif // defined(_WIN32) |