Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'MC_LTL'
[simgrid.git] / src / bindings / lua / lua_process.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /* SimGrid Lua bindings                                                     */
8
9 #include "lua_private.h"
10 #include <lauxlib.h>
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua_process, bindings, "Lua Bindings (process module)");
13
14 #define PROCESS_MODULE_NAME "simgrid.process"
15
16 /* ********************************************************************************* */
17 /*                              simgrid.process API                                  */
18 /* ********************************************************************************* */
19
20 /**
21  * \brief Makes the current process sleep for a while.
22  * \param L a Lua state
23  * \return number of values returned to Lua
24  *
25  * - Argument 1 (number): duration of the sleep
26  * - Return value (nil or string): nil in everything went ok, or a string error
27  * if case of failure ("host failure")
28  */
29 static int l_process_sleep(lua_State* L)
30 {
31   double duration = luaL_checknumber(L, 1);
32   MSG_error_t res = MSG_process_sleep(duration);
33
34   switch (res) {
35
36   case MSG_OK:
37     return 0;
38
39   case MSG_HOST_FAILURE:
40     lua_pushliteral(L, "host failure");
41     return 1;
42
43   default:
44     xbt_die("Unexpected result of MSG_process_sleep: %d, please report this bug", res);
45   }
46 }
47
48 static const luaL_reg process_functions[] = {
49     {"sleep", l_process_sleep},
50     /* TODO: self, create, kill, suspend, is_suspended, resume, get_name,
51      * get_pid, get_ppid, migrate
52      */
53     {NULL, NULL}
54 };
55
56 /**
57  * \brief Registers the process functions into the table simgrid.process.
58  * \param L a lua state
59  */
60 void sglua_register_process_functions(lua_State* L)
61 {
62   luaL_openlib(L, PROCESS_MODULE_NAME, process_functions, 0);
63                                   /* simgrid.process */
64   lua_pop(L, 1);
65 }
66