1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef COMMON_COMPONENTS_MUTATOR_THREAD_LOCAL_H 17 #define COMMON_COMPONENTS_MUTATOR_THREAD_LOCAL_H 18 19 #include <cstdint> 20 21 namespace common { 22 class AllocationBuffer; 23 class Mutator; 24 25 enum class ThreadType { ARK_PROCESSOR = 0, GC_THREAD, FP_THREAD, HOT_UPDATE_THREAD }; 26 27 // Backend and ArkThread will use external tls var through offset calculation, so external tls 28 // must in the first place, followed by the internal tls. 29 struct ThreadLocalData { 30 // External thread local var. 31 AllocationBuffer* buffer; 32 Mutator* mutator {nullptr}; 33 uint8_t* thread; 34 uint8_t* schedule; 35 uint8_t* preemptFlag; 36 uint8_t* protectAddr; 37 uint64_t tid; 38 void* foreignThread; 39 // Internal thread local var. 40 ThreadType threadType; 41 bool isArkProcessor; 42 }; 43 44 class ThreadLocal { // merge this to ThreadLocalData. 45 public: 46 static ThreadLocalData* GetThreadLocalData(); 47 SetMutator(Mutator * newMutator)48 static void SetMutator(Mutator* newMutator) { GetThreadLocalData()->mutator = newMutator; } 49 GetMutator()50 static Mutator* GetMutator() { return GetThreadLocalData()->mutator; } 51 GetAllocBuffer()52 static AllocationBuffer* GetAllocBuffer() { return GetThreadLocalData()->buffer; } 53 static void ClearAllocBufferRegion(); 54 SetAllocBuffer(AllocationBuffer * buffer)55 static void SetAllocBuffer(AllocationBuffer* buffer) { GetThreadLocalData()->buffer = buffer; } 56 GetPreemptFlag()57 static uint8_t* GetPreemptFlag() { return GetThreadLocalData()->preemptFlag; } 58 SetProtectAddr(uint8_t * addr)59 static void SetProtectAddr(uint8_t* addr) { GetThreadLocalData()->protectAddr = addr; } 60 SetThreadType(ThreadType type)61 static void SetThreadType(ThreadType type) { GetThreadLocalData()->threadType = type; } 62 GetThreadType()63 static ThreadType GetThreadType() { return GetThreadLocalData()->threadType; } 64 SetProcessorFlag(bool flag)65 static void SetProcessorFlag(bool flag) { GetThreadLocalData()->isArkProcessor = flag; } 66 IsArkProcessor()67 static bool IsArkProcessor() { return GetThreadLocalData()->isArkProcessor; } 68 SetForeignThread(void * thread)69 static void SetForeignThread(void* thread) 70 { 71 GetThreadLocalData()->foreignThread = thread; 72 } 73 GetForeignThread()74 static void* GetForeignThread() 75 { 76 return GetThreadLocalData()->foreignThread; 77 } 78 SetArkThread(void * thread)79 static void SetArkThread(void* thread) 80 { 81 GetThreadLocalData()->thread = reinterpret_cast<uint8_t*>(thread); 82 } 83 SetSchedule(void * schedule)84 static void SetSchedule(void* schedule) 85 { 86 GetThreadLocalData()->schedule = reinterpret_cast<uint8_t*>(schedule); 87 } 88 }; 89 } // namespace common 90 91 #endif // COMMON_COMPONENTS_MUTATOR_THREAD_LOCAL_H 92