Skip to content

Commit 0a9d8ec

Browse files
committed
Use a git version for RawReentrantMutex + RawMutexStatus
1 parent e110f5e commit 0a9d8ec

File tree

3 files changed

+19
-92
lines changed

3 files changed

+19
-92
lines changed

Cargo.lock

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ smallbox = "0.8"
7171
bstr = "0.2.12"
7272
crossbeam-utils = "0.7"
7373
generational-arena = "0.2"
74-
parking_lot = "0.10"
74+
parking_lot = { git = "https://github.com/coolreader18/parking_lot", branch = "pub-raw-reentrant" }
7575

7676
## unicode stuff
7777
unicode_names2 = "0.4"

vm/src/stdlib/thread.rs

Lines changed: 7 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ use crate::pyobject::{PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue};
55
use crate::vm::VirtualMachine;
66

77
use parking_lot::{
8-
lock_api::{GetThreadId, RawMutex as RawMutexT, RawMutexTimed},
8+
lock_api::{RawMutex as RawMutexT, RawMutexTimed, RawReentrantMutex},
99
RawMutex, RawThreadId,
1010
};
11-
use std::cell::Cell;
1211
use std::fmt;
13-
use std::sync::atomic::{AtomicUsize, Ordering};
1412
use std::time::Duration;
1513

1614
#[cfg(not(target_os = "windows"))]
@@ -93,89 +91,13 @@ impl LockProtocol for PyLock {
9391

9492
#[pyimpl(with(LockProtocol))]
9593
impl PyLock {
96-
// TODO: locked(), might require something to change in parking_lot
97-
}
98-
99-
// Copied from lock_api
100-
// TODO: open a PR to make this public in lock_api
101-
struct RawReentrantMutex<R, G> {
102-
owner: AtomicUsize,
103-
lock_count: Cell<usize>,
104-
mutex: R,
105-
get_thread_id: G,
106-
}
107-
108-
impl<R: RawMutexT, G: GetThreadId> RawReentrantMutex<R, G> {
109-
#[inline]
110-
fn lock_internal<F: FnOnce() -> bool>(&self, try_lock: F) -> bool {
111-
let id = self.get_thread_id.nonzero_thread_id().get();
112-
if self.owner.load(Ordering::Relaxed) == id {
113-
self.lock_count.set(
114-
self.lock_count
115-
.get()
116-
.checked_add(1)
117-
.expect("ReentrantMutex lock count overflow"),
118-
);
119-
} else {
120-
if !try_lock() {
121-
return false;
122-
}
123-
self.owner.store(id, Ordering::Relaxed);
124-
debug_assert_eq!(self.lock_count.get(), 0);
125-
self.lock_count.set(1);
126-
}
127-
true
128-
}
129-
}
130-
131-
unsafe impl<R: RawMutexT + Send, G: GetThreadId + Send> Send for RawReentrantMutex<R, G> {}
132-
unsafe impl<R: RawMutexT + Sync, G: GetThreadId + Sync> Sync for RawReentrantMutex<R, G> {}
133-
134-
unsafe impl<R: RawMutexT, G: GetThreadId> RawMutexT for RawReentrantMutex<R, G> {
135-
const INIT: Self = RawReentrantMutex {
136-
owner: AtomicUsize::new(0),
137-
lock_count: Cell::new(0),
138-
mutex: R::INIT,
139-
get_thread_id: G::INIT,
140-
};
141-
142-
type GuardMarker = R::GuardMarker;
143-
144-
#[inline]
145-
fn lock(&self) {
146-
self.lock_internal(|| {
147-
self.mutex.lock();
148-
true
149-
});
150-
}
151-
152-
#[inline]
153-
fn try_lock(&self) -> bool {
154-
self.lock_internal(|| self.mutex.try_lock())
155-
}
156-
157-
#[inline]
158-
fn unlock(&self) {
159-
let lock_count = self.lock_count.get() - 1;
160-
self.lock_count.set(lock_count);
161-
if lock_count == 0 {
162-
self.owner.store(0, Ordering::Relaxed);
163-
self.mutex.unlock();
94+
#[pymethod]
95+
fn locked(&self) -> bool {
96+
let acquired_lock = self.mu.try_lock();
97+
if acquired_lock {
98+
self.mu.unlock();
16499
}
165-
}
166-
}
167-
168-
unsafe impl<R: RawMutexTimed, G: GetThreadId> RawMutexTimed for RawReentrantMutex<R, G> {
169-
type Instant = R::Instant;
170-
type Duration = R::Duration;
171-
#[inline]
172-
fn try_lock_until(&self, timeout: R::Instant) -> bool {
173-
self.lock_internal(|| self.mutex.try_lock_until(timeout))
174-
}
175-
176-
#[inline]
177-
fn try_lock_for(&self, timeout: R::Duration) -> bool {
178-
self.lock_internal(|| self.mutex.try_lock_for(timeout))
100+
!acquired_lock
179101
}
180102
}
181103

0 commit comments

Comments
 (0)