codon/stdlib/threading.codon

55 lines
1.3 KiB
Python
Raw Normal View History

# Copyright (C) 2022-2023 Exaloop Inc. <https://exaloop.io>
2022-01-24 18:16:45 +08:00
2021-09-28 02:02:44 +08:00
@tuple
class Lock:
p: cobj
def __new__() -> Lock:
return (_C.seq_lock_new(),)
2022-01-24 18:16:45 +08:00
def acquire(self, block: bool = True, timeout: float = -1.0) -> bool:
2021-09-28 02:02:44 +08: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 23:51:16 +08:00
def release(self):
2021-09-28 02:02:44 +08:00
_C.seq_lock_release(self.p)
2022-02-16 23:51:16 +08:00
def __enter__(self):
2021-09-28 02:02:44 +08:00
self.acquire()
2022-02-16 23:51:16 +08:00
def __exit__(self):
2021-09-28 02:02:44 +08:00
self.release()
@tuple
class RLock:
p: cobj
def __new__() -> RLock:
return (_C.seq_rlock_new(),)
2022-01-24 18:16:45 +08:00
def acquire(self, block: bool = True, timeout: float = -1.0) -> bool:
2021-09-28 02:02:44 +08: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 23:51:16 +08:00
def release(self):
2021-09-28 02:02:44 +08:00
_C.seq_rlock_release(self.p)
2022-02-16 23:51:16 +08:00
def __enter__(self):
2021-09-28 02:02:44 +08:00
self.acquire()
2022-02-16 23:51:16 +08:00
def __exit__(self):
2021-09-28 02:02:44 +08:00
self.release()
2022-01-24 18:16:45 +08:00
def active_count() -> int:
2021-09-28 02:02:44 +08:00
from openmp import get_num_threads
return get_num_threads()
2022-01-24 18:16:45 +08:00
def get_native_id() -> int:
2021-09-28 02:02:44 +08:00
from openmp import get_thread_num
return get_thread_num()
2022-01-24 18:16:45 +08:00
def get_ident() -> int:
2021-09-28 02:02:44 +08:00
return get_native_id() + 1