From 349ba35358f10923a62947aaee8056d14f1be74a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 14 Jan 2022 15:33:12 +0100 Subject: Fixed function linkage spec in cstr. Moved typedefs of cstr and csview to forward.h- --- examples/threads_demo.c | 21 +++++++++++---------- include/stc/cstr.h | 4 +--- include/stc/csview.h | 5 +---- include/stc/forward.h | 7 +++++++ 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/examples/threads_demo.c b/examples/threads_demo.c index 667ffb5b..24ac951a 100644 --- a/examples/threads_demo.c +++ b/examples/threads_demo.c @@ -19,10 +19,10 @@ mtx_t mtx; void* thr(BaseArc* p) { - thrd_sleep(&(struct timespec){.tv_sec=1}, NULL); // sleep 1 sec + struct timespec one_sec = {.tv_sec=1}; + thrd_sleep(&one_sec, NULL); BaseArc lp = BaseArc_clone(*p); // thread-safe, even though the // shared use_count is incremented - c_autoscope (mtx_lock(&mtx), mtx_unlock(&mtx)) { printf("local pointer in a thread:\n" @@ -30,7 +30,6 @@ void* thr(BaseArc* p) /* safe to modify base here */ lp.get->value += 1; } - /* atomically decrease ref. */ BaseArc_drop(&lp); BaseArc_drop(p); return NULL; @@ -43,17 +42,19 @@ int main() mtx_init(&mtx, mtx_plain); printf("Created a shared Base\n" " p.get() = %p, p.use_count() = %ld\n", (void*)p.get, BaseArc_use_count(p)); - thrd_t t1, t2, t3; - thrd_create(&t1, (thrd_start_t)thr, (BaseArc[]){BaseArc_clone(p)}); - thrd_create(&t2, (thrd_start_t)thr, (BaseArc[]){BaseArc_clone(p)}); - thrd_create(&t3, (thrd_start_t)thr, (BaseArc[]){BaseArc_clone(p)}); + enum {N = 3}; + struct { thrd_t t; BaseArc p; } task[N]; + c_forrange (i, N) { + task[i].p = BaseArc_clone(p); + thrd_create(&task[i].t, (thrd_start_t)thr, &task[i].p); + } BaseArc_reset(&p); - printf("Shared ownership between 3 threads and released\n" + printf("Shared ownership between %d threads and released\n" "ownership from main:\n" - " p.get() = %p, p.use_count() = %ld\n", (void*)p.get, BaseArc_use_count(p)); + " p.get() = %p, p.use_count() = %ld\n", N, (void*)p.get, BaseArc_use_count(p)); - thrd_join(t1, NULL); thrd_join(t3, NULL); thrd_join(t3, NULL); + c_forrange (i, N) thrd_join(task[i].t, NULL); printf("All threads completed, the last one deleted Base\n"); } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 04592b69..91bcc7ff 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -24,14 +24,13 @@ #define CSTR_H_INCLUDED #include "ccommon.h" +#include "forward.h" #include /* malloc */ #include #include #include /* vsnprintf */ #include -typedef struct cstr { char* str; } cstr; -typedef char cstr_value; #define cstr_npos (SIZE_MAX >> 1) typedef struct { size_t size, cap; char str[]; } _cstr_rep_t; @@ -62,7 +61,6 @@ STC_API size_t cstr_find_n(cstr s, const char* needle, size_t pos, size STC_API bool cstr_getdelim(cstr *self, int delim, FILE *stream); STC_API char* c_strnstrn(const char* s, const char* needle, size_t slen, size_t nlen); -STC_API int c_strncasecmp(const char* s1, const char* s2, size_t nmax); STC_INLINE cstr cstr_init() { return cstr_null; } #define cstr_str(self) (self)->str diff --git a/include/stc/csview.h b/include/stc/csview.h index f3a0ae11..7370336a 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -24,12 +24,9 @@ #define CSVIEW_H_INCLUDED #include "ccommon.h" +#include "forward.h" #include "utf8.h" -typedef struct csview { const char* str; size_t size; } csview; -typedef union csview_iter { const char *ref; csview cp; } csview_iter; -typedef char csview_value; - #define csview_null c_make(csview){"", 0} #define csview_npos (SIZE_MAX >> 1) #define c_PRIsv "%.*s" diff --git a/include/stc/forward.h b/include/stc/forward.h index ec30b922..0a14ad93 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -40,6 +40,13 @@ #define forward_cqueue(CX, VAL) _c_cdeq_types(CX, VAL) #define forward_cvec(CX, VAL) _c_cvec_types(CX, VAL) +typedef struct cstr { char* str; } cstr; +typedef char cstr_value; + +typedef struct csview { const char* str; size_t size; } csview; +typedef union csview_iter { const char *ref; csview cp; } csview_iter; +typedef char csview_value; + #ifndef MAP_SIZE_T #define MAP_SIZE_T uint32_t #endif -- cgit v1.2.3