1 /* 2 * Copyright (C) 2021 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 NSTACKX_UTIL_H 17 #define NSTACKX_UTIL_H 18 19 #include "nstackx_dev.h" 20 #include "sys_util.h" 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 typedef enum { 27 CPU_IDX_0 = 0, 28 CPU_IDX_1, 29 CPU_IDX_2, 30 CPU_IDX_3, 31 CPU_IDX_4, 32 CPU_IDX_5, 33 CPU_IDX_6, 34 CPU_IDX_7, 35 } CpuIdx; 36 37 #define FIRST_CPU_NUM_LEVEL 8 38 #define SECOND_CPU_NUM_LEVEL 4 39 #define THIRD_CPU_NUM_LEVEL 2 40 #define MAX_THREAD_NAME_LEN 100 41 #define THREAD_MAXIMUM_PRIORITY (-20) 42 43 #define BLOCK_LEN 512 44 #define MAX_NAME_LEN 99 45 46 NSTACKX_EXPORT int32_t GetTargetFileSize(const char *dir, uint64_t *size); 47 NSTACKX_EXPORT int32_t CheckPathSeprator(const char *path); 48 NSTACKX_EXPORT int32_t CheckFilenameSeprator(const char *fileName); 49 NSTACKX_EXPORT uint32_t GetFileNameLen(const char *dir); 50 NSTACKX_EXPORT int32_t GetFileName(const char *dir, char *name, uint32_t nameLen); 51 NSTACKX_EXPORT uint8_t IsAccessiblePath(const char *fileName, int32_t mode, uint32_t fileType); 52 NSTACKX_EXPORT int32_t TestAndCreateDirectory(const char *path); 53 NSTACKX_EXPORT uint8_t IsFileNameLegal(const char *fileName); 54 NSTACKX_EXPORT uint8_t IsExistingFile(const char *fileName); 55 56 NSTACKX_EXPORT void StartThreadBindCore(int32_t cpu); 57 NSTACKX_EXPORT void BindThreadToTargetMask(pid_t tid, uint32_t cpuMask); 58 NSTACKX_EXPORT int32_t GetCpuNum(void); 59 NSTACKX_EXPORT void SetThreadName(const char *name); 60 NSTACKX_EXPORT void ClockGetTime(clockid_t id, struct timespec *tp); 61 NSTACKX_EXPORT void SetMaximumPriorityForThread(void); 62 63 /* pthread series */ 64 NSTACKX_EXPORT void SemGetValue(sem_t *sem, int *sval); 65 NSTACKX_EXPORT void SemPost(sem_t *sem); 66 NSTACKX_EXPORT void SemWait(sem_t *sem); 67 NSTACKX_EXPORT void SemDestroy(sem_t *sem); 68 NSTACKX_EXPORT int32_t SemInit(sem_t *sem, int pshared, unsigned int value); 69 NSTACKX_EXPORT int32_t PthreadMutexInit(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); 70 NSTACKX_EXPORT void PthreadMutexDestroy(pthread_mutex_t *mutex); 71 NSTACKX_EXPORT int32_t PthreadMutexLock(pthread_mutex_t *mutex); 72 NSTACKX_EXPORT int32_t PthreadMutexUnlock(pthread_mutex_t *mutex); 73 NSTACKX_EXPORT int32_t PthreadCreate(pthread_t *tid, const pthread_attr_t *attr, void *(*entry)(void *), void *arg); 74 NSTACKX_EXPORT void PthreadJoin(pthread_t thread, void **retval); 75 76 NSTACKX_EXPORT int32_t IpAddrAnonymousFormat(char *buf, size_t len, const struct sockaddr *addr, size_t addrLen); 77 78 #ifdef __cplusplus 79 } 80 #endif 81 82 #endif // NSTACKX_UTIL_H 83