blob: 226f7f194ab3c22dfc032fb9578e2feb5934dcc6 [file] [log] [blame]
Josh Gao903b7492016-05-17 19:23:39 -07001/*
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 Cuib74c6492016-04-29 16:53:52 -070017#pragma once
Josh Gao903b7492016-05-17 19:23:39 -070018#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>
32namespace std {
33
34// CRITICAL_SECTION is recursive, so just wrap it in a Mutex-compatible class.
35class recursive_mutex {
36 public:
Yabin Cuib74c6492016-04-29 16:53:52 -070037 typedef CRITICAL_SECTION* native_handle_type;
38
Josh Gao903b7492016-05-17 19:23:39 -070039 recursive_mutex() {
Yabin Cuib74c6492016-04-29 16:53:52 -070040 InitializeCriticalSection(&cs_);
Josh Gao903b7492016-05-17 19:23:39 -070041 }
42
43 ~recursive_mutex() {
Yabin Cuib74c6492016-04-29 16:53:52 -070044 DeleteCriticalSection(&cs_);
Josh Gao903b7492016-05-17 19:23:39 -070045 }
46
47 void lock() {
Yabin Cuib74c6492016-04-29 16:53:52 -070048 EnterCriticalSection(&cs_);
Josh Gao903b7492016-05-17 19:23:39 -070049 }
50
51 bool try_lock() {
Yabin Cuib74c6492016-04-29 16:53:52 -070052 return TryEnterCriticalSection(&cs_);
Josh Gao903b7492016-05-17 19:23:39 -070053 }
54
55 void unlock() {
Yabin Cuib74c6492016-04-29 16:53:52 -070056 LeaveCriticalSection(&cs_);
57 }
58
59 native_handle_type native_handle() {
60 return &cs_;
Josh Gao903b7492016-05-17 19:23:39 -070061 }
62
63 private:
Yabin Cuib74c6492016-04-29 16:53:52 -070064 CRITICAL_SECTION cs_;
Josh Gao903b7492016-05-17 19:23:39 -070065
66 DISALLOW_COPY_AND_ASSIGN(recursive_mutex);
67};
68
69class mutex {
70 public:
Yabin Cuib74c6492016-04-29 16:53:52 -070071 typedef CRITICAL_SECTION* native_handle_type;
72
Josh Gao903b7492016-05-17 19:23:39 -070073 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 Cuib74c6492016-04-29 16:53:52 -0700107 native_handle_type native_handle() {
108 return mutex_.native_handle();
109 }
110
Josh Gao903b7492016-05-17 19:23:39 -0700111 private:
112 recursive_mutex mutex_;
113 size_t lock_count_ = 0;
Yabin Cuib74c6492016-04-29 16:53:52 -0700114
115 friend class condition_variable;
Josh Gao903b7492016-05-17 19:23:39 -0700116};
117
118}
119
Yabin Cuib74c6492016-04-29 16:53:52 -0700120#endif // defined(_WIN32)