This repository has been archived on 2026-04-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
bhlib-old/main.c

141 lines
2.7 KiB
C
Raw Normal View History

2024-04-13 14:52:29 +03:00
#include <bh/queue.h>
#include <bh/hashmap.h>
2024-04-14 22:32:58 +03:00
#include <bh/thread.h>
2024-04-13 14:52:29 +03:00
#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);
}
2024-04-14 22:32:58 +03:00
int factor(int x)
{
if (x < 2)
return 1;
2024-04-14 22:38:00 +03:00
2024-04-14 22:32:58 +03:00
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);
volatile int i = factor(16);
//printf("Factor: %d\n", factor(15));
2024-04-14 22:32:58 +03:00
}
#include <x86intrin.h>
2024-04-14 22:32:58 +03:00
void bar()
{
bh_thread_pool_t *pool;
bh_task_t *task;
size_t i;
bh_uint64_t start, end;
2024-04-14 22:32:58 +03:00
//printf("Pool create\n");
//fflush(stdout);
2024-04-14 22:32:58 +03:00
pool = bh_thread_pool_new(16);
if (!pool)
{
//printf("Pool creation error\n");
return;
}
2024-04-14 22:32:58 +03:00
//printf("Prepare\n");
//fflush(stdout);
start = __rdtsc();
for (i = 0; i < 128 * 1024; i++)
2024-04-14 22:32:58 +03:00
{
//printf("Task create\n");
//fflush(stdout);
2024-04-14 22:32:58 +03:00
task = bh_task_new(factor_task, NULL, BH_THREAD_CLEANUP);
bh_thread_pool_add(pool, task);
}
//system("pause");
2024-04-14 22:32:58 +03:00
bh_thread_pool_wait(pool);
end = __rdtsc();
2024-04-14 22:32:58 +03:00
bh_thread_pool_free(pool);
printf("Elapsed: %llu\n", end - start);
//system("pause");
2024-04-14 22:32:58 +03:00
}
2024-04-13 14:52:29 +03:00
int main()
{
bh_queue_t *queue;
void *iter;
size_t i, j;
//foo();
2024-04-13 14:52:29 +03:00
2024-04-14 22:32:58 +03:00
printf("Thread?\n");
fflush(stdout);
bar();
return 0;
2024-04-14 22:32:58 +03:00
2024-04-13 14:52:29 +03:00
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)));
2024-04-13 14:52:29 +03:00
iter = bh_queue_iter_next(queue, iter);
}
bh_queue_free(queue);
return 0;
}