11#include <common.h>
22#include <streaming_compress.h>
3+ #include <ruby/thread.h>
34
45struct streaming_compress_t {
56 ZSTD_CCtx * ctx ;
@@ -76,6 +77,30 @@ rb_streaming_compress_initialize(int argc, VALUE *argv, VALUE obj)
7677 : (FIX2INT((val))))
7778#define ARG_CONTINUE (val ) FIXNUMARG((val), ZSTD_e_continue)
7879
80+ struct compress_stream_nogvl_t {
81+ ZSTD_CCtx * ctx ;
82+ ZSTD_outBuffer * output ;
83+ ZSTD_inBuffer * input ;
84+ ZSTD_EndDirective endOp ;
85+ size_t ret ;
86+ };
87+
88+ static void *
89+ compressStream2_nogvl (void * arg )
90+ {
91+ struct compress_stream_nogvl_t * params = arg ;
92+ params -> ret = ZSTD_compressStream2 (params -> ctx , params -> output , params -> input , params -> endOp );
93+ return NULL ;
94+ }
95+
96+ static size_t
97+ compressStream2 (ZSTD_CCtx * ctx , ZSTD_outBuffer * output , ZSTD_inBuffer * input , ZSTD_EndDirective endOp )
98+ {
99+ struct compress_stream_nogvl_t params = { ctx , output , input , endOp , 0 };
100+ rb_thread_call_without_gvl (compressStream2_nogvl , & params , NULL , NULL );
101+ return params .ret ;
102+ }
103+
79104static VALUE
80105no_compress (struct streaming_compress_t * sc , ZSTD_EndDirective endOp )
81106{
@@ -86,7 +111,7 @@ no_compress(struct streaming_compress_t* sc, ZSTD_EndDirective endOp)
86111 do {
87112 ZSTD_outBuffer output = { (void * )output_data , sc -> buf_size , 0 };
88113
89- size_t const ret = ZSTD_compressStream2 (sc -> ctx , & output , & input , endOp );
114+ size_t const ret = compressStream2 (sc -> ctx , & output , & input , endOp );
90115 if (ZSTD_isError (ret )) {
91116 rb_raise (rb_eRuntimeError , "flush error error code: %s" , ZSTD_getErrorName (ret ));
92117 }
@@ -109,7 +134,7 @@ rb_streaming_compress_compress(VALUE obj, VALUE src)
109134 VALUE result = rb_str_new (0 , 0 );
110135 while (input .pos < input .size ) {
111136 ZSTD_outBuffer output = { (void * )output_data , sc -> buf_size , 0 };
112- size_t const ret = ZSTD_compressStream2 (sc -> ctx , & output , & input , ZSTD_e_continue );
137+ size_t const ret = compressStream2 (sc -> ctx , & output , & input , ZSTD_e_continue );
113138 if (ZSTD_isError (ret )) {
114139 rb_raise (rb_eRuntimeError , "compress error error code: %s" , ZSTD_getErrorName (ret ));
115140 }
@@ -132,7 +157,7 @@ rb_streaming_compress_addstr(VALUE obj, VALUE src)
132157
133158 while (input .pos < input .size ) {
134159 ZSTD_outBuffer output = { (void * )output_data , sc -> buf_size , 0 };
135- size_t const result = ZSTD_compressStream2 (sc -> ctx , & output , & input , ZSTD_e_continue );
160+ size_t const result = compressStream2 (sc -> ctx , & output , & input , ZSTD_e_continue );
136161 if (ZSTD_isError (result )) {
137162 rb_raise (rb_eRuntimeError , "compress error error code: %s" , ZSTD_getErrorName (result ));
138163 }
0 commit comments