Skip to content

Commit

Permalink
corrections, match c++11 naming, reorder struct
Browse files Browse the repository at this point in the history
** todo replace mutex usage to atomic spinlock **
  • Loading branch information
TheTechsTech committed Nov 25, 2024
1 parent d8a4e36 commit 7fa2a33
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This library uses an custom version of [rpmalloc](https://github.com/mjansson/rp

> As a side benefit, just including a single `#include "raii.h"` will make your **Linux** only application **Windows** compatible, see `work-steal.c` in [examples](https://github.com/zelang-dev/c-raii/tree/main/examples) folder, it's from [Complementary Concurrency Programs for course "Linux Kernel Internals"](https://github.com/sysprog21/concurrent-programs), _2 minor changes_, using macro `make_atomic`.
> All aspect of this library as been incorporated into an **c++11** like _threading model_ as outlined in [std::async](https://cplusplus.com/reference/future/async/), any thread launched with signature `thrd_for(thread_func, args_for("six", "text & num & another func", 3, worker_func))` has function executed within `guard` block as further described below, all allocations is automatically cleaned up and `defer` statements **run**.
> All aspect of this library as been incorporated into an **c++11** like _threading model_ as outlined in [std::async](https://cplusplus.com/reference/future/async/), any thread launched with signature `thrd_async(thread_func, args_for("six", "text & num & another func", 3, worker_func))` has function executed within `guard` block as further described below, all allocations is automatically cleaned up and `defer` statements **run**.
The C++ version from <https://cplusplus.com/reference/future/future/wait/>

Expand All @@ -51,7 +51,7 @@ void *is_prime(args_t arg) {
int main(int argc, char **argv) {
int prime = 194232491;
// call function asynchronously:
future *fut = thrd_for(is_prime, &prime);
future *fut = thrd_async(is_prime, &prime);

// a status check, use to guarantee any `thrd_get` call will be ready (and not block)
// must be part of some external event loop handling routine
Expand Down Expand Up @@ -303,7 +303,7 @@ Here too the same process is in effect through an **new** _typedef_ `unique_t` a
/* Calls fn (with args as arguments) in separate thread, returning without waiting
for the execution of fn to complete. The value returned by fn can be accessed
by calling `thrd_get()`. */
C_API future *thrd_for(thrd_func_t fn, void_t args);
C_API future *thrd_async(thrd_func_t fn, void_t args);

/* Returns the value of a `future` ~promise~ thread's shared object, If not ready, this
function blocks the calling thread and waits until it is ready. */
Expand Down
6 changes: 3 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This library uses an custom version of [rpmalloc](https://github.com/mjansson/rp

> As a side benefit, just including a single `#include "raii.h"` will make your **Linux** only application **Windows** compatible, see `work-steal.c` in [examples](https://github.com/zelang-dev/c-raii/tree/main/examples) folder, it's from [Complementary Concurrency Programs for course "Linux Kernel Internals"](https://github.com/sysprog21/concurrent-programs), _2 minor changes_, using macro `make_atomic`.
> All aspect of this library as been incorporated into an **c++11** like _threading model_ as outlined in [std::async](https://cplusplus.com/reference/future/async/), any thread launched with signature `thrd_for(thread_func, args_for("six", "text & num & another func", 3, worker_func))` has function executed within `guard` block as further described below, all allocations is automatically cleaned up and `defer` statements **run**.
> All aspect of this library as been incorporated into an **c++11** like _threading model_ as outlined in [std::async](https://cplusplus.com/reference/future/async/), any thread launched with signature `thrd_async(thread_func, args_for("six", "text & num & another func", 3, worker_func))` has function executed within `guard` block as further described below, all allocations is automatically cleaned up and `defer` statements **run**.
The C++ version from <https://cplusplus.com/reference/future/future/wait/>

Expand All @@ -51,7 +51,7 @@ void *is_prime(args_t arg) {
int main(int argc, char **argv) {
int prime = 194232491;
// call function asynchronously:
future *fut = thrd_for(is_prime, &prime);
future *fut = thrd_async(is_prime, &prime);

// a status check, use to guarantee any `thrd_get` call will be ready (and not block)
// must be part of some external event loop handling routine
Expand Down Expand Up @@ -300,7 +300,7 @@ Here too the same process is in effect through an **new** _typedef_ `unique_t` a
/* Calls fn (with args as arguments) in separate thread, returning without waiting
for the execution of fn to complete. The value returned by fn can be accessed
by calling `thrd_get()`. */
C_API future *thrd_for(thrd_func_t fn, void_t args);
C_API future *thrd_async(thrd_func_t fn, void_t args);

/* Returns the value of a `future` ~promise~ thread's shared object, If not ready, this
function blocks the calling thread and waits until it is ready. */
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ set(TARGET_LIST defer_thread
try_signal_sigsegv
try_unprotected
work-steal
thrd_for )
thrd_async )
foreach (TARGET ${TARGET_LIST})
add_executable(${TARGET} ${TARGET}.c )
target_link_libraries(${TARGET} raii)
Expand Down
2 changes: 1 addition & 1 deletion examples/thrd_for.c → examples/thrd_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void *is_prime(args_t arg) {
int main(int argc, char **argv) {
int prime = 194232491;
// call function asynchronously:
future *fut = thrd_for(is_prime, &prime);
future *fut = thrd_async(is_prime, &prime);

// a status check, use to guarantee any `thrd_get` call will be ready (and not block)
// must be part of some external event loop handling routine
Expand Down
20 changes: 13 additions & 7 deletions include/raii.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,16 @@ typedef struct {

struct memory_s {
void *arena;
int status;
bool is_recovered;
void *volatile err;
const char *volatile panic;
bool is_protected;
bool is_arena;
bool is_emulated;
ex_ptr_t *protector;
defer_t defer;
int status;
size_t mid;
defer_t defer;
ex_ptr_t *protector;
void *volatile err;
const char *volatile panic;
};

typedef struct args_s {
Expand All @@ -178,10 +178,13 @@ typedef struct args_s {

typedef void_t (*thrd_func_t)(args_t);
typedef void_t (*result_func_t)(void_t result, size_t id, values_type value);
make_atomic(c89atomic_spinlock, atomic_spinlock)
typedef struct _promise {
raii_type type;
bool done;
// atomic_flag done;
int id;
// atomic_spinlock mutex;
mtx_t mutex;
cnd_t cond;
memory_t *scope;
Expand Down Expand Up @@ -222,7 +225,7 @@ typedef struct _future_arg {
/* Calls fn (with args as arguments) in separate thread, returning without waiting
for the execution of fn to complete. The value returned by fn can be accessed
by calling `thrd_get()`. */
C_API future *thrd_for(thrd_func_t fn, void_t args);
C_API future *thrd_async(thrd_func_t fn, void_t args);

/* Returns the value of a `future` ~promise~ thread's shared object, If not ready, this
function blocks the calling thread and waits until it is ready. */
Expand All @@ -234,11 +237,14 @@ C_API bool thrd_is_done(future *);
C_API uintptr_t thrd_self(void);
C_API raii_values_t *thrd_value(uintptr_t value);

C_API future *thrd_for_ex(thrd_func_t fn, size_t times, const char *desc, ...);
C_API future *thrd_for(thrd_func_t fn, size_t times, const char *desc, ...);
C_API thrd_values *thrd_sync(future *f);
C_API values_type thrd_then(result_func_t callback, thrd_values *iter, void_t result);
C_API bool thrd_is_finish(future_t *f);

#define atomic_lock(mutex) c89atomic_spinlock_lock((atomic_spinlock *)mutex)
#define atomic_unlock(mutex) c89atomic_spinlock_unlock((atomic_spinlock *)mutex)

/**
* `Release/free` allocated memory, must be called if not using `get_args()` function.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/test-future.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void *is_future(args_t args) {
return thrd_value(true);
}

TEST(thrd_for) {
future *fut = thrd_for(is_future, args_for("six", "hello world", 128, some_worker));
TEST(thrd_async) {
future *fut = thrd_async(is_future, args_for("six", "hello world", 128, some_worker));

ASSERT_TRUE(is_type(fut, RAII_FUTURE));
ASSERT_FALSE(thrd_is_done(fut));
Expand All @@ -31,7 +31,7 @@ TEST(thrd_for) {
TEST(list) {
int result = 0;

EXEC_TEST(thrd_for);
EXEC_TEST(thrd_async);

return result;
}
Expand Down

0 comments on commit 7fa2a33

Please sign in to comment.