@@ -97,20 +97,11 @@ OutputBuffer_OnError(_BlocksOutputBuffer *buffer)
9797#endif /* ! BZ_CONFIG_ERROR */
9898
9999
100- #define ACQUIRE_LOCK (obj ) do { \
101- if (!PyThread_acquire_lock((obj)->lock, 0)) { \
102- Py_BEGIN_ALLOW_THREADS \
103- PyThread_acquire_lock((obj)->lock, 1); \
104- Py_END_ALLOW_THREADS \
105- } } while (0)
106- #define RELEASE_LOCK (obj ) PyThread_release_lock((obj)->lock)
107-
108-
109100typedef struct {
110101 PyObject_HEAD
111102 bz_stream bzs ;
112103 int flushed ;
113- PyThread_type_lock lock ;
104+ PyMutex mutex ;
114105} BZ2Compressor ;
115106
116107typedef struct {
@@ -126,7 +117,7 @@ typedef struct {
126117 separately. Conversion and looping is encapsulated in
127118 decompress_buf() */
128119 size_t bzs_avail_in_real ;
129- PyThread_type_lock lock ;
120+ PyMutex mutex ;
130121} BZ2Decompressor ;
131122
132123#define _BZ2Compressor_CAST (op ) ((BZ2Compressor *)(op))
@@ -271,12 +262,12 @@ _bz2_BZ2Compressor_compress_impl(BZ2Compressor *self, Py_buffer *data)
271262{
272263 PyObject * result = NULL ;
273264
274- ACQUIRE_LOCK ( self );
265+ PyMutex_Lock ( & self -> mutex );
275266 if (self -> flushed )
276267 PyErr_SetString (PyExc_ValueError , "Compressor has been flushed" );
277268 else
278269 result = compress (self , data -> buf , data -> len , BZ_RUN );
279- RELEASE_LOCK ( self );
270+ PyMutex_Unlock ( & self -> mutex );
280271 return result ;
281272}
282273
@@ -296,14 +287,14 @@ _bz2_BZ2Compressor_flush_impl(BZ2Compressor *self)
296287{
297288 PyObject * result = NULL ;
298289
299- ACQUIRE_LOCK ( self );
290+ PyMutex_Lock ( & self -> mutex );
300291 if (self -> flushed )
301292 PyErr_SetString (PyExc_ValueError , "Repeated call to flush()" );
302293 else {
303294 self -> flushed = 1 ;
304295 result = compress (self , NULL , 0 , BZ_FINISH );
305296 }
306- RELEASE_LOCK ( self );
297+ PyMutex_Unlock ( & self -> mutex );
307298 return result ;
308299}
309300
@@ -357,13 +348,7 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
357348 return NULL ;
358349 }
359350
360- self -> lock = PyThread_allocate_lock ();
361- if (self -> lock == NULL ) {
362- Py_DECREF (self );
363- PyErr_SetString (PyExc_MemoryError , "Unable to allocate lock" );
364- return NULL ;
365- }
366-
351+ self -> mutex = (PyMutex ){0 };
367352 self -> bzs .opaque = NULL ;
368353 self -> bzs .bzalloc = BZ2_Malloc ;
369354 self -> bzs .bzfree = BZ2_Free ;
@@ -383,9 +368,6 @@ BZ2Compressor_dealloc(PyObject *op)
383368{
384369 BZ2Compressor * self = _BZ2Compressor_CAST (op );
385370 BZ2_bzCompressEnd (& self -> bzs );
386- if (self -> lock != NULL ) {
387- PyThread_free_lock (self -> lock );
388- }
389371 PyTypeObject * tp = Py_TYPE (self );
390372 tp -> tp_free ((PyObject * )self );
391373 Py_DECREF (tp );
@@ -619,12 +601,12 @@ _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
619601{
620602 PyObject * result = NULL ;
621603
622- ACQUIRE_LOCK ( self );
604+ PyMutex_Lock ( & self -> mutex );
623605 if (self -> eof )
624606 PyErr_SetString (PyExc_EOFError , "End of stream already reached" );
625607 else
626608 result = decompress (self , data -> buf , data -> len , max_length );
627- RELEASE_LOCK ( self );
609+ PyMutex_Unlock ( & self -> mutex );
628610 return result ;
629611}
630612
@@ -650,13 +632,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
650632 return NULL ;
651633 }
652634
653- self -> lock = PyThread_allocate_lock ();
654- if (self -> lock == NULL ) {
655- Py_DECREF (self );
656- PyErr_SetString (PyExc_MemoryError , "Unable to allocate lock" );
657- return NULL ;
658- }
659-
635+ self -> mutex = (PyMutex ){0 };
660636 self -> needs_input = 1 ;
661637 self -> bzs_avail_in_real = 0 ;
662638 self -> input_buffer = NULL ;
@@ -684,9 +660,6 @@ BZ2Decompressor_dealloc(PyObject *op)
684660 }
685661 BZ2_bzDecompressEnd (& self -> bzs );
686662 Py_CLEAR (self -> unused_data );
687- if (self -> lock != NULL ) {
688- PyThread_free_lock (self -> lock );
689- }
690663
691664 PyTypeObject * tp = Py_TYPE (self );
692665 tp -> tp_free ((PyObject * )self );
0 commit comments