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