1
1
#include <common.h>
2
2
#include <streaming_compress.h>
3
+ #include <ruby/thread.h>
3
4
4
5
struct streaming_compress_t {
5
6
ZSTD_CCtx * ctx ;
@@ -76,6 +77,30 @@ rb_streaming_compress_initialize(int argc, VALUE *argv, VALUE obj)
76
77
: (FIX2INT((val))))
77
78
#define ARG_CONTINUE (val ) FIXNUMARG((val), ZSTD_e_continue)
78
79
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
+
79
104
static VALUE
80
105
no_compress (struct streaming_compress_t * sc , ZSTD_EndDirective endOp )
81
106
{
@@ -86,7 +111,7 @@ no_compress(struct streaming_compress_t* sc, ZSTD_EndDirective endOp)
86
111
do {
87
112
ZSTD_outBuffer output = { (void * )output_data , sc -> buf_size , 0 };
88
113
89
- size_t const ret = ZSTD_compressStream2 (sc -> ctx , & output , & input , endOp );
114
+ size_t const ret = compressStream2 (sc -> ctx , & output , & input , endOp );
90
115
if (ZSTD_isError (ret )) {
91
116
rb_raise (rb_eRuntimeError , "flush error error code: %s" , ZSTD_getErrorName (ret ));
92
117
}
@@ -109,7 +134,7 @@ rb_streaming_compress_compress(VALUE obj, VALUE src)
109
134
VALUE result = rb_str_new (0 , 0 );
110
135
while (input .pos < input .size ) {
111
136
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 );
113
138
if (ZSTD_isError (ret )) {
114
139
rb_raise (rb_eRuntimeError , "compress error error code: %s" , ZSTD_getErrorName (ret ));
115
140
}
@@ -132,7 +157,7 @@ rb_streaming_compress_addstr(VALUE obj, VALUE src)
132
157
133
158
while (input .pos < input .size ) {
134
159
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 );
136
161
if (ZSTD_isError (result )) {
137
162
rb_raise (rb_eRuntimeError , "compress error error code: %s" , ZSTD_getErrorName (result ));
138
163
}
0 commit comments