summaryrefslogtreecommitdiff
path: root/jq.pyx
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2020-06-24 22:18:20 +0100
committerMichael Williamson <mike@zwobble.org>2020-06-24 22:18:20 +0100
commit5f7f1e8866d13f3ecb662cf814f0b599b82de09c (patch)
treecfc15124b87cb772a00f78231be5b2964eeeaaca /jq.pyx
parentfe7726f9168683d2aa8979b697e95f247c3df2fc (diff)
Make _JqStatePool thread-safe
Diffstat (limited to 'jq.pyx')
-rw-r--r--jq.pyx25
1 files changed, 15 insertions, 10 deletions
diff --git a/jq.pyx b/jq.pyx
index 1846cfd..f319c18 100644
--- a/jq.pyx
+++ b/jq.pyx
@@ -1,4 +1,5 @@
import json
+import threading
from cpython.bytes cimport PyBytes_AsString
@@ -119,27 +120,31 @@ _NO_VALUE = _EmptyValue()
cdef class _JqStatePool(object):
cdef jq_state* _jq_state
cdef object _program_bytes
+ cdef object _lock
def __cinit__(self, program_bytes):
self._program_bytes = program_bytes
self._jq_state = _compile(self._program_bytes)
+ self._lock = threading.Lock()
def __dealloc__(self):
jq_teardown(&self._jq_state)
cdef jq_state* acquire(self):
- if self._jq_state == NULL:
- return _compile(self._program_bytes)
- else:
- state = self._jq_state
- self._jq_state = NULL
- return state
+ with self._lock:
+ if self._jq_state == NULL:
+ return _compile(self._program_bytes)
+ else:
+ state = self._jq_state
+ self._jq_state = NULL
+ return state
cdef void release(self, jq_state* state):
- if self._jq_state == NULL:
- self._jq_state = state
- else:
- jq_teardown(&state)
+ with self._lock:
+ if self._jq_state == NULL:
+ self._jq_state = state
+ else:
+ jq_teardown(&state)
cdef class _Program(object):