1 /* 2 * Copyright (c) 2024 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 META_INTERFACE_THREADING_PRIMITIVE_API_H 17 #define META_INTERFACE_THREADING_PRIMITIVE_API_H 18 19 #include <stdint.h> 20 21 #include <core/namespace.h> 22 23 CORE_BEGIN_NAMESPACE() 24 25 /// Mutex type 26 enum class MutexType { 27 NORMAL, 28 // RECURSIVE, 29 // SHARED 30 }; 31 32 #ifdef _WIN32 33 static constexpr size_t MUTEX_HANDLE_STORAGE_SIZE = 8; 34 #else 35 static constexpr size_t MUTEX_HANDLE_STORAGE_SIZE = 40; 36 #endif 37 38 struct alignas(8) MutexHandle { // 8: align 39 union { 40 void* ptr = nullptr; 41 char storage[MUTEX_HANDLE_STORAGE_SIZE]; 42 }; 43 }; 44 45 using CreateMutexType = void (*)(MutexType, MutexHandle&); 46 using DestroyMutexType = void (*)(MutexHandle&); 47 using LockMutexType = bool (*)(MutexHandle&); 48 using UnlockMutexType = bool (*)(MutexHandle&); 49 50 using NativeThreadHandle = uint64_t; 51 using GetThreadIdType = NativeThreadHandle (*)(); 52 53 /// Synchronisation api which can be passed over dynamic library boundaries 54 struct SyncApi { 55 struct MutexApi { 56 CreateMutexType create = nullptr; 57 DestroyMutexType destroy = nullptr; 58 LockMutexType lock = nullptr; 59 UnlockMutexType unlock = nullptr; 60 } mutex; 61 62 GetThreadIdType getThreadId = nullptr; 63 }; 64 65 CORE_END_NAMESPACE() 66 67 #endif