22#define ZSTD_RUBY_H 1
33
44#include <ruby.h>
5+ #ifdef HAVE_RUBY_THREAD_H
6+ #include <ruby/thread.h>
7+ #endif
8+ #include <stdbool.h>
59#include "./libzstd/zstd.h"
610
711static int convert_compression_level (VALUE compression_level_value )
@@ -12,11 +16,6 @@ static int convert_compression_level(VALUE compression_level_value)
1216 return NUM2INT (compression_level_value );
1317}
1418
15- static size_t zstd_compress (ZSTD_CCtx * const ctx , ZSTD_outBuffer * output , ZSTD_inBuffer * input , ZSTD_EndDirective endOp )
16- {
17- return ZSTD_compressStream2 (ctx , output , input , endOp );
18- }
19-
2019static void set_compress_params (ZSTD_CCtx * const ctx , VALUE level_from_args , VALUE kwargs )
2120{
2221 ID kwargs_keys [2 ];
@@ -45,6 +44,36 @@ static void set_compress_params(ZSTD_CCtx* const ctx, VALUE level_from_args, VAL
4544 }
4645}
4746
47+ struct compress_params {
48+ ZSTD_CCtx * ctx ;
49+ ZSTD_outBuffer * output ;
50+ ZSTD_inBuffer * input ;
51+ ZSTD_EndDirective endOp ;
52+ size_t ret ;
53+ };
54+
55+ static void * compress_wrapper (void * args )
56+ {
57+ struct compress_params * params = args ;
58+ params -> ret = ZSTD_compressStream2 (params -> ctx , params -> output , params -> input , params -> endOp );
59+ return NULL ;
60+ }
61+
62+ static size_t zstd_compress (ZSTD_CCtx * const ctx , ZSTD_outBuffer * output , ZSTD_inBuffer * input , ZSTD_EndDirective endOp , bool gvl )
63+ {
64+ #ifdef HAVE_RUBY_THREAD_H
65+ if (gvl ) {
66+ return ZSTD_compressStream2 (ctx , output , input , endOp );
67+ } else {
68+ struct compress_params params = { ctx , output , input , endOp };
69+ rb_thread_call_without_gvl (compress_wrapper , & params , NULL , NULL );
70+ return params .ret ;
71+ }
72+ #else
73+ return ZSTD_compressStream2 (ctx , output , input , endOp );
74+ #endif
75+ }
76+
4877static void set_decompress_params (ZSTD_DCtx * const dctx , VALUE kwargs )
4978{
5079 ID kwargs_keys [1 ];
@@ -63,4 +92,33 @@ static void set_decompress_params(ZSTD_DCtx* const dctx, VALUE kwargs)
6392 }
6493}
6594
95+ struct decompress_params {
96+ ZSTD_DCtx * dctx ;
97+ ZSTD_outBuffer * output ;
98+ ZSTD_inBuffer * input ;
99+ size_t ret ;
100+ };
101+
102+ static void * decompress_wrapper (void * args )
103+ {
104+ struct decompress_params * params = args ;
105+ params -> ret = ZSTD_decompressStream (params -> dctx , params -> output , params -> input );
106+ return NULL ;
107+ }
108+
109+ static size_t zstd_decompress (ZSTD_DCtx * const dctx , ZSTD_outBuffer * output , ZSTD_inBuffer * input , bool gvl )
110+ {
111+ #ifdef HAVE_RUBY_THREAD_H
112+ if (gvl ) {
113+ return ZSTD_decompressStream (dctx , output , input );
114+ } else {
115+ struct decompress_params params = { dctx , output , input };
116+ rb_thread_call_without_gvl (decompress_wrapper , & params , NULL , NULL );
117+ return params .ret ;
118+ }
119+ #else
120+ return ZSTD_decompressStream (dctx , output , input );
121+ #endif
122+ }
123+
66124#endif /* ZSTD_RUBY_H */
0 commit comments