File indexing completed on 2026-04-17 08:35:04
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef _THRIFT_WINDOWS_Sync_H_
0021 #define _THRIFT_WINDOWS_Sync_H_ 1
0022
0023 #ifndef _WIN32
0024 #error "windows/Sync.h is only usable on Windows"
0025 #endif
0026
0027 #include <thrift/concurrency/Exception.h>
0028 #include <thrift/TNonCopyable.h>
0029
0030
0031
0032 #ifndef NOMINMAX
0033 #define NOMINMAX
0034 #define _THRIFT_UNDEF_NOMINMAX
0035 #endif
0036 #ifndef WIN32_LEAN_AND_MEAN
0037 #define WIN32_LEAN_AND_MEAN
0038 #define _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN
0039 #endif
0040 #include <Windows.h>
0041 #ifdef _THRIFT_UNDEF_NOMINMAX
0042 #undef NOMINMAX
0043 #undef _THRIFT_UNDEF_NOMINMAX
0044 #endif
0045 #ifdef _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN
0046 #undef WIN32_LEAN_AND_MEAN
0047 #undef _THRIFT_UNDEF_WIN32_LEAN_AND_MEAN
0048 #endif
0049
0050
0051
0052
0053
0054
0055 namespace apache {
0056 namespace thrift {
0057
0058 struct TCriticalSection : apache::thrift::TNonCopyable {
0059 CRITICAL_SECTION cs;
0060 TCriticalSection() { InitializeCriticalSection(&cs); }
0061 virtual ~TCriticalSection() { DeleteCriticalSection(&cs); }
0062 };
0063
0064 class TAutoCrit : apache::thrift::TNonCopyable {
0065 private:
0066 CRITICAL_SECTION* cs_;
0067
0068 public:
0069 explicit TAutoCrit(TCriticalSection& cs) : cs_(&cs.cs) { EnterCriticalSection(cs_); }
0070 virtual ~TAutoCrit() { LeaveCriticalSection(cs_); }
0071 };
0072
0073 struct TAutoResetEvent : apache::thrift::TNonCopyable {
0074 HANDLE h;
0075
0076 TAutoResetEvent() {
0077 h = CreateEvent(nullptr, FALSE, FALSE, nullptr);
0078 if (h == nullptr) {
0079 GlobalOutput.perror("TAutoResetEvent unable to create event, GLE=", GetLastError());
0080 throw apache::thrift::concurrency::SystemResourceException("CreateEvent failed");
0081 }
0082 }
0083 virtual ~TAutoResetEvent() { CloseHandle(h); }
0084 };
0085
0086 struct TManualResetEvent : apache::thrift::TNonCopyable {
0087 HANDLE h;
0088
0089 TManualResetEvent() {
0090 h = CreateEvent(nullptr, TRUE, FALSE, nullptr);
0091 if (h == nullptr) {
0092 GlobalOutput.perror("TManualResetEvent unable to create event, GLE=", GetLastError());
0093 throw apache::thrift::concurrency::SystemResourceException("CreateEvent failed");
0094 }
0095 }
0096 virtual ~TManualResetEvent() { CloseHandle(h); }
0097 };
0098
0099 struct TAutoHandle : apache::thrift::TNonCopyable {
0100 HANDLE h;
0101 explicit TAutoHandle(HANDLE h_ = INVALID_HANDLE_VALUE) : h(h_) {}
0102 ~TAutoHandle() {
0103 if (h != INVALID_HANDLE_VALUE)
0104 CloseHandle(h);
0105 }
0106
0107 HANDLE release() {
0108 HANDLE retval = h;
0109 h = INVALID_HANDLE_VALUE;
0110 return retval;
0111 }
0112 void reset(HANDLE h_ = INVALID_HANDLE_VALUE) {
0113 if (h_ == h)
0114 return;
0115 if (h != INVALID_HANDLE_VALUE)
0116 CloseHandle(h);
0117 h = h_;
0118 }
0119 };
0120 }
0121 }
0122
0123 #endif