@@ -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 ;
@@ -382,10 +367,8 @@ static void
382367BZ2Compressor_dealloc (PyObject * op )
383368{
384369 BZ2Compressor * self = _BZ2Compressor_CAST (op );
370+ assert (!PyMutex_IsLocked (& self -> mutex ));
385371 BZ2_bzCompressEnd (& self -> bzs );
386- if (self -> lock != NULL ) {
387- PyThread_free_lock (self -> lock );
388- }
389372 PyTypeObject * tp = Py_TYPE (self );
390373 tp -> tp_free ((PyObject * )self );
391374 Py_DECREF (tp );
@@ -619,12 +602,12 @@ _bz2_BZ2Decompressor_decompress_impl(BZ2Decompressor *self, Py_buffer *data,
619602{
620603 PyObject * result = NULL ;
621604
622- ACQUIRE_LOCK ( self );
605+ PyMutex_Lock ( & self -> mutex );
623606 if (self -> eof )
624607 PyErr_SetString (PyExc_EOFError , "End of stream already reached" );
625608 else
626609 result = decompress (self , data -> buf , data -> len , max_length );
627- RELEASE_LOCK ( self );
610+ PyMutex_Unlock ( & self -> mutex );
628611 return result ;
629612}
630613
@@ -650,13 +633,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
650633 return NULL ;
651634 }
652635
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-
636+ self -> mutex = (PyMutex ){0 };
660637 self -> needs_input = 1 ;
661638 self -> bzs_avail_in_real = 0 ;
662639 self -> input_buffer = NULL ;
@@ -678,15 +655,13 @@ static void
678655BZ2Decompressor_dealloc (PyObject * op )
679656{
680657 BZ2Decompressor * self = _BZ2Decompressor_CAST (op );
658+ assert (!PyMutex_IsLocked (& self -> mutex ));
681659
682660 if (self -> input_buffer != NULL ) {
683661 PyMem_Free (self -> input_buffer );
684662 }
685663 BZ2_bzDecompressEnd (& self -> bzs );
686664 Py_CLEAR (self -> unused_data );
687- if (self -> lock != NULL ) {
688- PyThread_free_lock (self -> lock );
689- }
690665
691666 PyTypeObject * tp = Py_TYPE (self );
692667 tp -> tp_free ((PyObject * )self );
0 commit comments