Commit graph

48 commits

Author SHA1 Message Date
Andrei Alexeyev
da67472719
coroutine: break down into smaller sub-modules 2022-12-28 23:47:48 +01:00
Andrei Alexeyev
8c804397dd
coroutine,stage: remove global sched; inherit sched from calling task
This allows having multiple task schedulers without having to switch the
global "target" scheduler for INVOKE_ macros.

An important side effect of this change is that it's not possible to use
the regular INVOKE_ macros from a non-coroutine context anymore. A
series of complimentary SCHED_INVOKE_ macros was added that behave
identically, but allow explicitly specifying a scheduler as the first
argument. The stage loop has been slightly refactored to accomodate this
behavior.
2022-12-28 23:47:48 +01:00
Andrei Alexeyev
993bef8a2a
coroutine: add NOT_NULL_OR_DIE() convenience macro
Returns its argument if it's not a NULL pointer, otherwise cancels the
running task.
2021-09-22 20:41:39 +03:00
Andrei Alexeyev
8d2ee76710
replace include guards with #pragma once 2021-08-31 23:34:46 +03:00
Andrei Alexeyev
f7ed0befdf
coroutine: allow omitting the args struct when declaring tasks 2021-08-31 23:33:16 +03:00
Andrei Alexeyev
373edc33ba
coroutine: deprecate NO_ARGS and enhance macros with GNU extensions
Removed dummy char fields from arg structs, as their only purpose was to
hack around silly standard preprocessor limitations. { } should now be
used instead of NO_ARGS.
2021-08-31 23:33:16 +03:00
Andrei Alexeyev
af0efa8b1e
coroutine: make cotask_cancel accept NULL pointers
This allows us to simplify (and fix) the CANCEL_TASK macro
2021-06-10 11:42:58 +03:00
Andrei Alexeyev
77efa24f3c
coroutine: add AWAIT_SUBTASKS statement
Waits for all subtasks to finish, and returns the amount of frames
waited. If there are no running subtasks, doesn't yield and simply
returns 0.
2021-06-08 09:39:36 +03:00
Andrei Alexeyev
7924930613
coroutine: update koishi 2021-06-04 09:48:03 +03:00
Andrei Alexeyev
3659725121
coroutine: remove unused prologue from thunks 2021-05-19 12:22:35 +03:00
Andrei Alexeyev
aa2cdd5409
coroutine: add a "sized pointer" type for interface arg structs
A sized pointer struct simply contains a pointer to an args struct of
the interface's corresponding type, and a size_t field with the size of
that struct. In case of interface inheritance, the sized pointer can be
"cast" to the base type by accessing the union member "base", enabling a
form of polymorphism.

A sized pointer can be constructed like so:

// alias for convenience
typedef TASK_IFACE_ARGS_SIZED_PTR_TYPE(MyInterface) MyInterfaceSptr;

MyInterfaceSptr sptr = TASK_IFACE_SARGS(MyInterface,
        .foo = bar,
        .spam = eggs
);
2021-05-19 03:22:19 +03:00
Andrei Alexeyev
4127555ed4
coroutine: basic task interface inheritance
Also removed cotask_new() because it wasn't used
2021-05-19 03:22:19 +03:00
Andrei Alexeyev
31adaa7e0f
Add WAIT_EVENT_ONCE(event)
Waits for event if it hasn't been signaled yet. It if has, returns
without waiting, as if the event has just been signaled.
2020-04-30 21:49:10 +03:00
Andrei Alexeyev
e16e2184a3
Refactor entity system (#214)
* Smarter generic entity macros

The list of "core" entities is now defined in one macro, and hardcoded
_Generic dispatch tables are eliminated

* Get rid of "custom" entities

All entities are now "first-class". The list of known entity types has
been moved to known_entities.h. The system no longer needs to know the
definition of all entity structs.

* Refactor guts of ENT_BOX/ENT_UNBOX

Made the functions inline, Box::ent is now a proper pointer type (but
please don't use it directly), ENT_UNBOX returns NULL if the box is
"empty" (references NULL entity)

* Merge TASK_BIND_UNBOXED with TASK_BIND

* s/YoumuMyon/YoumuAMyon for consistency
2020-04-17 10:18:53 +03:00
Andrei Alexeyev
4223b023dc
CANCEL_TASK macro 2020-04-06 04:34:55 +03:00
Andrei Alexeyev
fe3aabcdc1
Add TASK_HOST_CUSTOM_ENT and TASK_HOST_EVENTS APIs (#208)
TASK_HOST_CUSTOM_ENT(type) allocates a custom entity from a reserved
region on the task's stack (same as TASK_MALLOC) and registers it. The
entity will be automatically unregistered when the task terminates for
any reason. Only one entity can be hosted by any particular task. This
macro returns a pointer to the hosted entity.

TASK_HOST_EVENTS(events) associates a custom array of events (created
with COEVENTS_ARRAY) with the task. All events in the array are
automatically initialized when this macro is called, and cancelled when
the task terminates for any reason. Only one array of events can be
hosted by any particular task.
2020-04-06 04:34:13 +03:00
Andrei Alexeyev
7fbe987b94
Add API for task-bound dynamic memory allocation (#201)
TASK_MALLOC(size) allocates a memory region with a lifetime bound to the
active task. It can't be free'd manually.

Allocation requests are served from a dedicated region on the task's
stack whenever possible, which is fast and essentially free. If there is
not enough free space to serve the request, then the memory is allocated
on the heap. Such heap allocations are automatically free'd when the
task expires.

The allocated memory is zero-initialized and aligned as strictly as
max_align_t.
2020-04-06 04:30:52 +03:00
Andrei Alexeyev
0cbc86c66e
Add generic type-safe facility for dynamic arrays (#207)
Replace most ad-hoc opencoded dynamic arrays across the codebase
2020-04-05 05:51:00 +03:00
Andrei Alexeyev
431237f0f2
Coroutinize MarisaA (#206) 2020-04-01 23:08:40 +03:00
Andrei Alexeyev
6413f2aadc
fix linking failures with clang+ASan 2020-03-08 12:41:43 +02:00
Andrei Alexeyev
d54c58a7bc
Add ability to wait for task's "finished" event
This works even if the scheduler is shutting down, allowing reliable
"cleanup" routines to be registered.
2020-03-08 02:35:53 +02:00
Andrei Alexeyev
92222f8888
Move most cotask control data to the task's stack 2020-03-04 22:26:55 +02:00
Andrei Alexeyev
e3d5518e9b
Remove coroutine finalizers 2020-03-04 22:26:50 +02:00
Andrei Alexeyev
129566d543
Remove some old unused coroutine macros 2020-03-04 22:26:49 +02:00
Andrei Alexeyev
d756790b6e
show num. of context swiches/frame in coroutine stats 2020-03-04 22:26:48 +02:00
Andrei Alexeyev
3d1c0eee4e
WIP proj draw rule revamp; YoumuB revamp; misc changes; giant mess
cursed commit
2020-03-04 22:26:45 +02:00
Andrei Alexeyev
9868fbcaa4
display number of active/allocated tasks 2020-03-04 22:26:44 +02:00
Andrei Alexeyev
de5b821010
Experimental coroutine waiting optimization (WIP)
Avoids unnecessary back and forth context switches when waiting for a
delay or an event. Should be helpful for Emscripten, but needs
benchmarking.
2020-03-04 22:26:43 +02:00
Andrei Alexeyev
afd9d492dc
more verbose coroutine debugging output 2020-03-04 22:26:42 +02:00
Andrei Alexeyev
6e5ab085e5
WIP stage 1 post-midboss filler 2020-03-04 22:26:40 +02:00
Andrei Alexeyev
7ca54ec350
Add CANCEL_TASK_AFTER and macros for boss attacks
Also document various coroutine system macros
2020-03-04 22:25:44 +02:00
Andrei Alexeyev
490b224bd7
run upkeep 2020-03-04 22:06:39 +02:00
Andrei Alexeyev
a846e4598f
event-based way to wait for boss defeat and retrieve the waiting time 2020-03-04 22:06:39 +02:00
Andrei Alexeyev
6596b76641
Add dependent subtasks; fix finalization bugs 2020-03-04 22:06:38 +02:00
Andrei Alexeyev
9498e64dd1
further task-boss infrastructure WIP 2020-03-04 22:06:38 +02:00
Andrei Alexeyev
e14eb36e5c
implement some infrastructure for coroutine bosses 2020-03-04 22:06:37 +02:00
Andrei Alexeyev
b45fdfdd84
Use INLINE macro for various static-inline functions 2020-03-04 21:52:54 +02:00
Andrei Alexeyev
c9ab42dc02
wip stage 1 progress 2020-03-04 21:52:53 +02:00
Andrei Alexeyev
4e4224dc59
task system tweaks and early Stage 1 port attempt 2020-03-04 21:50:51 +02:00
Andrei Alexeyev
76aef22e23
integrate task scheduling into the stage loop 2020-03-04 21:50:51 +02:00
Andrei Alexeyev
5b0b676c0e
INVOKE_TASK_DELAYED; CANCEL_TASK_WHEN; event robustness fixes 2020-03-04 21:50:51 +02:00
Andrei Alexeyev
156d8147b1
Add a way to define extern tasks (to reference across files) 2020-03-04 21:50:51 +02:00
Andrei Alexeyev
13b5e33b0c
add 'killed' event for projectiles 2020-03-04 21:50:51 +02:00
Andrei Alexeyev
2a34f7bad4
generalize common bullet motion rules; add some math helpers 2020-03-04 21:50:50 +02:00
Andrei Alexeyev
3f99739d62
coroutines: task finalizers; fix enemy death event 2020-03-04 21:50:50 +02:00
Andrei Alexeyev
9fe54e0335
experimental event system for coroutine-based stages 2020-03-04 21:50:50 +02:00
Andrei Alexeyev
1579082e30
"Boxed" entities; facility to bind tasks to entity lifetime 2020-03-04 21:50:50 +02:00
Andrei Alexeyev
bdef62d7f4
Initial coroutine/task system implementation 2020-03-04 21:50:22 +02:00