Skip to content

Commit d6ee047

Browse files
add queue and threading
1 parent 849220b commit d6ee047

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

internal_filesystem/lib/queue.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
try:
2+
import _thread
3+
except ImportError:
4+
_thread = None
5+
6+
class Queue:
7+
def __init__(self, maxsize=0):
8+
self._queue = []
9+
self.maxsize = maxsize # 0 means unlimited
10+
self._lock = _thread.allocate_lock() if _thread else None
11+
12+
def put(self, item):
13+
if self._lock:
14+
with self._lock:
15+
if self.maxsize > 0 and len(self._queue) >= self.maxsize:
16+
raise RuntimeError("Queue is full")
17+
self._queue.append(item)
18+
else:
19+
if self.maxsize > 0 and len(self._queue) >= self.maxsize:
20+
raise RuntimeError("Queue is full")
21+
self._queue.append(item)
22+
23+
def get(self):
24+
if self._lock:
25+
with self._lock:
26+
if not self._queue:
27+
raise RuntimeError("Queue is empty")
28+
return self._queue.pop(0)
29+
else:
30+
if not self._queue:
31+
raise RuntimeError("Queue is empty")
32+
return self._queue.pop(0)
33+
34+
def qsize(self):
35+
if self._lock:
36+
with self._lock:
37+
return len(self._queue)
38+
return len(self._queue)
39+
40+
def empty(self):
41+
return self.qsize() == 0
42+
43+
def full(self):
44+
return self.maxsize > 0 and self.qsize() >= self.maxsize

internal_filesystem/lib/secp256k1_compat.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def secp256k1_ecdsa_signature_serialize_der(self, ctx, output, outputlen, raw_si
9696
try:
9797
if isinstance(raw_sig, FFI.CData):
9898
raw_sig = raw_sig._data
99+
if isinstance(output, FFI.CData):
100+
output = output._data
101+
if isinstance(outputlen, FFI.CData):
102+
outputlen = outputlen._data
99103
result = usecp256k1.ecdsa_signature_serialize_der(raw_sig)
100104
if result is None:
101105
return 0
@@ -121,6 +125,8 @@ def secp256k1_ecdsa_signature_serialize_compact(self, ctx, output, raw_sig):
121125
try:
122126
if isinstance(raw_sig, FFI.CData):
123127
raw_sig = raw_sig._data
128+
if isinstance(output, FFI.CData):
129+
output = output._data
124130
result = usecp256k1.ecdsa_signature_serialize_compact(raw_sig)
125131
if result is None:
126132
return 0
@@ -181,6 +187,10 @@ def secp256k1_ecdsa_recoverable_signature_serialize_compact(self, ctx, output, r
181187
try:
182188
if isinstance(recover_sig, FFI.CData):
183189
recover_sig = recover_sig._data
190+
if isinstance(output, FFI.CData):
191+
output = output._data
192+
if isinstance(recid, FFI.CData):
193+
recid = recid._data
184194
result, rec_id = usecp256k1.ecdsa_sign_recoverable(recover_sig)
185195
if result is None:
186196
return 0
@@ -246,6 +256,8 @@ def secp256k1_schnorrsig_sign_custom(self, ctx, sig64, msg, msg_len, keypair, au
246256
try:
247257
if isinstance(keypair, FFI.CData):
248258
keypair = keypair._data
259+
if isinstance(sig64, FFI.CData):
260+
sig64 = sig64._data
249261
result = usecp256k1.schnorrsig_sign(msg, keypair)
250262
if result is None:
251263
return 0
@@ -264,6 +276,8 @@ def secp256k1_schnorrsig_verify(self, ctx, schnorr_sig, msg, msg_len, xonly_pubk
264276

265277
def secp256k1_tagged_sha256(self, ctx, hash32, tag, tag_len, msg, msg_len):
266278
try:
279+
if isinstance(hash32, FFI.CData):
280+
hash32 = hash32._data
267281
result = usecp256k1.tagged_sha256(tag, msg)
268282
if result is None:
269283
return 0
@@ -276,12 +290,15 @@ def secp256k1_ec_pubkey_serialize(self, ctx, output, outlen, pubkey, flags):
276290
try:
277291
if isinstance(pubkey, FFI.CData):
278292
pubkey = pubkey._data
293+
if isinstance(output, FFI.CData):
294+
output = output._data
279295
if isinstance(outlen, FFI.CData):
280296
outlen = outlen._data
281297
result = usecp256k1.ec_pubkey_serialize(pubkey, flags)
282298
if result is None:
283299
return 0
284-
output[:outlen[0]] = result
300+
output[:len(result)] = result
301+
outlen[0] = len(result)
285302
return 1
286303
except (ValueError, AttributeError):
287304
return 0
@@ -405,6 +422,8 @@ def secp256k1_ecdh(self, ctx, output, pubkey, seckey, hashfn=FFI.NULL, hasharg=F
405422
try:
406423
if isinstance(pubkey, FFI.CData):
407424
pubkey = pubkey._data
425+
if isinstance(output, FFI.CData):
426+
output = output._data
408427
result = usecp256k1.ecdh(pubkey, seckey)
409428
if result is None:
410429
return 0
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
try:
2+
import _thread
3+
except ImportError:
4+
_thread = None
5+
6+
class Lock:
7+
def __init__(self):
8+
self._lock = _thread.allocate_lock() if _thread else None
9+
10+
def __enter__(self):
11+
if self._lock:
12+
self._lock.acquire()
13+
return self
14+
15+
def __exit__(self, exc_type, exc_val, exc_tb):
16+
if self._lock:
17+
self._lock.release()
18+
19+
def acquire(self):
20+
if self._lock:
21+
return self._lock.acquire()
22+
return True
23+
24+
def release(self):
25+
if self._lock:
26+
self._lock.release()

0 commit comments

Comments
 (0)