@@ -174,7 +174,9 @@ isal_zlib_adler32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
174
174
return NULL ;
175
175
}
176
176
}
177
+ Py_BEGIN_ALLOW_THREADS
177
178
value = isal_adler32 (value , data .buf , (uint64_t )data .len );
179
+ Py_END_ALLOW_THREADS
178
180
return_value = PyLong_FromUnsignedLong (value & 0xffffffffU );
179
181
PyBuffer_Release (& data );
180
182
return return_value ;
@@ -219,7 +221,9 @@ isal_zlib_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
219
221
return NULL ;
220
222
}
221
223
}
224
+ Py_BEGIN_ALLOW_THREADS
222
225
value = crc32_gzip_refl (value , data .buf , (uint64_t )data .len );
226
+ Py_END_ALLOW_THREADS
223
227
return_value = PyLong_FromUnsignedLong (value & 0xffffffffU );
224
228
PyBuffer_Release (& data );
225
229
return return_value ;
@@ -325,6 +329,7 @@ typedef struct
325
329
uint8_t * level_buf ;
326
330
PyObject * zdict ;
327
331
int is_initialised ;
332
+ PyThread_type_lock lock ;
328
333
// isal_zstream should be at the bottom as it contains buffers inside the struct.
329
334
struct isal_zstream zst ;
330
335
} compobject ;
@@ -334,6 +339,7 @@ Comp_dealloc(compobject *self)
334
339
{
335
340
if (self -> is_initialised && self -> level_buf != NULL )
336
341
PyMem_Free (self -> level_buf );
342
+ PyThread_free_lock (self -> lock );
337
343
Py_XDECREF (self -> zdict );
338
344
Py_TYPE (self )-> tp_free ((PyObject * )self );
339
345
}
@@ -348,6 +354,12 @@ newcompobject()
348
354
self -> is_initialised = 0 ;
349
355
self -> zdict = NULL ;
350
356
self -> level_buf = NULL ;
357
+ self -> lock = PyThread_allocate_lock ();
358
+ if (self -> lock == NULL ) {
359
+ Py_DECREF (self );
360
+ PyErr_SetString (PyExc_MemoryError , "Unable to allocate lock" );
361
+ return NULL ;
362
+ }
351
363
return self ;
352
364
}
353
365
@@ -361,13 +373,15 @@ typedef struct
361
373
int is_initialised ;
362
374
int method_set ;
363
375
char eof ;
376
+ PyThread_type_lock lock ;
364
377
// inflate_state should be at the bottom as it contains buffers inside the struct.
365
378
struct inflate_state zst ;
366
379
} decompobject ;
367
380
368
381
static void
369
382
Decomp_dealloc (decompobject * self )
370
383
{
384
+ PyThread_free_lock (self -> lock );
371
385
Py_XDECREF (self -> unused_data );
372
386
Py_XDECREF (self -> unconsumed_tail );
373
387
Py_XDECREF (self -> zdict );
@@ -419,6 +433,12 @@ newdecompobject()
419
433
if (self -> unconsumed_tail == NULL ) {
420
434
Py_DECREF (self );
421
435
return NULL ;
436
+ }
437
+ self -> lock = PyThread_allocate_lock ();
438
+ if (self -> lock == NULL ) {
439
+ Py_DECREF (self );
440
+ PyErr_SetString (PyExc_MemoryError , "Unable to allocate lock" );
441
+ return NULL ;
422
442
}
423
443
return self ;
424
444
}
@@ -562,6 +582,8 @@ isal_zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
562
582
Py_ssize_t ibuflen , obuflen = DEF_BUF_SIZE ;
563
583
int err ;
564
584
585
+ ENTER_ZLIB (self );
586
+
565
587
self -> zst .next_in = data -> buf ;
566
588
ibuflen = data -> len ;
567
589
@@ -573,7 +595,9 @@ isal_zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
573
595
if (obuflen < 0 )
574
596
goto error ;
575
597
598
+ Py_BEGIN_ALLOW_THREADS
576
599
err = isal_deflate (& self -> zst );
600
+ Py_END_ALLOW_THREADS
577
601
578
602
if (err != COMP_OK ) {
579
603
isal_deflate_error (err );
@@ -591,6 +615,7 @@ isal_zlib_Compress_compress_impl(compobject *self, Py_buffer *data)
591
615
error :
592
616
Py_CLEAR (RetVal );
593
617
success :
618
+ LEAVE_ZLIB (self );
594
619
return RetVal ;
595
620
}
596
621
@@ -668,13 +693,16 @@ isal_zlib_Decompress_decompress_impl(decompobject *self, Py_buffer *data,
668
693
}
669
694
self -> method_set = 1 ;
670
695
}
671
- self -> zst .next_in = data -> buf ;
672
- ibuflen = data -> len ;
673
696
674
697
/* limit amount of data allocated to max_length */
675
698
if (max_length && obuflen > max_length )
676
699
obuflen = max_length ;
677
700
701
+ ENTER_ZLIB (self );
702
+
703
+ self -> zst .next_in = data -> buf ;
704
+ ibuflen = data -> len ;
705
+
678
706
do {
679
707
arrange_input_buffer (& (self -> zst .avail_in ), & ibuflen );
680
708
@@ -693,7 +721,10 @@ isal_zlib_Decompress_decompress_impl(decompobject *self, Py_buffer *data,
693
721
goto abort ;
694
722
}
695
723
724
+ Py_BEGIN_ALLOW_THREADS
696
725
err = isal_inflate (& self -> zst );
726
+ Py_END_ALLOW_THREADS
727
+
697
728
if (err != ISAL_DECOMP_OK ){
698
729
isal_inflate_error (err );
699
730
goto abort ;
@@ -719,6 +750,7 @@ isal_zlib_Decompress_decompress_impl(decompobject *self, Py_buffer *data,
719
750
abort :
720
751
Py_CLEAR (RetVal );
721
752
success :
753
+ LEAVE_ZLIB (self );
722
754
return RetVal ;
723
755
}
724
756
@@ -732,8 +764,12 @@ isal_zlib_Compress_flush_impl(compobject *self, int mode)
732
764
/* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
733
765
doing any work at all; just return an empty string. */
734
766
if (mode == Z_NO_FLUSH ) {
735
- return PyBytes_FromStringAndSize (NULL , 0 );
736
- } else if (mode == Z_FINISH ) {
767
+ return PyBytes_FromStringAndSize (NULL , 0 );
768
+ }
769
+
770
+ ENTER_ZLIB (self );
771
+
772
+ if (mode == Z_FINISH ) {
737
773
self -> zst .flush = FULL_FLUSH ;
738
774
self -> zst .end_of_stream = 1 ;
739
775
} else if (mode == Z_FULL_FLUSH ){
@@ -756,7 +792,9 @@ isal_zlib_Compress_flush_impl(compobject *self, int mode)
756
792
goto error ;
757
793
}
758
794
795
+ Py_BEGIN_ALLOW_THREADS
759
796
err = isal_deflate (& self -> zst );
797
+ Py_END_ALLOW_THREADS
760
798
761
799
if (err != COMP_OK ) {
762
800
isal_deflate_error (err );
@@ -784,6 +822,7 @@ isal_zlib_Compress_flush_impl(compobject *self, int mode)
784
822
Py_CLEAR (RetVal );
785
823
786
824
error :
825
+ LEAVE_ZLIB (self );
787
826
return RetVal ;
788
827
}
789
828
@@ -800,7 +839,10 @@ isal_zlib_Decompress_flush_impl(decompobject *self, Py_ssize_t length)
800
839
return NULL ;
801
840
}
802
841
842
+ ENTER_ZLIB (self );
843
+
803
844
if (PyObject_GetBuffer (self -> unconsumed_tail , & data , PyBUF_SIMPLE ) == -1 ) {
845
+ LEAVE_ZLIB (self );
804
846
return NULL ;
805
847
}
806
848
@@ -817,7 +859,9 @@ isal_zlib_Decompress_flush_impl(decompobject *self, Py_ssize_t length)
817
859
if (length < 0 )
818
860
goto abort ;
819
861
862
+ Py_BEGIN_ALLOW_THREADS
820
863
err = isal_inflate (& self -> zst );
864
+ Py_END_ALLOW_THREADS
821
865
822
866
if (err != ISAL_DECOMP_OK ) {
823
867
isal_inflate_error (err );
@@ -846,6 +890,7 @@ isal_zlib_Decompress_flush_impl(decompobject *self, Py_ssize_t length)
846
890
Py_CLEAR (RetVal );
847
891
success :
848
892
PyBuffer_Release (& data );
893
+ LEAVE_ZLIB (self );
849
894
return RetVal ;
850
895
}
851
896
0 commit comments