summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2024-10-24 19:25:49 +0900
committergit <[email protected]>2024-10-24 10:30:37 +0000
commit4755d28f9b2aba730563547ae44abcc5af0bc46f (patch)
tree22b1aa1b9e3ec1608927ea7da321cfdd99896329
parentae67325dd00143fd87b10e258bf073d1df3b709e (diff)
[ruby/zlib] Reduce `ensure` nesting
https://github.com/ruby/zlib/commit/5a02eac37f
-rw-r--r--ext/zlib/zlib.c34
1 files changed, 13 insertions, 21 deletions
diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c
index e9535aaf4a..7b6cc4e7d4 100644
--- a/ext/zlib/zlib.c
+++ b/ext/zlib/zlib.c
@@ -1100,6 +1100,12 @@ zstream_run_try(VALUE value_arg)
int err;
VALUE old_input = Qnil;
+ /* Cannot start zstream while it is in progress. */
+ if (z->flags & ZSTREAM_IN_PROGRESS) {
+ rb_raise(cInProgressError, "zlib stream is in progress");
+ }
+ z->flags |= ZSTREAM_IN_PROGRESS;
+
if (NIL_P(z->input) && len == 0) {
z->stream.next_in = (Bytef*)"";
z->stream.avail_in = 0;
@@ -1167,9 +1173,6 @@ loop:
rb_str_resize(old_input, 0);
}
- if (args->jump_state)
- rb_jump_tag(args->jump_state);
-
return Qnil;
}
@@ -1177,25 +1180,11 @@ static VALUE
zstream_run_ensure(VALUE value_arg)
{
struct zstream_run_args *args = (struct zstream_run_args *)value_arg;
+ struct zstream *z = args->z;
/* Remove ZSTREAM_IN_PROGRESS flag to signal that this zstream is not in use. */
- args->z->flags &= ~ZSTREAM_IN_PROGRESS;
-
- return Qnil;
-}
-
-static VALUE
-zstream_run_synchronized(VALUE value_arg)
-{
- struct zstream_run_args *args = (struct zstream_run_args *)value_arg;
-
- /* Cannot start zstream while it is in progress. */
- if (args->z->flags & ZSTREAM_IN_PROGRESS) {
- rb_raise(cInProgressError, "zlib stream is in progress");
- }
- args->z->flags |= ZSTREAM_IN_PROGRESS;
-
- rb_ensure(zstream_run_try, value_arg, zstream_run_ensure, value_arg);
+ z->flags &= ~ZSTREAM_IN_PROGRESS;
+ rb_mutex_unlock(z->mutex);
return Qnil;
}
@@ -1212,7 +1201,10 @@ zstream_run(struct zstream *z, Bytef *src, long len, int flush)
.jump_state = 0,
.stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p(),
};
- rb_mutex_synchronize(z->mutex, zstream_run_synchronized, (VALUE)&args);
+ rb_mutex_lock(z->mutex);
+ rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args);
+ if (args.jump_state)
+ rb_jump_tag(args.jump_state);
}
static VALUE