Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Fork.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "HAL/PlatformAffinity.h"
7
8#ifndef DEFAULT_SERVER_FAKE_FORKS
9 #define DEFAULT_SERVER_FAKE_FORKS 0
10#endif
11
12class FRunnable;
13class FRunnableThread;
14
16{
17 Parent,
18 Child,
19};
20
21/**
22 * Helper functions for processes that fork in order to share memory pages.
23 *
24 * About multithreading:
25 * When a process gets forked, any existing threads will not exist on the new forked process.
26 * To solve this we use forkable threads that are notified when the fork occurs and will automatically convert themselves into real runnable threads.
27 * On the master process, these forkable threads will be fake threads that are executed on the main thread and will block the critical path.
28 *
29 * Currently the game code is responsible for calling Fork on itself than calling FForkProcessHelper::OnForkingOccured to transform the forkable threads.
30 * Ideally the fork point is done right after the game has loaded all the assets it wants to share so it can maximize the shared memory pool.
31 * From the fork point any memory page that gets written into by a forked process will be transferred into a unique page for this process.
32 *
33 */
35{
36public:
37
38 /**
39 * Returns true if the server process was launched with the intention to fork.
40 * This could be a process on a fork-supported platform that will launch real child processes. (-WaitAndFork is set)
41 * Or it could be a process that will simulate forking by tranforming itself into a child process via fake forking (-FakeForking is set)
42 */
43 static bool IsForkRequested();
44
45 /**
46 * Are we a forked process that supports multithreading
47 * This only becomes true after its safe to be multithread.
48 * Since a process can be forked mid-tick, there is a period of time where IsForkedChildProcess is true but IsForkedMultithreadInstance will be false
49 */
51
52 /**
53 * Is this a process that was forked
54 */
55 static bool IsForkedChildProcess();
56
57 /**
58 * Sets the forked child process flag and index given to this child process
59 */
60 static void SetIsForkedChildProcess(uint16 ChildIndex=1);
61
62 /**
63 * Returns the unique index of this forked child process. Index 0 is for the master server
64 */
66
67 /**
68 * Event triggered when a fork occurred on the child process and its safe to create real threads
69 */
70 static void OnForkingOccured();
71
72 /**
73 * Tells if we allow multithreading on forked processes.
74 * Default is set to false but can be configured to always be true via DEFAULT_MULTITHREAD_FORKED_PROCESSES
75 * Enabled via -PostForkThreading
76 * Disabled via -DisablePostForkThreading
77 */
79
80 /**
81 * Creates a thread according to the environment it's in:
82 * In environments with SupportsMultithreading: create a real thread that will tick the runnable object itself
83 * In environments without multithreading: create a fake thread that is ticked by the main thread.
84 * In environments without multithreading but that allows multithreading post-fork:
85 * If called on the original master process: will create a forkable thread that is ticked in the main thread pre-fork but becomes a real thread post-fork
86 * If called on a forked child process: will create a real thread immediately
87 */
89 class FRunnable* InRunnable,
90 const TCHAR* InThreadName,
91 uint32 InStackSize = 0,
92 EThreadPriority InThreadPri = TPri_Normal,
93 uint64 InThreadAffinityMask = FPlatformAffinity::GetNoAffinityMask(),
95 );
96
97 /**
98 * Performs low-level cross-platform actions that should happen immediately BEFORE forking in a well-specified order.
99 * Runs after any higher level code like calling into game-level constructs or anything that may allocate memory.
100 * E.g. notifies GMalloc to optimize for memory sharing across parent/child process
101 * Note: This will be called multiple times on the parent before each fork.
102 */
103 static void LowLevelPreFork();
104
105 /**
106 * Performs low-level cross-platform actions that should happen immediately AFTER forking in the PARENT process in a well-specified order.
107 * Runs before any higher level code like calling into game-level constructs.
108 * E.g. notifies GMalloc to optimize for memory sharing across parent/child process
109 */
111
112 /**
113 * Performs low-level cross-platform actions that should happen immediately AFTER forking in the CHILD process in a well-specified order.
114 * Runs before any higher level code like calling into game-level constructs.
115 * E.g. notifies GMalloc to optimize for memory sharing across parent/child process
116 */
117 static void LowLevelPostForkChild(uint16 ChildIndex=1);
118};
EThreadCreateFlags
Definition Enums.h:11288
EForkProcessRole
Definition Enums.h:9492
EThreadPriority
Definition Enums.h:5969
FGenericPlatformAffinity FPlatformAffinity
static void OnForkingOccured()
static void LowLevelPostForkParent()
static void LowLevelPostForkChild(uint16 ChildIndex=1)
static bool IsForkedChildProcess()
static bool IsForkRequested()
static bool SupportsMultithreadingPostFork()
static uint16 GetForkedChildProcessIndex()
static bool IsForkedMultithreadInstance()
static void LowLevelPreFork()
static void SetIsForkedChildProcess(uint16 ChildIndex=1)
static FRunnableThread * CreateForkableThread(class FRunnable *InRunnable, const TCHAR *InThreadName, uint32 InStackSize=0, EThreadPriority InThreadPri=TPri_Normal, uint64 InThreadAffinityMask=FPlatformAffinity::GetNoAffinityMask(), EThreadCreateFlags InCreateFlags=EThreadCreateFlags::None)
static const uint64 GetNoAffinityMask()