A
lgorithmique
N
umérique
D
istribuée
Public GIT Repository
projects
/
simgrid.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
[Lua] Fixed garbage collection segfault
[simgrid.git]
/
src
/
bindings
/
lua
/
lua_task.c
diff --git
a/src/bindings/lua/lua_task.c
b/src/bindings/lua/lua_task.c
index
0305642
..
890bbe6
100644
(file)
--- a/
src/bindings/lua/lua_task.c
+++ b/
src/bindings/lua/lua_task.c
@@
-1,4
+1,4
@@
-/* Copyright (c) 2010. The SimGrid Team.
+/* Copyright (c) 2010
, 2012-2015
. The SimGrid Team.
* All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
* All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
@@
-19,18
+19,22
@@
XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_task, bindings, "Lua bindings (task module)"
/**
* \brief Ensures that a value in the stack is a valid task and returns it.
/**
* \brief Ensures that a value in the stack is a valid task and returns it.
+ * A task is "valid" if the value in the stack at the given index is
+ * (a) a table
+ * (b) has a field called "__simgrid_task" which
+ * (c) contains the ctask itself (which can be typecast to msg_task_t*)
* \param L a Lua state
* \param index an index in the Lua stack
* \return the C task corresponding to this Lua task
*/
* \param L a Lua state
* \param index an index in the Lua stack
* \return the C task corresponding to this Lua task
*/
-m_task_t sglua_check_task(lua_State* L, int index)
+m
sg
_task_t sglua_check_task(lua_State* L, int index)
{
sglua_stack_dump("check task: ", L);
luaL_checktype(L, index, LUA_TTABLE);
/* ... task ... */
lua_getfield(L, index, "__simgrid_task");
/* ... task ... ctask */
{
sglua_stack_dump("check task: ", L);
luaL_checktype(L, index, LUA_TTABLE);
/* ... task ... */
lua_getfield(L, index, "__simgrid_task");
/* ... task ... ctask */
- m
_task_t task = *((m
_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME));
+ m
sg_task_t task = *((msg
_task_t*) luaL_checkudata(L, -1, TASK_MODULE_NAME));
lua_pop(L, 1);
/* ... task ... */
lua_pop(L, 1);
/* ... task ... */
@@
-42,7
+46,7
@@
m_task_t sglua_check_task(lua_State* L, int index)
}
/**
}
/**
- * \brief Creates a new task and
leav
es it onto the stack.
+ * \brief Creates a new task and
plac
es it onto the stack.
* \param L a Lua state
* \return number of values returned to Lua
*
* \param L a Lua state
* \return number of values returned to Lua
*
@@
-53,7
+57,7
@@
m_task_t sglua_check_task(lua_State* L, int index)
*
* A Lua task is a regular table with a full userdata inside, and both share
* the same metatable. For the regular table, the metatable allows OO-style
*
* A Lua task is a regular table with a full userdata inside, and both share
* the same metatable. For the regular table, the metatable allows OO-style
- * writing such as your_task:send(someone)
.
+ * writing such as your_task:send(someone)
instead of your_task.send(your_task, someone)
* For the userdata, the metatable is used to check its type.
* TODO: make the task name an optional last parameter
*/
* For the userdata, the metatable is used to check its type.
* TODO: make the task name an optional last parameter
*/
@@
-61,12
+65,12
@@
static int l_task_new(lua_State* L)
{
XBT_DEBUG("Task new");
const char* name = luaL_checkstring(L, 1);
{
XBT_DEBUG("Task new");
const char* name = luaL_checkstring(L, 1);
- int comp_size
= luaL_checkint
(L, 2);
- int msg_size
= luaL_checkint
(L, 3);
+ int comp_size
= (int) luaL_checkinteger
(L, 2);
+ int msg_size
= (int) luaL_checkinteger
(L, 3);
/* name comp comm */
lua_settop(L, 0);
/* -- */
/* name comp comm */
lua_settop(L, 0);
/* -- */
- m_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
+ m
sg
_task_t msg_task = MSG_task_create(name, comp_size, msg_size, NULL);
lua_newtable(L);
/* task */
lua_newtable(L);
/* task */
@@
-74,7
+78,7
@@
static int l_task_new(lua_State* L)
/* task mt */
lua_setmetatable(L, -2);
/* task */
/* task mt */
lua_setmetatable(L, -2);
/* task */
- m
_task_t* lua_task = (m_task_t*) lua_newuserdata(L, sizeof(m
_task_t));
+ m
sg_task_t* lua_task = (msg_task_t*) lua_newuserdata(L, sizeof(msg
_task_t));
/* task ctask */
*lua_task = msg_task;
luaL_getmetatable(L, TASK_MODULE_NAME);
/* task ctask */
*lua_task = msg_task;
luaL_getmetatable(L, TASK_MODULE_NAME);
@@
-96,7
+100,7
@@
static int l_task_new(lua_State* L)
*/
static int l_task_get_name(lua_State* L)
{
*/
static int l_task_get_name(lua_State* L)
{
- m_task_t task = sglua_check_task(L, 1);
+ m
sg
_task_t task = sglua_check_task(L, 1);
lua_pushstring(L, MSG_task_get_name(task));
return 1;
}
lua_pushstring(L, MSG_task_get_name(task));
return 1;
}
@@
-111,8
+115,8
@@
static int l_task_get_name(lua_State* L)
*/
static int l_task_get_computation_duration(lua_State* L)
{
*/
static int l_task_get_computation_duration(lua_State* L)
{
- m_task_t task = sglua_check_task(L, 1);
- lua_pushnumber(L, MSG_task_get_
compute_duration
(task));
+ m
sg
_task_t task = sglua_check_task(L, 1);
+ lua_pushnumber(L, MSG_task_get_
flops_amount
(task));
return 1;
}
return 1;
}
@@
-128,8
+132,9
@@
static int l_task_get_computation_duration(lua_State* L)
*/
static int l_task_execute(lua_State* L)
{
*/
static int l_task_execute(lua_State* L)
{
- m_task_t task = sglua_check_task(L, 1);
- MSG_error_t res = MSG_task_execute(task);
+ XBT_DEBUG("Called task_execute");
+ msg_task_t task = sglua_check_task(L, 1);
+ msg_error_t res = MSG_task_execute(task);
if (res == MSG_OK) {
return 0;
if (res == MSG_OK) {
return 0;
@@
-147,7
+152,7
@@
static int l_task_execute(lua_State* L)
*/
void sglua_task_register(lua_State* L) {
*/
void sglua_task_register(lua_State* L) {
- m_task_t task = sglua_check_task(L, -1);
+ m
sg
_task_t task = sglua_check_task(L, -1);
/* ... task */
/* put in the C task a ref to the lua task so that the receiver finds it */
unsigned long ref = luaL_ref(L, LUA_REGISTRYINDEX);
/* ... task */
/* put in the C task a ref to the lua task so that the receiver finds it */
unsigned long ref = luaL_ref(L, LUA_REGISTRYINDEX);
@@
-164,7
+169,7
@@
void sglua_task_register(lua_State* L) {
* \param L a lua state
* \param task a C task
*/
* \param L a lua state
* \param task a C task
*/
-void sglua_task_unregister(lua_State* L, m_task_t task) {
+void sglua_task_unregister(lua_State* L, m
sg
_task_t task) {
/* ... */
/* the task is in my registry, put it onto my stack */
/* ... */
/* the task is in my registry, put it onto my stack */
@@
-179,7
+184,8
@@
void sglua_task_unregister(lua_State* L, m_task_t task) {
* \brief This function is called when a C task has just been copied.
*
* This callback is used to move the corresponding Lua task from the sender
* \brief This function is called when a C task has just been copied.
*
* This callback is used to move the corresponding Lua task from the sender
- * process to the receiver process.
+ * process to the receiver process (so that the receiver can access and deal
+ * with it).
* It is executed in SIMIX kernel mode when the communication finishes,
* before both processes are awaken. Thus, this function is thread-safe when
* user processes are executed in parallel, though it modifies the Lua
* It is executed in SIMIX kernel mode when the communication finishes,
* before both processes are awaken. Thus, this function is thread-safe when
* user processes are executed in parallel, though it modifies the Lua
@@
-192,11
+198,11
@@
void sglua_task_unregister(lua_State* L, m_task_t task) {
* \param src_process the sender
* \param dst_process the receiver
*/
* \param src_process the sender
* \param dst_process the receiver
*/
-static void task_copy_callback(m
_task_t task, m
_process_t src_process,
- m_process_t dst_process) {
+static void task_copy_callback(m
sg_task_t task, msg
_process_t src_process,
+ m
sg
_process_t dst_process) {
- lua_State* src = MSG_process_get_data(src_process);
- lua_State* dst = MSG_process_get_data(dst_process);
+ lua_State* src =
(lua_State*)
MSG_process_get_data(src_process);
+ lua_State* dst =
(lua_State*)
MSG_process_get_data(dst_process);
/* src: ...
dst: ... */
/* src: ...
dst: ... */
@@
-205,13
+211,14
@@
static void task_copy_callback(m_task_t task, m_process_t src_process,
sglua_copy_value(src, dst);
/* src: ... task
dst: ... task */
sglua_copy_value(src, dst);
/* src: ... task
dst: ... task */
- sglua_task_register(dst); /* dst: ... */
+ sglua_task_register(dst);
+ /* dst: ... */
/* the receiver is now the owner of the task and may destroy it:
* make the sender forget the C task so that it doesn't garbage */
lua_getfield(src, -1, "__simgrid_task");
/* src: ... task ctask */
/* the receiver is now the owner of the task and may destroy it:
* make the sender forget the C task so that it doesn't garbage */
lua_getfield(src, -1, "__simgrid_task");
/* src: ... task ctask */
- m
_task_t* udata = (m
_task_t*) luaL_checkudata(src, -1, TASK_MODULE_NAME);
+ m
sg_task_t* udata = (msg
_task_t*) luaL_checkudata(src, -1, TASK_MODULE_NAME);
*udata = NULL;
lua_pop(src, 2);
/* src: ... */
*udata = NULL;
lua_pop(src, 2);
/* src: ... */
@@
-225,20
+232,28
@@
static void task_copy_callback(m_task_t task, m_process_t src_process,
* - Argument 1 (task): the task to send
* - Argument 2 (string or compatible): mailbox name, as a real string or any
* type convertible to string (numbers always are)
* - Argument 1 (task): the task to send
* - Argument 2 (string or compatible): mailbox name, as a real string or any
* type convertible to string (numbers always are)
+ * - Argument 3 (number, optional): timeout (default is no timeout)
* - Return values (boolean + string): true if the communication was successful,
* or false plus an error string in case of failure, which may be "timeout",
* "host failure" or "transfer failure"
*/
static int l_task_send(lua_State* L)
{
* - Return values (boolean + string): true if the communication was successful,
* or false plus an error string in case of failure, which may be "timeout",
* "host failure" or "transfer failure"
*/
static int l_task_send(lua_State* L)
{
- m_task_t task = sglua_check_task(L, 1);
const char* mailbox = luaL_checkstring(L, 2);
const char* mailbox = luaL_checkstring(L, 2);
- /* task mailbox ... */
+ msg_task_t task = sglua_check_task(L, 1);
+ double timeout;
+ if (lua_gettop(L) >= 3) {
+ timeout = luaL_checknumber(L, 3);
+ }
+ else {
+ timeout = -1;
+ /* no timeout by default */
+ }
lua_settop(L, 1);
/* task */
sglua_task_register(L);
/* -- */
lua_settop(L, 1);
/* task */
sglua_task_register(L);
/* -- */
-
MSG_error_t res = MSG_task_send(task, mailbox
);
+
msg_error_t res = MSG_task_send_with_timeout(task, mailbox, timeout
);
if (res == MSG_OK) {
lua_pushboolean(L, 1);
if (res == MSG_OK) {
lua_pushboolean(L, 1);
@@
-258,7
+273,7
@@
static int l_task_send(lua_State* L)
}
/**
}
/**
- * \brief Sends a task
on
a mailbox.
+ * \brief Sends a task
to
a mailbox.
* \param L a Lua state
* \return number of values returned to Lua
*
* \param L a Lua state
* \return number of values returned to Lua
*
@@
-272,8
+287,8
@@
static int l_task_send(lua_State* L)
*/
static int l_task_isend(lua_State* L)
{
*/
static int l_task_isend(lua_State* L)
{
- m_task_t task = sglua_check_task(L, 1);
const char* mailbox = luaL_checkstring(L, 2);
const char* mailbox = luaL_checkstring(L, 2);
+ msg_task_t task = sglua_check_task(L, 1);
/* task mailbox ... */
lua_settop(L, 1);
/* task */
/* task mailbox ... */
lua_settop(L, 1);
/* task */
@@
-303,8
+318,8
@@
static int l_task_isend(lua_State* L)
*/
static int l_task_dsend(lua_State* L)
{
*/
static int l_task_dsend(lua_State* L)
{
- m_task_t task = sglua_check_task(L, 1);
const char* mailbox = luaL_checkstring(L, 2);
const char* mailbox = luaL_checkstring(L, 2);
+ msg_task_t task = sglua_check_task(L, 1);
/* task mailbox ... */
lua_settop(L, 1);
/* task */
/* task mailbox ... */
lua_settop(L, 1);
/* task */
@@
-327,9
+342,9
@@
static int l_task_dsend(lua_State* L)
*/
static int l_task_recv(lua_State* L)
{
*/
static int l_task_recv(lua_State* L)
{
- m_task_t task = NULL;
+ m
sg
_task_t task = NULL;
const char* mailbox = luaL_checkstring(L, 1);
const char* mailbox = luaL_checkstring(L, 1);
-
int
timeout;
+
double
timeout;
if (lua_gettop(L) >= 2) {
/* mailbox timeout ... */
timeout = luaL_checknumber(L, 2);
if (lua_gettop(L) >= 2) {
/* mailbox timeout ... */
timeout = luaL_checknumber(L, 2);
@@
-340,7
+355,7
@@
static int l_task_recv(lua_State* L)
/* no timeout by default */
}
/* mailbox ... */
/* no timeout by default */
}
/* mailbox ... */
-
MSG
_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
+
msg
_error_t res = MSG_task_receive_with_timeout(&task, mailbox, timeout);
if (res == MSG_OK) {
sglua_task_unregister(L, task);
if (res == MSG_OK) {
sglua_task_unregister(L, task);
@@
-374,14
+389,14
@@
static int l_task_irecv(lua_State* L)
{
const char* mailbox = luaL_checkstring(L, 1);
/* mailbox ... */
{
const char* mailbox = luaL_checkstring(L, 1);
/* mailbox ... */
- m
_task_t* task = xbt_new0(m
_task_t, 1); // FIXME fix this leak
+ m
sg_task_t* task = xbt_new0(msg
_task_t, 1); // FIXME fix this leak
msg_comm_t comm = MSG_task_irecv(task, mailbox);
sglua_push_comm(L, comm);
/* mailbox ... comm */
return 1;
}
msg_comm_t comm = MSG_task_irecv(task, mailbox);
sglua_push_comm(L, comm);
/* mailbox ... comm */
return 1;
}
-static const luaL_
r
eg task_functions[] = {
+static const luaL_
R
eg task_functions[] = {
{"new", l_task_new},
{"get_name", l_task_get_name},
{"get_computation_duration", l_task_get_computation_duration},
{"new", l_task_new},
{"get_name", l_task_get_name},
{"get_computation_duration", l_task_get_computation_duration},
@@
-404,12
+419,17
@@
static const luaL_reg task_functions[] = {
*/
static int l_task_gc(lua_State* L)
{
*/
static int l_task_gc(lua_State* L)
{
- /* ctask */
- m_task_t task = *((m_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
+ void *p = luaL_testudata(L, 1, TASK_MODULE_NAME);
+ if (p == NULL) {
+ return 0;
+ }
+
+ msg_task_t task = *((msg_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
/* the task is NULL if I sent it to someone else */
if (task != NULL) {
MSG_task_destroy(task);
}
/* the task is NULL if I sent it to someone else */
if (task != NULL) {
MSG_task_destroy(task);
}
+ lua_settop(L, 0);
return 0;
}
return 0;
}
@@
-423,7
+443,7
@@
static int l_task_gc(lua_State* L)
*/
static int l_task_tostring(lua_State* L)
{
*/
static int l_task_tostring(lua_State* L)
{
- m
_task_t task = *((m
_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
+ m
sg_task_t task = *((msg
_task_t*) luaL_checkudata(L, 1, TASK_MODULE_NAME));
lua_pushfstring(L, "Task: %p", task);
return 1;
}
lua_pushfstring(L, "Task: %p", task);
return 1;
}
@@
-431,7
+451,7
@@
static int l_task_tostring(lua_State* L)
/**
* \brief Metamethods of both a task table and the userdata inside it.
*/
/**
* \brief Metamethods of both a task table and the userdata inside it.
*/
-static const luaL_
r
eg task_meta[] = {
+static const luaL_
R
eg task_meta[] = {
{"__gc", l_task_gc}, /* will be called only for userdata */
{"__tostring", l_task_tostring},
{NULL, NULL}
{"__gc", l_task_gc}, /* will be called only for userdata */
{"__tostring", l_task_tostring},
{NULL, NULL}
@@
-447,25
+467,25
@@
static const luaL_reg task_meta[] = {
void sglua_register_task_functions(lua_State* L)
{
/* create a table simgrid.task and fill it with task functions */
void sglua_register_task_functions(lua_State* L)
{
/* create a table simgrid.task and fill it with task functions */
- lua
L_openlib(L, TASK_MODULE_NAME, task_functions, 0);
-
/*
simgrid.task */
+ lua
_getglobal(L, "simgrid"); /* simgrid */
+
luaL_newlib(L, task_functions); /* simgrid
simgrid.task */
/* create the metatable for tasks, add it to the Lua registry */
/* create the metatable for tasks, add it to the Lua registry */
- luaL_newmetatable(L, TASK_MODULE_NAME);
- /* simgrid.task mt */
+ luaL_newmetatable(L, TASK_MODULE_NAME);
/* simgrid simgrid.task mt */
+
/* fill the metatable */
/* fill the metatable */
- luaL_openlib(L, NULL, task_meta, 0);
- /* simgrid.task mt */
- lua_pushvalue(L, -2);
- /* simgrid.task mt simgrid.task */
+ luaL_setfuncs(L, task_meta, 0); /* simgrid simgrid.task mt */
+ lua_pushvalue(L, -2); /* simgrid simgrid.task mt simgrid.task */
+
/* metatable.__index = simgrid.task
* we put the task functions inside the task itself:
* this allows to write my_task:method(args) for
* simgrid.task.method(my_task, args) */
/* metatable.__index = simgrid.task
* we put the task functions inside the task itself:
* this allows to write my_task:method(args) for
* simgrid.task.method(my_task, args) */
- lua_setfield(L, -2, "__index");
- /* simgrid.task mt */
- lua_pop(L, 2);
- /* -- */
+ lua_setfield(L, -2, "__index"); /* simgrid simgrid.task mt */
+
+ lua_setmetatable(L, -2); /* simgrid simgrid.task */
+ lua_setfield(L, -2, "task"); /* simgrid */
+ lua_pop(L, 1); /* -- */
/* set up MSG to copy Lua tasks between states */
MSG_task_set_copy_callback(task_copy_callback);
/* set up MSG to copy Lua tasks between states */
MSG_task_set_copy_callback(task_copy_callback);