Sync to the latest version.

Can be broken or partially implemented.
This commit is contained in:
2024-04-23 23:45:43 +03:00
parent ec499b6cfc
commit 692b5b4297
26 changed files with 785 additions and 60 deletions

View File

@@ -28,12 +28,19 @@ set(BHLIB_SOURCE
src/hashmap.c src/hashmap.c
src/queue.c src/queue.c
src/thread.c src/thread.c
src/io.c
src/deflate.c
) )
set(BHLIB_HEADER set(BHLIB_HEADER
include/bh/bh.h
include/bh/platform.h
include/bh/algo.h include/bh/algo.h
include/bh/hashmap.h include/bh/hashmap.h
include/bh/queue.h include/bh/queue.h
include/bh/thread.h
include/bh/io.h
include/bh/deflate.h
) )
set(BHLIB_INCLUDE_DIRS set(BHLIB_INCLUDE_DIRS

View File

@@ -23,10 +23,10 @@ void bh_swap(void *lhs,
* @param element Element size * @param element Element size
* @param equal Equal/compare function * @param equal Equal/compare function
* *
* @return Pointer to the first element that are greater-or-equal to pivot * @return The return value is the pointer to the first element of the second
* element. * partition.
* *
* @warning Pivot element can be an element of the partitioned array itself. * @warning Pivot element can be a part of the partitioned array.
*/ */
void *bh_partition(void *pivot, void *bh_partition(void *pivot,
void *array, void *array,

View File

@@ -3,18 +3,20 @@
#include <bh/config.h> #include <bh/config.h>
#include <stddef.h> #include <stddef.h>
#include "platform.h"
#define BH_INT_TO_PTR(x) \ #define BH_INT_TO_PTR(x) \
((void *)((long)(x))) ((void *)((bh_intptr_t)(x)))
#define BH_UINT_TO_PTR(x) \ #define BH_UINT_TO_PTR(x) \
((void *)((unsigned long)(x))) ((void *)((bh_uintptr_t)(x)))
#define BH_PTR_TO_INT(x) \ #define BH_PTR_TO_INT(x) \
((long)(x)) ((bh_intptr_t)(x))
#define BH_PTR_TO_UINT(x) \ #define BH_PTR_TO_UINT(x) \
((unsigned long)(x)) ((bh_uintptr_t)(x))
typedef int (*bh_equal_cb_t)(const void *, const void *); typedef int (*bh_equal_cb_t)(const void *, const void *);
typedef size_t (*bh_hash_cb_t)(const void *); typedef size_t (*bh_hash_cb_t)(const void *);

5
include/bh/deflate.h Normal file
View File

@@ -0,0 +1,5 @@
#ifndef BHLIB_DEFLATE_H
#define BHLIB_DEFLATE_H
#endif /* BHLIB_DEFLATE_H */

37
include/bh/file.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef BHLIB_FILE_H
#define BHLIB_FILE_H
#include "io.h"
typedef struct bh_file_s bh_file_t;
bh_file_t *bh_file_new(const char *path,
const char *mode);
void bh_file_free(bh_file_t *file);
size_t bh_file_read(bh_file_t *file,
char *buffer,
size_t size);
size_t bh_file_write(bh_file_t *file,
const char *buffer,
size_t size);
void bh_file_flush(bh_file_t *file);
void bh_file_seek(bh_file_t *file,
bh_off_t pos,
int dir);
bh_off_t bh_file_tell(bh_file_t *file);
bh_off_t bh_file_available(bh_file_t *file);
int bh_file_error(bh_file_t *file);
int bh_file_eof(bh_file_t *file);
void bh_file_clear(bh_file_t *file);
#endif /* BHLIB_FILE_H */

View File

@@ -1,7 +1,7 @@
#ifndef BHLIB_HASHMAP_H #ifndef BHLIB_HASHMAP_H
#define BHLIB_HASHMAP_H #define BHLIB_HASHMAP_H
#include <bh/bh.h> #include "bh.h"
typedef struct bh_hashmap_s bh_hashmap_t; typedef struct bh_hashmap_s bh_hashmap_t;
@@ -15,9 +15,6 @@ typedef struct bh_hashmap_s bh_hashmap_t;
* hashmap object. * hashmap object.
* @return If the function fails, the return value is NULL. * @return If the function fails, the return value is NULL.
* *
* @warning The quality of the supplied hash function will affect performance
* of the hashmap.
*
* @sa bh_hashmap_free, bh_hashmap_reserve, bh_hashmap_insert * @sa bh_hashmap_free, bh_hashmap_reserve, bh_hashmap_insert
*/ */
bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal, bh_hashmap_t *bh_hashmap_new(bh_equal_cb_t equal,
@@ -37,7 +34,7 @@ void bh_hashmap_free(bh_hashmap_t *hashmap);
* *
* @param hashmap Valid pointer to the hashmap object. * @param hashmap Valid pointer to the hashmap object.
* *
* @warning Clearing the hashmap does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bh_hashmap_remove * @sa bh_hashmap_remove
*/ */
@@ -52,7 +49,7 @@ void bh_hashmap_clear(bh_hashmap_t *hashmap);
* @return If the function succeeds, the return value is zero. * @return If the function succeeds, the return value is zero.
* @return If the function fails, the return value is non-zero. * @return If the function fails, the return value is non-zero.
* *
* @warning Reserving hashmap space does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bh_hashmap_capacity, bh_hashmap_insert * @sa bh_hashmap_capacity, bh_hashmap_insert
*/ */
@@ -70,9 +67,9 @@ int bh_hashmap_reserve(bh_hashmap_t *hashmap,
* @return If the function fails, the return value is non-zero. * @return If the function fails, the return value is non-zero.
* *
* @warning Inserted element is owned by the caller of the function. * @warning Inserted element is owned by the caller of the function.
* @warning Inserting elements into the hashmap does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bbh_hashmap_remove, bh_hashmap_at * @sa bh_hashmap_remove, bh_hashmap_at
*/ */
int bh_hashmap_insert(bh_hashmap_t *hashmap, int bh_hashmap_insert(bh_hashmap_t *hashmap,
void *key, void *key,
@@ -84,7 +81,7 @@ int bh_hashmap_insert(bh_hashmap_t *hashmap,
* @param hashmap Valid pointer to the hashmap object. * @param hashmap Valid pointer to the hashmap object.
* @param key Key. * @param key Key.
* *
* @warning Removing elements from the hashmap does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bh_hashmap_insert, bh_hashmap_at * @sa bh_hashmap_insert, bh_hashmap_at
*/ */
@@ -96,15 +93,12 @@ void bh_hashmap_remove(bh_hashmap_t *hashmap,
* *
* @param hashmap Valid pointer to the hashmap. * @param hashmap Valid pointer to the hashmap.
* @param key Key. * @param key Key.
* @param exists Pointer to the exists flag (optional). * @param exists Optional pointer to the exists flag.
* *
* @return If the function succeeds, the return value is a valid pointer to * @return If the function succeeds, the return value is a valid pointer to
* the element value. * the element value.
* @return If the function fails, the return value is NULL. * @return If the function fails, the return value is NULL.
* *
* @note If the hashmap does not contain any element with the key, the
* function will fail.
*
* @sa bh_hashmap_empty, bh_hashmap_insert * @sa bh_hashmap_empty, bh_hashmap_insert
*/ */
void *bh_hashmap_at(bh_hashmap_t *hashmap, void *bh_hashmap_at(bh_hashmap_t *hashmap,
@@ -205,7 +199,7 @@ void *bh_hashmap_iter_next(bh_hashmap_t *hashmap,
* @param hashmap Valid pointer to the hashmap object. * @param hashmap Valid pointer to the hashmap object.
* @param key Valid iterator. * @param key Valid iterator.
* *
* @warning Removing elements from the hashmap does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bh_hashmap_insert, bh_hashmap_at * @sa bh_hashmap_insert, bh_hashmap_at
*/ */

View File

View File

12
include/bh/internal/io.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef BHLIB_INTERNAL_IO_H
#define BHLIB_INTERNAL_IO_H
#include <bh/io.h>
void bh_io_init(bh_io_t *io,
bh_io_table_t *table);
void bh_io_destroy(bh_io_t *io);
#endif /* BHLIB_INTERNAL_IO_H */

View File

@@ -0,0 +1,28 @@
struct bh_thread_s
{
void *handle;
};
struct bh_mutex_s
{
void *handle;
};
struct bh_cond_s
{
void *handle;
};
int bh_thread_init(bh_thread_t *thread,
bh_task_t *task);
int bh_mutex_init(bh_mutex_t *mutex);
void bh_mutex_destroy(bh_mutex_t *mutex);
int bh_cond_init(bh_cond_t *cond);
void bh_cond_destroy(bh_cond_t *cond);
int bh_thread_pool_init(bh_thread_pool_t *pool,
size_t size);
void bh_thread_pool_destroy(bh_thread_pool_t *pool);

View File

@@ -3,6 +3,7 @@
struct bh_thread_s struct bh_thread_s
{ {
pthread_t handle; pthread_t handle;
int allocated;
}; };
struct bh_mutex_s struct bh_mutex_s
@@ -27,4 +28,4 @@ void bh_cond_destroy(bh_cond_t *cond);
int bh_thread_pool_init(bh_thread_pool_t *pool, int bh_thread_pool_init(bh_thread_pool_t *pool,
size_t size); size_t size);
void bh_thread_pool_destroy(bh_thread_pool_t *pool); void bh_thread_pool_destroy(bh_thread_pool_t *pool);

View File

@@ -0,0 +1,48 @@
#include <process.h>
struct bh_thread_s
{
HANDLE handle;
int allocated;
};
struct bh_thread_data_s
{
bh_task_t *task;
bh_thread_end_cb_t end;
} bh_thread_data_t;
struct bh_mutex_s
{
CRITICAL_SECTION handle;
};
struct bh_cond_s
{
void *handle;
};
int bh_thread_init_base(bh_thread_t *thread,
bh_task_t *task,
bh_thread_begin_cb_t begin,
bh_thread_end_cb_t end);
#define bh_thread_init(thread, task) \
bh_thread_init_base((thread), (task), _beginthreadex, _endthreadex)
int bh_mutex_init(bh_mutex_t *mutex);
void bh_mutex_destroy(bh_mutex_t *mutex);
int bh_cond_init(bh_cond_t *cond);
void bh_cond_destroy(bh_cond_t *cond);
int bh_thread_pool_init_base(bh_thread_pool_t *pool,
size_t size,
bh_thread_begin_cb_t begin,
bh_thread_end_cb_t end);
#define bh_thread_pool_init(pool, size) \
bh_thread_pool_init_base((pool), (size), _beginthreadex, _endthreadex)
void bh_thread_pool_destroy(bh_thread_pool_t *pool);

182
include/bh/io.h Normal file
View File

@@ -0,0 +1,182 @@
#ifndef BHLIB_IO_H
#define BHLIB_IO_H
#include "bh.h"
#define BH_IO_ERROR (1 << 0)
#define BH_IO_EOF (1 << 1)
#define BH_IO_CAST(x) \
((bh_io_t *)(x))
struct bh_io_s;
typedef struct bh_io_table_s
{
size_t (*read)(struct bh_io_s *io,
char *data,
size_t size);
size_t (*write)(struct bh_io_s *io,
const char *data,
size_t size);
void (*flush)(struct bh_io_s *io);
void (*seek)(struct bh_io_s *io,
bh_off_t offset,
int dir);
bh_off_t (*tell)(struct bh_io_s *io);
bh_off_t (*available)(struct bh_io_s *io);
void (*clear)(struct bh_io_s *io);
void (*destroy)(struct bh_io_s *io);
} bh_io_table_t;
typedef struct bh_io_s
{
bh_io_table_t *table;
int flags;
} bh_io_t;
/**
* @brief Create new IO object.
*
* @param table Valid pointer to the IO table.
* @param size Size of the IO object.
*
* @return If the function succeeds, the return value is a pointer to the
* new, semi-initalized, IO object.
* @return If the function fails, the return value is NULL.
*
* @warning This function should be used in context of implementing child
* IO objects (files, sockets, streaming compression, etc).
*
* @sa bh_io_free
*/
bh_io_t *bh_io_new(bh_io_table_t *table,
size_t size);
/**
* @brief Free IO object.
*
* @param io Valid pointer to the IO object.
*/
void bh_io_free(bh_io_t *io);
/**
* @brief Read data from IO.
*
* @param io Valid pointer to the IO object.
* @param data Valid pointer to the buffer.
* @param size Size of the buffer.
*
* @return If the function succeeds, the return value is the amount
* of bytes read from the IO object.
* @return If the function fails, the return value is zero and error
* flag is set.
*
* @sa bh_io_write, bh_io_eof, bh_io_error
*/
size_t bh_io_read(bh_io_t *io,
char *data,
size_t size);
/**
* @brief Write data to IO.
*
* @param io Valid pointer to the IO object.
* @param data Valid pointer to the buffer.
* @param size Size of the buffer.
*
* @return If the function succeeds, the return value is the amount
* of bytes written to the IO object.
* @return If the function fails, the return value is zero and error
* flag is set.
*
* @sa bh_io_read, bh_io_error, bh_io_flush
*/
size_t bh_io_write(bh_io_t *io,
const char* data,
size_t size);
/**
* @brief Writes any uncommited changes (if possible).
*
* @param io Valid pointer to the IO object.
*/
void bh_io_flush(bh_io_t *io);
/**
* @brief Seeks IO object to the specified position (if possible).
*
* @param io Valid pointer to the IO object.
* @param offset Position
* @param dir Direction
*
* @sa bh_io_tell
*/
void bh_io_seek(bh_io_t *io,
bh_off_t offset,
int dir);
/**
* @brief Return current position in IO.
*
* @param io Valid pointer to the IO object.
*
* @return If the function succeeds, the return value is current
* position in the IO.
* @return If the function fails, the return value is -1.
*
* @sa bh_io_seek
*/
bh_off_t bh_io_tell(bh_io_t *io);
/**
* @brief Return available bytes in the IO.
*
* @param io Valid pointer to the IO object.
*
* @return If the function succeeds, the return value is the amount
* of the available bytes for reading.
* @return If the function fails, the return value is zero.
*/
bh_off_t bh_io_available(bh_io_t *io);
/**
* @brief Return error flag of the IO.
*
* @param io Valid pointer to the IO object.
*
* @return The return value is error flag.
*
* @sa bh_io_eof, bh_io_clear
*/
int bh_io_error(bh_io_t *io);
/**
* @brief Return end-of-file flag of the IO.
*
* @param io Valid pointer to the IO object.
*
* @return The return value is end-of-file flag.
*
* @sa bh_io_error, bh_io_clear
*/
int bh_io_eof(bh_io_t *io);
/**
* @brief Crear IO object state.
*
* @param io Valid pointer to the IO object.
*
* @sa bh_io_error, bh_io_eof
*/
void bh_io_clear(bh_io_t *io);
#endif /* BHLIB_IO_H */

46
include/bh/platform.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef BHLIB_PLATFORM_H
#define BHLIB_PLATFORM_H
#include <stddef.h>
/* Common type definitions */
typedef signed char bh_int8_t;
typedef unsigned char bh_uint8_t;
typedef signed short bh_int16_t;
typedef unsigned short bh_uint16_t;
typedef float bh_float32_t;
typedef double bh_float64_t;
/* Platform specific type definition */
#if __STDC_VERSION__ >= 199901L || defined(__GNUC__)
#include <stdint.h>
typedef int32_t bh_int32_t;
typedef uint32_t bh_uint32_t;
typedef int64_t bh_int64_t;
typedef uint64_t bh_uint64_t;
typedef intptr_t bh_intptr_t;
typedef uintptr_t bh_uintptr_t;
#elif defined(_WIN32)
typedef __int32 bh_int32_t;
typedef unsigned __int32 bh_uint32_t;
typedef __int64 bh_int64_t;
typedef unsigned __int64 bh_uint64_t;
#if defined(_WIN64)
typedef __int64 bh_intptr_t;
typedef unsigned __int64 bh_uintptr_t;
#else
typedef __int32 bh_intptr_t;
typedef unsigned __int32 bh_uintptr_t;
#endif
#else
#error "Unsupported platform"
#endif
typedef bh_int64_t bh_off_t;
#endif /* BHLIB_PLATFORM_H */

View File

@@ -1,7 +1,7 @@
#ifndef BH_QUEUE_H #ifndef BH_QUEUE_H
#define BH_QUEUE_H #define BH_QUEUE_H
#include <bh/bh.h> #include "bh.h"
typedef struct bh_queue_s bh_queue_t; typedef struct bh_queue_s bh_queue_t;
@@ -30,7 +30,7 @@ void bh_queue_free(bh_queue_t *queue);
* *
* @param queue Valid pointer to the queue object. * @param queue Valid pointer to the queue object.
* *
* @warning Clearing the queue does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bh_queue_remove * @sa bh_queue_remove
*/ */
@@ -45,7 +45,7 @@ void bh_queue_clear(bh_queue_t *queue);
* @return If the function succeeds, the return value is zero. * @return If the function succeeds, the return value is zero.
* @return If the function fails, the return value is non-zero. * @return If the function fails, the return value is non-zero.
* *
* @warning Reserving queue space does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bh_queue_capacity, bh_queue_insert * @sa bh_queue_capacity, bh_queue_insert
*/ */
@@ -62,7 +62,7 @@ int bh_queue_reserve(bh_queue_t *queue,
* @return If the function fails, the return value is non-zero. * @return If the function fails, the return value is non-zero.
* *
* @warning Inserted element is owned by the caller of the function. * @warning Inserted element is owned by the caller of the function.
* @warning Inserting elements into the queue does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bh_queue_remove, bh_queue_front * @sa bh_queue_remove, bh_queue_front
*/ */
@@ -74,7 +74,7 @@ int bh_queue_insert(bh_queue_t *queue,
* *
* @param queue Valid pointer to the queue object. * @param queue Valid pointer to the queue object.
* *
* @warning Removing elements from the queue does invalidate iterators. * @warning Calling this function will invalidate iterators.
* *
* @sa bh_queue_insert, bh_queue_front * @sa bh_queue_insert, bh_queue_front
*/ */
@@ -89,9 +89,6 @@ void bh_queue_remove(bh_queue_t *queue);
* the element. * the element.
* @return If the function fails, the return value is NULL. * @return If the function fails, the return value is NULL.
* *
* @note If the queue is empty, function will fail to return element from
* the queue.
*
* @sa bh_queue_empty, bh_queue_insert * @sa bh_queue_empty, bh_queue_insert
*/ */
void *bh_queue_front(bh_queue_t *queue); void *bh_queue_front(bh_queue_t *queue);

View File

@@ -42,6 +42,50 @@ bh_thread_pool_t *bh_thread_pool_new_base(size_t size,
#endif #endif
/**
* @brief Create new task
*
* @param func Function
* @param data Function data
* @param flags Task flags
*
* @return Pointer to the new task
*
* @sa bh_task_free, bh_task_reuse, bh_task_done
*/
bh_task_t *bh_task_new(void (*func)(void *),
void *data,
int flags);
/**
* @brief Free the task.
*
* @param task Pointer to the task
*/
void bh_task_free(bh_task_t *task);
/**
* @brief Reuse task.
*
* @param task Pointer to the task
* @param func Function
* @param data Data
*
* @sa bh_task_free, bh_task_done
*/
void bh_task_reuse(bh_task_t *task,
void (*func)(void *),
void *data);
/**
* @brief Check if task is done.
*
* @param task Pointer to the task
*
* @return The return value is boolean flag, indicating if the task is done.
*/
int bh_task_done(bh_task_t *task);
/** /**
* @function bh_thread_new * @function bh_thread_new
* *
@@ -205,18 +249,6 @@ int bh_cond_signal(bh_cond_t *cond);
*/ */
int bh_cond_broadcast(bh_cond_t *cond); int bh_cond_broadcast(bh_cond_t *cond);
bh_task_t *bh_task_new(void (*func)(void *),
void *data,
int flags);
void bh_task_free(bh_task_t *task);
void bh_task_reuse(bh_task_t *task,
void (*func)(void *),
void *data);
int bh_task_done(bh_task_t *task);
/** /**
* @brief Submit task to the thread pool. * @brief Submit task to the thread pool.
* *

16
main.c
View File

@@ -4,18 +4,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#define BH_INT_TO_PTR(x) \
((void *)((long)(x)))
#define BH_UINT_TO_PTR(x) \
((void *)((unsigned long)(x)))
#define BH_PTR_TO_INT(x) \
((long)(x))
#define BH_PTR_TO_UINT(x) \
((unsigned long)(x))
size_t ptr_hash(const void *item) size_t ptr_hash(const void *item)
{ {
return BH_PTR_TO_INT(item); return BH_PTR_TO_INT(item);
@@ -59,7 +47,7 @@ void factor_task(void *arg)
{ {
printf("Task start\n"); printf("Task start\n");
fflush(stdout); fflush(stdout);
printf("Factor: %d\n", factor(48)); printf("Factor: %d\n", factor(42));
fflush(stdout); fflush(stdout);
} }
@@ -119,7 +107,7 @@ int main()
iter = bh_queue_iter_next(queue, NULL); iter = bh_queue_iter_next(queue, NULL);
while (iter) while (iter)
{ {
printf("%d\n", (int)bh_queue_iter_value(iter)); printf("%d\n", BH_PTR_TO_INT(bh_queue_iter_value(iter)));
iter = bh_queue_iter_next(queue, iter); iter = bh_queue_iter_next(queue, iter);
} }

View File

@@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
ulimit -Sv 320000 #ulimit -Sv 320000
rm -r build coverage rm -r build coverage
mkdir build coverage mkdir build coverage
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS_DEBUG="-g -fprofile-arcs -ftest-coverage" cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS_DEBUG="-g -fprofile-arcs -ftest-coverage"

1
src/deflate.c Normal file
View File

@@ -0,0 +1 @@
#include <bh/deflate.h>

0
src/file_posix.c Normal file
View File

0
src/file_win.c Normal file
View File

83
src/io.c Normal file
View File

@@ -0,0 +1,83 @@
#include <bh/internal/io.h>
#include <stdlib.h>
bh_io_t *bh_io_new(bh_io_table_t *table,
size_t size)
{
bh_io_t *result;
result = malloc(size);
if (result)
bh_io_init(result, table);
return result;
}
void bh_io_free(bh_io_t *io)
{
bh_io_destroy(io);
free(io);
}
void bh_io_init(bh_io_t *io,
bh_io_table_t *table)
{
io->table = table;
io->flags = 0;
}
void bh_io_destroy(bh_io_t *io)
{
io->table->destroy(io);
}
size_t bh_io_read(bh_io_t *io,
char *data,
size_t size)
{
return io->table->read(io, data, size);
}
size_t bh_io_write(bh_io_t *io,
const char* data,
size_t size)
{
return io->table->write(io, data, size);
}
void bh_io_flush(bh_io_t *io)
{
io->table->flush(io);
}
void bh_io_seek(bh_io_t *io,
bh_off_t offset,
int dir)
{
io->table->seek(io, offset, dir);
}
bh_off_t bh_io_tell(bh_io_t *io)
{
return io->table->tell(io);
}
bh_off_t bh_io_available(bh_io_t *io)
{
return io->table->available(io);
}
int bh_io_error(bh_io_t *io)
{
return (io->flags & BH_IO_ERROR) > 0;
}
int bh_io_eof(bh_io_t *io)
{
return (io->flags & BH_IO_EOF) > 0;
}
void bh_io_clear(bh_io_t *io)
{
io->table->clear(io);
}

View File

@@ -216,4 +216,4 @@ void bh_thread_pool_worker(void *arg)
bh_mutex_unlock(&pool->lock); bh_mutex_unlock(&pool->lock);
bh_cond_broadcast(&pool->done_task); bh_cond_broadcast(&pool->done_task);
} }
} }

113
src/thread_null.c Normal file
View File

@@ -0,0 +1,113 @@
#include <bh/internal/thread.h>
int bh_thread_init(bh_thread_t *thread,
bh_task_t *task)
{
(void)thread;
(void)task;
return -1;
}
bh_thread_t *bh_thread_new(bh_task_t *task)
{
(void)task;
return NULL;
}
int bh_thread_join(bh_thread_t *thread)
{
(void)thread;
return -1;
}
int bh_thread_detach(bh_thread_t *thread)
{
(void)thread;
return -1;
}
int bh_mutex_init(bh_mutex_t *mutex)
{
(void)mutex;
return -1;
}
void bh_mutex_destroy(bh_mutex_t *mutex)
{
(void)mutex;
}
int bh_mutex_lock(bh_mutex_t *mutex)
{
(void)mutex;
return -1;
}
int bh_mutex_try_lock(bh_mutex_t *mutex)
{
(void)mutex;
return -1;
}
int bh_mutex_unlock(bh_mutex_t *mutex)
{
(void)mutex;
return -1;
}
int bh_cond_init(bh_cond_t *cond)
{
(void)cond;
return -1;
}
void bh_cond_destroy(bh_cond_t *cond)
{
(void)cond;
}
int bh_cond_wait(bh_cond_t *cond,
bh_mutex_t *mutex)
{
(void)cond;
(void)mutex;
return -1;
}
int bh_cond_wait_for(bh_cond_t *cond,
bh_mutex_t *mutex,
unsigned long timeout)
{
(void)cond;
(void)mutex;
(void)timeout;
return -1;
}
int bh_cond_signal(bh_cond_t *cond)
{
(void)cond;
return -1;
}
int bh_cond_broadcast(bh_cond_t *cond)
{
(void)cond;
return -1;
}
int bh_thread_pool_init(bh_thread_pool_t *pool,
size_t size)
{
(void)pool;
(void)size;
return -1;
}
bh_thread_pool_t *bh_thread_pool_new(size_t size)
{
(void)size;
return NULL;
}

View File

@@ -20,17 +20,46 @@ static void *bh_thread_run(void *arg)
int bh_thread_init(bh_thread_t *thread, int bh_thread_init(bh_thread_t *thread,
bh_task_t *task) bh_task_t *task)
{ {
thread->allocated = 0;
return pthread_create(&thread->handle, NULL, bh_thread_run, task); return pthread_create(&thread->handle, NULL, bh_thread_run, task);
} }
bh_thread_t *bh_thread_new(bh_task_t *task)
{
bh_thread_t *result;
result = malloc(sizeof(*result));
if (result && bh_thread_init(result, task))
{
free(result);
result = NULL;
}
if (result)
result->allocated = 1;
return result;
}
int bh_thread_join(bh_thread_t *thread) int bh_thread_join(bh_thread_t *thread)
{ {
return pthread_join(thread->handle, NULL); int result = pthread_join(thread->handle, NULL);
if (thread->allocated)
free(thread);
return result;
} }
int bh_thread_detach(bh_thread_t *thread) int bh_thread_detach(bh_thread_t *thread)
{ {
return pthread_detach(thread->handle); int result = pthread_detach(thread->handle);
if (thread->allocated)
free(thread);
return result;
} }
int bh_mutex_init(bh_mutex_t *mutex) int bh_mutex_init(bh_mutex_t *mutex)

120
src/thread_win.c Normal file
View File

@@ -0,0 +1,120 @@
#include <bh/internal/thread.h>
int bh_thread_init_base(bh_thread_t *thread,
bh_task_t *task,
bh_thread_begin_cb_t begin,
bh_thread_end_cb_t end)
{
(void)thread;
(void)task;
return -1;
}
bh_thread_t *bh_thread_new_base(bh_task_t *task,
bh_thread_begin_cb_t begin,
bh_thread_end_cb_t end)
{
(void)task;
return NULL;
}
int bh_thread_join(bh_thread_t *thread)
{
(void)thread;
return -1;
}
int bh_thread_detach(bh_thread_t *thread)
{
(void)thread;
return -1;
}
int bh_mutex_init(bh_mutex_t *mutex)
{
(void)mutex;
return -1;
}
void bh_mutex_destroy(bh_mutex_t *mutex)
{
(void)mutex;
}
int bh_mutex_lock(bh_mutex_t *mutex)
{
(void)mutex;
return -1;
}
int bh_mutex_try_lock(bh_mutex_t *mutex)
{
(void)mutex;
return -1;
}
int bh_mutex_unlock(bh_mutex_t *mutex)
{
(void)mutex;
return -1;
}
int bh_cond_init(bh_cond_t *cond)
{
(void)cond;
return -1;
}
void bh_cond_destroy(bh_cond_t *cond)
{
(void)cond;
}
int bh_cond_wait(bh_cond_t *cond,
bh_mutex_t *mutex)
{
(void)cond;
(void)mutex;
return -1;
}
int bh_cond_wait_for(bh_cond_t *cond,
bh_mutex_t *mutex,
unsigned long timeout)
{
(void)cond;
(void)mutex;
(void)timeout;
return -1;
}
int bh_cond_signal(bh_cond_t *cond)
{
(void)cond;
return -1;
}
int bh_cond_broadcast(bh_cond_t *cond)
{
(void)cond;
return -1;
}
int bh_thread_pool_init_base(bh_thread_pool_t *pool,
size_t size,
bh_thread_begin_cb_t begin,
bh_thread_end_cb_t end)
{
(void)pool;
(void)size;
return -1;
}
bh_thread_pool_t *bh_thread_pool_new_base(size_t size,
bh_thread_begin_cb_t begin,
bh_thread_end_cb_t end)
{
(void)size;
return NULL;
}