Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_WINDOWS_Sync_H_ |
| 21 | #define _THRIFT_WINDOWS_Sync_H_ 1 |
| 22 | |
| 23 | #ifndef _WIN32 |
| 24 | #error "windows/Sync.h is only usable on Windows" |
| 25 | #endif |
| 26 | |
| 27 | #include <thrift/concurrency/Exception.h> |
Mario Emmenlauer | d270b35 | 2020-11-19 09:43:34 +0100 | [diff] [blame] | 28 | #include <thrift/TNonCopyable.h> |
| 29 | |
Mario Emmenlauer | 034c935 | 2020-05-21 23:04:12 +0200 | [diff] [blame] | 30 | // Including Windows.h can conflict with Winsock2 usage, and also |
| 31 | // adds problematic macros like min() and max(). Try to work around: |
Dirk Sandbrink | cbb6530 | 2022-02-04 09:55:19 +0100 | [diff] [blame] | 32 | #ifndef NOMINMAX |
Mario Emmenlauer | 034c935 | 2020-05-21 23:04:12 +0200 | [diff] [blame] | 33 | #define NOMINMAX |
Dirk Sandbrink | cbb6530 | 2022-02-04 09:55:19 +0100 | [diff] [blame] | 34 | #define _THRIFT_UNDEF_NOMINMAX |
| 35 | #endif |
| 36 | #ifndef WIN32_LEAN_AND_MEAN |
Mario Emmenlauer | 034c935 | 2020-05-21 23:04:12 +0200 | [diff] [blame] | 37 | #define WIN32_LEAN_AND_MEAN |
Dirk Sandbrink | cbb6530 | 2022-02-04 09:55:19 +0100 | [diff] [blame] | 38 | #define _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN |
| 39 | #endif |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 40 | #include <Windows.h> |
Dirk Sandbrink | cbb6530 | 2022-02-04 09:55:19 +0100 | [diff] [blame] | 41 | #ifdef _THRIFT_UNDEF_NOMINMAX |
Mario Emmenlauer | 034c935 | 2020-05-21 23:04:12 +0200 | [diff] [blame] | 42 | #undef NOMINMAX |
Dirk Sandbrink | cbb6530 | 2022-02-04 09:55:19 +0100 | [diff] [blame] | 43 | #undef _THRIFT_UNDEF_NOMINMAX |
| 44 | #endif |
| 45 | #ifdef _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN |
Mario Emmenlauer | 034c935 | 2020-05-21 23:04:12 +0200 | [diff] [blame] | 46 | #undef WIN32_LEAN_AND_MEAN |
Dirk Sandbrink | cbb6530 | 2022-02-04 09:55:19 +0100 | [diff] [blame] | 47 | #undef _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN |
| 48 | #endif |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | Lightweight synchronization objects that only make sense on Windows. For cross-platform |
| 52 | code, use the classes found in the concurrency namespace |
| 53 | */ |
| 54 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 55 | namespace apache { |
| 56 | namespace thrift { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 57 | |
Mario Emmenlauer | d270b35 | 2020-11-19 09:43:34 +0100 | [diff] [blame] | 58 | struct TCriticalSection : apache::thrift::TNonCopyable { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 59 | CRITICAL_SECTION cs; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 60 | TCriticalSection() { InitializeCriticalSection(&cs); } |
| 61 | ~TCriticalSection() { DeleteCriticalSection(&cs); } |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
Mario Emmenlauer | d270b35 | 2020-11-19 09:43:34 +0100 | [diff] [blame] | 64 | class TAutoCrit : apache::thrift::TNonCopyable { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 65 | private: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 66 | CRITICAL_SECTION* cs_; |
| 67 | |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 68 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 69 | explicit TAutoCrit(TCriticalSection& cs) : cs_(&cs.cs) { EnterCriticalSection(cs_); } |
| 70 | ~TAutoCrit() { LeaveCriticalSection(cs_); } |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 71 | }; |
| 72 | |
Mario Emmenlauer | d270b35 | 2020-11-19 09:43:34 +0100 | [diff] [blame] | 73 | struct TAutoResetEvent : apache::thrift::TNonCopyable { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 74 | HANDLE h; |
| 75 | |
| 76 | TAutoResetEvent() { |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 77 | h = CreateEvent(nullptr, FALSE, FALSE, nullptr); |
| 78 | if (h == nullptr) { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 79 | GlobalOutput.perror("TAutoResetEvent unable to create event, GLE=", GetLastError()); |
| 80 | throw apache::thrift::concurrency::SystemResourceException("CreateEvent failed"); |
| 81 | } |
| 82 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 83 | ~TAutoResetEvent() { CloseHandle(h); } |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 84 | }; |
| 85 | |
Mario Emmenlauer | d270b35 | 2020-11-19 09:43:34 +0100 | [diff] [blame] | 86 | struct TManualResetEvent : apache::thrift::TNonCopyable { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 87 | HANDLE h; |
| 88 | |
| 89 | TManualResetEvent() { |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame] | 90 | h = CreateEvent(nullptr, TRUE, FALSE, nullptr); |
| 91 | if (h == nullptr) { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 92 | GlobalOutput.perror("TManualResetEvent unable to create event, GLE=", GetLastError()); |
| 93 | throw apache::thrift::concurrency::SystemResourceException("CreateEvent failed"); |
| 94 | } |
| 95 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 96 | ~TManualResetEvent() { CloseHandle(h); } |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 97 | }; |
| 98 | |
Mario Emmenlauer | d270b35 | 2020-11-19 09:43:34 +0100 | [diff] [blame] | 99 | struct TAutoHandle : apache::thrift::TNonCopyable { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 100 | HANDLE h; |
| 101 | explicit TAutoHandle(HANDLE h_ = INVALID_HANDLE_VALUE) : h(h_) {} |
| 102 | ~TAutoHandle() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 103 | if (h != INVALID_HANDLE_VALUE) |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 104 | CloseHandle(h); |
| 105 | } |
| 106 | |
| 107 | HANDLE release() { |
| 108 | HANDLE retval = h; |
| 109 | h = INVALID_HANDLE_VALUE; |
| 110 | return retval; |
| 111 | } |
| 112 | void reset(HANDLE h_ = INVALID_HANDLE_VALUE) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 113 | if (h_ == h) |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 114 | return; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 115 | if (h != INVALID_HANDLE_VALUE) |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 116 | CloseHandle(h); |
| 117 | h = h_; |
| 118 | } |
| 119 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 120 | } |
| 121 | } // apache::thrift |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 122 | |
| 123 | #endif |