Pyrogenesis  trunk
ScriptContext.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 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.
27 constexpr int DEFAULT_CONTEXT_SIZE = 16 * 1024 * 1024;
28 constexpr int DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER = 2 * 1024 * 1024;
29 
30 /**
31  * Abstraction around a SpiderMonkey JSContext.
32  *
33  * A single ScriptContext, with the associated context,
34  * should only be used on a single thread.
35  *
36  * (One means to share data between threads and contexts is to create
37  * a Script::StructuredClone.)
38  */
39 
41 {
42 public:
43  ScriptContext(int contextSize, int heapGrowthBytesGCTrigger);
45 
46  /**
47  * Returns a context, in which any number of ScriptInterfaces compartments can live.
48  * Each context should only ever be used on a single thread.
49  * @param parentContext Parent context from the parent thread, with which we share some thread-safe data
50  * @param contextSize Maximum size in bytes of the new context
51  * @param heapGrowthBytesGCTrigger Size in bytes of cumulated allocations after which a GC will be triggered
52  */
53  static std::shared_ptr<ScriptContext> CreateContext(
54  int contextSize = DEFAULT_CONTEXT_SIZE,
55  int heapGrowthBytesGCTrigger = DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER);
56 
57 
58  /**
59  * MaybeIncrementalGC tries to determine whether a context-wide garbage collection would free up enough memory to
60  * be worth the amount of time it would take. It does this with our own logic and NOT some predefined JSAPI logic because
61  * such functionality currently isn't available out of the box.
62  * It does incremental GC which means it will collect one slice each time it's called until the garbage collection is done.
63  * This can and should be called quite regularly. The delay parameter allows you to specify a minimum time since the last GC
64  * in seconds (the delay should be a fraction of a second in most cases though).
65  * It will only start a new incremental GC or another GC slice if this time is exceeded. The user of this function is
66  * responsible for ensuring that GC can run with a small enough delay to get done with the work.
67  */
68  void MaybeIncrementalGC(double delay);
69  void ShrinkingGC();
70 
71  /**
72  * This is used to keep track of realms which should be prepared for a GC.
73  */
74  void RegisterRealm(JS::Realm* realm);
75  void UnRegisterRealm(JS::Realm* realm);
76 
77  /**
78  * GetGeneralJSContext returns the context without starting a GC request and without
79  * entering any compartment. It should only be used in specific situations, such as
80  * creating a new compartment, or when initializing a persistent rooted.
81  * If you need the compartmented context of a ScriptInterface, you should create a
82  * ScriptRequest and use the context from that.
83  */
84  JSContext* GetGeneralJSContext() const { return m_cx; }
85 
86 private:
87 
88  JSContext* m_cx;
89 
90  void PrepareZonesForIncrementalGC() const;
91  std::list<JS::Realm*> m_Realms;
92 
96  double m_LastGCCheck;
97 };
98 
99 // Using a global object for the context is a workaround until Simulation, AI, etc,
100 // use their own threads and also their own contexts.
101 extern thread_local std::shared_ptr<ScriptContext> g_ScriptContext;
102 
103 #endif // INCLUDED_SCRIPTCONTEXT
std::list< JS::Realm * > m_Realms
Definition: ScriptContext.h:91
void PrepareZonesForIncrementalGC() const
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:80
Abstraction around a SpiderMonkey JSContext.
Definition: ScriptContext.h:40
JSContext * m_cx
Definition: ScriptContext.h:88
int m_LastGCBytes
Definition: ScriptContext.h:95
ScriptContext(int contextSize, int heapGrowthBytesGCTrigger)
Definition: ScriptContext.cpp:85
thread_local std::shared_ptr< ScriptContext > g_ScriptContext
Definition: GameSetup.cpp:108
constexpr int DEFAULT_HEAP_GROWTH_BYTES_GCTRIGGER
Definition: ScriptContext.h:28
double m_LastGCCheck
Definition: ScriptContext.h:96
void ShrinkingGC()
Definition: ScriptContext.cpp:254
int m_HeapGrowthBytesGCTrigger
Definition: ScriptContext.h:94
void MaybeIncrementalGC(double delay)
MaybeIncrementalGC tries to determine whether a context-wide garbage collection would free up enough ...
Definition: ScriptContext.cpp:152
~ScriptContext()
Definition: ScriptContext.cpp:128
constexpr int DEFAULT_CONTEXT_SIZE
Definition: ScriptContext.h:27
void RegisterRealm(JS::Realm *realm)
This is used to keep track of realms which should be prepared for a GC.
Definition: ScriptContext.cpp:136
void UnRegisterRealm(JS::Realm *realm)
Definition: ScriptContext.cpp:142
int m_ContextSize
Definition: ScriptContext.h:93
JSContext * GetGeneralJSContext() const
GetGeneralJSContext returns the context without starting a GC request and without entering any compar...
Definition: ScriptContext.h:84