1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GIN_THREAD_ISOLATION_H_
6 #define GIN_THREAD_ISOLATION_H_
8 #include "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_buildflags.h"
10 #if BUILDFLAG(ENABLE_THREAD_ISOLATION)
12 #include "base/allocator/partition_allocator/src/partition_alloc/thread_isolation/alignment.h"
13 #include "base/no_destructor.h"
14 #include "gin/gin_export.h"
15 #include "gin/v8_platform_thread_isolated_allocator.h"
19 // All data used for thread isolation needs to live in this struct since we need
20 // to write-protect it with the same thread isolation mechanism.
21 // The implementation is platform specific, e.g. using pkeys on x64.
22 struct GIN_EXPORT PA_THREAD_ISOLATED_ALIGN ThreadIsolationData {
23 // If we're using pkeys for isolation, we need to allocate the key before
24 // spawning any threads. Otherwise, the other threads will not have read
25 // permissions to memory tagged with this key.
26 void InitializeBeforeThreadCreation();
28 // Returns if InitializeBeforeThreadCreation has been called and the
29 // initialization was successful, i.e. the platform supports the isolation
31 bool Initialized() const;
33 base::NoDestructor<ThreadIsolatedAllocator> allocator;
36 static_assert(PA_THREAD_ISOLATED_FILL_PAGE_SZ(sizeof(ThreadIsolationData)) ==
39 GIN_EXPORT ThreadIsolationData& GetThreadIsolationData();
43 #endif // BUILDFLAG(ENABLE_THREAD_ISOLATION)
45 #endif // GIN_THREAD_ISOLATION_H_