Skip to content

Commit 5a02eac

Browse files
committed
Reduce ensure nesting
1 parent 34320bd commit 5a02eac

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

ext/zlib/zlib.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,12 @@ zstream_run_try(VALUE value_arg)
11001100
int err;
11011101
VALUE old_input = Qnil;
11021102

1103+
/* Cannot start zstream while it is in progress. */
1104+
if (z->flags & ZSTREAM_IN_PROGRESS) {
1105+
rb_raise(cInProgressError, "zlib stream is in progress");
1106+
}
1107+
z->flags |= ZSTREAM_IN_PROGRESS;
1108+
11031109
if (NIL_P(z->input) && len == 0) {
11041110
z->stream.next_in = (Bytef*)"";
11051111
z->stream.avail_in = 0;
@@ -1167,35 +1173,18 @@ zstream_run_try(VALUE value_arg)
11671173
rb_str_resize(old_input, 0);
11681174
}
11691175

1170-
if (args->jump_state)
1171-
rb_jump_tag(args->jump_state);
1172-
11731176
return Qnil;
11741177
}
11751178

11761179
static VALUE
11771180
zstream_run_ensure(VALUE value_arg)
11781181
{
11791182
struct zstream_run_args *args = (struct zstream_run_args *)value_arg;
1183+
struct zstream *z = args->z;
11801184

11811185
/* Remove ZSTREAM_IN_PROGRESS flag to signal that this zstream is not in use. */
1182-
args->z->flags &= ~ZSTREAM_IN_PROGRESS;
1183-
1184-
return Qnil;
1185-
}
1186-
1187-
static VALUE
1188-
zstream_run_synchronized(VALUE value_arg)
1189-
{
1190-
struct zstream_run_args *args = (struct zstream_run_args *)value_arg;
1191-
1192-
/* Cannot start zstream while it is in progress. */
1193-
if (args->z->flags & ZSTREAM_IN_PROGRESS) {
1194-
rb_raise(cInProgressError, "zlib stream is in progress");
1195-
}
1196-
args->z->flags |= ZSTREAM_IN_PROGRESS;
1197-
1198-
rb_ensure(zstream_run_try, value_arg, zstream_run_ensure, value_arg);
1186+
z->flags &= ~ZSTREAM_IN_PROGRESS;
1187+
rb_mutex_unlock(z->mutex);
11991188

12001189
return Qnil;
12011190
}
@@ -1212,7 +1201,10 @@ zstream_run(struct zstream *z, Bytef *src, long len, int flush)
12121201
.jump_state = 0,
12131202
.stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p(),
12141203
};
1215-
rb_mutex_synchronize(z->mutex, zstream_run_synchronized, (VALUE)&args);
1204+
rb_mutex_lock(z->mutex);
1205+
rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args);
1206+
if (args.jump_state)
1207+
rb_jump_tag(args.jump_state);
12161208
}
12171209

12181210
static VALUE

0 commit comments

Comments
 (0)