Esempio n. 1
0
/* //////////////////////////////////////////////////////////////////////////////////////
 * main
 */
tb_int_t tb_demo_container_single_list_entry_main(tb_int_t argc, tb_char_t** argv)
{
    // init the entries
    tb_demo_entry_t entries[12] = 
    {
        {{0}, 0}
    ,   {{0}, 1}
    ,   {{0}, 2}
    ,   {{0}, 3}
    ,   {{0}, 4}
    ,   {{0}, 5}
    ,   {{0}, 6}
    ,   {{0}, 7}
    ,   {{0}, 8}
    ,   {{0}, 9}
    ,   {{0}, 10}
    ,   {{0}, 11}
    };

    // init the list
    tb_single_list_entry_head_t list;
    tb_single_list_entry_init(&list, tb_demo_entry_t, entry, tb_demo_entry_copy);

    // insert entries
    tb_single_list_entry_insert_tail(&list, &entries[5].entry);
    tb_single_list_entry_insert_tail(&list, &entries[6].entry);
    tb_single_list_entry_insert_tail(&list, &entries[7].entry);
    tb_single_list_entry_insert_tail(&list, &entries[8].entry);
    tb_single_list_entry_insert_tail(&list, &entries[9].entry);
    tb_single_list_entry_insert_head(&list, &entries[4].entry);
    tb_single_list_entry_insert_head(&list, &entries[3].entry);
    tb_single_list_entry_insert_head(&list, &entries[2].entry);
    tb_single_list_entry_insert_head(&list, &entries[1].entry);
    tb_single_list_entry_insert_head(&list, &entries[0].entry);

    // the entry
    tb_demo_entry_t* entry = (tb_demo_entry_t*)tb_single_list_entry(&list, &entries[5].entry);
    tb_trace_i("entry: %lu", entry->data);
    tb_trace_i("");

    // walk it
    tb_trace_i("insert: %lu", tb_single_list_entry_size(&list));
    tb_for_all_if(tb_demo_entry_t*, item0, tb_single_list_entry_itor(&list), item0)
    {
        tb_trace_i("%lu", item0->data);
    }
Esempio n. 2
0
// the once function
static tb_bool_t tb_thread_local_once(tb_cpointer_t priv)
{
    // check
    tb_value_ref_t tuple = (tb_value_ref_t)priv;
    tb_check_return_val(tuple, tb_false);

    // the thread local
    tb_thread_local_ref_t local = (tb_thread_local_ref_t)tuple[0].ptr;
    tb_check_return_val(local, tb_false);

    // save the free function
    local->free = (tb_thread_local_free_t)tuple[1].ptr;

    // check the pthread key space size
    tb_assert_static(sizeof(pthread_key_t) * 2 <= sizeof(local->priv));

    // create the pthread key for data
    tb_bool_t ok = pthread_key_create(&((pthread_key_t*)local->priv)[0], tb_null) == 0;
    if (ok)
    {
        // create the pthread key for mark
        ok = pthread_key_create(&((pthread_key_t*)local->priv)[1], tb_null) == 0;

        // failed? remove the data key
        if (!ok) pthread_key_delete(((pthread_key_t*)local->priv)[0]);
    }

    // save this thread local to list if ok
    if (ok)
    {
        tb_spinlock_enter(&g_thread_local_lock);
        tb_single_list_entry_insert_tail(&g_thread_local_list, &local->entry);
        tb_spinlock_leave(&g_thread_local_lock);
    }

    // init ok
    local->inited = tb_true;

    // ok?
    return ok;
}