Skip to content
Merged
1 change: 1 addition & 0 deletions lib/schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ bool lf_check_deadline(void* self, bool invoke_deadline_handler) {
}

void lf_update_deadline(void* self, interval_t updated_deadline) {
LF_PRINT_DEBUG("lf_update_deadline: update deadline to " PRINTF_TIME ".", updated_deadline);
reaction_t* reaction = ((self_base_t*)self)->executing_reaction;
if (reaction != NULL) {
reaction->deadline = updated_deadline;
Expand Down
17 changes: 17 additions & 0 deletions python/include/pythontarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ PyObject* py_package_directory(PyObject* self, PyObject* args);
*/
PyObject* py_check_deadline(PyObject* self, PyObject* args);

/**
* @brief Update the deadline of the currently executing reaction.
*
* Updating the deadline with this function does not affect
* the deadline check that has been performed (at the beginning
* of the caller reaction or check_deadline called before).
* Therefore, you may want to invoke check_deadline after
* updating the deadline through this function to confirm
* whether the newly updated deadline has been violated.
*
* @param self The Python object of the reactor.
* @param args contains:
* - updated_deadline: The updated deadline.
* @return Py_None or NULL if an error occurs.
*/
PyObject* py_update_deadline(PyObject* self, PyObject* args);

/**
* @brief Register a user trace event. Returns an opaque handle for use with
* tracepoint_user_event and tracepoint_user_value.
Expand Down
31 changes: 31 additions & 0 deletions python/lib/pythontarget.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,35 @@ PyObject* py_check_deadline(PyObject* self, PyObject* args) {
return PyBool_FromLong(result);
}

PyObject* py_update_deadline(PyObject* self, PyObject* args) {
PyObject* py_self;
int64_t updated_deadline = 0; // Default to 0
double updated_deadline_in_double =
0.0; // Deadline may be passed as a floating-point value in nanoseconds, e.g., SEC(0.5) → 0.5 * 1e9.

if (!PyArg_ParseTuple(args, "O|d", &py_self, &updated_deadline_in_double)) {
Comment thread
edwardalee marked this conversation as resolved.
return NULL;
}

// Check overflow before converting a double to int64_t (interval_t).
if (updated_deadline_in_double > (double)INT64_MAX || updated_deadline_in_double < (double)INT64_MIN) {
PyErr_SetString(PyExc_OverflowError, "The updated deadline value is out of int64 range");
return NULL;
}

// Convert double to int64_t
updated_deadline = (int64_t)updated_deadline_in_double;
Comment thread
edwardalee marked this conversation as resolved.

Comment thread
byeonggiljun marked this conversation as resolved.
void* self_ptr = get_lf_self_pointer(py_self);
if (self_ptr == NULL) {
return NULL;
}
lf_update_deadline(self_ptr, updated_deadline);

Py_INCREF(Py_None);
return Py_None;
}

/**
* Register a user trace event. Returns an opaque handle (as a Python int)
* that must be passed to tracepoint_user_event and tracepoint_user_value.
Expand Down Expand Up @@ -386,6 +415,8 @@ static PyMethodDef GEN_NAME(MODULE_NAME, _methods)[] = {
{"package_directory", py_package_directory, METH_NOARGS, "Root package directory path"},
{"check_deadline", (PyCFunction)py_check_deadline, METH_VARARGS,
"Check whether the deadline of the currently executing reaction has passed"},
{"update_deadline", (PyCFunction)py_update_deadline, METH_VARARGS,
"Update the deadline of the currently executing reaction"},
{"register_user_trace_event", (PyCFunction)py_register_user_trace_event, METH_VARARGS,
"Register a user trace event; returns a handle for use with tracepoint_user_event and tracepoint_user_value"},
{"tracepoint_user_event", (PyCFunction)py_tracepoint_user_event, METH_VARARGS,
Expand Down
Loading