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> |
| 28 | #include <boost/noncopyable.hpp> |
| 29 | #include <Windows.h> |
| 30 | |
| 31 | /* |
| 32 | Lightweight synchronization objects that only make sense on Windows. For cross-platform |
| 33 | code, use the classes found in the concurrency namespace |
| 34 | */ |
| 35 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 36 | namespace apache { |
| 37 | namespace thrift { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 38 | |
| 39 | struct TCriticalSection : boost::noncopyable { |
| 40 | CRITICAL_SECTION cs; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 41 | TCriticalSection() { InitializeCriticalSection(&cs); } |
| 42 | ~TCriticalSection() { DeleteCriticalSection(&cs); } |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | class TAutoCrit : boost::noncopyable { |
| 46 | private: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 47 | CRITICAL_SECTION* cs_; |
| 48 | |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 49 | public: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 50 | explicit TAutoCrit(TCriticalSection& cs) : cs_(&cs.cs) { EnterCriticalSection(cs_); } |
| 51 | ~TAutoCrit() { LeaveCriticalSection(cs_); } |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | struct TAutoResetEvent : boost::noncopyable { |
| 55 | HANDLE h; |
| 56 | |
| 57 | TAutoResetEvent() { |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame^] | 58 | h = CreateEvent(nullptr, FALSE, FALSE, nullptr); |
| 59 | if (h == nullptr) { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 60 | GlobalOutput.perror("TAutoResetEvent unable to create event, GLE=", GetLastError()); |
| 61 | throw apache::thrift::concurrency::SystemResourceException("CreateEvent failed"); |
| 62 | } |
| 63 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 64 | ~TAutoResetEvent() { CloseHandle(h); } |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct TManualResetEvent : boost::noncopyable { |
| 68 | HANDLE h; |
| 69 | |
| 70 | TManualResetEvent() { |
zeshuai007 | 26681fb | 2020-06-03 17:24:38 +0800 | [diff] [blame^] | 71 | h = CreateEvent(nullptr, TRUE, FALSE, nullptr); |
| 72 | if (h == nullptr) { |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 73 | GlobalOutput.perror("TManualResetEvent unable to create event, GLE=", GetLastError()); |
| 74 | throw apache::thrift::concurrency::SystemResourceException("CreateEvent failed"); |
| 75 | } |
| 76 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 77 | ~TManualResetEvent() { CloseHandle(h); } |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | struct TAutoHandle : boost::noncopyable { |
| 81 | HANDLE h; |
| 82 | explicit TAutoHandle(HANDLE h_ = INVALID_HANDLE_VALUE) : h(h_) {} |
| 83 | ~TAutoHandle() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 84 | if (h != INVALID_HANDLE_VALUE) |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 85 | CloseHandle(h); |
| 86 | } |
| 87 | |
| 88 | HANDLE release() { |
| 89 | HANDLE retval = h; |
| 90 | h = INVALID_HANDLE_VALUE; |
| 91 | return retval; |
| 92 | } |
| 93 | void reset(HANDLE h_ = INVALID_HANDLE_VALUE) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 94 | if (h_ == h) |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 95 | return; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 96 | if (h != INVALID_HANDLE_VALUE) |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 97 | CloseHandle(h); |
| 98 | h = h_; |
| 99 | } |
| 100 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 101 | } |
| 102 | } // apache::thrift |
Ben Craig | b2501a7 | 2013-09-13 12:29:43 -0500 | [diff] [blame] | 103 | |
| 104 | #endif |