290 lines
5.4 KiB
C
290 lines
5.4 KiB
C
#include <bh/queue.h>
|
|
#include <bh/hashmap.h>
|
|
#include <bh/thread.h>
|
|
#include <bh/file.h>
|
|
#include <bh/buffer.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);
|
|
volatile int i = factor(16);
|
|
|
|
//printf("Factor: %d\n", factor(15));
|
|
}
|
|
|
|
#include <x86intrin.h>
|
|
|
|
void bar()
|
|
{
|
|
bh_thread_pool_t *pool;
|
|
bh_task_t *task;
|
|
size_t i;
|
|
|
|
bh_uint64_t start, end;
|
|
|
|
//printf("Pool create\n");
|
|
//fflush(stdout);
|
|
pool = bh_thread_pool_new(16);
|
|
if (!pool)
|
|
{
|
|
//printf("Pool creation error\n");
|
|
return;
|
|
}
|
|
|
|
//printf("Prepare\n");
|
|
//fflush(stdout);
|
|
|
|
start = __rdtsc();
|
|
|
|
for (i = 0; i < 128 * 1024; i++)
|
|
{
|
|
//printf("Task create\n");
|
|
//fflush(stdout);
|
|
task = bh_task_new(factor_task, NULL, BH_THREAD_CLEANUP);
|
|
bh_thread_pool_add(pool, task);
|
|
}
|
|
|
|
//system("pause");
|
|
|
|
bh_thread_pool_wait(pool);
|
|
end = __rdtsc();
|
|
bh_thread_pool_free(pool);
|
|
|
|
|
|
|
|
printf("Elapsed: %llu\n", end - start);
|
|
//system("pause");
|
|
}
|
|
|
|
int fib(int x)
|
|
{
|
|
static int memory[128] = {0};
|
|
int result;
|
|
|
|
if (x < 128 && memory[x] != 0)
|
|
return memory[x];
|
|
|
|
if (x <= 1)
|
|
return 1;
|
|
|
|
|
|
result = fib(x - 1) + fib(x - 2);
|
|
|
|
if (x < 128)
|
|
memory[x] = result;
|
|
|
|
return result;
|
|
}
|
|
|
|
#include <string.h>
|
|
|
|
char* itoa (unsigned long long value, char str[], int radix)
|
|
{
|
|
char buf [66];
|
|
char* dest = buf + sizeof(buf);
|
|
int sign = 0;
|
|
|
|
if (value == 0) {
|
|
memcpy (str, "0", 2);
|
|
return str;
|
|
}
|
|
|
|
if (radix < 0) {
|
|
radix = -radix;
|
|
if ( (long long) value < 0) {
|
|
value = -value;
|
|
sign = 1;
|
|
}
|
|
}
|
|
|
|
*--dest = '\0';
|
|
|
|
switch (radix)
|
|
{
|
|
case 16:
|
|
while (value) {
|
|
* --dest = '0' + (value & 0xF);
|
|
if (*dest > '9') *dest += 'A' - '9' - 1;
|
|
value >>= 4;
|
|
}
|
|
break;
|
|
case 10:
|
|
while (value) {
|
|
*--dest = '0' + (value % 10);
|
|
value /= 10;
|
|
}
|
|
break;
|
|
|
|
case 8:
|
|
while (value) {
|
|
*--dest = '0' + (value & 7);
|
|
value >>= 3;
|
|
}
|
|
break;
|
|
|
|
case 2:
|
|
while (value) {
|
|
*--dest = '0' + (value & 1);
|
|
value >>= 1;
|
|
}
|
|
break;
|
|
|
|
default: // The slow version, but universal
|
|
while (value) {
|
|
*--dest = '0' + (value % radix);
|
|
if (*dest > '9') *dest += 'A' - '9' - 1;
|
|
value /= radix;
|
|
}
|
|
break;
|
|
}
|
|
|
|
if (sign) *--dest = '-';
|
|
|
|
memcpy (str, dest, buf +sizeof(buf) - dest);
|
|
return str;
|
|
}
|
|
|
|
void test_api(bh_io_t *io)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < 128; i++)
|
|
{
|
|
char str[65];
|
|
|
|
itoa(i, str, 10);
|
|
bh_io_write(io, str, strlen(str));
|
|
bh_io_write(io, " ", 1);
|
|
itoa(fib(i), str, 10);
|
|
bh_io_write(io, str, strlen(str));
|
|
bh_io_write(io, "\n", 1);
|
|
}
|
|
}
|
|
|
|
void test(void)
|
|
{
|
|
bh_file_t *file = bh_file_new("log.txt");
|
|
|
|
if (!file)
|
|
return;
|
|
|
|
if (bh_file_open(file, BH_IO_APPEND | BH_IO_WRITE))
|
|
{
|
|
printf("File not opened?");
|
|
bh_file_free(file);
|
|
return;
|
|
}
|
|
|
|
bh_file_write(file, "Hello, world!\n", 14);
|
|
test_api((bh_io_t *)file);
|
|
bh_file_free(file);
|
|
}
|
|
|
|
void ddump()
|
|
{
|
|
bh_buffer_t *buffer;
|
|
bh_file_t *file;
|
|
|
|
buffer = bh_buffer_new();
|
|
bh_buffer_open(buffer, BH_IO_WRITE);
|
|
|
|
bh_buffer_write(buffer, "Hello, world!", 13);
|
|
bh_buffer_close(buffer);
|
|
|
|
file = bh_file_new("debug.txt");
|
|
bh_file_open(file, BH_IO_WRITE | BH_IO_TRUNCATE);
|
|
bh_file_write(file, bh_buffer_data(buffer), bh_buffer_size(buffer));
|
|
|
|
bh_file_free(file);
|
|
bh_buffer_free(buffer);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
bh_queue_t *queue;
|
|
void *iter;
|
|
size_t i, j;
|
|
|
|
ddump();
|
|
return 0;
|
|
|
|
//foo();
|
|
test();
|
|
|
|
printf("Thread?\n");
|
|
fflush(stdout);
|
|
bar();
|
|
return 0;
|
|
|
|
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;
|
|
}
|