Ejemplo n.º 1
0
static int
ts_lua_sleep(lua_State *L)
{
  int sec;
  TSAction action;
  TSCont contp;
  ts_lua_async_item *ai;
  ts_lua_cont_info *ci;

  ci = ts_lua_get_cont_info(L);
  if (ci == NULL)
    return 0;

  sec = luaL_checknumber(L, 1);
  if (sec < 1) {
    sec = 1;
  }

  contp = TSContCreate(ts_lua_sleep_handler, ci->mutex);
  action = TSContSchedule(contp, sec * 1000, TS_THREAD_POOL_DEFAULT);

  ai = ts_lua_async_create_item(contp, ts_lua_sleep_cleanup, (void *)action, ci);
  TSContDataSet(contp, ai);

  return lua_yield(L, 0);
}
Ejemplo n.º 2
0
static int
ts_lua_cache_remove(lua_State *L)
{
    const char              *keystr, *optstr;
    size_t                  key_len, opt_len;
    int                     n;
    TSCont                  contp;
    TSCacheKey              key;
    TSAction                action;
    ts_lua_cont_info        *ci;
    ts_lua_async_item       *ai;
    ts_lua_cache_info       *info;

    ci = ts_lua_get_cont_info(L);
    if (ci == NULL)
        return 0;

    n = lua_gettop(L);
    if (n < 1) {
        return luaL_error(L, "'ts.cache_remove' requires parameter");
    }

    /* keystr */
    if (!lua_isstring(L, 1)) {
        return luaL_error(L, "'ts.cache_remove' first param is not string");
    }

    keystr = luaL_checklstring(L, 1, &key_len);

    optstr = NULL;
    opt_len = 0;

    /* option */
    if (n >= 2 && lua_isstring(L, 2)) {
        optstr = luaL_checklstring(L, 2, &opt_len);;
    }

    key = ts_lua_cache_key_create(keystr, key_len, optstr, opt_len);
    if (key == NULL) {
        return luaL_error(L, "'ts.cache_remove' failed");
    }

    info = (ts_lua_cache_info*)TSmalloc(sizeof(ts_lua_cache_info));
    memset(info, 0, sizeof(ts_lua_cache_info));
    info->cache_key = key;

    contp = TSContCreate(ts_lua_cache_remove_handler, ci->mutex);
    ai = ts_lua_async_create_item(contp, ts_lua_cache_cleanup, info, ci);
    TSContDataSet(contp, ai);

    action = TSCacheRemove(contp, key);

    if (!TSActionDone(action)) {
        info->cache_action = action;
        return lua_yield(L, 0);

    } else {            // action done
        ts_lua_release_cache_info(info, 1);
        ai->data = NULL;
        return 0;
    }
}
Ejemplo n.º 3
0
static int
ts_lua_cache_open(lua_State *L)
{
    const char              *keystr, *optstr;
    size_t                  key_len, opt_len;
    int                     operate, n;
    TSCont                  contp;
    TSCacheKey              key;
    TSAction                action;
    ts_lua_cont_info        *ci;
    ts_lua_async_item       *ai;
    ts_lua_cache_info       *info;

    ci = ts_lua_get_cont_info(L);
    if (ci == NULL)
        return 0;

    n = lua_gettop(L);
    if (n < 2) {
        return luaL_error(L, "'ts.cache_open' requires parameter");
    }

    /* keystr */
    if (!lua_isstring(L, 1)) {
        return luaL_error(L, "'ts.cache_open' first param is not string");

    } else if (!lua_isnumber(L, 2)) {
        return luaL_error(L, "'ts.cache_open' second param is not TS_LUA_CACHE_READ/TS_LUA_CACHE_WRITE");
    }

    keystr = luaL_checklstring(L, 1, &key_len);
    operate = lua_tonumber(L, 2);

    if (operate != TS_LUA_CACHE_READ && operate != TS_LUA_CACHE_WRITE) {
        return luaL_error(L, "'ts.cache_open' second param is not TS_LUA_CACHE_READ/TS_LUA_CACHE_WRITE");
    }

    optstr = NULL;
    opt_len = 0;

    /* option */
    if (n >= 3 && lua_isstring(L, 3)) {
        optstr = luaL_checklstring(L, 3, &opt_len);
    }

    key = ts_lua_cache_key_create(keystr, key_len, optstr, opt_len);
    if (key == NULL) {
        return luaL_error(L, "'ts.cache_open' failed");
    }

    info = (ts_lua_cache_info*)TSmalloc(sizeof(ts_lua_cache_info));
    memset(info, 0, sizeof(ts_lua_cache_info));

    info->cache_key = key;
    info->optype = operate;  

    contp = TSContCreate(ts_lua_cache_main_handler, ci->mutex);
    ai = ts_lua_async_create_item(contp, ts_lua_cache_cleanup, info, ci);

    TSContDataSet(contp, ai);

    info->contp = contp;
    info->ioh.buffer = TSIOBufferCreate();
    info->ioh.reader = TSIOBufferReaderAlloc(info->ioh.buffer);

    if (operate == TS_LUA_CACHE_READ) {
        info->reserved.buffer = TSIOBufferCreate();
        info->reserved.reader = TSIOBufferReaderAlloc(info->reserved.buffer);
        info->current_handler = &ts_lua_cache_open_read;

        action = TSCacheRead(contp, key);

    } else {
        info->current_handler = &ts_lua_cache_open_write;
        action = TSCacheWrite(contp, key);
    }

    if (TSActionDone(action)) {     // done
        return 1;

    } else {                        // undone
        info->cache_action = action;
        return lua_yield(L, 0);
    }
}