Skip to content

Commit

Permalink
keep ictx consistent before and after run fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
pyama86 committed Nov 11, 2019
1 parent 8d8585c commit 0fb0860
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
26 changes: 15 additions & 11 deletions src/stream/ngx_stream_mruby_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ typedef struct {
mrb_state *mrb;
mrb_value *fiber;
ngx_stream_session_t *s;
ngx_int_t stream_status;
} ngx_stream_mrb_reentrant_t;

static mrb_value ngx_stream_mrb_run_fiber(mrb_state *mrb, mrb_value *fiber_proc, mrb_value *result)
static mrb_value ngx_stream_mrb_run_fiber(ngx_stream_session_t *s, mrb_state *mrb, mrb_value *fiber_proc,
mrb_value *result)
{
mrb_value resume_result = mrb_nil_value();
mrb_value aliving = mrb_false_value();
mrb_value handler_result = mrb_nil_value();
ngx_stream_mruby_ctx_t *ctx;

ngx_stream_session_t *s = ngx_mrb_get_session();
ctx = ngx_stream_mrb_get_module_ctx(mrb, s);
ctx->fiber_proc = fiber_proc;

Expand Down Expand Up @@ -72,19 +73,20 @@ mrb_value ngx_stream_mrb_start_fiber(ngx_stream_session_t *s, mrb_state *mrb, st
return mrb_false_value();
}

return ngx_stream_mrb_run_fiber(mrb, fiber_proc, result);
return ngx_stream_mrb_run_fiber(s, mrb, fiber_proc, result);
}

static ngx_int_t ngx_stream_mrb_post_fiber(ngx_stream_mrb_reentrant_t *re, ngx_stream_mruby_ctx_t *ctx)
{
int ai;
ai = mrb_gc_arena_save(re->mrb);

ngx_stream_mruby_internal_ctx_t *ictx = re->mrb->ud;
ictx->s = re->s;
ictx->stream_status = re->stream_status;

if (re->fiber != NULL) {
ngx_mrb_push_session(re->s);

if (mrb_test(ngx_stream_mrb_run_fiber(re->mrb, re->fiber, ctx->async_handler_result))) {
if (mrb_test(ngx_stream_mrb_run_fiber(re->s, re->mrb, re->fiber, ctx->async_handler_result))) {
mrb_gc_arena_restore(re->mrb, ai);
return NGX_DONE;
} else {
Expand Down Expand Up @@ -145,22 +147,24 @@ static mrb_value ngx_stream_mrb_async_sleep(mrb_state *mrb, mrb_value self)
mrb_int timer;
u_char *p;
ngx_stream_mrb_reentrant_t *re;
ngx_stream_session_t *s;
ngx_stream_mruby_ctx_t *ctx;
ngx_stream_mruby_internal_ctx_t *ictx;

mrb_get_args(mrb, "i", &timer);

if (timer <= 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "value of the timer must be a positive number");
}

s = ngx_mrb_get_session();
p = ngx_palloc(s->connection->pool, sizeof(ngx_event_t) + sizeof(ngx_stream_mrb_reentrant_t));
ictx = mrb->ud;
p = ngx_palloc(ictx->s->connection->pool, sizeof(ngx_event_t) + sizeof(ngx_stream_mrb_reentrant_t));
re = (ngx_stream_mrb_reentrant_t *)(p + sizeof(ngx_event_t));
re->mrb = mrb;
re->s = s;

ctx = ngx_stream_mrb_get_module_ctx(mrb, s);
re->s = ictx->s;
re->stream_status = ictx->stream_status;

ctx = ngx_stream_mrb_get_module_ctx(mrb, ictx->s);
re->fiber = ctx->fiber_proc;

// keeps the object from GC when can resume the fiber
Expand Down
13 changes: 0 additions & 13 deletions src/stream/ngx_stream_mruby_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,18 +678,6 @@ static ngx_int_t ngx_stream_mrb_run_conf(ngx_conf_t *cf, mrb_state *mrb, ngx_mrb
}
*/

ngx_stream_session_t *ngx_mruby_session = NULL;
ngx_int_t ngx_mrb_push_session(ngx_stream_session_t *s)
{
ngx_mruby_session = s;
return NGX_OK;
}

ngx_stream_session_t *ngx_mrb_get_session(void)
{
return ngx_mruby_session;
}

static ngx_int_t ngx_stream_mruby_handler(ngx_stream_session_t *s)
{
ngx_stream_mruby_srv_conf_t *mscf;
Expand All @@ -712,7 +700,6 @@ static ngx_int_t ngx_stream_mruby_handler(ngx_stream_session_t *s)
mrb_value *mrb_result = (mrb_value *)ngx_palloc(s->connection->pool, sizeof(mrb_value));
*mrb_result = mrb_nil_value();

ngx_mrb_push_session(s);
if (mrb_test(ngx_stream_mrb_start_fiber(s, mrb, mscf->code->proc, mrb_result))) {
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, "%s INFO %s:%d: already can resume this fiber", MODULE_NAME,
__func__, __LINE__);
Expand Down
2 changes: 0 additions & 2 deletions src/stream/ngx_stream_mruby_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,5 @@ typedef struct {

} ngx_stream_mruby_srv_conf_t;

ngx_int_t ngx_mrb_push_session(ngx_stream_session_t *s);
ngx_stream_session_t *ngx_mrb_get_session(void);
void ngx_stream_mruby_raise_error(mrb_state *mrb, mrb_value obj, ngx_stream_session_t *s);
#endif // NGX_STREAM_MRUBY_MODULE_H

0 comments on commit 0fb0860

Please sign in to comment.