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
19#ifdef SCRIPTAPI_LOCK_DEBUG
20#include <cassert>
21
22class LockChecker {
23public:
24 LockChecker(int *recursion_counter, std::thread::id *owning_thread)
25 {
26 m_lock_recursion_counter = recursion_counter;
27 m_owning_thread = owning_thread;
28 m_original_level = *recursion_counter;
29
30 if (*m_lock_recursion_counter > 0) {
31 assert(*m_owning_thread == std::this_thread::get_id());
32 } else {
33 *m_owning_thread = std::this_thread::get_id();
34 }
35
36 (*m_lock_recursion_counter)++;
37 }
38
39 ~LockChecker()
40 {
41 assert(*m_owning_thread == std::this_thread::get_id());
42 assert(*m_lock_recursion_counter > 0);
43
44 (*m_lock_recursion_counter)--;
45
46 assert(*m_lock_recursion_counter == m_original_level);
47 }
48
49private:
50 int *m_lock_recursion_counter;
51 int m_original_level;
52 std::thread::id *m_owning_thread;
53};
54
55#define SCRIPTAPI_LOCK_CHECK \
56 LockChecker scriptlock_checker( \
57 &this->m_lock_recursion_count, \
58 &this->m_owning_thread)
59
60#else
61 #define SCRIPTAPI_LOCK_CHECK while(0)
62#endif
63
64#define SCRIPTAPI_PRECHECKHEADER \
65 RecursiveMutexAutoLock scriptlock(this->m_luastackmutex); \
66 SCRIPTAPI_LOCK_CHECK; \
67 realityCheck(); \
68 lua_State *L = getStack(); \
69 assert(lua_checkstack(L, 20)); \
70 StackUnroller stack_unroller(L);