|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef THIRD_PARTY_CEL_PYTHON_FREE_THREADING_MUTEX_H_ |
| 16 | +#define THIRD_PARTY_CEL_PYTHON_FREE_THREADING_MUTEX_H_ |
| 17 | + |
| 18 | +#include <Python.h> // IWYU pragma: keep - Needed for Py_GIL_DISABLED |
| 19 | + |
| 20 | +#include "absl/base/attributes.h" |
| 21 | +#include "absl/base/thread_annotations.h" |
| 22 | + |
| 23 | +#ifndef Py_GIL_DISABLED |
| 24 | +#else |
| 25 | +#include "absl/synchronization/mutex.h" |
| 26 | +#endif |
| 27 | + |
| 28 | +namespace cel_python { |
| 29 | + |
| 30 | +// Zero-cost mutex wrapper that compiles away to nothing in standard GIL builds, |
| 31 | +// and uses absl::Mutex in free-threaded builds (Py_GIL_DISABLED). |
| 32 | +class ABSL_LOCKABLE ABSL_ATTRIBUTE_WARN_UNUSED FreeThreadingMutex { |
| 33 | + public: |
| 34 | + FreeThreadingMutex() = default; |
| 35 | + FreeThreadingMutex(const FreeThreadingMutex&) = delete; |
| 36 | + FreeThreadingMutex& operator=(const FreeThreadingMutex&) = delete; |
| 37 | + |
| 38 | +#ifndef Py_GIL_DISABLED |
| 39 | + // In GIL-enabled builds, this mutex compiles away to zero-cost no-ops while |
| 40 | + // retaining thread-safety annotations for Clang static analysis. |
| 41 | + // |
| 42 | + // Relationship with the GIL: |
| 43 | + // - For operations accessing Python state or cached PyObjects (such as |
| 44 | + // PyCelValue::Value() or PyMessageFactory::GetMessageClass()), mutual |
| 45 | + // exclusion between threads is provided by the Python GIL itself. |
| 46 | + // - For pure C++ operations (such as PyCelEnv::Compile() or deserialization), |
| 47 | + // the GIL is intentionally released (via py::gil_scoped_release) to enable |
| 48 | + // parallel execution and prevent deadlocks with DescriptorPool's mutex. |
| 49 | + // Objects may therefore be constructed or moved while the GIL is NOT held. |
| 50 | + // - Consequently, Lock() and Unlock() do not assert PyGILState_Check(), |
| 51 | + // allowing lock acquisition and move semantics to function safely whether |
| 52 | + // the GIL is currently held or released. Call sites that strictly require |
| 53 | + // the GIL explicitly verify or acquire it themselves. |
| 54 | + void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() {} |
| 55 | + void Unlock() ABSL_UNLOCK_FUNCTION() {} |
| 56 | +#else |
| 57 | + // Free-threaded build: real mutex |
| 58 | + void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() { mutex_.Lock(); } |
| 59 | + void Unlock() ABSL_UNLOCK_FUNCTION() { mutex_.Unlock(); } |
| 60 | + |
| 61 | + private: |
| 62 | + absl::Mutex mutex_; |
| 63 | +#endif |
| 64 | +}; |
| 65 | + |
| 66 | +// RAII lock guard for FreeThreadingMutex. |
| 67 | +class ABSL_SCOPED_LOCKABLE FreeThreadingLockGuard { |
| 68 | + public: |
| 69 | + explicit FreeThreadingLockGuard(FreeThreadingMutex& mutex) |
| 70 | + ABSL_EXCLUSIVE_LOCK_FUNCTION(mutex) |
| 71 | + : mutex_(mutex) { |
| 72 | + mutex_.Lock(); |
| 73 | + } |
| 74 | + ~FreeThreadingLockGuard() ABSL_UNLOCK_FUNCTION() { mutex_.Unlock(); } |
| 75 | + |
| 76 | + FreeThreadingLockGuard(const FreeThreadingLockGuard&) = delete; |
| 77 | + FreeThreadingLockGuard& operator=(const FreeThreadingLockGuard&) = delete; |
| 78 | + |
| 79 | + private: |
| 80 | + FreeThreadingMutex& mutex_; |
| 81 | +}; |
| 82 | + |
| 83 | +} // namespace cel_python |
| 84 | + |
| 85 | +#endif // THIRD_PARTY_CEL_PYTHON_FREE_THREADING_MUTEX_H_ |
0 commit comments