Skip to content

Commit

Permalink
complete reworking use vector for argument passing
Browse files Browse the repository at this point in the history
- simplified thread argument passing and handling
- removed custom args/struct functions no longer necessary, moved struct fields to vector_metadata_t
  • Loading branch information
TheTechsTech committed Dec 21, 2024
1 parent 98325d2 commit 62ff583
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 315 deletions.
51 changes: 22 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ C_API raii_values_t *thrd_value(uintptr_t value);
C_API void thrd_init(size_t queue_size);
C_API future_t thrd_scope(void);
C_API future_t thrd_sync(future_t);
C_API result_t thrd_spawn_ex(thrd_func_t fn, const char *desc, ...);
C_API result_t thrd_spawn(thrd_func_t fn, void_t args);
C_API values_type thrd_result(result_t value);

Expand All @@ -338,38 +337,32 @@ C_API void thrd_destroy(future_t);
C_API bool thrd_is_finish(future_t);

/**
* Creates an scoped container for arbitrary arguments passing to an single `args` function.
* Use `get_args()` or `args_in()` for retrieval.
* Creates an scoped `vector/array/container` for arbitrary arguments passing into an single `paramater` function.
* - Use standard `array access` for retrieval of an `union` storage type.
*
* @param scope callers context to bind `allocated` arguments to
* @param desc format, similar to `printf()`:
* * `i` unsigned integer,
* * `d` signed integer,
* * `l` signed long,
* * `z` size_t - max size,
* * `c` character,
* * `s` string,
* * `a` array,
* * `x` function,
* * `f` double/float,
* * `p` void pointer for any arbitrary object
* @param arguments indexed by `desc` format order
*/
C_API args_t raii_args_for(memory_t *scope, const char *desc, ...);
C_API args_t args_for(const char *desc, ...);

/**
* Returns generic union `values_type` of argument, will auto `release/free`
* allocated memory when scoped return/exit.
* - MUST CALL `args_destructor_set()` to have memory auto released
* within ~callers~ current scoped `context`, will happen either at return/exist or panics.
*
* - OTHERWISE `memory leak` will be shown in DEBUG build.
*
* Must be called at least once to release `allocated` memory.
* NOTE: `vector_for` does auto memory cleanup.
*
* @param params arbitrary arguments
* @param item index number
* @param count numbers of parameters, `0` will create empty `vector/array`.
* @param arguments indexed in given order.
*/
C_API values_type raii_get_args(memory_t *scope, void_t params, int item);
C_API values_type get_args(void *params, int item);
C_API values_type get_arg(void_t params);
C_API args_t args_for(size_t, ...);

#define array(count, ...) args_for(count, __VA_ARGS__)
#define array_defer(arr) args_destructor_set(arr)
#define vectorize(vec) vectors_t vec = vector_any()
#define vector(vec, count, ...) vectors_t vec = vector_for(nil, count, __VA_ARGS__)

#define $push_back(vec, value) vector_push_back(vec, (void_t)value)
#define $insert(vec, index, value) vector_insert(vec, index, (void_t)value)
#define $clear(vec) vector_clear(vec)
#define $size(vec) vector_size(vec)
#define $capacity(vec) vector_capacity(vec)
#define $erase(vec, index) vector_erase(vec, index)
```
### There is _1 way_ to create an smart memory pointer
Expand Down
51 changes: 22 additions & 29 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ C_API raii_values_t *thrd_value(uintptr_t value);
C_API void thrd_init(size_t queue_size);
C_API future_t thrd_scope(void);
C_API future_t thrd_sync(future_t);
C_API result_t thrd_spawn_ex(thrd_func_t fn, const char *desc, ...);
C_API result_t thrd_spawn(thrd_func_t fn, void_t args);
C_API values_type thrd_result(result_t value);

Expand All @@ -333,38 +332,32 @@ C_API void thrd_destroy(future_t);
C_API bool thrd_is_finish(future_t);

/**
* Creates an scoped container for arbitrary arguments passing to an single `args` function.
* Use `get_args()` or `args_in()` for retrieval.
* Creates an scoped `vector/array/container` for arbitrary arguments passing into an single `paramater` function.
* - Use standard `array access` for retrieval of an `union` storage type.
*
* @param scope callers context to bind `allocated` arguments to
* @param desc format, similar to `printf()`:
* * `i` unsigned integer,
* * `d` signed integer,
* * `l` signed long,
* * `z` size_t - max size,
* * `c` character,
* * `s` string,
* * `a` array,
* * `x` function,
* * `f` double/float,
* * `p` void pointer for any arbitrary object
* @param arguments indexed by `desc` format order
*/
C_API args_t raii_args_for(memory_t *scope, const char *desc, ...);
C_API args_t args_for(const char *desc, ...);

/**
* Returns generic union `values_type` of argument, will auto `release/free`
* allocated memory when scoped return/exit.
* - MUST CALL `args_destructor_set()` to have memory auto released
* within ~callers~ current scoped `context`, will happen either at return/exist or panics.
*
* - OTHERWISE `memory leak` will be shown in DEBUG build.
*
* Must be called at least once to release `allocated` memory.
* NOTE: `vector_for` does auto memory cleanup.
*
* @param params arbitrary arguments
* @param item index number
* @param count numbers of parameters, `0` will create empty `vector/array`.
* @param arguments indexed in given order.
*/
C_API values_type raii_get_args(memory_t *scope, void_t params, int item);
C_API values_type get_args(void *params, int item);
C_API values_type get_arg(void_t params);
C_API args_t args_for(size_t, ...);

#define array(count, ...) args_for(count, __VA_ARGS__)
#define array_defer(arr) args_destructor_set(arr)
#define vectorize(vec) vectors_t vec = vector_any()
#define vector(vec, count, ...) vectors_t vec = vector_for(nil, count, __VA_ARGS__)

#define $push_back(vec, value) vector_push_back(vec, (void_t)value)
#define $insert(vec, index, value) vector_insert(vec, index, (void_t)value)
#define $clear(vec) vector_clear(vec)
#define $size(vec) vector_size(vec)
#define $capacity(vec) vector_capacity(vec)
#define $erase(vec, index) vector_erase(vec, index)
```
### There is _1 way_ to create an smart memory pointer
Expand Down
99 changes: 29 additions & 70 deletions include/raii.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ typedef union {
uintptr_t **array_uint;
raii_func_t func;
char buffer[256];
} values_type, *vectors_t;
} values_type, *vectors_t, *args_t;

typedef struct {
values_type value;
Expand Down Expand Up @@ -180,20 +180,6 @@ struct memory_s {
future_deque_t *queued;
};

typedef struct args_s {
raii_type type;
int defer_set;
int is_returning;

unique_t *context;
size_t args_size;
/* total number of args in set */
size_t n_args;

/* allocated array of arguments */
raii_values_t *args;
} *args_t;

typedef void (*for_func_t)(i64, i64);
typedef void_t (*thrd_func_t)(args_t);
typedef void_t (*result_func_t)(void_t result, size_t id, values_type iter);
Expand Down Expand Up @@ -263,7 +249,6 @@ C_API raii_values_t *thrd_value(uintptr_t value);
C_API void thrd_init(size_t queue_size);
C_API future_t thrd_scope(void);
C_API future_t thrd_sync(future_t);
C_API result_t thrd_spawn_ex(thrd_func_t fn, const char *desc, ...);
C_API result_t thrd_spawn(thrd_func_t fn, void_t args);
C_API values_type thrd_result(result_t value);

Expand All @@ -273,56 +258,6 @@ C_API void thrd_then(result_func_t callback, future_t iter, void_t result);
C_API void thrd_destroy(future_t);
C_API bool thrd_is_finish(future_t);

/**
* `Release/free` allocated memory, must be called if not using `get_args()` function.
*
* @param params arbitrary arguments
*/
C_API void args_free(args_t params);

/**
* Creates an scoped container for arbitrary arguments passing to an single `args` function.
* Use `get_args()` or `args_in()` for retrieval.
*
* @param scope callers context to bind `allocated` arguments to
* @param desc format, similar to `printf()`:
* * `i` unsigned integer,
* * `d` signed integer,
* * `l` signed long,
* * `z` size_t - max size,
* * `c` character,
* * `s` string,
* * `a` array,
* * `x` function,
* * `f` double/float,
* * `p` void pointer for any arbitrary object
* @param arguments indexed by `desc` format order
*/
C_API args_t raii_args_for(memory_t *scope, const char *desc, ...);
C_API args_t args_for(const char *desc, ...);
C_API args_t raii_args_ex(memory_t *scope, const char *desc, va_list);

/**
* Returns generic union `values_type` of argument, will auto `release/free`
* allocated memory when scoped return/exit.
*
* Must be called at least once to release `allocated` memory.
*
* @param params arbitrary arguments
* @param item index number
*/
C_API values_type raii_get_args(memory_t *scope, void_t params, int item);
C_API values_type get_args(args_t params, int item);
C_API values_type get_arg(void_t params);

/**
* Returns generic union `values_type` of argument.
*
* @param params arguments instance
* @param index item number
*/
C_API values_type args_in(args_t params, size_t index);

C_API memory_t *raii_local(void);
/* Return current `thread` smart memory pointer. */
C_API memory_t *raii_init(void);
Expand Down Expand Up @@ -615,16 +550,40 @@ C_API bool is_base64(u_string_t src);
C_API int strpos(const char *text, char *pattern);

C_API vectors_t vector_any(void);
C_API vectors_t vector_of(vectors_t, size_t, ...);
C_API vectors_t vector_for(vectors_t, size_t, ...);
C_API void vector_insert(vectors_t vec, int pos, void_t val);
C_API void vector_clear(vectors_t);
C_API void vector_erase(vectors_t, int);
C_API size_t vector_size(vectors_t);
C_API size_t vector_capacity(vectors_t v);
C_API size_t vector_capacity(vectors_t);
C_API memory_t *vector_scope(vectors_t);
C_API void vector_push_back(vectors_t, void_t);

#define vectorize(vec) vectors_t vec = vector_any();
#define vector(vec, count, ...) vectors_t vec = nullptr; vec = vector_of(vec, count, __VA_ARGS__)
/**
* Creates an scoped `vector/array/container` for arbitrary arguments passing into an single `paramater` function.
* - Use standard `array access` for retrieval of an `union` storage type.
*
* - MUST CALL `args_destructor_set()` to have memory auto released
* within ~callers~ current scoped `context`, will happen either at return/exist or panics.
*
* - OTHERWISE `memory leak` will be shown in DEBUG build.
*
* NOTE: `vector_for` does auto memory cleanup.
*
* @param count numbers of parameters, `0` will create empty `vector/array`.
* @param arguments indexed in given order.
*/
C_API args_t args_for(size_t, ...);
C_API void args_destructor_set(args_t);
C_API void args_returning_set(args_t);
C_API bool is_args(args_t);
C_API bool is_args_returning(args_t);
C_API values_type get_arg(void_t);

#define array(count, ...) args_for(count, __VA_ARGS__)
#define array_defer(arr) args_destructor_set(arr)
#define vectorize(vec) vectors_t vec = vector_any()
#define vector(vec, count, ...) vectors_t vec = vector_for(nil, count, __VA_ARGS__)

#define $push_back(vec, value) vector_push_back(vec, (void_t)value)
#define $insert(vec, index, value) vector_insert(vec, index, (void_t)value)
Expand Down
25 changes: 11 additions & 14 deletions src/future.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static int thrd_raii_wrapper(void_t arg) {
res = f->func((args_t)f->arg);
} guarded_exception(f->value->scope);

promise_set(f->value, res, (is_type(f->arg, RAII_ARGS) && ((args_t)f->arg)->is_returning));
promise_set(f->value, res, is_args_returning((args_t)f->arg));
if (is_type(f, RAII_FUTURE_ARG)) {
if (is_pool) {
if (!is_empty(f->local))
Expand Down Expand Up @@ -375,8 +375,11 @@ values_type thrd_get(future f) {
result->value.array = r->value.array;
}

if (thrd_join(f->thread, NULL) == thrd_success)
if (thrd_join(f->thread, NULL) == thrd_success) {
if (!is_empty(f->value->scope->err))
raii_defer((func_t)thrd_delete, f);
promise_close(f->value);
}

thrd_delete(f);

Expand All @@ -396,12 +399,12 @@ RAII_INLINE void thrd_wait(future f, wait_func yield) {
}

RAII_INLINE raii_values_t *thrd_returning(args_t args, void_t value, size_t size) {
raii_deferred(args->context, RAII_FREE, value);
raii_values_t *result = (raii_values_t *)malloc_full(args->context, sizeof(raii_values_t), RAII_FREE);
raii_deferred(vector_scope(args), RAII_FREE, value);
raii_values_t *result = (raii_values_t *)malloc_full(vector_scope(args), sizeof(raii_values_t), RAII_FREE);
result->value.object = value;
result->size = size;
result->extended = value;
args->is_returning = true;
args_returning_set(args);
return result;
}

Expand Down Expand Up @@ -586,15 +589,6 @@ result_t thrd_spawn(thrd_func_t fn, void_t args) {
return result;
}

result_t thrd_spawn_ex(thrd_func_t fn, const char *desc, ...) {
va_list args;
va_start(args, desc);
args_t params = raii_args_ex(raii_local(), desc, args);
va_end(args);

return thrd_spawn(fn, (void_t)params);
}

future_t thrd_sync(future_t pool) {
if (!is_type(pool, RAII_SPAWN))
throw(logic_error);
Expand All @@ -618,6 +612,9 @@ future_t thrd_sync(future_t pool) {
results[id]->is_ready = true;
atomic_store_explicit(&pool->queue->results, results, memory_order_release);
future_close(pool->futures[i]);
if (!is_empty(pool->futures[i]->value->scope->err))
raii_defer((func_t)thrd_delete, pool->futures[i]);

promise_close(pool->futures[i]->value);
}

Expand Down
Loading

0 comments on commit 62ff583

Please sign in to comment.