aboutsummaryrefslogtreecommitdiff
path: root/src/universe.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/universe.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/universe.cpp b/src/universe.cpp
new file mode 100644
index 0000000..4c53987
--- /dev/null
+++ b/src/universe.cpp
@@ -0,0 +1,106 @@
1/*
2 * UNIVERSE.C Copyright (c) 2017, Benoit Germain
3 */
4
5/*
6===============================================================================
7
8Copyright (C) 2017 Benoit Germain <bnt.germain@gmail.com>
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27
28===============================================================================
29*/
30
31#include <string.h>
32#include <assert.h>
33
34#include "universe.h"
35#include "compat.h"
36#include "macros_and_utils.h"
37#include "uniquekey.h"
38
39// xxh64 of string "UNIVERSE_FULL_REGKEY" generated at http://www.nitrxgen.net/hashgen/
40static constexpr UniqueKey UNIVERSE_FULL_REGKEY{ 0x99CA130C09EDC074ull };
41// xxh64 of string "UNIVERSE_LIGHT_REGKEY" generated at http://www.nitrxgen.net/hashgen/
42static constexpr UniqueKey UNIVERSE_LIGHT_REGKEY{ 0x3663C07C742CEB81ull };
43
44// ################################################################################################
45
46Universe::Universe()
47{
48 //---
49 // Linux needs SCHED_RR to change thread priorities, and that is only
50 // allowed for sudo'ers. SCHED_OTHER (default) has no priorities.
51 // SCHED_OTHER threads are always lower priority than SCHED_RR.
52 //
53 // ^-- those apply to 2.6 kernel. IF **wishful thinking** these
54 // constraints will change in the future, non-sudo priorities can
55 // be enabled also for Linux.
56 //
57#ifdef PLATFORM_LINUX
58 // If lower priorities (-2..-1) are wanted, we need to lift the main
59 // thread to SCHED_RR and 50 (medium) level. Otherwise, we're always below
60 // the launched threads (even -2).
61 //
62#ifdef LINUX_SCHED_RR
63 if (m_sudo)
64 {
65 struct sched_param sp;
66 sp.sched_priority = _PRIO_0;
67 PT_CALL(pthread_setschedparam(pthread_self(), SCHED_RR, &sp));
68 }
69#endif // LINUX_SCHED_RR
70#endif // PLATFORM_LINUX
71}
72
73// ################################################################################################
74
75// only called from the master state
76Universe* universe_create(lua_State* L)
77{
78 ASSERT_L(universe_get(L) == nullptr);
79 Universe* const U = static_cast<Universe*>(lua_newuserdatauv(L, sizeof(Universe), 0)); // universe
80 U->Universe::Universe();
81 STACK_CHECK_START_REL(L, 1);
82 UNIVERSE_FULL_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); });
83 UNIVERSE_LIGHT_REGKEY.setValue(L, [U](lua_State* L) { lua_pushlightuserdata(L, U); });
84 STACK_CHECK(L, 1);
85 return U;
86}
87
88// ################################################################################################
89
90void universe_store(lua_State* L, Universe* U)
91{
92 ASSERT_L(!U || universe_get(L) == nullptr);
93 STACK_CHECK_START_REL(L, 0);
94 UNIVERSE_LIGHT_REGKEY.setValue(L, [U](lua_State* L) { U ? lua_pushlightuserdata(L, U) : lua_pushnil(L); });
95 STACK_CHECK(L, 0);
96}
97
98// ################################################################################################
99
100Universe* universe_get(lua_State* L)
101{
102 STACK_CHECK_START_REL(L, 0);
103 Universe* const universe{ UNIVERSE_LIGHT_REGKEY.readLightUserDataValue<Universe>(L) };
104 STACK_CHECK(L, 0);
105 return universe;
106}