Example #1
0
static int
qlsleep(lua_State *state) {
  int         second;
  qactor_t   *actor;
  qengine_t  *engine;
  qid_t       id;
  qltimer_t  *timer;

  second = (int)lua_tonumber(state, 1);
  if (second <= 0) {
    lua_pushnil(state);
    lua_pushliteral(state, "wrong param");
    return 2;
  }

  timer = new_timer(NULL, NULL, NULL);
  if (timer == NULL) {
    lua_pushnil(state);
    lua_pushliteral(state, "create timer error");
    return 2;
  }

  actor = qlua_get_actor(state);
  engine = qactor_get_engine(actor->aid);
  id = qtimer_add(engine, second * 1000, timer_handler,
                  free_timer, 0, timer);
  timer->state = state;
  timer->id = id;
  timer->engine = engine;
  timer->actor = actor;
  qdict_set_numdata(actor->timers, id, timer, engine_free_timer);

  return lua_yield(state, 0);
}
Example #2
0
int
qlogger_new(int thread_num, qthread_start_pt done) {
  qlog_init_free_list();
  logger = qcalloc(sizeof(qlogger_t));
  if (logger == NULL) {
    return QERROR;
  }

  if (pthread_key_create(&thread_log_key, log_key_destroy) < 0) {
    return QERROR;
  }

  logger->fd = -1;
  logger->log_size = 0;
  if (config.daemon) {
    qlogger_open_file();
  }
  logger->engine = qengine_new();
  if (logger->engine == NULL) {
    return QERROR;
  }
  qmailbox_init(&(logger->box), logger_msg_handler,
                logger->engine, logger);
  logger->box.done = logger_handle_msglist_done;

  logger->thread_num = thread_num;
  qlist_entry_init(&(logger->free_list));
  log_time_handler(NULL);
  qtimer_add(logger->engine, 1000, log_time_handler,
             NULL, 1000, NULL);
  logger->running = 0;
  logger->done = done;
  pthread_create(&logger->id, NULL,
                 logger_main, logger);

  return QOK;
}
Example #3
0
static int
qltimer_add(lua_State *state) {
  int         timeout, cycle;
  const char *mod, *func;
  qactor_t   *actor;
  qdict_t    *args;
  qengine_t  *engine;
  qid_t       id;
  qltimer_t  *timer;

  timeout = (int)lua_tonumber(state, 1);
  cycle   = (int)lua_tonumber(state, 2);
  mod     = lua_tostring(state, 3);
  func    = lua_tostring(state, 4);
  if (timeout < 0 || cycle < 0 || mod == NULL || func == NULL) {
    lua_pushnil(state);
    lua_pushliteral(state, "wrong param");
    return 2;
  }

  /* if there exist mod.fun? */
  lua_getglobal(state, mod);
  if (!lua_istable(state, -1)) {
    lua_pushnil(state);
    lua_pushfstring(state, "mod %s not exist", mod);
    return 2;
  }

  lua_getfield(state, -1, func);
  if (!lua_isfunction(state, -1)) {
    lua_pushnil(state);
    lua_pushfstring(state, "%s.%s is not lua function", mod, func);
    return 2;
  }
  /* pop the result */
  lua_pop(state, -1);

  args = qdict_new(5);
  if (args == NULL) {
    lua_pushnil(state);
    lua_pushliteral(state, "create args table error");
    return 2;
  }

  if (qlua_copy_table(state, 4, args) != QOK) {
    qdict_free(args);
    lua_pushnil(state);
    lua_pushliteral(state, "copy args table error");
    return 2;
  }

  timer = new_timer(args, mod, func);
  if (timer == NULL) {
    qdict_free(args);
    lua_pushnil(state);
    lua_pushliteral(state, "create timer error");
    return 2;
  }

  actor = qlua_get_actor(state);
  engine = qactor_get_engine(actor->aid);
  id = qtimer_add(engine, timeout, timer_handler,
                  free_timer, cycle, timer);
  timer->state = state;
  timer->id = id;
  timer->engine = engine;
  qdict_set_numdata(actor->timers, id, timer, engine_free_timer);

  lua_pushnumber(state, id);

  return 1;
}