Acme-Parataxis
view release on metacpan or search on metacpan
lib/Acme/Parataxis.c view on Meta::CPAN
}
/**
* @struct para_fiber_t
* @brief The complete execution context of a Perl Fiber.
*
* This structure encapsulates both the OS-level register state (via context)
* and the entire internal state of the Perl interpreter required to pause
* and resume execution of Perl code.
*/
typedef struct {
coro_handle_t context; /**< OS-specific context handle */
#ifndef _WIN32
void * stack_p; /**< Pointer to dynamically allocated fiber stack (Unix only) */
size_t stack_sz; /**< Size of the allocated stack (Unix only) */
#endif
/*
* Perl Interpreter State Pointers.
* These must be saved and restored during every context switch.
*/
PERL_SI * si; /**< Current Stack Info (tracks recursion and eval frames) */
AV * curstack; /**< The active Argument Stack (AV*) */
SSize_t stack_sp_offset; /**< Stack Pointer offset from stack base */
I32 * markstack; /**< Base of the Mark Stack (tracks list start points) */
I32 * markstack_ptr; /**< Current pointer into the Mark Stack */
I32 * markstack_max; /**< Limit of the Mark Stack */
I32 * scopestack; /**< Base of the Scope Stack (tracks block nesting) */
I32 scopestack_ix; /**< Current index in the Scope Stack */
I32 scopestack_max; /**< Limit of the Scope Stack */
ANY * savestack; /**< Base of the Save Stack (tracks local/my variables for cleanup) */
I32 savestack_ix; /**< Current index in the Save Stack */
I32 savestack_max; /**< Limit of the Save Stack */
SV ** tmps_stack; /**< Base of the Mortal Stack (tracks SVs needing refcnt decrement) */
I32 tmps_ix; /**< Current index in the Mortal Stack */
I32 tmps_floor; /**< Current floor of the Mortal Stack */
I32 tmps_max; /**< Limit of the Mortal Stack */
JMPENV * top_env; /**< Pointer to the top exception environment (eval/die buffers) */
COP * curcop; /**< Current Op Pointer (location in the source/bytecode) */
OP * op; /**< Current Operation being executed */
PAD * comppad; /**< Current lexical Pad (variable storage) */
SV ** curpad; /**< Array pointer to the current lexical Pad */
PMOP * curpm; /**< Current pattern match state */
PMOP * curpm_under; /**< Current pattern match state under */
PMOP * reg_curpm; /**< Current regex match state */
GV * defgv; /**< The $_ global */
GV * last_in_gv; /**< GV used in last <FH> */
SV * rs; /**< The $/ global */
GV * ofsgv; /**< The $, global */
SV * ors_sv; /**< The $\ global */
GV * defoutgv; /**< The default output filehandle */
HV * curstash; /**< Current package stash */
HV * defstash; /**< Default package stash */
SV * errors; /**< Outstanding queued errors */
SV * user_cv; /**< The Perl sub/coderef this fiber is running */
SV * self_ref; /**< The Acme::Parataxis Perl object wrapper */
SV * transfer_data; /**< Arguments or return values passed during yield/transfer */
int id; /**< Numeric ID of this fiber */
int finished; /**< Flag: 1 if the fiber has completed its entry_point */
int parent_id; /**< ID of the fiber that 'called' this one (asymmetric) */
int last_sender; /**< ID of the fiber that last switched control to this one */
} para_fiber_t;
/** @name Job Status Constants */
///@{
#define JOB_FREE 0 /**< Slot is available for new tasks */
#define JOB_NEW 1 /**< Task is submitted but not yet picked up by a worker */
#define JOB_BUSY 2 /**< Task is currently being processed by a worker thread */
#define JOB_DONE 3 /**< Task has completed and results are ready */
///@}
/** @name Task Type Constants */
///@{
#define TASK_SLEEP 0 /**< Sleep for N milliseconds */
#define TASK_GET_CPU 1 /**< Retrieve current core ID */
#define TASK_READ 2 /**< Wait for read-readiness on a file descriptor */
#define TASK_WRITE 3 /**< Wait for write-readiness on a file descriptor */
///@}
/**
* @union value_t
* @brief Generic container for task input/output data.
*/
typedef union {
int64_t i; /**< Integer/Pointer storage */
double d; /**< Floating point storage */
char * s; /**< String storage */
} value_t;
/**
* @struct job_t
* @brief Represents a task in the background thread pool queue.
*/
typedef struct {
int fiber_id; /**< ID of the Fiber that submitted this task */
int target_thread; /**< Index of the assigned worker thread */
int type; /**< Type of task to perform (TASK_*) */
value_t input; /**< Input data for the task */
value_t output; /**< Result data populated by the worker */
int timeout_ms; /**< Timeout duration for I/O tasks */
int status; /**< Current lifecycle state (JOB_*) */
} job_t;
// Global Registry and State
/** @brief Maximum number of concurrent fibers allowed */
#define MAX_FIBERS 1024
/** @brief Array of active fiber structures */
static para_fiber_t * fibers[MAX_FIBERS];
/** @brief The context representing the main Perl thread */
static para_fiber_t main_context;
lib/Acme/Parataxis.c view on Meta::CPAN
/**
* @brief Swaps the internal Perl Interpreter state pointers.
*
* This is the core of the fiber implementation. It manually saves all
* global pointers that define the "state" of the Perl virtual machine for
* the current context and restores them for the target context.
*
* @param from Context being paused.
* @param to Context being resumed.
*/
void swap_perl_state(para_fiber_t * from, para_fiber_t * to) {
dTHX;
/* Save current state into 'from' context */
from->si = PL_curstackinfo;
// The Argument Stack (Main Perl stack)
from->curstack = PL_curstack;
from->stack_sp_offset = PL_stack_sp - PL_stack_base;
// The Mark Stack (Tracks where lists begin on the argument stack)
from->markstack = PL_markstack;
from->markstack_ptr = PL_markstack_ptr;
from->markstack_max = PL_markstack_max;
// The Scope Stack (Tracks block entry/exit for cleanup)
from->scopestack = PL_scopestack;
from->scopestack_ix = PL_scopestack_ix;
from->scopestack_max = PL_scopestack_max;
// The Save Stack (Tracks 'local' variables and destructors)
from->savestack = PL_savestack;
from->savestack_ix = PL_savestack_ix;
from->savestack_max = PL_savestack_max;
// The Mortal Stack (Tracks temporary SVs that need decrementing)
from->tmps_stack = PL_tmps_stack;
from->tmps_ix = PL_tmps_ix;
from->tmps_floor = PL_tmps_floor;
from->tmps_max = PL_tmps_max;
// Exception Environment (setjmp/longjmp buffers for eval/die)
from->top_env = PL_top_env;
// Op and Pad pointers (Where we are in the bytecode)
from->curcop = PL_curcop;
from->op = PL_op;
from->comppad = PL_comppad;
from->curpad = PL_curpad;
from->curpm = PL_curpm;
from->curpm_under = PL_curpm_under;
from->reg_curpm = PL_reg_curpm;
from->defgv = PL_defgv;
from->last_in_gv = PL_last_in_gv;
from->rs = PL_rs;
from->ofsgv = PL_ofsgv;
from->ors_sv = PL_ors_sv;
from->defoutgv = PL_defoutgv;
from->curstash = PL_curstash;
from->defstash = PL_defstash;
from->errors = PL_errors;
/* Load target state from 'to' context */
PL_curstackinfo = to->si;
PL_curstack = to->curstack;
// Re-calculate stack bounds based on the new array (AV)
PL_stack_base = AvARRAY(PL_curstack);
PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
PL_stack_sp = PL_stack_base + to->stack_sp_offset;
AvFILLp(PL_curstack) = to->stack_sp_offset; // Keep stack AV metadata synced
PL_markstack = to->markstack;
PL_markstack_ptr = to->markstack_ptr;
PL_markstack_max = to->markstack_max;
PL_scopestack = to->scopestack;
PL_scopestack_ix = to->scopestack_ix;
PL_scopestack_max = to->scopestack_max;
PL_savestack = to->savestack;
PL_savestack_ix = to->savestack_ix;
PL_savestack_max = to->savestack_max;
PL_tmps_stack = to->tmps_stack;
PL_tmps_ix = to->tmps_ix;
PL_tmps_floor = to->tmps_floor;
PL_tmps_max = to->tmps_max;
PL_top_env = to->top_env;
PL_curcop = to->curcop;
PL_op = to->op;
PL_comppad = to->comppad;
PL_curpm = to->curpm;
PL_curpm_under = to->curpm_under;
PL_reg_curpm = to->reg_curpm;
PL_defgv = to->defgv;
PL_last_in_gv = to->last_in_gv;
PL_rs = to->rs;
PL_ofsgv = to->ofsgv;
PL_ors_sv = to->ors_sv;
PL_defoutgv = to->defoutgv;
PL_curstash = to->curstash;
PL_defstash = to->defstash;
PL_errors = to->errors;
if (PL_comppad)
PL_curpad = AvARRAY(PL_comppad);
else
PL_curpad = to->curpad;
// Restore CvDEPTH and clean landing pads
_activate_current_depths(aTHX_ to);
}
/**
* @brief Allocates and initializes new Perl stacks for a fiber.
*
* Each fiber needs a complete set of independent stacks (Argument, Mark,
* Scope, Save, Mortal) to function as a separate execution thread.
*
* @param c The fiber context to initialize.
*/
void init_perl_stacks(para_fiber_t * c) {
dTHX;
// Allocate Stack Info (SI)
Newxz(c->si, 1, PERL_SI);
c->si->si_cxmax = 64;
// Use Newxz to ensure the context stack is zeroed.
Newxz(c->si->si_cxstack, c->si->si_cxmax, PERL_CONTEXT);
c->si->si_cxix = -1;
c->si->si_type = PERLSI_MAIN;
// Allocate Argument Stack (AV)
c->curstack = newAV();
AvREAL_off(c->curstack); // Stacks do not 'own' their elements in the refcnt sense
av_extend(c->curstack, 128);
// Initialize stack with a dummy undef at index 0, matching Perl's main stack
AvARRAY(c->curstack)[0] = &PL_sv_undef;
AvFILLp(c->curstack) = 0;
c->stack_sp_offset = 0;
// Link the SI to the AV. Perl uses this linkage during stack unwinding.
c->si->si_stack = c->curstack;
// Allocate Control Stacks
I32 sz = 2048; /* Recursion depth support */
Newx(c->markstack, sz, I32);
c->markstack_ptr = c->markstack;
*c->markstack_ptr = 0;
c->markstack_max = c->markstack + sz - 1;
Newx(c->scopestack, sz, I32);
c->scopestack_ix = 0;
c->scopestack_max = sz;
Newx(c->savestack, sz, ANY);
c->savestack_ix = 0;
c->savestack_max = sz;
Newx(c->tmps_stack, sz, SV *);
c->tmps_ix = -1;
c->tmps_floor = -1;
c->tmps_max = sz;
// Inherit initial globals from current interpreter state
c->curcop = PL_curcop;
c->op = PL_op;
c->top_env = PL_top_env;
c->curpm = PL_curpm;
c->curpm_under = PL_curpm_under;
c->reg_curpm = NULL;
c->defgv = PL_defgv;
c->last_in_gv = PL_last_in_gv;
c->rs = PL_rs;
c->ofsgv = PL_ofsgv;
c->ors_sv = PL_ors_sv;
c->defoutgv = PL_defoutgv;
c->curstash = PL_curstash;
c->defstash = PL_defstash;
c->errors = PL_errors;
// Start with fresh pads to avoid interfering with caller.
c->comppad = NULL;
c->curpad = NULL;
}
/**
* @brief Initializes the fiber system and converts the main thread.
*
* This function must be called once before any other fiber operations.
* It captures the state of the main Perl interpreter thread.
*
* @return int 0 on success.
*/
DLLEXPORT int init_system() {
dTHX;
if (system_initialized)
return 0;
if (max_thread_pool_size == 0) {
max_thread_pool_size = get_cpu_count();
if (max_thread_pool_size > MAX_THREADS)
max_thread_pool_size = MAX_THREADS;
}
main_context.si = PL_curstackinfo;
main_context.transfer_data = &PL_sv_undef;
main_context.id = -1;
main_context.finished = 0;
main_context.last_sender = -1;
main_context.curpm = PL_curpm;
main_context.curpm_under = PL_curpm_under;
main_context.reg_curpm = PL_reg_curpm;
main_context.defgv = PL_defgv;
main_context.last_in_gv = PL_last_in_gv;
main_context.rs = PL_rs;
main_context.ofsgv = PL_ofsgv;
main_context.ors_sv = PL_ors_sv;
main_context.defoutgv = PL_defoutgv;
main_context.curstash = PL_curstash;
main_context.defstash = PL_defstash;
main_context.errors = PL_errors;
system_initialized = 1;
#ifdef _WIN32
/* Convert the main thread into a fiber so it can be switched out */
if (!main_fiber_handle) {
main_fiber_handle = ConvertThreadToFiber(NULL);
if (!main_fiber_handle) {
if (GetLastError() == ERROR_ALREADY_FIBER)
main_fiber_handle = GetCurrentFiber();
}
}
#endif
init_threads();
return 0;
}
/**
* @brief Performs the low-level OS context switch.
*
* Saves the Perl state and then uses OS primitives (SwitchToFiber or
* swapcontext) to change execution flow.
*
* @param target_id ID of the target fiber (-1 for Main).
*/
void perform_switch(int target_id) {
dTHX;
if (target_id == current_fiber_id)
return;
para_fiber_t * from = (current_fiber_id == -1) ? &main_context : fibers[current_fiber_id];
para_fiber_t * to = (target_id == -1) ? &main_context : fibers[target_id];
to->last_sender = current_fiber_id;
current_fiber_id = target_id;
swap_perl_state(from, to);
#ifdef _WIN32
if (target_id == -1)
SwitchToFiber(main_fiber_handle);
else
SwitchToFiber(to->context);
#else
swapcontext(&from->context, &to->context);
#endif
}
/**
* @brief Yields execution back to the caller or the main thread.
*
* Suspends the current fiber and returns a value to the context that
* last resumed or called this fiber.
*
* @param ret_val The Perl SV to "return" to the caller.
* @return SV* The value passed in when this fiber is eventually resumed.
*/
DLLEXPORT SV * coro_yield(SV * ret_val) {
dTHX;
if (current_fiber_id == -1)
return &PL_sv_undef;
para_fiber_t * self = fibers[current_fiber_id];
int parent = self->parent_id;
if (parent != -1 && (!fibers[parent] || fibers[parent]->finished))
parent = self->last_sender;
else if (parent == -1)
parent = self->last_sender;
if (parent >= 0 && (!fibers[parent] || fibers[parent]->finished))
parent = -1;
para_fiber_t * caller = (parent == -1) ? &main_context : fibers[parent];
/* Pass return value to caller */
if (caller->transfer_data != ret_val) {
if (caller->transfer_data && caller->transfer_data != &PL_sv_undef)
SvREFCNT_dec(caller->transfer_data);
caller->transfer_data = ret_val;
if (ret_val && ret_val != &PL_sv_undef)
SvREFCNT_inc(ret_val);
}
perform_switch(parent);
/* Retrieve value passed back during resume */
SV * res = self->transfer_data;
self->transfer_data = &PL_sv_undef;
if (res && res != &PL_sv_undef)
sv_2mortal(res);
return res;
}
/**
* @brief Entry point function for all new fibers.
*
* Sets up the Perl environment (ENTER/SAVETMPS), unpacks arguments,
* calls the user coderef, handles results/errors, and manages the
* fiber's completion lifecycle.
*
* @param c Pointer to the fiber context being started.
*/
static void entry_point(para_fiber_t * c) {
dTHX;
ENTER;
SAVETMPS;
dSP;
PUSHMARK(SP);
/* Unpack arguments passed during coro_call */
if (c->transfer_data && SvROK(c->transfer_data) && SvTYPE(SvRV(c->transfer_data)) == SVt_PVAV) {
AV * args = (AV *)SvRV(c->transfer_data);
I32 len = av_top_index(args) + 1;
for (I32 i = 0; i < len; i++) {
SV ** svp = av_fetch(args, i, 0);
if (svp)
XPUSHs(*svp);
}
}
PUTBACK;
/* Execute the Perl sub */
int count = call_sv(c->user_cv, G_SCALAR | G_EVAL);
SPAGAIN;
SV * ret_val = &PL_sv_undef;
if (count == 1)
ret_val = POPs;
PUTBACK;
c->finished = true;
/* Cleanup transfer data and store result */
if (c->transfer_data && c->transfer_data != &PL_sv_undef) {
SvREFCNT_dec(c->transfer_data);
c->transfer_data = &PL_sv_undef;
}
if (ret_val && ret_val != &PL_sv_undef) {
SvREFCNT_inc(ret_val);
c->transfer_data = ret_val;
}
/* Update the Perl-level Acme::Parataxis object */
if (c->self_ref && SvROK(c->self_ref)) {
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(c->self_ref);
if (SvTRUE(ERRSV)) {
XPUSHs(ERRSV);
PUTBACK;
call_method("set_error", G_DISCARD);
}
else {
XPUSHs(ret_val);
PUTBACK;
call_method("set_result", G_DISCARD);
}
FREETMPS;
LEAVE;
}
FREETMPS;
LEAVE;
/* Final yield back to caller */
coro_yield(c->transfer_data ? c->transfer_data : &PL_sv_undef);
/* Loop indefinitely if resumed after finish */
while (1)
coro_yield(&PL_sv_undef);
}
#ifdef _WIN32
/** @brief Windows fiber callback wrapper. */
static void WINAPI fiber_entry(void * param) { entry_point((para_fiber_t *)param); }
#else
/** @brief POSIX makecontext callback wrapper. */
static void posix_entry(int fiber_id) { entry_point(fibers[fiber_id]); }
#endif
/**
* @brief Allocates and prepares a new Fiber context.
*
* @param user_code Coderef to execute in the fiber.
* @param self_ref Acme::Parataxis object to notify on completion.
* @return int Unique ID of the new fiber, or negative on error.
*/
DLLEXPORT int create_fiber(SV * user_code, SV * self_ref) {
dTHX;
int idx = -1;
for (int i = 0; i < MAX_FIBERS; i++) {
if (fibers[i] == NULL) {
idx = i;
break;
}
}
if (idx == -1)
return -2;
para_fiber_t * c = (para_fiber_t *)malloc(sizeof(para_fiber_t));
if (!c)
return -3;
memset(c, 0, sizeof(para_fiber_t));
c->user_cv = user_code;
if (user_code && user_code != &PL_sv_undef)
SvREFCNT_inc(user_code);
c->self_ref = self_ref;
if (self_ref && self_ref != &PL_sv_undef)
SvREFCNT_inc(self_ref);
c->id = idx;
c->parent_id = -1;
c->last_sender = -1;
c->transfer_data = &PL_sv_undef;
fibers[idx] = c;
/* Initialize Perl stacks */
init_perl_stacks(c);
#ifdef _WIN32
c->context = CreateFiber(0, fiber_entry, c);
#else
c->stack_sz = 512 * 1024; // 512KB is plenty for Perl fibers
if (posix_memalign(&c->stack_p, 16, c->stack_sz) != 0) {
destroy_coro(idx);
return -3;
}
getcontext(&c->context);
c->context.uc_stack.ss_sp = c->stack_p;
c->context.uc_stack.ss_size = c->stack_sz;
c->context.uc_link = &main_context.context;
makecontext(&c->context, (void (*)())posix_entry, 1, c->id);
#endif
return idx;
}
/**
* @brief Resumes a fiber (asymmetric call).
*
* Suspends the caller and switches execution to the specified fiber.
* Sets the caller as the 'parent' for future yields.
*
* @param fiber_id Fiber ID to call.
* @param args Perl SV (usually arrayref) to pass as arguments to the fiber.
* @return SV* Result yielded by the fiber.
*/
DLLEXPORT SV * coro_call(int fiber_id, SV * args) {
dTHX;
( run in 1.007 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )