aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: dc5dbaf49c7d301e71b4f4ece29d7d39c3d3823d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <bh/queue.h>
#include <bh/hashmap.h>
#include <bh/thread.h>
#include <stdio.h>
#include <stdint.h>

size_t ptr_hash(const void *item)
{
    return BH_PTR_TO_INT(item);
}

int ptr_equal(const void *lhs, const void *rhs)
{
    return BH_PTR_TO_INT(lhs) - BH_PTR_TO_INT(rhs);
}

void foo()
{
    bh_hashmap_t *hashmap;
    size_t i;
    void *iter;

    hashmap = bh_hashmap_new((bh_equal_cb_t)ptr_equal, (bh_hash_cb_t)ptr_hash);

    for (i = 0; i < 16; i++)
        bh_hashmap_insert(hashmap, (void*)i, (void*)(i * 4));

    iter = bh_hashmap_iter_next(hashmap, NULL);
    while (iter)
    {
        printf("%zu: %zu\n", BH_PTR_TO_INT(bh_hashmap_iter_key(iter)), BH_PTR_TO_INT(bh_hashmap_iter_value(iter)));
        iter = bh_hashmap_iter_next(hashmap, iter);
    }

    bh_hashmap_free(hashmap);
}

int factor(int x)
{
    if (x < 2)
        return 1;

    return factor(x - 1) + factor(x - 2);
}

void factor_task(void *arg)
{
    printf("Task start\n");
    fflush(stdout);
    printf("Factor: %d\n", factor(42));
    fflush(stdout);
}

void bar()
{
    bh_thread_pool_t *pool;
    bh_task_t *task;
    size_t i;

    printf("Pool create\n");
    fflush(stdout);
    pool = bh_thread_pool_new(16);

    printf("Prepare\n");
    fflush(stdout);
    for (i = 0; i < 32; i++)
    {
        printf("Task create\n");
        fflush(stdout);
        task = bh_task_new(factor_task, NULL, BH_THREAD_CLEANUP);
        bh_thread_pool_add(pool, task);
    }

    bh_thread_pool_wait(pool);
    bh_thread_pool_free(pool);
}

int main()
{
    bh_queue_t *queue;
    void *iter;
    size_t i, j;

    foo();

    printf("Thread?\n");
    fflush(stdout);
    bar();

    queue = bh_queue_new();

    for (j = 0; j < 32; j++)
    {
        printf("%zu %zu\n", bh_queue_size(queue), bh_queue_capacity(queue));

        for (i = 0; i < 4; i++)
            bh_queue_insert(queue, (void *)(j * 4 + i));

        printf("%zu %zu\n", bh_queue_size(queue), bh_queue_capacity(queue));

        for (i = 0; i < 2; i++)
            bh_queue_remove(queue);
    }

    printf("%zu %zu\n", bh_queue_size(queue), bh_queue_capacity(queue));

    iter = bh_queue_iter_next(queue, NULL);
    while (iter)
    {
        printf("%d\n", BH_PTR_TO_INT(bh_queue_iter_value(iter)));
        iter = bh_queue_iter_next(queue, iter);
    }

    bh_queue_free(queue);
    return 0;
}