Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions hexhamming/python_hexhamming.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,24 @@ static PyObject * check_bytes_arrays_within_dist_wrapper(PyObject *self, PyObjec
return NULL;
}

int return_value = -1;

Py_BEGIN_ALLOW_THREADS

int res;
uint64_t number_of_elements = big_array_size / small_array_size;
uint8_t* pBig = big_array;
for (uint64_t i = 0; i < number_of_elements; i++, pBig += small_array_size) {
res = (int)ptr__hamming_distance_bytes(pBig, small_array, small_array_size, max_dist);
if (res == 1)
return Py_BuildValue("i", i);
if (res == 1) {
return_value = i;
break;
}
return Py_BuildValue("i", -1);
}

Py_END_ALLOW_THREADS

return Py_BuildValue("i", return_value);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions test/test_hexhamming.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,29 @@ def test_check_hexstrings_within_dist_bench(benchmark):
)
def test_check_bytes_arrays_within_dist_bench(benchmark, bytes1, bytes2, max_dist):
benchmark(check_bytes_arrays_within_dist, bytes1, bytes2, max_dist)


def test_check_bytes_arrays_within_dist_gil():
import threading
import time

def run_check():
big_array = b"\x00" * 16 * 1000000
small_array = b"\x00" * 16
max_dist = 0
result = check_bytes_arrays_within_dist(big_array, small_array, max_dist)
assert result == 0

def run_other_task():
for _ in range(5):
print("Running other task")
Comment thread
mrecachinas marked this conversation as resolved.
Outdated
time.sleep(0.1)

check_thread = threading.Thread(target=run_check)
other_task_thread = threading.Thread(target=run_other_task)

check_thread.start()
other_task_thread.start()

check_thread.join()
other_task_thread.join()