summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authortylov <[email protected]>2023-07-20 14:42:00 +0200
committertylov <[email protected]>2023-07-20 15:00:02 +0200
commit224a04f7fa7549ed94d2a1415eb25829e39a7cca (patch)
tree1d1be262be71b512d334ded99741a64f2aa6ca67 /include
parent313c1d7bb9b92e75801429c1f7f132589860292e (diff)
downloadSTC-modified-224a04f7fa7549ed94d2a1415eb25829e39a7cca.tar.gz
STC-modified-224a04f7fa7549ed94d2a1415eb25829e39a7cca.zip
Added Task-object to coroutines and true stackless execution.
Diffstat (limited to 'include')
-rw-r--r--include/stc/algo/coroutine.h50
-rw-r--r--include/stc/cspan.h12
-rw-r--r--include/stc/cstr.h34
3 files changed, 57 insertions, 39 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h
index e4c0915c..7c6989c3 100644
--- a/include/stc/algo/coroutine.h
+++ b/include/stc/algo/coroutine.h
@@ -64,9 +64,8 @@ enum {
};
typedef enum {
CCO_DONE = 0,
- CCO_YIELD = 1,
- CCO_AWAIT = 2,
- CCO_ERROR = -1,
+ CCO_AWAIT = 1<<0,
+ CCO_YIELD = 1<<1,
} cco_result;
#define cco_initial(co) ((co)->cco_state == 0)
@@ -132,26 +131,45 @@ typedef enum {
(void)((co)->cco_state = 0)
/*
- * Closure (optional)
+ * Tasks (optional)
*/
-#define cco_closure(Name, ...) \
+struct cco_runtime;
+
+#define cco_task_struct(Name, ...) \
struct Name { \
- int (*cco_fn)(struct Name*); \
- int cco_state; \
+ int (*cco_fn)(struct Name*, struct cco_runtime*); \
+ int cco_state, cco_expect; \
__VA_ARGS__ \
}
-typedef struct cco_base {
- int (*cco_fn)(struct cco_base*);
- int cco_state;
-} cco_base;
+typedef cco_task_struct(cco_task, /**/) cco_task;
+
+typedef struct cco_runtime {
+ int result, top;
+ cco_task* stack[];
+} cco_runtime;
+
+#define cco_cast_task(task) \
+ ((cco_task *)(task) + 0*sizeof((task)->cco_fn(task, (cco_runtime*)0) + ((int*)0 == &(task)->cco_state)))
-#define cco_resume(closure) \
- (closure)->cco_fn(closure)
+#define cco_resume(task, rt) \
+ (task)->cco_fn(task, rt)
-#define cco_cast(closure) \
- ((cco_base *)(closure) + 0*sizeof((cco_resume(closure), (int*)0 == &(closure)->cco_state)))
+#define cco_block_task(...) c_MACRO_OVERLOAD(cco_block_task, __VA_ARGS__)
+#define cco_block_task_1(task) cco_block_task_3(task, rt, 16)
+#define cco_block_task_3(task, rt, STACKDEPTH) \
+ for (struct { int result, top; cco_task* stack[STACKDEPTH]; } rt = {.stack={cco_cast_task(task)}}; \
+ (((rt.result = cco_resume(rt.stack[rt.top], (cco_runtime*)&rt)) & rt.stack[rt.top]->cco_expect) || --rt.top >= 0); )
+
+#define cco_await_task(...) c_MACRO_OVERLOAD(cco_await_task, __VA_ARGS__)
+#define cco_await_task_2(task, rt) cco_await_task_3(task, rt, CCO_DONE)
+#define cco_await_task_3(task, rt, resultbits) \
+ do { \
+ cco_runtime* _rt = rt; \
+ (_rt->stack[++_rt->top] = cco_cast_task(task))->cco_expect = ~(resultbits); \
+ cco_yield_v(CCO_AWAIT); \
+ } while (0)
/*
* Semaphore
@@ -182,7 +200,7 @@ typedef struct { intptr_t count; } cco_sem;
#else
#define _c_LINKC __declspec(dllimport)
#endif
- #if _WIN32_WINNT < _WIN32_WINNT_WIN8 || defined __TINYC__
+ #if 1 // _WIN32_WINNT < _WIN32_WINNT_WIN8 || defined __TINYC__
#define _c_getsystime GetSystemTimeAsFileTime
#else
#define _c_getsystime GetSystemTimePreciseAsFileTime
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index dcb02961..08045010 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -80,19 +80,19 @@ int demo2() {
STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \
return (Self){.data=raw, .shape={(int32_t)n}}; \
} \
- STC_INLINE Self Self##_slice_(Self##_value* v, const int32_t shape[], const int32_t stri[], \
+ STC_INLINE Self Self##_slice_(Self##_value* d, const int32_t shape[], const int32_t stri[], \
const int rank, const int32_t a[][2]) { \
- Self s; s.data = v; int outrank; \
- s.data += _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \
+ Self s; int outrank; \
+ s.data = d + _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \
c_assert(outrank == RANK); \
return s; \
} \
STC_INLINE Self##_iter Self##_begin(const Self* self) { \
- Self##_iter it = {.ref=self->data, .pos={0}, ._s=self}; \
+ Self##_iter it = {.ref=self->data, ._s=self}; \
return it; \
} \
STC_INLINE Self##_iter Self##_end(const Self* self) { \
- Self##_iter it = {.ref=NULL}; \
+ Self##_iter it = {0}; \
return it; \
} \
STC_INLINE void Self##_next(Self##_iter* it) { \
@@ -120,7 +120,7 @@ using_cspan_tuple(7); using_cspan_tuple(8);
#define cspan_init(SpanType, ...) \
{.data=(SpanType##_value[])__VA_ARGS__, .shape={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}, .stride={.d={1}}}
-/* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */
+/* create a cspan from a cvec, cstack, or cpque (heap) */
#define cspan_from(container) \
{.data=(container)->data, .shape={(int32_t)(container)->_len}, .stride={.d={1}}}
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index 17943ad5..2648e267 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -170,14 +170,14 @@ STC_INLINE intptr_t cstr_capacity(const cstr* self)
// utf8 methods defined in/depending on src/utf8code.c:
-STC_API cstr cstr_casefold_sv(csview sv);
-STC_API cstr cstr_tolower_sv(csview sv);
-STC_API cstr cstr_toupper_sv(csview sv);
-STC_API cstr cstr_tolower(const char* str);
-STC_API cstr cstr_toupper(const char* str);
-STC_API void cstr_lowercase(cstr* self);
-STC_API void cstr_uppercase(cstr* self);
-STC_API bool cstr_valid_utf8(const cstr* self);
+extern cstr cstr_casefold_sv(csview sv);
+extern cstr cstr_tolower_sv(csview sv);
+extern cstr cstr_toupper_sv(csview sv);
+extern cstr cstr_tolower(const char* str);
+extern cstr cstr_toupper(const char* str);
+extern void cstr_lowercase(cstr* self);
+extern void cstr_uppercase(cstr* self);
+extern bool cstr_valid_utf8(const cstr* self);
// other utf8
@@ -394,7 +394,7 @@ fn_tocase[] = {{tolower, utf8_casefold},
{tolower, utf8_tolower},
{toupper, utf8_toupper}};
-STC_DEF cstr cstr_tocase(csview sv, int k) {
+static cstr cstr_tocase(csview sv, int k) {
cstr out = cstr_init();
char *buf = cstr_reserve(&out, sv.size*3/2);
const char *end = sv.str + sv.size;
@@ -415,28 +415,28 @@ STC_DEF cstr cstr_tocase(csview sv, int k) {
return out;
}
-STC_DEF cstr cstr_casefold_sv(csview sv)
+cstr cstr_casefold_sv(csview sv)
{ return cstr_tocase(sv, 0); }
-STC_DEF cstr cstr_tolower_sv(csview sv)
+cstr cstr_tolower_sv(csview sv)
{ return cstr_tocase(sv, 1); }
-STC_DEF cstr cstr_toupper_sv(csview sv)
+cstr cstr_toupper_sv(csview sv)
{ return cstr_tocase(sv, 2); }
-STC_DEF cstr cstr_tolower(const char* str)
+cstr cstr_tolower(const char* str)
{ return cstr_tolower_sv(c_sv(str, c_strlen(str))); }
-STC_DEF cstr cstr_toupper(const char* str)
+cstr cstr_toupper(const char* str)
{ return cstr_toupper_sv(c_sv(str, c_strlen(str))); }
-STC_DEF void cstr_lowercase(cstr* self)
+void cstr_lowercase(cstr* self)
{ cstr_take(self, cstr_tolower_sv(cstr_sv(self))); }
-STC_DEF void cstr_uppercase(cstr* self)
+void cstr_uppercase(cstr* self)
{ cstr_take(self, cstr_toupper_sv(cstr_sv(self))); }
-STC_DEF bool cstr_valid_utf8(const cstr* self)
+bool cstr_valid_utf8(const cstr* self)
{ return utf8_valid(cstr_str(self)); }
#endif // i_import