1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-17 22:24:49 +00:00

Remove some extra fiber state and use a flag.

This commit is contained in:
Calvin Rose 2023-10-07 12:07:05 -07:00
parent fea8242ea7
commit e8c013a778
7 changed files with 11 additions and 10 deletions

View File

@ -258,7 +258,7 @@ void janet_async_end(JanetFiber *fiber) {
janet_gcunroot(janet_wrap_abstract(fiber->ev_stream)); janet_gcunroot(janet_wrap_abstract(fiber->ev_stream));
fiber->ev_callback = NULL; fiber->ev_callback = NULL;
if (fiber->ev_state) { if (fiber->ev_state) {
if (!fiber->ev_in_flight) { if (!(fiber->flags & JANET_FIBER_EV_FLAG_IN_FLIGHT)) {
janet_free(fiber->ev_state); janet_free(fiber->ev_state);
janet_ev_dec_refcount(); janet_ev_dec_refcount();
} }
@ -1483,7 +1483,7 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp to) {
fiber = stream->write_fiber; fiber = stream->write_fiber;
} }
if (fiber != NULL) { if (fiber != NULL) {
fiber->ev_in_flight = 0; fiber->flags &= ~JANET_FIBER_EV_FLAG_IN_FLIGHT;
/* System is done with this, we can reused this data */ /* System is done with this, we can reused this data */
overlapped->InternalHigh = (ULONG_PTR) num_bytes_transfered; overlapped->InternalHigh = (ULONG_PTR) num_bytes_transfered;
fiber->ev_callback(fiber, result ? JANET_ASYNC_EVENT_COMPLETE : JANET_ASYNC_EVENT_FAILED); fiber->ev_callback(fiber, result ? JANET_ASYNC_EVENT_COMPLETE : JANET_ASYNC_EVENT_FAILED);
@ -2232,7 +2232,7 @@ void ev_callback_read(JanetFiber *fiber, JanetAsyncEvent event) {
return; return;
} }
} }
fiber->ev_in_flight = 1; fiber->flags |= JANET_FIBER_EV_FLAG_IN_FLIGHT;
} }
break; break;
#else #else
@ -2456,7 +2456,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
status = WSASendTo(sock, &state->wbuf, 1, NULL, state->flags, to, tolen, &state->overlapped, NULL); status = WSASendTo(sock, &state->wbuf, 1, NULL, state->flags, to, tolen, &state->overlapped, NULL);
if (status) { if (status) {
if (WSA_IO_PENDING == WSAGetLastError()) { if (WSA_IO_PENDING == WSAGetLastError()) {
fiber->ev_in_flight = 1; fiber->flags |= JANET_FIBER_EV_FLAG_IN_FLIGHT;
} else { } else {
janet_cancel(fiber, janet_ev_lasterr()); janet_cancel(fiber, janet_ev_lasterr());
janet_async_end(fiber); janet_async_end(fiber);
@ -2481,7 +2481,7 @@ void ev_callback_write(JanetFiber *fiber, JanetAsyncEvent event) {
status = WriteFile(stream->handle, bytes, len, NULL, &state->overlapped); status = WriteFile(stream->handle, bytes, len, NULL, &state->overlapped);
if (!status) { if (!status) {
if (ERROR_IO_PENDING == GetLastError()) { if (ERROR_IO_PENDING == GetLastError()) {
fiber->ev_in_flight = 1; fiber->flags |= JANET_FIBER_EV_FLAG_IN_FLIGHT;
} else { } else {
janet_cancel(fiber, janet_ev_lasterr()); janet_cancel(fiber, janet_ev_lasterr());
janet_async_end(fiber); janet_async_end(fiber);

View File

@ -44,7 +44,6 @@ static void fiber_reset(JanetFiber *fiber) {
fiber->ev_state = NULL; fiber->ev_state = NULL;
fiber->ev_stream = NULL; fiber->ev_stream = NULL;
fiber->supervisor_channel = NULL; fiber->supervisor_channel = NULL;
fiber->ev_in_flight = 0;
#endif #endif
janet_fiber_set_status(fiber, JANET_STATUS_NEW); janet_fiber_set_status(fiber, JANET_STATUS_NEW);
} }

View File

@ -59,6 +59,9 @@
#define JANET_FIBER_EV_FLAG_CANCELED 0x10000 #define JANET_FIBER_EV_FLAG_CANCELED 0x10000
#define JANET_FIBER_EV_FLAG_SUSPENDED 0x20000 #define JANET_FIBER_EV_FLAG_SUSPENDED 0x20000
#define JANET_FIBER_FLAG_ROOT 0x40000 #define JANET_FIBER_FLAG_ROOT 0x40000
#define JANET_FIBER_EV_FLAG_IN_FLIGHT 0x1
/* used only on windows, should otherwise be unset */
#define janet_fiber_set_status(f, s) do {\ #define janet_fiber_set_status(f, s) do {\
(f)->flags &= ~JANET_FIBER_STATUS_MASK;\ (f)->flags &= ~JANET_FIBER_STATUS_MASK;\

View File

@ -330,7 +330,7 @@ static void janet_deinit_block(JanetGCObject *mem) {
{ {
JanetFiber *f = (JanetFiber *)mem; JanetFiber *f = (JanetFiber *)mem;
#ifdef JANET_EV #ifdef JANET_EV
if (f->ev_state && !f->ev_in_flight) { if (f->ev_state && !(f->flags & JANET_FIBER_EV_FLAG_IN_FLIGHT)) {
janet_ev_dec_refcount(); janet_ev_dec_refcount();
janet_free(f->ev_state); janet_free(f->ev_state);
} }

View File

@ -1053,7 +1053,6 @@ static const uint8_t *unmarshal_one_fiber(
fiber->ev_state = NULL; fiber->ev_state = NULL;
fiber->ev_callback = NULL; fiber->ev_callback = NULL;
fiber->ev_stream = NULL; fiber->ev_stream = NULL;
fiber->ev_in_flight = 0;
#endif #endif
/* Push fiber to seen stack */ /* Push fiber to seen stack */

View File

@ -24,6 +24,7 @@
#include "features.h" #include "features.h"
#include <janet.h> #include <janet.h>
#include "util.h" #include "util.h"
#include "fiber.h"
#endif #endif
#ifdef JANET_NET #ifdef JANET_NET
@ -252,7 +253,7 @@ static int net_sched_accept_impl(NetStateAccept *state, JanetFiber *fiber, Janet
int code = WSAGetLastError(); int code = WSAGetLastError();
if (code == WSA_IO_PENDING) { if (code == WSA_IO_PENDING) {
/* indicates io is happening async */ /* indicates io is happening async */
fiber->ev_in_flight = 1; fiber->flags |= JANET_FIBER_EV_FLAG_IN_FLIGHT;
return 0; return 0;
} }
*err = janet_ev_lasterr(); *err = janet_ev_lasterr();

View File

@ -927,7 +927,6 @@ struct JanetFiber {
JanetStream *ev_stream; /* which stream we are waiting on */ JanetStream *ev_stream; /* which stream we are waiting on */
void *ev_state; /* Extra data for ev callback state. On windows, first element must be OVERLAPPED. */ void *ev_state; /* Extra data for ev callback state. On windows, first element must be OVERLAPPED. */
void *supervisor_channel; /* Channel to push self to when complete */ void *supervisor_channel; /* Channel to push self to when complete */
int ev_in_flight; /* If overlapped operation is in flight */
#endif #endif
}; };