summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-06-14 13:24:29 +0200
committerTyge Løvset <[email protected]>2023-06-14 13:24:29 +0200
commit3f919a3b38a88e1c96399cd6096dec16060802a1 (patch)
tree8c071e33522d31cd7c90834423760da514fc7f46
parentc51bdc8d8aeac63c0af955f81593ef0be326a7e0 (diff)
downloadSTC-modified-3f919a3b38a88e1c96399cd6096dec16060802a1.tar.gz
STC-modified-3f919a3b38a88e1c96399cd6096dec16060802a1.zip
Fixed a bug in cco_await_on(), and added _pull() function to random access containers (moves element out of container, ie no drop).
-rw-r--r--include/stc/algo/coroutine.h4
-rw-r--r--include/stc/cdeq.h7
-rw-r--r--include/stc/cqueue.h9
-rw-r--r--include/stc/cstack.h5
-rw-r--r--include/stc/cvec.h4
-rw-r--r--misc/examples/scheduler.c9
6 files changed, 27 insertions, 11 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h
index 61581f64..e0952e1f 100644
--- a/include/stc/algo/coroutine.h
+++ b/include/stc/algo/coroutine.h
@@ -93,8 +93,8 @@ enum {
#define cco_resume(closure) (closure)->coroutine(closure)
#define cco_await_on(...) c_MACRO_OVERLOAD(cco_await_on, __VA_ARGS__)
-#define cco_await_on_1(closure) cco_await_on_2(closure, cco_resume(closure))
-#define cco_await_on_2(co, func) cco_await_1((func(co), !cco_done(co)))
+#define cco_await_on_1(closure) cco_await_2((cco_resume(closure), cco_done(closure)), )
+#define cco_await_on_2(co, func) cco_await_2((func(co), cco_done(co)), )
#define cco_block_on(...) c_MACRO_OVERLOAD(cco_block_on, __VA_ARGS__)
#define cco_block_on_1(closure) while (cco_resume(closure), !cco_done(closure))
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index 8bb62602..a406c2b0 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -22,6 +22,7 @@
*/
#define _i_prefix cdeq_
#define _pop _pop_front
+#define _pull _pull_front
#ifdef i_more
#include "cqueue.h"
#define i_more
@@ -55,6 +56,12 @@ _cx_memb(_pop_back)(_cx_self* self) {
i_keydrop((self->data + self->end));
}
+STC_INLINE _cx_value _cx_memb(_pull_back)(_cx_self* self) { // move back out of deq
+ assert(!_cx_memb(_empty)(self));
+ self->end = (self->end - 1) & self->capmask;
+ return self->data[self->end];
+}
+
STC_INLINE _cx_iter
_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, const _cx_value val) {
intptr_t idx = _cdeq_toidx(self, it.pos);
diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h
index 3adc1bcb..aa3d7384 100644
--- a/include/stc/cqueue.h
+++ b/include/stc/cqueue.h
@@ -93,6 +93,13 @@ STC_INLINE void _cx_memb(_pop)(_cx_self* self) { // pop_front
self->start = (self->start + 1) & self->capmask;
}
+STC_INLINE _cx_value _cx_memb(_pull)(_cx_self* self) { // move front out of queue
+ assert(!_cx_memb(_empty)(self));
+ intptr_t s = self->start;
+ self->start = (s + 1) & self->capmask;
+ return self->data[s];
+}
+
STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) {
if (self->data == other->data) return;
_cx_memb(_drop)(self);
@@ -162,7 +169,7 @@ _cx_memb(_reserve)(_cx_self* self, const intptr_t n) {
if (!data)
return false;
intptr_t head = oldcap - self->start;
- if (self->start < self->end || self->start == 0)
+ if (self->start <= self->end)
;
else if (head < self->end) {
self->start = newcap - head;
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index fb4eae4b..5f0ffe2b 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -129,7 +129,10 @@ STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) {
}
STC_INLINE void _cx_memb(_pop)(_cx_self* self)
- { assert(!_cx_memb(_empty)(self)); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); }
+ { assert(self->_len); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); }
+
+STC_INLINE _cx_value _cx_memb(_pull)(_cx_self* self)
+ { assert(self->_len); return self->data[--self->_len]; }
STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n)
{ while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); }
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index 874f4f47..71787733 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -133,7 +133,9 @@ STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->da
STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self)
{ return self->data + self->_len - 1; }
STC_INLINE void _cx_memb(_pop)(_cx_self* self)
- { assert(!_cx_memb(_empty)(self)); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); }
+ { assert(self->_len); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); }
+STC_INLINE _cx_value _cx_memb(_pull)(_cx_self* self)
+ { assert(self->_len); return self->data[--self->_len]; }
STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value)
{ return _cx_memb(_push)(self, value); }
STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); }
diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c
index aecb1a26..54fefc47 100644
--- a/misc/examples/scheduler.c
+++ b/misc/examples/scheduler.c
@@ -2,12 +2,9 @@
#include <stdio.h>
#include <stc/calgo.h>
-struct Scheduler;
-struct Task {
- bool (*resume)(struct Task*);
+cco_closure(bool, Task,
struct Scheduler* sched;
- int cco_state;
-};
+);
#define i_type Scheduler
#define i_val struct Task
@@ -20,7 +17,7 @@ static bool schedule(Scheduler* sched)
Scheduler_pop(sched);
if (!cco_done(&task))
- task.resume(&task);
+ cco_resume(&task);
return !Scheduler_empty(sched);
}