Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
s_internal.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5/******************************************************************************/
6/******************************************************************************/
7/* WARNING!!!! do NOT add this header in any include file or any code file */
8/* not being a modapi file!!!!!!!! */
9/******************************************************************************/
10/******************************************************************************/
11
12#pragma once
13
14#include <thread>
15#include "common/c_internal.h"
16#include "cpp_api/s_base.h"
18#include "common/c_types.h"
19
20#ifdef SCRIPTAPI_LOCK_DEBUG
21#include <cassert>
22
23class LockChecker {
24public:
25 LockChecker(int *recursion_counter, std::thread::id *owning_thread)
26 {
27 m_lock_recursion_counter = recursion_counter;
28 m_owning_thread = owning_thread;
29 m_original_level = *recursion_counter;
30
31 if (*m_lock_recursion_counter > 0) {
32 assert(*m_owning_thread == std::this_thread::get_id());
33 } else {
34 *m_owning_thread = std::this_thread::get_id();
35 }
36
37 (*m_lock_recursion_counter)++;
38 }
39
40 ~LockChecker()
41 {
42 assert(*m_owning_thread == std::this_thread::get_id());
43 assert(*m_lock_recursion_counter > 0);
44
45 (*m_lock_recursion_counter)--;
46
47 assert(*m_lock_recursion_counter == m_original_level);
48 }
49
50private:
51 int *m_lock_recursion_counter;
52 int m_original_level;
53 std::thread::id *m_owning_thread;
54};
55
56#define SCRIPTAPI_LOCK_CHECK \
57 LockChecker scriptlock_checker( \
58 &this->m_lock_recursion_count, \
59 &this->m_owning_thread)
60
61#else
62 #define SCRIPTAPI_LOCK_CHECK while(0)
63#endif
64
65#define SCRIPTAPI_PRECHECKHEADER \
66 RecursiveMutexAutoLock scriptlock(this->m_luastackmutex); \
67 SCRIPTAPI_LOCK_CHECK; \
68 realityCheck(); \
69 lua_State *L = getStack(); \
70 assert(lua_checkstack(L, 20)); \
71 StackUnroller stack_unroller(L);