1 /* Copyright (c) 2010, 2012-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include "lua_private.h"
9 #include "lua_state_cloner.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_task, bindings, "Lua bindings (task module)");
14 #define TASK_MODULE_NAME "simgrid.task"
16 /* ********************************************************************************* */
17 /* simgrid.task API */
18 /* ********************************************************************************* */
21 * \brief Ensures that a value in the stack is a valid task and returns it.
22 * A task is "valid" if the value in the stack at the given index is
24 * (b) has a field called "__simgrid_task" which
25 * (c) contains the ctask itself (which can be typecast to msg_task_t*)
26 * \param L a Lua state
27 * \param index an index in the Lua stack
28 * \return the C task corresponding to this Lua task
30 msg_task_t sglua_check_task(lua_State* L, int index)
32 sglua_stack_dump("check task: ", L);
33 luaL_checktype(L, index, LUA_TTABLE);
35 lua_getfield(L, index, "__simgrid_task");
36 /* ... task ... ctask */
37 msg_task_t task = *((msg_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME));
42 luaL_error(L, "This task was sent to someone else, you cannot access it anymore");
49 * \brief Creates a new task and places it onto the stack.
50 * \param L a Lua state
51 * \return number of values returned to Lua
53 * - Argument 1 (string): name of the task
54 * - Argument 2 (number): computation size
55 * - Argument 3 (number): communication size
56 * - Return value (task): the task created
58 * A Lua task is a regular table with a full userdata inside, and both share
59 * the same metatable. For the regular table, the metatable allows OO-style
60 * writing such as your_task:send(someone) instead of your_task.send(your_task, someone)
61 * For the userdata, the metatable is used to check its type.
62 * TODO: make the task name an optional last parameter
64 static int l_task_new(lua_State* L)
66 XBT_DEBUG("Task new");
67 const char* name = luaL_checkstring(L, 1);
68 int comp_size = (int) luaL_checkinteger(L, 2);
69 int msg_size = (int) luaL_checkinteger(L, 3);
73 msg_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
77 luaL_getmetatable(L, TASK_MODULE_NAME);
79 lua_setmetatable(L, -2);
81 msg_task_t* lua_task = (msg_task_t*) lua_newuserdata(L, sizeof(msg_task_t));
84 luaL_getmetatable(L, TASK_MODULE_NAME);
86 lua_setmetatable(L, -2);
88 lua_setfield(L, -2, "__simgrid_task");
94 * \brief Returns the name of a task.
95 * \param L a Lua state
96 * \return number of values returned to Lua
98 * - Argument 1 (task): a task
99 * - Return value (string): name of the task
101 static int l_task_get_name(lua_State* L)
103 msg_task_t task = sglua_check_task(L, 1);
104 lua_pushstring(L, MSG_task_get_name(task));
109 * \brief Returns the computation duration of a task.
110 * \param L a Lua state
111 * \return number of values returned to Lua
113 * - Argument 1 (task): a task
114 * - Return value (number): computation duration of this task
116 static int l_task_get_computation_duration(lua_State* L)
118 msg_task_t task = sglua_check_task(L, 1);
119 lua_pushnumber(L, MSG_task_get_flops_amount(task));
124 * \brief Executes a task.
125 * \param L a Lua state
126 * \return number of values returned to Lua
128 * - Argument 1 (task): the task to execute
129 * - Return value (nil or string): nil if the task was successfully executed,
130 * or an error string in case of failure, which may be "task canceled" or
133 static int l_task_execute(lua_State* L)
135 XBT_DEBUG("Called task_execute");
136 msg_task_t task = sglua_check_task(L, 1);
137 msg_error_t res = MSG_task_execute(task);
143 lua_pushstring(L, sglua_get_msg_error(res));
149 * \brief Pops the Lua task from the stack and registers it so that the
150 * process can retrieve it later knowing the C task.
151 * \param L a lua state
153 void sglua_task_register(lua_State* L) {
155 msg_task_t task = sglua_check_task(L, -1);
157 /* put in the C task a ref to the lua task so that the receiver finds it */
158 unsigned long ref = luaL_ref(L, LUA_REGISTRYINDEX);
160 MSG_task_set_data(task, (void*) ref);
164 * \brief Pushes onto the stack the Lua task corresponding to a C task.
166 * The Lua task must have been previously registered with task_register so
167 * that it can be retrieved knowing the C task.
169 * \param L a lua state
170 * \param task a C task
172 void sglua_task_unregister(lua_State* L, msg_task_t task) {
175 /* the task is in my registry, put it onto my stack */
176 unsigned long ref = (unsigned long) MSG_task_get_data(task);
177 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
179 luaL_unref(L, LUA_REGISTRYINDEX, ref);
180 MSG_task_set_data(task, NULL);
184 * \brief This function is called when a C task has just been copied.
186 * This callback is used to move the corresponding Lua task from the sender
187 * process to the receiver process (so that the receiver can access and deal
189 * It is executed in SIMIX kernel mode when the communication finishes,
190 * before both processes are awaken. Thus, this function is thread-safe when
191 * user processes are executed in parallel, though it modifies the Lua
192 * stack of both processes to move the task.
193 * After this function, both Lua stacks are restored in their previous state.
194 * The task is moved from the registry of the sender to the registry of the
197 * \param task the task copied
198 * \param src_process the sender
199 * \param dst_process the receiver
201 static void task_copy_callback(msg_task_t task, msg_process_t src_process,
202 msg_process_t dst_process) {
204 lua_State* src = (lua_State*) MSG_process_get_data(src_process);
205 lua_State* dst = (lua_State*) MSG_process_get_data(dst_process);
209 sglua_task_unregister(src, task);
211 sglua_copy_value(src, dst);
214 sglua_task_register(dst);
217 /* the receiver is now the owner of the task and may destroy it:
218 * make the sender forget the C task so that it doesn't garbage */
219 lua_getfield(src, -1, "__simgrid_task");
220 /* src: ... task ctask */
221 msg_task_t* udata = (msg_task_t*) luaL_checkudata(src, -1, TASK_MODULE_NAME);
228 * \brief Sends a task to a mailbox and waits for its completion.
229 * \param L a Lua state
230 * \return number of values returned to Lua
232 * - Argument 1 (task): the task to send
233 * - Argument 2 (string or compatible): mailbox name, as a real string or any
234 * type convertible to string (numbers always are)
235 * - Argument 3 (number, optional): timeout (default is no timeout)
236 * - Return values (boolean + string): true if the communication was successful,
237 * or false plus an error string in case of failure, which may be "timeout",
238 * "host failure" or "transfer failure"
240 static int l_task_send(lua_State* L)
242 const char* mailbox = luaL_checkstring(L, 2);
243 msg_task_t task = sglua_check_task(L, 1);
245 if (lua_gettop(L) >= 3) {
246 timeout = luaL_checknumber(L, 3);
250 /* no timeout by default */
254 sglua_task_register(L);
256 msg_error_t res = MSG_task_send_with_timeout(task, mailbox, timeout);
259 lua_pushboolean(L, 1);
264 /* the communication has failed, I'm still the owner of the task */
265 sglua_task_unregister(L, task);
267 lua_pushboolean(L, 0);
269 lua_pushstring(L, sglua_get_msg_error(res));
270 /* task false error */
276 * \brief Sends a task to a mailbox.
277 * \param L a Lua state
278 * \return number of values returned to Lua
280 * This is a non-blocking function: use simgrid.comm.wait() or
281 * simgrid.comm.test() to end the communication.
283 * - Argument 1 (task): the task to send
284 * - Argument 2 (string or compatible): mailbox name, as a real string or any
285 * type convertible to string (numbers always are)
286 * - Return value (comm): a communication object to be used later with wait or test
288 static int l_task_isend(lua_State* L)
290 const char* mailbox = luaL_checkstring(L, 2);
291 msg_task_t task = sglua_check_task(L, 1);
292 /* task mailbox ... */
295 sglua_task_register(L);
297 msg_comm_t comm = MSG_task_isend(task, mailbox);
299 sglua_push_comm(L, comm);
305 * \brief Sends a task on a mailbox on a best effort way (detached send).
306 * \param L a Lua state
307 * \return number of values returned to Lua
309 * Like simgrid.task.isend, this is a non-blocking function.
310 * You can use this function if you don't care about when the communication
311 * ends and whether it succeeds.
312 * FIXME: isn't this equivalent to calling simgrid.task.isend() and ignoring
315 * - Argument 1 (task): the task to send
316 * - Argument 2 (string or compatible): mailbox name, as a real string or any
317 * type convertible to string (numbers always are)
319 static int l_task_dsend(lua_State* L)
321 const char* mailbox = luaL_checkstring(L, 2);
322 msg_task_t task = sglua_check_task(L, 1);
323 /* task mailbox ... */
326 sglua_task_register(L);
328 MSG_task_dsend(task, mailbox, NULL);
333 * \brief Receives a task.
334 * \param L a Lua state
335 * \return number of values returned to Lua
337 * - Argument 1 (string or compatible): mailbox name, as a real string or any
338 * type convertible to string (numbers always are)
339 * - Argument 2 (number, optional): timeout (default is no timeout)
340 * - Return values (task or nil + string): the task received, or nil plus an
341 * error message if the communication has failed
343 static int l_task_recv(lua_State* L)
345 msg_task_t task = NULL;
346 const char* mailbox = luaL_checkstring(L, 1);
348 if (lua_gettop(L) >= 2) {
349 /* mailbox timeout ... */
350 timeout = luaL_checknumber(L, 2);
355 /* no timeout by default */
358 msg_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
361 sglua_task_unregister(L, task);
362 /* mailbox ... task */
367 /* mailbox ... nil */
368 lua_pushstring(L, sglua_get_msg_error(res));
369 /* mailbox ... nil error */
375 * \brief Asynchronously receives a task on a mailbox.
376 * \param L a Lua state
377 * \return number of values returned to Lua
379 * This is a non-blocking function: use simgrid.comm.wait() or
380 * simgrid.comm.test() to end the communication and get the task in case of
383 * - Argument 1 (string or compatible): mailbox name, as a real string or any
384 * type convertible to string (numbers always are)
385 * - Return value (comm): a communication object to be used later with wait or test
388 static int l_task_irecv(lua_State* L)
390 const char* mailbox = luaL_checkstring(L, 1);
392 msg_task_t* task = xbt_new0(msg_task_t, 1); // FIXME fix this leak
393 msg_comm_t comm = MSG_task_irecv(task, mailbox);
394 sglua_push_comm(L, comm);
395 /* mailbox ... comm */
399 static const luaL_Reg task_functions[] = {
401 {"get_name", l_task_get_name},
402 {"get_computation_duration", l_task_get_computation_duration},
403 {"execute", l_task_execute},
404 {"send", l_task_send},
405 {"isend", l_task_isend},
406 {"dsend", l_task_dsend},
407 {"recv", l_task_recv},
408 {"irecv", l_task_irecv},
413 * \brief Finalizes the userdata of a task.
414 * \param L a Lua state
415 * \return number of values returned to Lua
417 * - Argument 1 (userdata): a C task, possibly NULL if it was sent to another
420 static int l_task_gc(lua_State* L)
422 void *p = luaL_testudata(L, 1, TASK_MODULE_NAME);
427 msg_task_t task = *((msg_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
428 /* the task is NULL if I sent it to someone else */
430 MSG_task_destroy(task);
437 * \brief Returns a string representation of a C task.
438 * \param L a Lua state
439 * \return number of values returned to Lua
441 * - Argument 1 (userdata): a task
442 * - Return value (string): a string describing this task
444 static int l_task_tostring(lua_State* L)
446 msg_task_t task = *((msg_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
447 lua_pushfstring(L, "Task: %p", task);
452 * \brief Metamethods of both a task table and the userdata inside it.
454 static const luaL_Reg task_meta[] = {
455 {"__gc", l_task_gc}, /* will be called only for userdata */
456 {"__tostring", l_task_tostring},
461 * \brief Registers the task functions into the table simgrid.task.
463 * Also initialize the metatable of the task userdata type.
465 * \param L a lua state
467 void sglua_register_task_functions(lua_State* L)
469 /* create a table simgrid.task and fill it with task functions */
470 lua_getglobal(L, "simgrid"); /* simgrid */
471 luaL_newlib(L, task_functions); /* simgrid simgrid.task */
473 /* create the metatable for tasks, add it to the Lua registry */
474 luaL_newmetatable(L, TASK_MODULE_NAME); /* simgrid simgrid.task mt */
476 /* fill the metatable */
477 luaL_setfuncs(L, task_meta, 0); /* simgrid simgrid.task mt */
478 lua_pushvalue(L, -2); /* simgrid simgrid.task mt simgrid.task */
480 /* metatable.__index = simgrid.task
481 * we put the task functions inside the task itself:
482 * this allows to write my_task:method(args) for
483 * simgrid.task.method(my_task, args) */
484 lua_setfield(L, -2, "__index"); /* simgrid simgrid.task mt */
486 lua_setmetatable(L, -2); /* simgrid simgrid.task */
487 lua_setfield(L, -2, "task"); /* simgrid */
488 lua_pop(L, 1); /* -- */
490 /* set up MSG to copy Lua tasks between states */
491 MSG_task_set_copy_callback(task_copy_callback);