diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2021-08-23 20:17:16 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2021-08-23 20:26:16 -0700 |
commit | 61e02a7895b05debe5328439b3ef3f5cfdb438cb (patch) | |
tree | 5a530c4043327c35a752311a64cddfe6f57498d1 | |
parent | caa8f4319a8e9ef096e03857f0f85b48c0930a5e (diff) |
thunk: add some helpers for filtering thunk returns
Naming may evolve in future...
thunk_mid(): pass-through negative values, otherwise return 1
"mid" name is used as this is intended to return the "keep me
around" value for mid-phase iterative processing.
thunk_end(): pass-through negative values, otherwise return 0
"end" name is used as this indicates "don't keep me around for
reuse, fin!"
In experimental code using this header it became common for thunk
implementations to return straight from thunk_dispatch() or bare
calls to other thunkified functions.
This is convenient for propagating out fatal errors, but now that
return value influences lifecycle, these blind propagating
returns as-is in non-error scenarios would be propagating
lifecycle controls out from a totally unrelated context.
These helpers are intended to wrap those call sites, blindly
propagating only errors, but otherwise being opinionated about
lifecycle.
Now it's kind of expected that every thunk implementation will
have its return paths covered with thunk_end() and/or thunk_mid()
calls, or just bare non-propagated return values.
This is all still very experimental, and shouldn't be considered
production code, it's mostly just a curiosity.
-rw-r--r-- | thunk.h | 17 |
1 files changed, 17 insertions, 0 deletions
@@ -726,4 +726,21 @@ static inline void thunk_free(thunk_t *thunk) { return thunk->free(thunk); } + +/* Convenience helper for turning all successful thunk returns into + * don't-free/reuse successful returns, while still propagating errors. + */ +static inline int thunk_mid(int r) +{ + return (r < 0) ? r : 1; +} + +/* Convenience helper for turning all successful thunk returns into + * yes-free/discard successful returns, while still propagating errors. + */ +static inline int thunk_end(int r) +{ + return (r < 0) ? r : 0; +} + #endif |