mirror of
https://github.com/exaloop/codon.git
synced 2025-06-03 15:03:52 +08:00
stdlib/threading.codon
This commit is contained in:
parent
c5f96cd5e8
commit
cb8384a019
@ -1,3 +1,6 @@
|
||||
# (c) 2022 Exaloop Inc. All rights reserved.
|
||||
|
||||
|
||||
@tuple
|
||||
class Lock:
|
||||
p: cobj
|
||||
@ -5,20 +8,21 @@ class Lock:
|
||||
def __new__() -> Lock:
|
||||
return (_C.seq_lock_new(),)
|
||||
|
||||
def acquire(self, block: bool = True, timeout: float = -1.0):
|
||||
def acquire(self, block: bool = True, timeout: float = -1.0) -> bool:
|
||||
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)
|
||||
|
||||
def release(self):
|
||||
def release(self) -> void:
|
||||
_C.seq_lock_release(self.p)
|
||||
|
||||
def __enter__(self):
|
||||
def __enter__(self) -> void:
|
||||
self.acquire()
|
||||
|
||||
def __exit__(self):
|
||||
def __exit__(self) -> void:
|
||||
self.release()
|
||||
|
||||
|
||||
@tuple
|
||||
class RLock:
|
||||
p: cobj
|
||||
@ -26,27 +30,32 @@ class RLock:
|
||||
def __new__() -> RLock:
|
||||
return (_C.seq_rlock_new(),)
|
||||
|
||||
def acquire(self, block: bool = True, timeout: float = -1.0):
|
||||
def acquire(self, block: bool = True, timeout: float = -1.0) -> bool:
|
||||
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)
|
||||
|
||||
def release(self):
|
||||
def release(self) -> void:
|
||||
_C.seq_rlock_release(self.p)
|
||||
|
||||
def __enter__(self):
|
||||
def __enter__(self) -> void:
|
||||
self.acquire()
|
||||
|
||||
def __exit__(self):
|
||||
def __exit__(self) -> void:
|
||||
self.release()
|
||||
|
||||
def active_count():
|
||||
|
||||
def active_count() -> int:
|
||||
from openmp import get_num_threads
|
||||
|
||||
return get_num_threads()
|
||||
|
||||
def get_native_id():
|
||||
|
||||
def get_native_id() -> int:
|
||||
from openmp import get_thread_num
|
||||
|
||||
return get_thread_num()
|
||||
|
||||
def get_ident():
|
||||
|
||||
def get_ident() -> int:
|
||||
return get_native_id() + 1
|
||||
|
Loading…
x
Reference in New Issue
Block a user