1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00
codon/stdlib/threading.codon

55 lines
1.3 KiB
Python
Raw Permalink Normal View History

# Copyright (C) 2022-2025 Exaloop Inc. <https://exaloop.io>
2022-01-24 11:16:45 +01:00
2021-09-27 14:02:44 -04:00
@tuple
class Lock:
p: cobj
def __new__() -> Lock:
return (_C.seq_lock_new(),)
2022-01-24 11:16:45 +01:00
def acquire(self, block: bool = True, timeout: float = -1.0) -> bool:
2021-09-27 14:02:44 -04:00
if timeout >= 0.0 and not block:
raise ValueError("can't specify a timeout for a non-blocking call")
return _C.seq_lock_acquire(self.p, block, timeout)
2022-02-16 16:51:16 +01:00
def release(self):
2021-09-27 14:02:44 -04:00
_C.seq_lock_release(self.p)
2022-02-16 16:51:16 +01:00
def __enter__(self):
2021-09-27 14:02:44 -04:00
self.acquire()
2022-02-16 16:51:16 +01:00
def __exit__(self):
2021-09-27 14:02:44 -04:00
self.release()
@tuple
class RLock:
p: cobj
def __new__() -> RLock:
return (_C.seq_rlock_new(),)
2022-01-24 11:16:45 +01:00
def acquire(self, block: bool = True, timeout: float = -1.0) -> bool:
2021-09-27 14:02:44 -04:00
if timeout >= 0.0 and not block:
raise ValueError("can't specify a timeout for a non-blocking call")
return _C.seq_rlock_acquire(self.p, block, timeout)
2022-02-16 16:51:16 +01:00
def release(self):
2021-09-27 14:02:44 -04:00
_C.seq_rlock_release(self.p)
2022-02-16 16:51:16 +01:00
def __enter__(self):
2021-09-27 14:02:44 -04:00
self.acquire()
2022-02-16 16:51:16 +01:00
def __exit__(self):
2021-09-27 14:02:44 -04:00
self.release()
2022-01-24 11:16:45 +01:00
def active_count() -> int:
2021-09-27 14:02:44 -04:00
from openmp import get_num_threads
return get_num_threads()
2022-01-24 11:16:45 +01:00
def get_native_id() -> int:
2021-09-27 14:02:44 -04:00
from openmp import get_thread_num
return get_thread_num()
2022-01-24 11:16:45 +01:00
def get_ident() -> int:
2021-09-27 14:02:44 -04:00
return get_native_id() + 1