Skip to content

Commit d10a2e0

Browse files
committed
✨ ztdc_thrd_ignore_all_attrs_errors and ztdc_thrd_stop_on_attr_error functions for use!
1 parent 708b14e commit d10a2e0

File tree

7 files changed

+65
-25
lines changed

7 files changed

+65
-25
lines changed

documentation/source/api.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ There's only a limited number of APIs to document, as the rest is covered by the
5757

5858

5959

60-
``ztdc_thrd_attr_err_func_t`` function type
61-
-------------------------------------------
60+
``ztdc_thrd_attr_err_func_t`` function type and functions
61+
---------------------------------------------------------
6262

6363
.. doxygentypedef:: ztdc_thrd_attr_err_func_t
6464

65+
.. doxygenfunction:: ztdc_thrd_ignore_all_attr_errors
66+
67+
.. doxygenfunction:: ztdc_thrd_stop_on_attr_error
68+
6569

6670

6771
``ztdc_thrd_create_attrs_err`` function

examples/source/custom_attribute_arbitrary_fail.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ inline static int thread_main(void* arg) {
7777
// attribute function `thread_custom_on_origin_attr`.
7878
}
7979

80-
inline static int thread_attr_passthrough(const ztdc_thrd_attr_kind* kind, int err, void* userdata) {
80+
inline static int thread_attr_check_error(const ztdc_thrd_attr_kind* kind, int err, void* userdata) {
8181
(void)kind;
8282
(void)userdata;
8383

@@ -111,7 +111,7 @@ int main(void) {
111111
};
112112

113113
int create_err0 = ztdc_thrd_create_attrs_err(
114-
&t0, thread_main, NULL, ztdc_c_array_size(attrs), attrs, thread_attr_passthrough, NULL);
114+
&t0, thread_main, NULL, ztdc_c_array_size(attrs), attrs, thread_attr_check_error, NULL);
115115
// creation error is always identical to whatever the error function returns, if necessary
116116
ZTD_ASSERT(create_err0 == thrd_timedout);
117117
ZTD_ASSERT(accumulator == EXPECTED_ACCUMULATION_VALUE);

examples/source/custom_attribute_guaranteed_order.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int main(void) {
9696
};
9797

9898
int create_err0 = ztdc_thrd_create_attrs_err(
99-
&t0, thread_main, &accumulator, ztdc_c_array_size(attrs), attrs, thread_attr_passthrough, NULL);
99+
&t0, thread_main, &accumulator, ztdc_c_array_size(attrs), attrs, ztdc_thrd_stop_on_attr_error, NULL);
100100
ZTD_ASSERT(create_err0 == thrd_success);
101101

102102
int res0 = 0;

examples/source/custom_attribute_name.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@
4848

4949
enum { EXPECTED_MARKER_VALUE = 32 };
5050

51-
inline static int thrd_custom_name(thrd_t t, ztdc_thrd_native_handle_t t_handle, ztdc_thrd_id_t t_id, void* userdata) {
51+
inline static int thread_custom_name_attr(
52+
thrd_t t, ztdc_thrd_native_handle_t t_handle, ztdc_thrd_id_t t_id, void* userdata) {
5253
(void)t;
5354
(void)t_handle;
5455
(void)t_id;
5556
int custom_marker = *((const int*)userdata);
56-
printf("thrd_custom_name called with custom maker: %d\n", custom_marker);
57+
printf("thread_custom_name_attr called with custom marker: %d\n", custom_marker);
5758
if (custom_marker != EXPECTED_MARKER_VALUE) {
5859
return thrd_error;
5960
}
@@ -85,7 +86,7 @@ int main(void) {
8586

8687
const ztdc_thrd_attr_custom_on_new custom_attr = {
8788
.kind = ztdc_thrd_attr_kind_custom_on_new,
88-
.func = thrd_custom_name,
89+
.func = thread_custom_name_attr,
8990
.userdata = &custom_marker,
9091
};
9192

include/ztd/thread/threads.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ int thrd_sleep(const struct timespec* __duration, struct timespec* __remaining);
139139
typedef int(ztdc_thrd_attr_err_func_t)(const ztdc_thrd_attr_kind* __kind, int __err, void* __userdata);
140140

141141

142+
ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
143+
ZTD_USE(ZTD_THREAD_API_LINKAGE)
144+
/// @brief A ztdc_thrd_attr_err_func_t that returns `thrd_success` unconditionally.
145+
///
146+
/// @param[in] __kind The thread attribute kind. Ignored.
147+
/// @param[in] __err The error that occurred. Ignored.
148+
/// @param[in] __userdata A pointer to some userdata, or null. Ignored.
149+
///
150+
/// @return `thrd_success`.
151+
int ztdc_thrd_ignore_all_attr_errors(const ztdc_thrd_attr_kind* __kind, int __err, void* __userdata);
152+
153+
ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
154+
ZTD_USE(ZTD_THREAD_API_LINKAGE)
155+
/// @brief A ztdc_thrd_attr_err_func_t that returns the error it was called with unconditionally. Has the effect of
156+
/// stopping on the first thread attribute that errors.
157+
///
158+
/// @param[in] __kind The thread attribute kind. Ignored.
159+
/// @param[in] __err The error that occurred.
160+
/// @param[in] __userdata A pointer to some userdata, or null. Ignored.
161+
///
162+
/// @return `__err`.
163+
int ztdc_thrd_stop_on_attr_error(const ztdc_thrd_attr_kind* __kind, int __err, void* __userdata);
164+
142165
ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
143166
ZTD_USE(ZTD_THREAD_API_LINKAGE)
144167
/// @brief Starts a thread and configures that thread with the given thread attributes, if any. Any failure in the

source/ztd/thread/threads.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,29 @@ const ztdc_thrd_id_t ztdc_thrd_null_id =
5858
#endif
5959
;
6060

61-
inline static int __ztdc_ignore_all_thrd_errors(const ztdc_thrd_attr_kind* __kind, int __err, void* __userdata) {
61+
ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
62+
ZTD_USE(ZTD_THREAD_API_LINKAGE)
63+
int ztdc_thrd_ignore_all_attr_errors(const ztdc_thrd_attr_kind* __kind, int __err, void* __userdata) {
6264
(void)__kind;
6365
(void)__err;
6466
(void)__userdata;
6567
return thrd_success;
6668
}
6769

70+
ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
71+
ZTD_USE(ZTD_THREAD_API_LINKAGE)
72+
int ztdc_thrd_stop_on_attr_error(const ztdc_thrd_attr_kind* __kind, int __err, void* __userdata) {
73+
(void)__kind;
74+
(void)__userdata;
75+
return __err;
76+
}
77+
6878
ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
6979
ZTD_USE(ZTD_THREAD_API_LINKAGE)
7080
int ztdc_thrd_create_attrs(
7181
thrd_t* __thr, thrd_start_t __func, void* __func_arg, size_t __attrs_size, const ztdc_thrd_attr_kind** __attrs) {
7282
return ztdc_thrd_create_attrs_err(
73-
__thr, __func, __func_arg, __attrs_size, __attrs, __ztdc_ignore_all_thrd_errors, NULL);
83+
__thr, __func, __func_arg, __attrs_size, __attrs, ztdc_thrd_ignore_all_attr_errors, NULL);
7484
}
7585

7686
ZTD_USE(ZTD_C_LANGUAGE_LINKAGE)
@@ -90,7 +100,7 @@ int ztdc_thrd_create_attrs_err(thrd_t* __thr, thrd_start_t __func, void* __func_
90100
return thrd_success;
91101
}
92102
if (!__attr_err_func) {
93-
__attr_err_func = __ztdc_ignore_all_thrd_errors;
103+
__attr_err_func = ztdc_thrd_ignore_all_attr_errors;
94104
}
95105
#if ZTD_IS_ON(ZTD_THREAD_PTHREAD_BASED)
96106
return __ztdc_pthreads_thrd_create_attrs_err(

source/ztd/thread/threads.windows.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ int __ztdc_win32_thrd_create_attrs_err(thrd_t* __thr, thrd_start_t __func, void*
508508
// store to invoke later (on the new thread, if success)
509509
ztdc_thrd_attr_custom_on_new* __attr = (ztdc_thrd_attr_custom_on_new*)__attr_kind;
510510
__trampoline_userdata->__custom_on_new_attr = __attr;
511-
}
511+
} break;
512512
default:
513513
// unrecognized attribute
514514
__attr_err = thrd_error;
@@ -518,7 +518,7 @@ int __ztdc_win32_thrd_create_attrs_err(thrd_t* __thr, thrd_start_t __func, void*
518518
int __attr_err_res = __attr_err_func(__attr_kind, __attr_err, __attr_err_func_arg);
519519
if (__attr_err_res != thrd_success) {
520520
free(__trampoline_userdata);
521-
return thrd_success;
521+
return __attr_err_res;
522522
}
523523
}
524524
}
@@ -661,32 +661,34 @@ int __ztdc_win32_thrd_create_attrs_err(thrd_t* __thr, thrd_start_t __func, void*
661661
// spinlock until the thread is started and all internal work is okay.
662662
}
663663
// return any from-thread failures and bail if necessary
664-
int __post_thread_start_results = __sync_result;
665-
if (__post_thread_start_results != thrd_success) {
666-
__post_thread_start_results = __attr_err_func(__sync_kind, __post_thread_start_results, __attr_err_func_arg);
667-
__sync_result = __post_thread_start_results;
664+
int __thread_setup_result = __sync_result;
665+
if (__thread_setup_result != thrd_success) {
666+
int __processed_thread_setup_result
667+
= __attr_err_func(__sync_kind, __thread_setup_result, __attr_err_func_arg);
668+
__sync_result = __processed_thread_setup_result;
668669
// send back the result to the thread so it knows to either go or quit
669670
atomic_store(&__sync_still_ok, false);
670-
if (__post_thread_start_results != thrd_success) {
671+
if (__processed_thread_setup_result != thrd_success) {
671672
CloseHandle(*__handle);
672673
*__thr = __original_thr;
673-
return __post_thread_start_results;
674+
return __processed_thread_setup_result;
674675
}
675676
}
676677
// if all goes well, then we check out origin custom error bit
677678
// invoke our origin custom thread work if possible
678679
if (__custom_on_origin_attr) {
679-
int __custom_err = __custom_on_origin_attr->func(*__thr, *__ztdc_win32_handle_ptr(__thr),
680+
int __custom_origin_err = __custom_on_origin_attr->func(*__thr, *__ztdc_win32_handle_ptr(__thr),
680681
*__ztdc_win32_handle_id(__thr), __custom_on_origin_attr->userdata);
681-
if (__custom_err != thrd_success) {
682-
__custom_err = __attr_err_func(__sync_kind, __custom_err, __attr_err_func_arg);
683-
__sync_result = __post_thread_start_results;
682+
if (__custom_origin_err != thrd_success) {
683+
int __processed_custom_origin_err
684+
= __attr_err_func(__sync_kind, __custom_origin_err, __attr_err_func_arg);
685+
__sync_result = __processed_custom_origin_err;
684686
// send back the result to the thread so it knows to either go or quit
685687
atomic_store(&__sync_still_ok, false);
686-
if (__post_thread_start_results != thrd_success) {
688+
if (__processed_custom_origin_err != thrd_success) {
687689
CloseHandle(*__handle);
688690
*__thr = __original_thr;
689-
return __post_thread_start_results;
691+
return __processed_custom_origin_err;
690692
}
691693
}
692694
}

0 commit comments

Comments
 (0)