Commit be40d060 authored by Vladimír Čunát's avatar Vladimír Čunát

move worker initialization to daemon/worker.c

Consequently, there's no use for worker_reserve in the header.
parent 29ee626a
......@@ -290,39 +290,6 @@ static void help(int argc, char *argv[])
" [rundir] Path to the working directory (default: .)\n");
}
static struct worker_ctx *init_worker(struct engine *engine, knot_mm_t *pool, int worker_id, int worker_count)
{
/* Load bindings */
engine_lualib(engine, "modules", lib_modules);
engine_lualib(engine, "net", lib_net);
engine_lualib(engine, "cache", lib_cache);
engine_lualib(engine, "event", lib_event);
engine_lualib(engine, "worker", lib_worker);
/* Create main worker. */
struct worker_ctx *worker = mm_alloc(pool, sizeof(*worker));
if(!worker) {
return NULL;
}
memset(worker, 0, sizeof(*worker));
worker->id = worker_id;
worker->count = worker_count;
worker->engine = engine;
worker_reserve(worker, MP_FREELIST_SIZE);
/* Register worker in Lua thread */
lua_pushlightuserdata(engine->L, worker);
lua_setglobal(engine->L, "__worker");
lua_getglobal(engine->L, "worker");
lua_pushnumber(engine->L, worker_id);
lua_setfield(engine->L, -2, "id");
lua_pushnumber(engine->L, getpid());
lua_setfield(engine->L, -2, "pid");
lua_pushnumber(engine->L, worker_count);
lua_setfield(engine->L, -2, "count");
lua_pop(engine->L, 1);
return worker;
}
static int run_worker(uv_loop_t *loop, struct engine *engine, fd_array_t *ipc_set, bool leader, int control_fd)
{
/* Control sockets or TTY */
......@@ -568,7 +535,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
/* Create worker */
struct worker_ctx *worker = init_worker(&engine, &pool, fork_id, forks);
struct worker_ctx *worker = worker_create(&engine, &pool, fork_id, forks);
if (!worker) {
kr_log_error("[system] not enough memory\n");
return EXIT_FAILURE;
......
......@@ -25,9 +25,12 @@
#include <malloc.h>
#endif
#include <assert.h>
#include <sys/types.h>
#include <unistd.h>
#include "lib/utils.h"
#include "lib/layer.h"
#include "daemon/worker.h"
#include "daemon/bindings.h"
#include "daemon/engine.h"
#include "daemon/io.h"
#include "daemon/tls.h"
......@@ -1010,7 +1013,8 @@ int worker_resolve(struct worker_ctx *worker, knot_pkt_t *query, unsigned option
return qr_task_step(task, NULL, query);
}
int worker_reserve(struct worker_ctx *worker, size_t ring_maxlen)
/** Reserve worker buffers */
static int worker_reserve(struct worker_ctx *worker, size_t ring_maxlen)
{
array_init(worker->pool_mp);
array_init(worker->pool_ioreq);
......@@ -1045,4 +1049,38 @@ void worker_reclaim(struct worker_ctx *worker)
map_clear(&worker->outgoing);
}
struct worker_ctx *worker_create(struct engine *engine, knot_mm_t *pool,
int worker_id, int worker_count)
{
/* Load bindings */
engine_lualib(engine, "modules", lib_modules);
engine_lualib(engine, "net", lib_net);
engine_lualib(engine, "cache", lib_cache);
engine_lualib(engine, "event", lib_event);
engine_lualib(engine, "worker", lib_worker);
/* Create main worker. */
struct worker_ctx *worker = mm_alloc(pool, sizeof(*worker));
if (!worker) {
return NULL;
}
memset(worker, 0, sizeof(*worker));
worker->id = worker_id;
worker->count = worker_count;
worker->engine = engine;
worker_reserve(worker, MP_FREELIST_SIZE);
/* Register worker in Lua thread */
lua_pushlightuserdata(engine->L, worker);
lua_setglobal(engine->L, "__worker");
lua_getglobal(engine->L, "worker");
lua_pushnumber(engine->L, worker_id);
lua_setfield(engine->L, -2, "id");
lua_pushnumber(engine->L, getpid());
lua_setfield(engine->L, -2, "pid");
lua_pushnumber(engine->L, worker_count);
lua_setfield(engine->L, -2, "count");
lua_pop(engine->L, 1);
return worker;
}
#undef DEBUG_MSG
......@@ -26,6 +26,10 @@ struct worker_ctx;
/** Worker callback */
typedef void (*worker_cb_t)(struct worker_ctx *worker, struct kr_request *req, void *baton);
/** Create and initialize the worker. */
struct worker_ctx *worker_create(struct engine *engine, knot_mm_t *pool,
int worker_id, int worker_count);
/**
* Process incoming packet (query or answer to subrequest).
* @return 0 or an error code
......@@ -55,9 +59,6 @@ int worker_end_tcp(struct worker_ctx *worker, uv_handle_t *handle);
int worker_resolve(struct worker_ctx *worker, knot_pkt_t *query, unsigned options,
worker_cb_t on_complete, void *baton);
/** Reserve worker buffers */
int worker_reserve(struct worker_ctx *worker, size_t ring_maxlen);
/** Collect worker mempools */
void worker_reclaim(struct worker_ctx *worker);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment