~ubuntu-branches/ubuntu/saucy/nginx/saucy-updates

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/src/ngx_http_lua_uthread.c

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry, Kartik Mistry, Cyril Lavier, Michael Lustfield
  • Date: 2012-12-18 10:29:18 UTC
  • mfrom: (4.2.56 sid)
  • Revision ID: package-import@ubuntu.com-20121218102918-dxtwj9vj89sbj8dz
Tags: 1.2.6-1
[ Kartik Mistry ]
* New upstream release.
* debian/nginx-common.nginx.init:
  + Used log_*_msg instead of echo for better init messages.
  + Added patch to check start-stop-daemon exit status, Thanks to
    Sergey B Kirpichev <skirpichev@gmail.com> (Closes: #695374).
* debian/po/ja.po:
  + Added new Japanese translation. Thanks to victory <victory.deb@gmail.com>
    (Closes: #692481).
* debian/po/pt_BR.po:
  + Added new Brazilian Portuguese translation. Thanks to
    Adriano Rafael Gomes <adrianorg@gmail.com> (Closes: #692481).

[ Cyril Lavier ]
* debian/rules
  + Added RealIP module in nginx-naxsi (Closes: #693302).
* debian/modules/nginx-cache-purge/
  + Updated nginx-cache-purge module with the 2.0 version.
* debian/modules/nginx-lua/
  + Updated nginx-lua module with the 0.7.8 version.
* debian/modules/nginx-echo/
  + Updated the nginx-echo module with the 0.41 version.
* debian/modules/headers-more-nginx-module/
  + Updated the Headers-more module with the 0.19 version.
* debian/modules/README.Modules-versions
  + Updated the current version of modules following the updates.

[ Michael Lustfield ]
* debian/conf/sites-available/default
  + Uncommented listen lines to make server block default.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef DDEBUG
 
2
#define DDEBUG 0
 
3
#endif
 
4
#include "ddebug.h"
 
5
 
 
6
 
 
7
#include "ngx_http_lua_uthread.h"
 
8
#include "ngx_http_lua_coroutine.h"
 
9
#include "ngx_http_lua_util.h"
 
10
#include "ngx_http_lua_probe.h"
 
11
 
 
12
 
 
13
#if 1
 
14
#undef ngx_http_lua_probe_info
 
15
#define ngx_http_lua_probe_info(msg)
 
16
#endif
 
17
 
 
18
 
 
19
static int ngx_http_lua_uthread_spawn(lua_State *L);
 
20
static int ngx_http_lua_uthread_wait(lua_State *L);
 
21
 
 
22
 
 
23
void
 
24
ngx_http_lua_inject_uthread_api(ngx_log_t *log, lua_State *L)
 
25
{
 
26
    /* new thread table */
 
27
    lua_newtable(L);
 
28
 
 
29
    lua_pushcfunction(L, ngx_http_lua_uthread_spawn);
 
30
    lua_setfield(L, -2, "spawn");
 
31
 
 
32
    lua_pushcfunction(L, ngx_http_lua_uthread_wait);
 
33
    lua_setfield(L, -2, "wait");
 
34
 
 
35
    lua_setfield(L, -2, "thread");
 
36
}
 
37
 
 
38
 
 
39
static int
 
40
ngx_http_lua_uthread_spawn(lua_State *L)
 
41
{
 
42
    int                           n;
 
43
    ngx_http_request_t           *r;
 
44
    ngx_http_lua_ctx_t           *ctx;
 
45
    ngx_http_lua_co_ctx_t        *coctx = NULL;
 
46
 
 
47
    n = lua_gettop(L);
 
48
 
 
49
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
 
50
    lua_rawget(L, LUA_GLOBALSINDEX);
 
51
    r = lua_touserdata(L, -1);
 
52
    lua_pop(L, 1);
 
53
 
 
54
    if (r == NULL) {
 
55
        return luaL_error(L, "no request found");
 
56
    }
 
57
 
 
58
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
 
59
    if (ctx == NULL) {
 
60
        return luaL_error(L, "no request ctx found");
 
61
    }
 
62
 
 
63
    ngx_http_lua_coroutine_create_helper(L, r, ctx, &coctx);
 
64
 
 
65
    /* anchor the newly created coroutine into the Lua registry */
 
66
 
 
67
    lua_pushlightuserdata(L, &ngx_http_lua_coroutines_key);
 
68
    lua_rawget(L, LUA_REGISTRYINDEX);
 
69
    lua_pushvalue(L, -2);
 
70
    coctx->co_ref = luaL_ref(L, -2);
 
71
    lua_pop(L, 1);
 
72
 
 
73
    if (n > 1) {
 
74
        lua_replace(L, 1);
 
75
        lua_xmove(L, coctx->co, n - 1);
 
76
    }
 
77
 
 
78
    coctx->is_uthread = 1;
 
79
    ctx->uthreads++;
 
80
 
 
81
    coctx->co_status = NGX_HTTP_LUA_CO_RUNNING;
 
82
    ctx->co_op = NGX_HTTP_LUA_USER_THREAD_RESUME;
 
83
 
 
84
    ctx->cur_co_ctx->thread_spawn_yielded = 1;
 
85
 
 
86
    if (ngx_http_lua_post_thread(r, ctx, ctx->cur_co_ctx) != NGX_OK) {
 
87
        return luaL_error(L, "out of memory");
 
88
    }
 
89
 
 
90
    coctx->parent_co_ctx = ctx->cur_co_ctx;
 
91
    ctx->cur_co_ctx = coctx;
 
92
 
 
93
    ngx_http_lua_probe_user_thread_spawn(r, L, coctx->co);
 
94
 
 
95
    return lua_yield(L, 1);
 
96
}
 
97
 
 
98
 
 
99
static int
 
100
ngx_http_lua_uthread_wait(lua_State *L)
 
101
{
 
102
    int                          i, nargs, nrets;
 
103
    lua_State                   *sub_co;
 
104
    ngx_http_request_t          *r;
 
105
    ngx_http_lua_ctx_t          *ctx;
 
106
    ngx_http_lua_co_ctx_t       *coctx, *sub_coctx;
 
107
 
 
108
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
 
109
    lua_rawget(L, LUA_GLOBALSINDEX);
 
110
    r = lua_touserdata(L, -1);
 
111
    lua_pop(L, 1);
 
112
 
 
113
    if (r == NULL) {
 
114
        return luaL_error(L, "no request found");
 
115
    }
 
116
 
 
117
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
 
118
    if (ctx == NULL) {
 
119
        return luaL_error(L, "no request ctx found");
 
120
    }
 
121
 
 
122
    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
 
123
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
 
124
                               | NGX_HTTP_LUA_CONTEXT_CONTENT);
 
125
 
 
126
    coctx = ctx->cur_co_ctx;
 
127
 
 
128
    nargs = lua_gettop(L);
 
129
 
 
130
    for (i = 1; i <= nargs; i++) {
 
131
        sub_co = lua_tothread(L, i);
 
132
 
 
133
        luaL_argcheck(L, sub_co, i, "lua thread expected");
 
134
 
 
135
        sub_coctx = ngx_http_lua_get_co_ctx(sub_co, ctx);
 
136
        if (sub_coctx == NULL) {
 
137
            return luaL_error(L, "no co ctx found for the ngx.thread "
 
138
                              "instance given");
 
139
        }
 
140
 
 
141
        if (!sub_coctx->is_uthread) {
 
142
            return luaL_error(L, "attempt to wait on a coroutine that is "
 
143
                              "not a user thread");
 
144
        }
 
145
 
 
146
        if (sub_coctx->parent_co_ctx != coctx) {
 
147
            return luaL_error(L, "only the parent coroutine can wait on the "
 
148
                              "thread");
 
149
        }
 
150
 
 
151
        switch (sub_coctx->co_status) {
 
152
        case NGX_HTTP_LUA_CO_ZOMBIE:
 
153
 
 
154
            ngx_http_lua_probe_info("found zombie child");
 
155
 
 
156
            nrets = lua_gettop(sub_coctx->co);
 
157
 
 
158
            dd("child retval count: %d, %s: %s", n,
 
159
                    luaL_typename(sub_coctx->co, -1),
 
160
                    lua_tostring(sub_coctx->co, -1));
 
161
 
 
162
            if (nrets) {
 
163
                lua_xmove(sub_coctx->co, L, nrets);
 
164
            }
 
165
 
 
166
#if 1
 
167
            ngx_http_lua_del_thread(r, L, ctx, sub_coctx);
 
168
            ctx->uthreads--;
 
169
#endif
 
170
 
 
171
            return nrets;
 
172
 
 
173
        default:
 
174
            /* still alive */
 
175
            break;
 
176
        }
 
177
 
 
178
        ngx_http_lua_probe_user_thread_wait(L, sub_coctx->co);
 
179
        sub_coctx->waited_by_parent = 1;
 
180
    }
 
181
 
 
182
    return lua_yield(L, 0);
 
183
}
 
184