diff options
| author | Tyge Løvset <[email protected]> | 2023-06-01 00:14:20 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-06-01 00:14:20 +0200 |
| commit | c82dffc657faedba4c7af75792aa26287d9cf9bc (patch) | |
| tree | 67021ee36d01a868a5a89cf444b8613dad6e463c | |
| parent | c23a90112ffc50ed5977874ec31cf4fd3d4afd9b (diff) | |
| download | STC-modified-c82dffc657faedba4c7af75792aa26287d9cf9bc.tar.gz STC-modified-c82dffc657faedba4c7af75792aa26287d9cf9bc.zip | |
Changed API for cco_timer and cco_sem.
| -rw-r--r-- | docs/ccommon_api.md | 25 | ||||
| -rw-r--r-- | include/stc/algo/coroutine.h | 47 |
2 files changed, 38 insertions, 34 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index fc4f196b..4d18120a 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -379,20 +379,21 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_yield(ret)` | Yield/suspend execution and return ret | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | +| | `cco_sleep(long msec)` | Sleep for milliseconds | | | Semaphores: | | -| | `csem` | Semaphore type | -| | `cco_await_sem(sem)` | Await for the semaphore count > 0 | -| | `cco_await_sem(sem, ret)` | Await with ret on the semaphore | -| | `csem_set(sem, long value)` | Set semaphore value | -| | `csem_signal(sem)` | Signal the semaphore | +| | `cco_sem` | Semaphore type | +| | `cco_sem_await(sem)` | Await for the semaphore count > 0 | +| | `cco_sem_await(sem, ret)` | Await with ret on the semaphore | +| cco_sem | `cco_sem_init(long value)` | Set semaphore value | +| | `cco_sem_release(sem)` | Signal the semaphore | | | Timers: | | -| | `ctimer` | Timer type | -| | `cco_await_timer(tm)` | Await for timer to expire | -| | `cco_await_timer(tm, ret)` | Await with ret for timer to expire | -| | `ctimer_start(tm, long msecs)` | Start timer msecs milliseconds | -| | `ctimer_restart(tm)` | Restart timer with same duration | -| `bool` | `ctimer_expired(tm)` | Return true if timer is expired | -| `long` | `ctimer_remaining(tm)` | Return milliseconds remaining | +| | `cco_timer` | Timer type | +| | `cco_timer_await(tm)` | Await for timer to expire | +| | `cco_timer_await(tm, ret)` | Await with ret for timer to expire | +| | `cco_timer_start(tm, long msecs)` | Start timer msecs milliseconds | +| | `cco_timer_restart(tm)` | Restart timer with same duration | +| `bool` | `cco_timer_expired(tm)` | Return true if timer is expired | +| `long` | `cco_timer_remaining(tm)` | Return milliseconds remaining | | | From caller side: | | | `void` | `cco_stop(co)` | Next call of coroutine returns `cco_end()` | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 486f6e23..d3f73229 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -114,18 +114,18 @@ enum { typedef struct { intptr_t count; -} csem; +} cco_sem; -#define cco_await_sem(...) c_MACRO_OVERLOAD(cco_await_sem, __VA_ARGS__) -#define cco_await_sem_1(sem) cco_await_sem_2(sem, ) -#define cco_await_sem_2(sem, ret) \ +#define cco_sem_await(...) c_MACRO_OVERLOAD(cco_sem_await, __VA_ARGS__) +#define cco_sem_await_1(sem) cco_sem_await_2(sem, ) +#define cco_sem_await_2(sem, ret) \ do { \ cco_await_2((sem)->count > 0, ret); \ --(sem)->count; \ } while (0) -#define csem_signal(sem) ++(sem)->count -#define csem_set(sem, value) ((sem)->count = (value)) +#define cco_sem_release(sem) ++(sem)->count +#define cco_sem_with(value) ((cco_sem){value}) /* * Timer @@ -134,48 +134,51 @@ typedef struct { typedef struct { clock_t start; clock_t interval; -} ctimer; +} cco_timer; -#define cco_await_timer(...) c_MACRO_OVERLOAD(cco_await_timer, __VA_ARGS__) -#define cco_await_timer_2(tm, msec) cco_await_timer_3(tm, msec, ) -#define cco_await_timer_3(tm, msec, ret) \ +#define cco_timer_await(...) c_MACRO_OVERLOAD(cco_timer_await, __VA_ARGS__) +#define cco_timer_await_2(tm, msec) cco_timer_await_3(tm, msec, ) +#define cco_timer_await_3(tm, msec, ret) \ do { \ - ctimer_start(tm, msec); \ - cco_await_2(ctimer_expired(tm), ret); \ + cco_timer_start(tm, msec); \ + cco_await_2(cco_timer_expired(tm), ret); \ } while (0) -#if defined _WIN32 - static inline void csleep_ms(long msec) { - __declspec(dllimport) void Sleep(unsigned long); +#ifdef _WIN32 + #ifdef __cplusplus + extern "C" + #endif + __declspec(dllimport) void __stdcall Sleep(unsigned long); + static inline void cco_sleep(long msec) { Sleep((unsigned long)msec); } #else #include <sys/time.h> - static inline void csleep_ms(long msec) { + static inline void cco_sleep(long msec) { struct timeval tv = {.tv_sec=msec/1000, .tv_usec=1000*(msec % 1000)}; select(0, NULL, NULL, NULL, &tv); } #endif -static inline void ctimer_start(ctimer* tm, long msec) { +static inline void cco_timer_start(cco_timer* tm, long msec) { tm->interval = msec*(CLOCKS_PER_SEC/1000); tm->start = clock(); } -static inline ctimer ctimer_with(long msec) { - ctimer tm = {msec*(CLOCKS_PER_SEC/1000), clock()}; +static inline cco_timer cco_timer_with(long msec) { + cco_timer tm = {msec*(CLOCKS_PER_SEC/1000), clock()}; return tm; } -static inline void ctimer_restart(ctimer* tm) { +static inline void cco_timer_restart(cco_timer* tm) { tm->start = clock(); } -static inline bool ctimer_expired(ctimer* tm) { +static inline bool cco_timer_expired(cco_timer* tm) { return clock() - tm->start >= tm->interval; } -static inline long ctimer_remaining(ctimer* tm) { +static inline long cco_timer_remaining(cco_timer* tm) { return (long)((double)(tm->start + tm->interval - clock())*(1000.0/CLOCKS_PER_SEC)); } |
