Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ScriptContext.h
Go to the documentation of this file.
1/* Copyright (C) 2024 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_SCRIPTCONTEXT
19#define INCLUDED_SCRIPTCONTEXT
20
21#include "ScriptTypes.h"
22#include "ScriptExtraHeaders.h"
23
24#include <list>
25
26// Those are minimal defaults. The runtime for the main game is larger and GCs upon a larger growth.
27constexpr int DEFAULT_CONTEXT_SIZE = 16 * 1024 * 1024;
28constexpr int DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER = 2 * 1024 * 1024;
29
30namespace Script
31{
32class JobQueue;
33}
34
35/**
36 * Abstraction around a SpiderMonkey JSContext.
37 *
38 * A single ScriptContext, with the associated context,
39 * should only be used on a single thread.
40 *
41 * (One means to share data between threads and contexts is to create
42 * a Script::StructuredClone.)
43 */
44
46{
47public:
48 ScriptContext(int contextSize, int heapGrowthBytesGCTrigger);
50
51 /**
52 * Returns a context, in which any number of ScriptInterfaces compartments can live.
53 * Each context should only ever be used on a single thread.
54 * @param parentContext Parent context from the parent thread, with which we share some thread-safe data
55 * @param contextSize Maximum size in bytes of the new context
56 * @param heapGrowthBytesGCTrigger Size in bytes of cumulated allocations after which a GC will be triggered
57 */
58 static std::shared_ptr<ScriptContext> CreateContext(
59 int contextSize = DEFAULT_CONTEXT_SIZE,
60 int heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER);
61
62
63 /**
64 * MaybeIncrementalGC tries to determine whether a context-wide garbage collection would free up enough memory to
65 * be worth the amount of time it would take. It does this with our own logic and NOT some predefined JSAPI logic because
66 * such functionality currently isn't available out of the box.
67 * It does incremental GC which means it will collect one slice each time it's called until the garbage collection is done.
68 * This can and should be called quite regularly. The delay parameter allows you to specify a minimum time since the last GC
69 * in seconds (the delay should be a fraction of a second in most cases though).
70 * It will only start a new incremental GC or another GC slice if this time is exceeded. The user of this function is
71 * responsible for ensuring that GC can run with a small enough delay to get done with the work.
72 */
73 void MaybeIncrementalGC(double delay);
74 void ShrinkingGC();
75
76 /**
77 * This is used to keep track of realms which should be prepared for a GC.
78 */
79 void RegisterRealm(JS::Realm* realm);
80 void UnRegisterRealm(JS::Realm* realm);
81
82 /**
83 * Runs the promise continuation.
84 * On contexts where promises can be used this function has to be
85 * called.
86 * This function has to be called frequently.
87 */
88 void RunJobs();
89
90 /**
91 * GetGeneralJSContext returns the context without starting a GC request and without
92 * entering any compartment. It should only be used in specific situations, such as
93 * creating a new compartment, or when initializing a persistent rooted.
94 * If you need the compartmented context of a ScriptInterface, you should create a
95 * ScriptRequest and use the context from that.
96 */
97 JSContext* GetGeneralJSContext() const { return m_cx; }
98
99private:
100
101 JSContext* m_cx;
102 const std::unique_ptr<Script::JobQueue> m_JobQueue;
103
104 void PrepareZonesForIncrementalGC() const;
105 std::list<JS::Realm*> m_Realms;
106
110 double m_LastGCCheck{0.0};
111};
112
113// Using a global object for the context is a workaround until Simulation, AI, etc,
114// use their own threads and also their own contexts.
115extern thread_local std::shared_ptr<ScriptContext> g_ScriptContext;
116
117#endif // INCLUDED_SCRIPTCONTEXT
constexpr int DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER
Definition: ScriptContext.h:28
constexpr int DEFAULT_CONTEXT_SIZE
Definition: ScriptContext.h:27
thread_local std::shared_ptr< ScriptContext > g_ScriptContext
Definition: GameSetup.cpp:108
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptContext.h:46
double m_LastGCCheck
Definition: ScriptContext.h:110
int m_ContextSize
Definition: ScriptContext.h:107
void UnRegisterRealm(JS::Realm *realm)
Definition: ScriptContext.cpp:152
const std::unique_ptr< Script::JobQueue > m_JobQueue
Definition: ScriptContext.h:102
void RunJobs()
Runs the promise continuation.
Definition: ScriptContext.cpp:274
ScriptContext(int contextSize, int heapGrowthBytesGCTrigger)
Definition: ScriptContext.cpp:86
std::list< JS::Realm * > m_Realms
Definition: ScriptContext.h:105
~ScriptContext()
Definition: ScriptContext.cpp:138
JSContext * m_cx
Definition: ScriptContext.h:101
void PrepareZonesForIncrementalGC() const
Definition: ScriptContext.cpp:279
void MaybeIncrementalGC(double delay)
MaybeIncrementalGC tries to determine whether a context-wide garbage collection would free up enough ...
Definition: ScriptContext.cpp:162
int m_HeapGrowthBytesGCTrigger
Definition: ScriptContext.h:108
void RegisterRealm(JS::Realm *realm)
This is used to keep track of realms which should be prepared for a GC.
Definition: ScriptContext.cpp:146
int m_LastGCBytes
Definition: ScriptContext.h:109
void ShrinkingGC()
Definition: ScriptContext.cpp:264
static std::shared_ptr< ScriptContext > CreateContext(int contextSize=DEFAULT_CONTEXT_SIZE, int heapGrowthBytesGCTrigger=DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER)
Returns a context, in which any number of ScriptInterfaces compartments can live.
Definition: ScriptContext.cpp:81
JSContext * GetGeneralJSContext() const
GetGeneralJSContext returns the context without starting a GC request and without entering any compar...
Definition: ScriptContext.h:97
Wraps SM APIs for manipulating JS objects.
Definition: JSON.h:35