Пример #1
0
// Finalizes a worker thread
static int ccr_dec_workers(lua_State *L)
{
    int count = (int)luaL_checkinteger(L, 1);
    lua_getfield(L, LUA_REGISTRYINDEX, CCR_SELF);
    process_t *proc = (process_t*)lua_touserdata(L, -1);

    // checks if the calling process is a main process
    if(proc->main)
    {
        if (thr_pool.size() - count < THR_SIZE)
        {
            lua_pushinteger(L, 0);
            lua_pushstring(L, "thread pool is already at the minimum size");
            return 2;
        }

        // sets the numbers threads to kill
        free_workers.fetch_and_add(count);
        // sets the flag indication to kill threads
        free_flag.compare_and_swap(true, false);
        // returns the current number of threads in the pool
        lua_pushinteger(L, (lua_Integer) thr_pool.size() - count);
        return 1;
    }
    lua_pushinteger(L, 0);
    lua_pushstring(L, "only a main process could free threads");
    return 2;
}
 // creates or finds an available fake TLS key
 inline void create_key( fake_key_t &k ) {
     if ( !(free_stack && pop_if_present( k )) ) {
         k = next_key.fetch_and_add(1);     
     } 
 }