This repository was archived by the owner on Feb 13, 2025. It is now read-only.
forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathstackless_util.c
More file actions
155 lines (132 loc) · 3.24 KB
/
stackless_util.c
File metadata and controls
155 lines (132 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "Python.h"
#ifdef STACKLESS
#include "pycore_stackless.h"
intptr_t * const _PyStackless__TryStacklessPtr = &_PyStackless_TRY_STACKLESS;
/* Initialize the Stackless runtime state */
void
slp_initialize(struct _stackless_runtime_state * state) {
/* initialize all fields to zero */
memset(state, 0, sizeof(*state));
}
/* Shorthands to return certain errors */
PyObject *
slp_type_error(const char *msg)
{
PyErr_SetString(PyExc_TypeError, msg);
return NULL;
}
PyObject *
slp_runtime_error(const char *msg)
{
PyErr_SetString(PyExc_RuntimeError, msg);
return NULL;
}
PyObject *
slp_value_error(const char *msg)
{
PyErr_SetString(PyExc_ValueError, msg);
return NULL;
}
PyObject *
slp_null_error(void)
{
if (!PyErr_Occurred())
PyErr_SetString(PyExc_SystemError,
"null argument to internal routine");
return NULL;
}
/* CAUTION: This function returns a borrowed reference */
PyFrameObject *
slp_get_frame(PyTaskletObject *task)
{
PyThreadState *ts;
assert(task->cstate != NULL);
ts = task->cstate->tstate;
return ts ? (ts->st.current == task ? ts->frame : task->f.frame) : NULL;
}
void slp_check_pending_irq()
{
PyThreadState *ts = _PyThreadState_GET();
PyTaskletObject *current = ts->st.current;
/* act only if hard irq is in effect */
if (current->flags.pending_irq && !(ts->st.runflags & PY_WATCHDOG_SOFT)) {
if (current->flags.atomic)
return;
if (!TASKLET_NESTING_OK(current))
return;
/* trigger interrupt */
ts->st.tick_watermark = ts->st.tick_counter + 1;
current->flags.pending_irq = 0;
}
}
int
slp_return_wrapper(PyObject *retval)
{
STACKLESS_ASSERT();
if (retval == NULL)
return -1;
if (STACKLESS_UNWINDING(retval)) {
STACKLESS_UNPACK(_PyThreadState_GET(), retval);
Py_XDECREF(retval);
return 1;
}
Py_DECREF(retval);
return 0;
}
int
slp_return_wrapper_hard(PyObject *retval)
{
STACKLESS_ASSERT();
if (retval == NULL)
return -1;
assert(!STACKLESS_UNWINDING(retval));
Py_DECREF(retval);
return 0;
}
int
slp_int_wrapper(PyObject *retval)
{
int ret = -909090;
STACKLESS_ASSERT();
if (retval != NULL) {
ret = PyLong_AsLong(retval);
Py_DECREF(retval);
}
return ret;
}
int
slp_current_wrapper( int(*func)(PyTaskletObject*), PyTaskletObject *task )
{
PyThreadState *ts = _PyThreadState_GET();
int ret;
ts->st.main = (PyTaskletObject*)Py_None;
ret = func(task);
ts->st.main = NULL;
return ret;
}
/*
* A thread id is either an unsigned long or the special value -1.
*/
long
slp_parse_thread_id(PyObject *thread_id, unsigned long *id)
{
int overflow;
long result1;
unsigned long result2;
assert(id != NULL);
if (thread_id == NULL)
return 1;
/* thread_id is either an unsigned long or -1. We distinguish these values */
result1 = PyLong_AsLongAndOverflow(thread_id, &overflow);
if (overflow == 0 && result1 == -1) {
/* a special negative id */
*id = (unsigned long)result1;
return result1;
}
result2 = PyLong_AsUnsignedLong(thread_id);
if (PyErr_Occurred())
return 0;
*id = result2;
return 1;
}
#endif