static PyObject * PyGreenlet_Switch(PyGreenlet *g, PyObject *args, PyObject *kwargs) { PyGreenlet *self = (PyGreenlet *) g; if (!PyGreenlet_Check(self)) { PyErr_BadArgument(); return NULL; } if (args == NULL) { args = Py_BuildValue("()"); } else { Py_INCREF(args); } if (kwargs != NULL && PyDict_Check(kwargs)) { Py_INCREF(kwargs); } else { kwargs = NULL; } return single_result(g_switch(self, args, kwargs)); }
static int kill_greenlet(greenlet* self) { /* Cannot raise an exception to kill the greenlet if it is not running in the same thread! */ /* The dying greenlet cannot be a parent of ts_current because the 'parent' field chain would hold a reference */ gr_param result; greenlet* oldparent; if (!STATE_OK) { return -1; } oldparent = self->parent; self->parent = ts_current; /* Send the greenlet a GreenletExit exception. */ //PyErr_SetNone(PyExc_GreenletExit); result = g_switch(self, 0); if (result == 0) return -1; free(self); return 0; }
static PyObject* green_switch( PyGreenlet* self, PyObject* args, PyObject* kwargs) { Py_INCREF(args); Py_XINCREF(kwargs); return single_result(g_switch(self, args, kwargs)); }
static PyObject * throw_greenlet(PyGreenlet *self, PyObject *typ, PyObject *val, PyObject *tb) { /* Note: _consumes_ a reference to typ, val, tb */ PyObject *result = NULL; PyErr_Restore(typ, val, tb); if (PyGreenlet_STARTED(self) && !PyGreenlet_ACTIVE(self)) { /* dead greenlet: turn GreenletExit into a regular return */ result = g_handle_exit(result); } return single_result(g_switch(self, result, NULL)); }
gr_param greenlet_switch(greenlet* self, gr_param data) { if (self->stack_stop && !self->stack_start) { assert(!"finished"); return 0; } if (!STATE_OK) return 0; return g_switch(self, data); }
static void GREENLET_NOINLINE(g_initialstub)(void* mark) { int err; gr_param ret; /* now use run_info to store the statedict */ /* start the greenlet */ ts_target->stack_start = NULL; ts_target->stack_stop = (char*) mark; if (ts_current->stack_start == NULL) { /* ts_current is dying */ ts_target->stack_prev = ts_current->stack_prev; } else { ts_target->stack_prev = ts_current; } err = g_switchstack(); /* returns twice! The 1st time with err=1: we are in the new greenlet The 2nd time with err=0: back in the caller's greenlet */ if (err == 1) { /* in the new greenlet */ greenlet* ts_self = ts_current; ts_self->stack_start = (char*) 1; ret = ts_self->callback(ts_self->data); //result = g_handle_exit(result); /* jump back to parent */ ts_self->stack_start = NULL; /* dead */ g_switch(ts_self->parent, ret); assert(0); /* must not return from here! */ //PyErr_WriteUnraisable((PyObject *) ts_self); //Py_FatalError("greenlets cannot continue"); } /* back in the parent */ }
static int kill_greenlet(PyGreenlet* self) { /* Cannot raise an exception to kill the greenlet if it is not running in the same thread! */ if (self->run_info == PyThreadState_GET()->dict) { /* The dying greenlet cannot be a parent of ts_current because the 'parent' field chain would hold a reference */ PyObject* result; PyGreenlet* oldparent; PyGreenlet* tmp; if (!STATE_OK) { return -1; } oldparent = self->parent; self->parent = ts_current; Py_INCREF(self->parent); /* Send the greenlet a GreenletExit exception. */ PyErr_SetNone(PyExc_GreenletExit); result = g_switch(self, NULL, NULL); tmp = self->parent; self->parent = oldparent; Py_XDECREF(tmp); if (result == NULL) return -1; Py_DECREF(result); return 0; } else { /* Not the same thread! Temporarily save the greenlet into its thread's ts_delkey list. */ PyObject* lst; lst = PyDict_GetItem(self->run_info, ts_delkey); if (lst == NULL) { lst = PyList_New(0); if (lst == NULL || PyDict_SetItem(self->run_info, ts_delkey, lst) < 0) return -1; } if (PyList_Append(lst, (PyObject*) self) < 0) return -1; if (!STATE_OK) /* to force ts_delkey to be reconsidered */ return -1; return 0; } }
static int GREENLET_NOINLINE(g_initialstub)(void* mark) { int err; PyObject *o, *run; PyObject *exc, *val, *tb; PyGreenlet* self = ts_target; PyObject* args = ts_passaround_args; PyObject* kwargs = ts_passaround_kwargs; /* save exception in case getattr clears it */ PyErr_Fetch(&exc, &val, &tb); /* self.run is the object to call in the new greenlet */ run = PyObject_GetAttrString((PyObject*) self, "run"); if (run == NULL) { Py_XDECREF(exc); Py_XDECREF(val); Py_XDECREF(tb); return -1; } /* restore saved exception */ PyErr_Restore(exc, val, tb); /* recheck the state in case getattr caused thread switches */ if (!STATE_OK) { Py_DECREF(run); return -1; } /* by the time we got here another start could happen elsewhere, * that means it should now be a regular switch */ if (PyGreenlet_STARTED(self)) { Py_DECREF(run); ts_passaround_args = args; ts_passaround_kwargs = kwargs; return 1; } /* restore arguments in case they are clobbered */ ts_target = self; ts_passaround_args = args; ts_passaround_kwargs = kwargs; /* now use run_info to store the statedict */ o = self->run_info; self->run_info = green_statedict(self->parent); Py_INCREF(self->run_info); Py_XDECREF(o); /* start the greenlet */ self->stack_start = NULL; self->stack_stop = (char*) mark; if (ts_current->stack_start == NULL) { /* ts_current is dying */ self->stack_prev = ts_current->stack_prev; } else { self->stack_prev = ts_current; } self->top_frame = NULL; self->exc_type = NULL; self->exc_value = NULL; self->exc_traceback = NULL; self->recursion_depth = PyThreadState_GET()->recursion_depth; err = g_switchstack(); /* returns twice! The 1st time with err=1: we are in the new greenlet The 2nd time with err=0: back in the caller's greenlet */ if (err == 1) { /* in the new greenlet */ PyObject* result; PyGreenlet* parent; self->stack_start = (char*) 1; /* running */ if (args == NULL) { /* pending exception */ result = NULL; } else { /* call g.run(*args, **kwargs) */ result = PyEval_CallObjectWithKeywords( run, args, kwargs); Py_DECREF(args); Py_XDECREF(kwargs); } Py_DECREF(run); result = g_handle_exit(result); /* jump back to parent */ self->stack_start = NULL; /* dead */ for (parent = self->parent; parent != NULL; parent = parent->parent) { result = g_switch(parent, result, NULL); /* Return here means switch to parent failed, * in which case we throw *current* exception * to the next parent in chain. */ } /* We ran out of parents, cannot continue */ PyErr_WriteUnraisable((PyObject *) self); Py_FatalError("greenlets cannot continue"); } /* back in the parent */ return err; }
void SwitchStatement (void) /* Handle a switch statement for chars with a cmp cascade for the selector */ { ExprDesc SwitchExpr; /* Switch statement expression */ CodeMark CaseCodeStart; /* Start of code marker */ CodeMark SwitchCodeStart;/* Start of switch code */ CodeMark SwitchCodeEnd; /* End of switch code */ unsigned ExitLabel; /* Exit label */ unsigned SwitchCodeLabel;/* Label for the switch code */ int HaveBreak = 0; /* True if the last statement had a break */ int RCurlyBrace; /* True if last token is right curly brace */ SwitchCtrl* OldSwitch; /* Pointer to old switch control data */ SwitchCtrl SwitchData; /* New switch data */ /* Eat the "switch" token */ NextToken (); /* Read the switch expression and load it into the primary. It must have * integer type. */ ConsumeLParen (); Expression0 (&SwitchExpr); if (!IsClassInt (SwitchExpr.Type)) { Error ("Switch quantity is not an integer"); /* To avoid any compiler errors, make the expression a valid int */ ED_MakeConstAbsInt (&SwitchExpr, 1); } ConsumeRParen (); /* Add a jump to the switch code. This jump is usually unnecessary, * because the switch code will moved up just behind the switch * expression. However, in rare cases, there's a label at the end of * the switch expression. This label will not get moved, so the code * jumps around the switch code, and after moving the switch code, * things look really weird. If we add a jump here, we will never have * a label attached to the current code position, and the jump itself * will get removed by the optimizer if it is unnecessary. */ SwitchCodeLabel = GetLocalLabel (); g_jump (SwitchCodeLabel); /* Remember the current code position. We will move the switch code * to this position later. */ GetCodePos (&CaseCodeStart); /* Setup the control structure, save the old and activate the new one */ SwitchData.Nodes = NewCollection (); SwitchData.ExprType = UnqualifiedType (SwitchExpr.Type[0].C); SwitchData.Depth = SizeOf (SwitchExpr.Type); SwitchData.DefaultLabel = 0; OldSwitch = Switch; Switch = &SwitchData; /* Get the exit label for the switch statement */ ExitLabel = GetLocalLabel (); /* Create a loop so we may use break. */ AddLoop (ExitLabel, 0); /* Make sure a curly brace follows */ if (CurTok.Tok != TOK_LCURLY) { Error ("`{' expected"); } /* Parse the following statement, which will actually be a compound * statement because of the curly brace at the current input position */ HaveBreak = Statement (&RCurlyBrace); /* Check if we had any labels */ if (CollCount (SwitchData.Nodes) == 0 && SwitchData.DefaultLabel == 0) { Warning ("No case labels"); } /* If the last statement did not have a break, we may have an open * label (maybe from an if or similar). Emitting code and then moving * this code to the top will also move the label to the top which is * wrong. So if the last statement did not have a break (which would * carry the label), add a jump to the exit. If it is useless, the * optimizer will remove it later. */ if (!HaveBreak) { g_jump (ExitLabel); } /* Remember the current position */ GetCodePos (&SwitchCodeStart); /* Output the switch code label */ g_defcodelabel (SwitchCodeLabel); /* Generate code */ if (SwitchData.DefaultLabel == 0) { /* No default label, use switch exit */ SwitchData.DefaultLabel = ExitLabel; } g_switch (SwitchData.Nodes, SwitchData.DefaultLabel, SwitchData.Depth); /* Move the code to the front */ GetCodePos (&SwitchCodeEnd); MoveCode (&SwitchCodeStart, &SwitchCodeEnd, &CaseCodeStart); /* Define the exit label */ g_defcodelabel (ExitLabel); /* Exit the loop */ DelLoop (); /* Switch back to the enclosing switch statement if any */ Switch = OldSwitch; /* Free the case value tree */ FreeCaseNodeColl (SwitchData.Nodes); /* If the case statement was (correctly) terminated by a closing curly * brace, skip it now. */ if (RCurlyBrace) { NextToken (); } }
static int GREENLET_NOINLINE(g_initialstub)(void* mark) { int err; PyObject *o, *run; PyObject *exc, *val, *tb; PyObject *run_info; PyGreenlet* self = ts_target; PyObject* args = ts_passaround_args; PyObject* kwargs = ts_passaround_kwargs; /* save exception in case getattr clears it */ PyErr_Fetch(&exc, &val, &tb); /* self.run is the object to call in the new greenlet */ run = PyObject_GetAttrString((PyObject*) self, "run"); if (run == NULL) { Py_XDECREF(exc); Py_XDECREF(val); Py_XDECREF(tb); return -1; } /* restore saved exception */ PyErr_Restore(exc, val, tb); /* recheck the state in case getattr caused thread switches */ if (!STATE_OK) { Py_DECREF(run); return -1; } /* recheck run_info in case greenlet reparented anywhere above */ run_info = green_statedict(self); if (run_info == NULL || run_info != ts_current->run_info) { Py_DECREF(run); PyErr_SetString(PyExc_GreenletError, run_info ? "cannot switch to a different thread" : "cannot switch to a garbage collected greenlet"); return -1; } /* by the time we got here another start could happen elsewhere, * that means it should now be a regular switch */ if (PyGreenlet_STARTED(self)) { Py_DECREF(run); ts_passaround_args = args; ts_passaround_kwargs = kwargs; return 1; } /* start the greenlet */ self->stack_start = NULL; self->stack_stop = (char*) mark; if (ts_current->stack_start == NULL) { /* ts_current is dying */ self->stack_prev = ts_current->stack_prev; } else { self->stack_prev = ts_current; } self->top_frame = NULL; self->exc_type = NULL; self->exc_value = NULL; self->exc_traceback = NULL; self->recursion_depth = PyThreadState_GET()->recursion_depth; /* restore arguments in case they are clobbered */ ts_target = self; ts_passaround_args = args; ts_passaround_kwargs = kwargs; /* perform the initial switch */ err = g_switchstack(); /* returns twice! The 1st time with err=1: we are in the new greenlet The 2nd time with err=0: back in the caller's greenlet */ if (err == 1) { /* in the new greenlet */ PyGreenlet* origin; #if GREENLET_USE_TRACING PyObject* tracefunc; #endif PyObject* result; PyGreenlet* parent; self->stack_start = (char*) 1; /* running */ /* grab origin while we still can */ origin = ts_origin; ts_origin = NULL; /* now use run_info to store the statedict */ o = self->run_info; self->run_info = green_statedict(self->parent); Py_INCREF(self->run_info); Py_XDECREF(o); #if GREENLET_USE_TRACING if ((tracefunc = PyDict_GetItem(self->run_info, ts_tracekey)) != NULL) { Py_INCREF(tracefunc); if (g_calltrace(tracefunc, args ? ts_event_switch : ts_event_throw, origin, self) < 0) { /* Turn trace errors into switch throws */ Py_CLEAR(kwargs); Py_CLEAR(args); } Py_DECREF(tracefunc); } #endif Py_DECREF(origin); if (args == NULL) { /* pending exception */ result = NULL; } else { /* call g.run(*args, **kwargs) */ result = PyEval_CallObjectWithKeywords( run, args, kwargs); Py_DECREF(args); Py_XDECREF(kwargs); } Py_DECREF(run); result = g_handle_exit(result); /* jump back to parent */ self->stack_start = NULL; /* dead */ for (parent = self->parent; parent != NULL; parent = parent->parent) { result = g_switch(parent, result, NULL); /* Return here means switch to parent failed, * in which case we throw *current* exception * to the next parent in chain. */ assert(result == NULL); } /* We ran out of parents, cannot continue */ PyErr_WriteUnraisable((PyObject *) self); Py_FatalError("greenlets cannot continue"); } /* back in the parent */ if (err < 0) { /* start failed badly, restore greenlet state */ self->stack_start = NULL; self->stack_stop = NULL; self->stack_prev = NULL; } return err; }