Debian Codegraph @

modules/jolt_physics/spaces/jolt_job_system.h

raw symbols SCIP
1 /**************************************************************************/
2 /* jolt_job_system.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/os/spin_lock.h"
34 #include "core/templates/hash_map.h"
35
36 #include "Jolt/Jolt.h"
37
38 #include "Jolt/Core/FixedSizeFreeList.h"
39 #include "Jolt/Core/JobSystemWithBarrier.h"
40
41 #include <atomic>
42
43 class JoltJobSystem final : public JPH::JobSystemWithBarrier {
44 class Job : public JPH::JobSystem::Job {
45 inline static std::atomic<Job *> completed_head = nullptr;
46
47 #ifdef DEBUG_ENABLED
48 const char *name = nullptr;
49 #endif
50
51 int64_t task_id = -1;
52
53 std::atomic<Job *> completed_next = nullptr;
54
55 static void _execute(void *p_user_data);
56
57 public:
58 Job(const char *p_name, JPH::ColorArg p_color, JPH::JobSystem *p_job_system, const JPH::JobSystem::JobFunction &p_job_function, JPH::uint32 p_dependency_count);
59 Job(const Job &p_other) = delete;
60 Job(Job &&p_other) = delete;
61 ~Job();
62
63 static void push_completed(Job *p_job);
64 static Job *pop_completed();
65
66 void queue();
67
68 Job &operator=(const Job &p_other) = delete;
69 Job &operator=(Job &&p_other) = delete;
70 };
71
72 #ifdef DEBUG_ENABLED
73 // We use `const void*` here to avoid the cost of hashing the actual string, since the job names
74 // are always literals and as such will point to the same address every time.
75 inline static HashMap<const void *, uint64_t> timings_by_job;
76
77 // TODO: Check whether the usage of SpinLock is justified or if this should be a mutex instead.
78 inline static SpinLock timings_lock;
79 #endif
80
81 JPH::FixedSizeFreeList<Job> jobs;
82
83 int thread_count = 0;
84
85 virtual int GetMaxConcurrency() const override;
86
87 virtual JPH::JobHandle CreateJob(const char *p_name, JPH::ColorArg p_color, const JPH::JobSystem::JobFunction &p_job_function, JPH::uint32 p_dependency_count = 0) override;
88 virtual void QueueJob(JPH::JobSystem::Job *p_job) override;
89 virtual void QueueJobs(JPH::JobSystem::Job **p_jobs, JPH::uint p_job_count) override;
90 virtual void FreeJob(JPH::JobSystem::Job *p_job) override;
91
92 void _reclaim_jobs();
93
94 public:
95 JoltJobSystem();
96
97 void pre_step();
98 void post_step();
99
100 #ifdef DEBUG_ENABLED
101 void flush_timings();
102 #endif
103 };