1 // SPDX-License-Identifier: GPL-2.0-only or License-Ref-kk-custom
2 /*
3  * Copyright (C) 2020 Kernkonzept GmbH.
4  * Author(s): Adam Lackorzynski <adam.lackorzynski@kernkonzept.com>
5  *            Sarah Hoffmann <sarah.hoffmann@kernkonzept.com>
6  *
7  * This file is distributed under the terms of the GNU General Public
8  * License, version 2.  Please see the COPYING-GPL-2 file for details.
9  */
10 #include "lua.h"
11 
12 #include <l4/util/util.h>
13 
14 namespace Lua { namespace {
15 
16 class Lib_sleep : public Lib
17 {
18 public:
sleep(lua_State * l)19   static int sleep(lua_State *l)
20   {
21     if (lua_gettop(l) >= 1 && !lua_isnil(l, 1)
22         && lua_isnumber(l, 1))
23       {
24         int s = lua_tointeger(l, 1);
25         printf("Ned: Sleeping %ds\n", s);
26         l4_sleep(s * 1000);
27       }
28     return 0;
29   }
30 
msleep(lua_State * l)31   static int msleep(lua_State *l)
32   {
33     if (lua_gettop(l) >= 1 && !lua_isnil(l, 1)
34         && lua_isnumber(l, 1))
35       {
36         int s = lua_tointeger(l, 1);
37         printf("Ned: Sleeping %dms\n", s);
38         l4_sleep(s);
39       }
40     return 0;
41   }
42 
43 
Lib_sleep()44   Lib_sleep() : Lib(P_env) {}
45 
init(lua_State * l)46   void init(lua_State *l)
47   {
48     lua_require_module(l, package);
49 
50     // L4.sleep()
51     lua_pushcfunction(l, sleep);
52     lua_setfield(l, -2, "sleep");
53 
54     // L4.msleep()
55     lua_pushcfunction(l, msleep);
56     lua_setfield(l, -2, "msleep");
57 
58   }
59 };
60 static Lib_sleep __libsleep;
61 
62 }}
63