66#include <stdlib.h>
77#include <time.h>
88#include <stdio.h>
9- #ifndef __STDC_NO_THREADS__
9+
10+ #if !defined(__STDC_NO_THREADS__ ) && DETECTED_THREAD_IMPL == 1 // C
1011#include <threads.h>
12+ #define THREADS_INCLUDED 1
13+ #define THREAD_RET int
14+ #define THREAD_RET_OK thrd_success
15+ #elif DETECTED_THREAD_IMPL == 2 // POSIX
16+ #include <pthread.h>
17+ #define PTHREAD_INCLUDED 1
18+ #define THREAD_RET void*
19+ #define THREAD_RET_OK NULL
20+ #else
21+ #define THREAD_RET int
22+ #define THREAD_RET_OK 0
1123#endif
1224
1325static size_t default_reservd = 0 ;
@@ -18,7 +30,7 @@ typedef struct {
1830 size_t test_data_len ;
1931} bench_t ;
2032
21- static int bench_cpp (void * userdata ) {
33+ static THREAD_RET bench_cpp (void * userdata ) {
2234 bench_t * const data = userdata ;
2335 struct timespec t [2 ] = {};
2436 cpp_vector (char ) * cpp = NULL ;
@@ -37,14 +49,10 @@ static int bench_cpp(void* userdata) {
3749 if (b )
3850 data -> res_time += (((double )t [1 ].tv_nsec )/1e9 )+ (t [1 ].tv_sec );
3951 }
40- #ifndef __STDC_NO_THREADS__
41- return thrd_success ;
42- #else
43- return 0 ;
44- #endif
52+ return THREAD_RET_OK ;
4553}
4654
47- static int bench_yavl (void * userdata ) {
55+ static THREAD_RET bench_yavl (void * userdata ) {
4856 bench_t * const data = userdata ;
4957 struct timespec t [2 ] = {};
5058 yavl_vec_t yavl = YAVL_VEC_T_ALLOCATOR ;
@@ -62,14 +70,10 @@ static int bench_yavl(void* userdata) {
6270 if (b )
6371 data -> res_time += ((double )t [1 ].tv_nsec /1e9 )+ (t [1 ].tv_sec );
6472 }
65- #ifndef __STDC_NO_THREADS__
66- return thrd_success ;
67- #else
68- return 0 ;
69- #endif
73+ return THREAD_RET_OK ;
7074}
7175
72- static int bench_inline (void * userdata ) {
76+ static THREAD_RET bench_inline (void * userdata ) {
7377 bench_t * const data = userdata ;
7478 struct timespec t [2 ] = {};
7579 data -> res_time = 0 ;
@@ -98,11 +102,7 @@ static int bench_inline(void* userdata) {
98102 if (b )
99103 data -> res_time += ((double )t [1 ].tv_nsec /1e9 )+ (t [1 ].tv_sec );
100104 }
101- #ifndef __STDC_NO_THREADS__
102- return thrd_success ;
103- #else
104- return 0 ;
105- #endif
105+ return THREAD_RET_OK ;
106106}
107107
108108it_should (push_not_slower_than_cpp ) {
@@ -129,9 +129,12 @@ it_should(push_not_slower_than_cpp) {
129129 {.test_data = tdata , .test_data_len = tdata_len },
130130 {.test_data = tdata , .test_data_len = tdata_len },
131131 };
132- #ifndef __STDC_NO_THREADS__
132+ #if THREADS_INCLUDED
133133 thrd_t thr [3 ];
134134 thrd_start_t funs [3 ] = { bench_yavl ,bench_cpp ,bench_inline };
135+ #elif PTHREAD_INCLUDED
136+ pthread_t thr [3 ];
137+ typeof (& bench_yavl ) funs [3 ] = { bench_yavl ,bench_cpp ,bench_inline };
135138 #else
136139 typeof (& bench_yavl ) funs [3 ] = { bench_yavl ,bench_cpp ,bench_inline };
137140 #endif
@@ -145,19 +148,27 @@ it_should(push_not_slower_than_cpp) {
145148 ind [j ] = temp ;
146149 }
147150
148- #ifndef __STDC_NO_THREADS__
151+ #if THREADS_INCLUDED || PTHREAD_INCLUDED
149152 printf ("Running benchmarks in their own threads...\n" );
150153 // Random thread creation, ordered thread collection
151154 for (size_t i = 0 ;i < sizeof (thr )/sizeof (thr [0 ]);++ i ){
152155 printf (" * Starting \"%s\"...\n" ,strs [ind [i ]]);
156+ #if THREADS_INCLUDED
153157 thrd_create (& thr [ind [i ]], funs [ind [i ]], & bdata [ind [i ]]);
158+ #else
159+ pthread_create (& thr [ind [i ]], NULL , funs [ind [i ]], & bdata [ind [i ]]);
160+ #endif
154161 }
155162 for (size_t i = 0 ;i < sizeof (thr )/sizeof (thr [0 ]);++ i ){
163+ #if THREADS_INCLUDED
156164 thrd_join (thr [i ], NULL );
165+ #else
166+ pthread_join (thr [i ], NULL );
167+ #endif
157168 printf (" * \"%s\" finished! \n" ,strs [i ]);
158169 }
159170 #else
160- printf ("Running in single thread only (no standard threads impl)\n" );
171+ printf ("Running in single thread only (no supported threads impl)\n" );
161172 for (size_t i = 0 ;i < sizeof (funs )/sizeof (funs [0 ]);++ i ){
162173 printf (" * Running \"%s\"...\n" ,strs [ind [i ]]);
163174 funs [ind [i ]](& bdata [ind [i ]]);
0 commit comments