Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:04

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
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 // Including Windows.h can conflict with Winsock2 usage, and also
0031 // adds problematic macros like min() and max(). Try to work around:
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   Lightweight synchronization objects that only make sense on Windows.  For cross-platform
0052   code, use the classes found in the concurrency namespace
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 } // apache::thrift
0122 
0123 #endif