22
33
44local ffi = require " ffi"
5+ local jit = require " jit"
56local base = require " resty.core.base"
7+ local ffi_cast = ffi .cast
68
79
810local C = ffi .C
9- local ffi_new = ffi .new
1011local new_tab = base .new_tab
1112local subsystem = ngx .config .subsystem
13+ local get_string_buf = base .get_string_buf
14+ local get_size_ptr = base .get_size_ptr
1215
1316
1417local ngx_lua_ffi_worker_id
1518local ngx_lua_ffi_worker_pid
1619local ngx_lua_ffi_worker_pids
1720local ngx_lua_ffi_worker_count
1821local ngx_lua_ffi_worker_exiting
22+ local ffi_intp_type = ffi .typeof (" int *" )
23+ local ffi_int_size = ffi .sizeof (" int" )
1924
2025
2126ngx .worker = new_tab (0 , 4 )
@@ -25,31 +30,42 @@ if subsystem == "http" then
2530 ffi .cdef [[
2631 int ngx_http_lua_ffi_worker_id (void );
2732 int ngx_http_lua_ffi_worker_pid (void );
28- int ngx_http_lua_ffi_worker_pids (int * pids , size_t * pids_len );
2933 int ngx_http_lua_ffi_worker_count (void );
3034 int ngx_http_lua_ffi_worker_exiting (void );
3135 ]]
3236
3337 ngx_lua_ffi_worker_id = C .ngx_http_lua_ffi_worker_id
3438 ngx_lua_ffi_worker_pid = C .ngx_http_lua_ffi_worker_pid
35- ngx_lua_ffi_worker_pids = C .ngx_http_lua_ffi_worker_pids
3639 ngx_lua_ffi_worker_count = C .ngx_http_lua_ffi_worker_count
3740 ngx_lua_ffi_worker_exiting = C .ngx_http_lua_ffi_worker_exiting
41+ if jit .os ~= " Windows" then
42+ ffi .cdef [[
43+ int ngx_http_lua_ffi_worker_pids (int * pids , size_t * pids_len );
44+ ]]
45+
46+ ngx_lua_ffi_worker_pids = C .ngx_http_lua_ffi_worker_pids
47+ end
3848
3949elseif subsystem == " stream" then
4050 ffi .cdef [[
4151 int ngx_stream_lua_ffi_worker_id (void );
4252 int ngx_stream_lua_ffi_worker_pid (void );
43- int ngx_stream_lua_ffi_worker_pids (int * pids , size_t * pids_len );
4453 int ngx_stream_lua_ffi_worker_count (void );
4554 int ngx_stream_lua_ffi_worker_exiting (void );
4655 ]]
4756
4857 ngx_lua_ffi_worker_id = C .ngx_stream_lua_ffi_worker_id
4958 ngx_lua_ffi_worker_pid = C .ngx_stream_lua_ffi_worker_pid
50- ngx_lua_ffi_worker_pids = C .ngx_stream_lua_ffi_worker_pids
5159 ngx_lua_ffi_worker_count = C .ngx_stream_lua_ffi_worker_count
5260 ngx_lua_ffi_worker_exiting = C .ngx_stream_lua_ffi_worker_exiting
61+
62+ if jit .os ~= " Windows" then
63+ ffi .cdef [[
64+ int ngx_stream_lua_ffi_worker_pids (int * pids , size_t * pids_len );
65+ ]]
66+
67+ ngx_lua_ffi_worker_pids = C .ngx_stream_lua_ffi_worker_pids
68+ end
5369end
5470
5571
@@ -62,23 +78,32 @@ function ngx.worker.pid()
6278 return ngx_lua_ffi_worker_pid ()
6379end
6480
65- local size_ptr = ffi_new (" size_t[1]" )
66- local pids_ptr = ffi_new (" int[1024]" ) -- using NGX_MAX_PROCESSES
6781
68- function ngx .worker .pids ()
69- if ngx .get_phase () == " init" or ngx .get_phase () == " init_worker" then
70- return nil , " API disabled in the current context"
71- end
82+ if jit .os ~= " Windows" then
83+ function ngx .worker .pids ()
84+ if ngx .get_phase () == " init" or ngx .get_phase () == " init_worker" then
85+ return nil , " API disabled in the current context"
86+ end
87+
88+ local pids = {}
89+ local size_ptr = get_size_ptr ()
90+ local worker_cnt = ngx .worker .count ()
91+ if worker_cnt == 0 then
92+ return pids
93+ end
7294
73- local res = ngx_lua_ffi_worker_pids (pids_ptr , size_ptr )
95+ size_ptr [0 ] = worker_cnt
96+ local pids_ptr = get_string_buf (worker_cnt * ffi_int_size )
97+ local intp_buf = ffi_cast (ffi_intp_type , pids_ptr )
98+ local res = ngx_lua_ffi_worker_pids (intp_buf , size_ptr )
7499
75- local pids = {}
76- if res == 0 then
77- for i = 1 , tonumber ( size_ptr [ 0 ]) do
78- pids [ i ] = pids_ptr [ i - 1 ]
100+ if res == 0 then
101+ for i = 1 , tonumber ( size_ptr [ 0 ]) do
102+ pids [ i ] = intp_buf [ i - 1 ]
103+ end
79104 end
105+ return pids
80106 end
81- return pids
82107end
83108
84109function ngx .worker .id ()
0 commit comments