Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:09

0001 // Licensed to the Apache Software Foundation (ASF) under one
0002 // or more contributor license agreements.  See the NOTICE file
0003 // distributed with this work for additional information
0004 // regarding copyright ownership.  The ASF licenses this file
0005 // to you under the Apache License, Version 2.0 (the
0006 // "License"); you may not use this file except in compliance
0007 // with the License.  You may obtain a copy of the License at
0008 //
0009 //   http://www.apache.org/licenses/LICENSE-2.0
0010 //
0011 // Unless required by applicable law or agreed to in writing,
0012 // software distributed under the License is distributed on an
0013 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0014 // KIND, either express or implied.  See the License for the
0015 // specific language governing permissions and limitations
0016 // under the License.
0017 
0018 #pragma once
0019 
0020 #include <memory>
0021 
0022 #include "arrow/util/macros.h"
0023 #include "arrow/util/visibility.h"
0024 
0025 namespace arrow {
0026 namespace util {
0027 
0028 /// A wrapper around std::mutex since we can't use it directly in
0029 /// public headers due to C++/CLI.
0030 /// https://docs.microsoft.com/en-us/cpp/standard-library/mutex#remarks
0031 class ARROW_EXPORT Mutex {
0032  public:
0033   Mutex();
0034   Mutex(Mutex&&) = default;
0035   Mutex& operator=(Mutex&&) = default;
0036 
0037   /// A Guard is falsy if a lock could not be acquired.
0038   class ARROW_EXPORT Guard {
0039    public:
0040     Guard() : locked_(NULLPTR, [](Mutex* mutex) {}) {}
0041     Guard(Guard&&) = default;
0042     Guard& operator=(Guard&&) = default;
0043 
0044     explicit operator bool() const { return bool(locked_); }
0045 
0046     void Unlock() { locked_.reset(); }
0047 
0048    private:
0049     explicit Guard(Mutex* locked);
0050 
0051     std::unique_ptr<Mutex, void (*)(Mutex*)> locked_;
0052     friend Mutex;
0053   };
0054 
0055   Guard TryLock();
0056   Guard Lock();
0057 
0058  private:
0059   struct Impl;
0060   std::unique_ptr<Impl, void (*)(Impl*)> impl_;
0061 };
0062 
0063 #ifndef _WIN32
0064 /// Return a pointer to a process-wide, process-specific Mutex that can be used
0065 /// at any point in a child process.  NULL is returned when called in the parent.
0066 ///
0067 /// The rule is to first check that getpid() corresponds to the parent process pid
0068 /// and, if not, call this function to lock any after-fork reinitialization code.
0069 /// Like this:
0070 ///
0071 ///   std::atomic<pid_t> pid{getpid()};
0072 ///   ...
0073 ///   if (pid.load() != getpid()) {
0074 ///     // In child process
0075 ///     auto lock = GlobalForkSafeMutex()->Lock();
0076 ///     if (pid.load() != getpid()) {
0077 ///       // Reinitialize internal structures after fork
0078 ///       ...
0079 ///       pid.store(getpid());
0080 ARROW_EXPORT
0081 Mutex* GlobalForkSafeMutex();
0082 #endif
0083 
0084 }  // namespace util
0085 }  // namespace arrow