blob: a296d7ec4ae76e984e0277205310bc7f76b864ea [file] [log] [blame]
Ben Craigb2501a72013-09-13 12:29:43 -05001/*
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 Grochowski16a23a62014-11-13 15:33:38 +010036namespace apache {
37namespace thrift {
Ben Craigb2501a72013-09-13 12:29:43 -050038
39struct TCriticalSection : boost::noncopyable {
40 CRITICAL_SECTION cs;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010041 TCriticalSection() { InitializeCriticalSection(&cs); }
42 ~TCriticalSection() { DeleteCriticalSection(&cs); }
Ben Craigb2501a72013-09-13 12:29:43 -050043};
44
45class TAutoCrit : boost::noncopyable {
46private:
Konrad Grochowski16a23a62014-11-13 15:33:38 +010047 CRITICAL_SECTION* cs_;
48
Ben Craigb2501a72013-09-13 12:29:43 -050049public:
Konrad Grochowski16a23a62014-11-13 15:33:38 +010050 explicit TAutoCrit(TCriticalSection& cs) : cs_(&cs.cs) { EnterCriticalSection(cs_); }
51 ~TAutoCrit() { LeaveCriticalSection(cs_); }
Ben Craigb2501a72013-09-13 12:29:43 -050052};
53
54struct TAutoResetEvent : boost::noncopyable {
55 HANDLE h;
56
57 TAutoResetEvent() {
zeshuai00726681fb2020-06-03 17:24:38 +080058 h = CreateEvent(nullptr, FALSE, FALSE, nullptr);
59 if (h == nullptr) {
Ben Craigb2501a72013-09-13 12:29:43 -050060 GlobalOutput.perror("TAutoResetEvent unable to create event, GLE=", GetLastError());
61 throw apache::thrift::concurrency::SystemResourceException("CreateEvent failed");
62 }
63 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064 ~TAutoResetEvent() { CloseHandle(h); }
Ben Craigb2501a72013-09-13 12:29:43 -050065};
66
67struct TManualResetEvent : boost::noncopyable {
68 HANDLE h;
69
70 TManualResetEvent() {
zeshuai00726681fb2020-06-03 17:24:38 +080071 h = CreateEvent(nullptr, TRUE, FALSE, nullptr);
72 if (h == nullptr) {
Ben Craigb2501a72013-09-13 12:29:43 -050073 GlobalOutput.perror("TManualResetEvent unable to create event, GLE=", GetLastError());
74 throw apache::thrift::concurrency::SystemResourceException("CreateEvent failed");
75 }
76 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +010077 ~TManualResetEvent() { CloseHandle(h); }
Ben Craigb2501a72013-09-13 12:29:43 -050078};
79
80struct TAutoHandle : boost::noncopyable {
81 HANDLE h;
82 explicit TAutoHandle(HANDLE h_ = INVALID_HANDLE_VALUE) : h(h_) {}
83 ~TAutoHandle() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084 if (h != INVALID_HANDLE_VALUE)
Ben Craigb2501a72013-09-13 12:29:43 -050085 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 Grochowski16a23a62014-11-13 15:33:38 +010094 if (h_ == h)
Ben Craigb2501a72013-09-13 12:29:43 -050095 return;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010096 if (h != INVALID_HANDLE_VALUE)
Ben Craigb2501a72013-09-13 12:29:43 -050097 CloseHandle(h);
98 h = h_;
99 }
100};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100101}
102} // apache::thrift
Ben Craigb2501a72013-09-13 12:29:43 -0500103
104#endif