1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License. 5060ff233Sopenharmony_ci * You may obtain a copy of the License at 6060ff233Sopenharmony_ci * 7060ff233Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8060ff233Sopenharmony_ci * 9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and 13060ff233Sopenharmony_ci * limitations under the License. 14060ff233Sopenharmony_ci */ 15060ff233Sopenharmony_ci#ifndef _GNU_SOURCE 16060ff233Sopenharmony_ci#define _GNU_SOURCE 17060ff233Sopenharmony_ci#endif 18060ff233Sopenharmony_ci 19060ff233Sopenharmony_ci#include "softbus_adapter_thread.h" 20060ff233Sopenharmony_ci 21060ff233Sopenharmony_ci#include <pthread.h> 22060ff233Sopenharmony_ci#include <sched.h> 23060ff233Sopenharmony_ci#include <securec.h> 24060ff233Sopenharmony_ci#include <string.h> 25060ff233Sopenharmony_ci 26060ff233Sopenharmony_ci#include "softbus_adapter_mem.h" 27060ff233Sopenharmony_ci#include "softbus_errcode.h" 28060ff233Sopenharmony_ci 29060ff233Sopenharmony_cistatic pthread_mutex_t g_adapterStaticLock = PTHREAD_MUTEX_INITIALIZER; 30060ff233Sopenharmony_ci/* mutex */ 31060ff233Sopenharmony_ciint32_t SoftBusMutexAttrInit(SoftBusMutexAttr *mutexAttr) 32060ff233Sopenharmony_ci{ 33060ff233Sopenharmony_ci if (mutexAttr == NULL) { 34060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "mutexAttr is null"); 35060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 36060ff233Sopenharmony_ci } 37060ff233Sopenharmony_ci 38060ff233Sopenharmony_ci mutexAttr->type = SOFTBUS_MUTEX_NORMAL; 39060ff233Sopenharmony_ci return SOFTBUS_OK; 40060ff233Sopenharmony_ci} 41060ff233Sopenharmony_ci 42060ff233Sopenharmony_ciint32_t SoftBusMutexInit(SoftBusMutex *mutex, SoftBusMutexAttr *mutexAttr) 43060ff233Sopenharmony_ci{ 44060ff233Sopenharmony_ci if (mutex == NULL) { 45060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "mutex is null"); 46060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 47060ff233Sopenharmony_ci } 48060ff233Sopenharmony_ci if (pthread_mutex_lock(&g_adapterStaticLock) != 0) { 49060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "mutex init : g_adapterStaticLock lock failed"); 50060ff233Sopenharmony_ci return SOFTBUS_ERR; 51060ff233Sopenharmony_ci } 52060ff233Sopenharmony_ci if ((void *)*mutex != NULL) { 53060ff233Sopenharmony_ci (void)pthread_mutex_unlock(&g_adapterStaticLock); 54060ff233Sopenharmony_ci return SOFTBUS_OK; 55060ff233Sopenharmony_ci } 56060ff233Sopenharmony_ci pthread_mutex_t *tempMutex; 57060ff233Sopenharmony_ci tempMutex = (pthread_mutex_t *)SoftBusCalloc(sizeof(pthread_mutex_t)); 58060ff233Sopenharmony_ci if (tempMutex == NULL) { 59060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "tempMutex is null"); 60060ff233Sopenharmony_ci (void)pthread_mutex_unlock(&g_adapterStaticLock); 61060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 62060ff233Sopenharmony_ci } 63060ff233Sopenharmony_ci 64060ff233Sopenharmony_ci pthread_mutexattr_t attr; 65060ff233Sopenharmony_ci pthread_mutexattr_init(&attr); 66060ff233Sopenharmony_ci if (mutexAttr == NULL) { 67060ff233Sopenharmony_ci#ifndef __LITEOS_M__ 68060ff233Sopenharmony_ci pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); 69060ff233Sopenharmony_ci#else 70060ff233Sopenharmony_ci pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 71060ff233Sopenharmony_ci#endif 72060ff233Sopenharmony_ci } else if (mutexAttr->type == SOFTBUS_MUTEX_NORMAL) { 73060ff233Sopenharmony_ci pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); 74060ff233Sopenharmony_ci } else if (mutexAttr->type == SOFTBUS_MUTEX_RECURSIVE) { 75060ff233Sopenharmony_ci pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 76060ff233Sopenharmony_ci } 77060ff233Sopenharmony_ci 78060ff233Sopenharmony_ci int32_t ret = pthread_mutex_init(tempMutex, &attr); 79060ff233Sopenharmony_ci if (ret != 0) { 80060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusMutexInit failed, ret=%{public}d", ret); 81060ff233Sopenharmony_ci SoftBusFree(tempMutex); 82060ff233Sopenharmony_ci tempMutex = NULL; 83060ff233Sopenharmony_ci (void)pthread_mutex_unlock(&g_adapterStaticLock); 84060ff233Sopenharmony_ci return SOFTBUS_ERR; 85060ff233Sopenharmony_ci } 86060ff233Sopenharmony_ci 87060ff233Sopenharmony_ci *mutex = (SoftBusMutex)tempMutex; 88060ff233Sopenharmony_ci (void)pthread_mutex_unlock(&g_adapterStaticLock); 89060ff233Sopenharmony_ci return SOFTBUS_OK; 90060ff233Sopenharmony_ci} 91060ff233Sopenharmony_ci 92060ff233Sopenharmony_ciint32_t SoftBusMutexLockInner(SoftBusMutex *mutex) 93060ff233Sopenharmony_ci{ 94060ff233Sopenharmony_ci return pthread_mutex_lock((pthread_mutex_t *)*mutex); 95060ff233Sopenharmony_ci} 96060ff233Sopenharmony_ci 97060ff233Sopenharmony_ciint32_t SoftBusMutexUnlockInner(SoftBusMutex *mutex) 98060ff233Sopenharmony_ci{ 99060ff233Sopenharmony_ci return pthread_mutex_unlock((pthread_mutex_t *)*mutex); 100060ff233Sopenharmony_ci} 101060ff233Sopenharmony_ci 102060ff233Sopenharmony_ciint32_t SoftBusMutexDestroy(SoftBusMutex *mutex) 103060ff233Sopenharmony_ci{ 104060ff233Sopenharmony_ci if ((mutex == NULL) || ((void *)(*mutex) == NULL)) { 105060ff233Sopenharmony_ci COMM_LOGD(COMM_ADAPTER, "mutex is null"); 106060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 107060ff233Sopenharmony_ci } 108060ff233Sopenharmony_ci 109060ff233Sopenharmony_ci int32_t ret = pthread_mutex_destroy((pthread_mutex_t *)*mutex); 110060ff233Sopenharmony_ci if (ret != 0) { 111060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusMutexDestroy failed, ret=%{public}d", ret); 112060ff233Sopenharmony_ci SoftBusFree((void *)*mutex); 113060ff233Sopenharmony_ci *mutex = (SoftBusMutex)NULL; 114060ff233Sopenharmony_ci return SOFTBUS_ERR; 115060ff233Sopenharmony_ci } 116060ff233Sopenharmony_ci 117060ff233Sopenharmony_ci SoftBusFree((void *)*mutex); 118060ff233Sopenharmony_ci *mutex = (SoftBusMutex)NULL; 119060ff233Sopenharmony_ci return SOFTBUS_OK; 120060ff233Sopenharmony_ci} 121060ff233Sopenharmony_ci 122060ff233Sopenharmony_ci/* pthread */ 123060ff233Sopenharmony_ciint32_t SoftBusThreadAttrInit(SoftBusThreadAttr *threadAttr) 124060ff233Sopenharmony_ci{ 125060ff233Sopenharmony_ci if (threadAttr == NULL) { 126060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "threadAttr is null"); 127060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 128060ff233Sopenharmony_ci } 129060ff233Sopenharmony_ci 130060ff233Sopenharmony_ci#ifndef __LITEOS_M__ 131060ff233Sopenharmony_ci threadAttr->policy = SOFTBUS_SCHED_OTHER; 132060ff233Sopenharmony_ci#else 133060ff233Sopenharmony_ci threadAttr->policy = SOFTBUS_SCHED_RR; 134060ff233Sopenharmony_ci#endif 135060ff233Sopenharmony_ci threadAttr->detachState = SOFTBUS_THREAD_JOINABLE; 136060ff233Sopenharmony_ci threadAttr->stackSize = 0; 137060ff233Sopenharmony_ci threadAttr->prior = SOFTBUS_PRIORITY_DEFAULT; 138060ff233Sopenharmony_ci threadAttr->taskName = NULL; 139060ff233Sopenharmony_ci 140060ff233Sopenharmony_ci return SOFTBUS_OK; 141060ff233Sopenharmony_ci} 142060ff233Sopenharmony_ci 143060ff233Sopenharmony_cistatic int32_t SoftbusSetThreadPolicy(SoftBusThreadAttr *threadAttr, pthread_attr_t *attr) 144060ff233Sopenharmony_ci{ 145060ff233Sopenharmony_ci if (threadAttr->policy == SOFTBUS_SCHED_OTHER) { 146060ff233Sopenharmony_ci pthread_attr_setschedpolicy(attr, SCHED_OTHER); 147060ff233Sopenharmony_ci } else if (threadAttr->policy == SOFTBUS_SCHED_RR) { 148060ff233Sopenharmony_ci pthread_attr_setschedpolicy(attr, SCHED_RR); 149060ff233Sopenharmony_ci } else { 150060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "set policy error"); 151060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 152060ff233Sopenharmony_ci } 153060ff233Sopenharmony_ci 154060ff233Sopenharmony_ci return SOFTBUS_OK; 155060ff233Sopenharmony_ci} 156060ff233Sopenharmony_ci 157060ff233Sopenharmony_cistatic int32_t SoftbusSetThreadDetachState(SoftBusThreadAttr *threadAttr, pthread_attr_t *attr) 158060ff233Sopenharmony_ci{ 159060ff233Sopenharmony_ci if (threadAttr->detachState == SOFTBUS_THREAD_JOINABLE) { 160060ff233Sopenharmony_ci pthread_attr_setdetachstate(attr, PTHREAD_CREATE_JOINABLE); 161060ff233Sopenharmony_ci } else if (threadAttr->detachState == SOFTBUS_THREAD_DETACH) { 162060ff233Sopenharmony_ci pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED); 163060ff233Sopenharmony_ci } else { 164060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "set detachState error"); 165060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 166060ff233Sopenharmony_ci } 167060ff233Sopenharmony_ci 168060ff233Sopenharmony_ci return SOFTBUS_OK; 169060ff233Sopenharmony_ci} 170060ff233Sopenharmony_ci 171060ff233Sopenharmony_cistatic int32_t SoftbusSetThreadPeriority(SoftBusThreadAttr *threadAttr, pthread_attr_t *attr) 172060ff233Sopenharmony_ci{ 173060ff233Sopenharmony_ci#ifdef __linux__ 174060ff233Sopenharmony_ci /* periorityParam is between 1 and 99 in linux */ 175060ff233Sopenharmony_ci #define PTHREAD_PERIOR_LOWEST 1 176060ff233Sopenharmony_ci #define PTHREAD_PERIOR_LOW 33 177060ff233Sopenharmony_ci #define PTHREAD_PERIOR_HIGH 66 178060ff233Sopenharmony_ci #define PTHREAD_PERIOR_HIGHEST 99 179060ff233Sopenharmony_ci#else 180060ff233Sopenharmony_ci /* periorityParam is between 0 and 31 in liteOS */ 181060ff233Sopenharmony_ci #define PTHREAD_PERIOR_LOWEST 30 182060ff233Sopenharmony_ci #define PTHREAD_PERIOR_LOW 20 183060ff233Sopenharmony_ci #define PTHREAD_PERIOR_HIGH 10 184060ff233Sopenharmony_ci #define PTHREAD_PERIOR_HIGHEST 0 185060ff233Sopenharmony_ci#endif 186060ff233Sopenharmony_ci 187060ff233Sopenharmony_ci struct sched_param periorityParam; 188060ff233Sopenharmony_ci (void)memset_s(&periorityParam, sizeof(periorityParam), 0, sizeof(periorityParam)); 189060ff233Sopenharmony_ci struct sched_param defaultPeri; 190060ff233Sopenharmony_ci pthread_attr_getschedparam(attr, &defaultPeri); 191060ff233Sopenharmony_ci switch (threadAttr->prior) { 192060ff233Sopenharmony_ci case SOFTBUS_PRIORITY_DEFAULT: 193060ff233Sopenharmony_ci periorityParam.sched_priority = defaultPeri.sched_priority; 194060ff233Sopenharmony_ci break; 195060ff233Sopenharmony_ci case SOFTBUS_PRIORITY_LOWEST: 196060ff233Sopenharmony_ci periorityParam.sched_priority = PTHREAD_PERIOR_LOWEST; 197060ff233Sopenharmony_ci break; 198060ff233Sopenharmony_ci case SOFTBUS_PRIORITY_LOW: 199060ff233Sopenharmony_ci periorityParam.sched_priority = PTHREAD_PERIOR_LOW; 200060ff233Sopenharmony_ci break; 201060ff233Sopenharmony_ci case SOFTBUS_PRIORITY_HIGH: 202060ff233Sopenharmony_ci periorityParam.sched_priority = PTHREAD_PERIOR_HIGH; 203060ff233Sopenharmony_ci break; 204060ff233Sopenharmony_ci case SOFTBUS_PRIORITY_HIGHEST: 205060ff233Sopenharmony_ci periorityParam.sched_priority = PTHREAD_PERIOR_HIGHEST; 206060ff233Sopenharmony_ci break; 207060ff233Sopenharmony_ci default: 208060ff233Sopenharmony_ci periorityParam.sched_priority = defaultPeri.sched_priority; 209060ff233Sopenharmony_ci break; 210060ff233Sopenharmony_ci } 211060ff233Sopenharmony_ci pthread_attr_setschedparam(attr, &periorityParam); 212060ff233Sopenharmony_ci 213060ff233Sopenharmony_ci return SOFTBUS_OK; 214060ff233Sopenharmony_ci} 215060ff233Sopenharmony_ci 216060ff233Sopenharmony_cistatic int32_t SoftBusConfTransPthreadAttr(SoftBusThreadAttr *threadAttr, pthread_attr_t *attr) 217060ff233Sopenharmony_ci{ 218060ff233Sopenharmony_ci if ((threadAttr == NULL) || (attr == NULL)) { 219060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "threadAttr or attr is null"); 220060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 221060ff233Sopenharmony_ci } 222060ff233Sopenharmony_ci 223060ff233Sopenharmony_ci int32_t ret = SoftbusSetThreadPolicy(threadAttr, attr); 224060ff233Sopenharmony_ci if (ret != SOFTBUS_OK) { 225060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftbusSetThreadPolicy failed, ret=%{public}d", ret); 226060ff233Sopenharmony_ci return SOFTBUS_ERR; 227060ff233Sopenharmony_ci } 228060ff233Sopenharmony_ci 229060ff233Sopenharmony_ci ret = SoftbusSetThreadDetachState(threadAttr, attr); 230060ff233Sopenharmony_ci if (ret != SOFTBUS_OK) { 231060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftbusSetThreadDetachState failed, ret=%{public}d", ret); 232060ff233Sopenharmony_ci return SOFTBUS_ERR; 233060ff233Sopenharmony_ci } 234060ff233Sopenharmony_ci 235060ff233Sopenharmony_ci ret = SoftbusSetThreadPeriority(threadAttr, attr); 236060ff233Sopenharmony_ci if (ret != SOFTBUS_OK) { 237060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftbusSetThreadPeriority failed, ret=%{public}d", ret); 238060ff233Sopenharmony_ci return SOFTBUS_ERR; 239060ff233Sopenharmony_ci } 240060ff233Sopenharmony_ci 241060ff233Sopenharmony_ci uint64_t stackSize = threadAttr->stackSize; 242060ff233Sopenharmony_ci if (stackSize != 0) { 243060ff233Sopenharmony_ci ret = pthread_attr_setstacksize(attr, stackSize); 244060ff233Sopenharmony_ci if (ret != 0) { 245060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "pthread_attr_setstacksize failed, ret=%{public}d", ret); 246060ff233Sopenharmony_ci return SOFTBUS_ERR; 247060ff233Sopenharmony_ci } 248060ff233Sopenharmony_ci } 249060ff233Sopenharmony_ci 250060ff233Sopenharmony_ci return SOFTBUS_OK; 251060ff233Sopenharmony_ci} 252060ff233Sopenharmony_ci 253060ff233Sopenharmony_ciint32_t SoftBusThreadCreate(SoftBusThread *thread, SoftBusThreadAttr *threadAttr, 254060ff233Sopenharmony_ci void *(*threadEntry) (void *), void *arg) 255060ff233Sopenharmony_ci{ 256060ff233Sopenharmony_ci if (thread == NULL) { 257060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "thread is null"); 258060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 259060ff233Sopenharmony_ci } 260060ff233Sopenharmony_ci 261060ff233Sopenharmony_ci if (threadEntry == NULL) { 262060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "threadEntry is null"); 263060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 264060ff233Sopenharmony_ci } 265060ff233Sopenharmony_ci 266060ff233Sopenharmony_ci int32_t ret; 267060ff233Sopenharmony_ci if (threadAttr == NULL) { 268060ff233Sopenharmony_ci ret = pthread_create((pthread_t *)thread, NULL, threadEntry, arg); 269060ff233Sopenharmony_ci if (ret != 0) { 270060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "Thread create failed, ret=%{public}d", ret); 271060ff233Sopenharmony_ci return SOFTBUS_ERR; 272060ff233Sopenharmony_ci } 273060ff233Sopenharmony_ci } else { 274060ff233Sopenharmony_ci pthread_attr_t attr; 275060ff233Sopenharmony_ci ret = pthread_attr_init(&attr); 276060ff233Sopenharmony_ci if (ret != 0) { 277060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "pthread_attr_init failed, ret=%{public}d", ret); 278060ff233Sopenharmony_ci return SOFTBUS_ERR; 279060ff233Sopenharmony_ci } 280060ff233Sopenharmony_ci ret = SoftBusConfTransPthreadAttr(threadAttr, &attr); 281060ff233Sopenharmony_ci if (ret != 0) { 282060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusConfTransPthreadAttr failed, ret=%{public}d", ret); 283060ff233Sopenharmony_ci return SOFTBUS_ERR; 284060ff233Sopenharmony_ci } 285060ff233Sopenharmony_ci ret = pthread_create((pthread_t *)thread, &attr, threadEntry, arg); 286060ff233Sopenharmony_ci if (ret != 0) { 287060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "Thread create failed, ret=%{public}d", ret); 288060ff233Sopenharmony_ci return SOFTBUS_ERR; 289060ff233Sopenharmony_ci } 290060ff233Sopenharmony_ci 291060ff233Sopenharmony_ci if (threadAttr->taskName != NULL) { 292060ff233Sopenharmony_ci ret = SoftBusThreadSetName(*thread, threadAttr->taskName); 293060ff233Sopenharmony_ci if (ret != 0) { 294060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "Thread set name failed, ret=%{public}d", ret); 295060ff233Sopenharmony_ci } 296060ff233Sopenharmony_ci } 297060ff233Sopenharmony_ci } 298060ff233Sopenharmony_ci 299060ff233Sopenharmony_ci return SOFTBUS_OK; 300060ff233Sopenharmony_ci} 301060ff233Sopenharmony_ci 302060ff233Sopenharmony_ciint32_t SoftBusThreadJoin(SoftBusThread thread, void **value) 303060ff233Sopenharmony_ci{ 304060ff233Sopenharmony_ci if (thread <= 0) { 305060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "thread is invalid"); 306060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 307060ff233Sopenharmony_ci } 308060ff233Sopenharmony_ci 309060ff233Sopenharmony_ci int32_t ret = pthread_join((pthread_t)thread, value); 310060ff233Sopenharmony_ci if (ret != 0) { 311060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "Thread join failed, ret=%{public}d", ret); 312060ff233Sopenharmony_ci return SOFTBUS_ERR; 313060ff233Sopenharmony_ci } 314060ff233Sopenharmony_ci 315060ff233Sopenharmony_ci return SOFTBUS_OK; 316060ff233Sopenharmony_ci} 317060ff233Sopenharmony_ci 318060ff233Sopenharmony_ciint32_t SoftBusThreadSetName(SoftBusThread thread, const char *name) 319060ff233Sopenharmony_ci{ 320060ff233Sopenharmony_ci if (thread <= 0) { 321060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "thread is invalid"); 322060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 323060ff233Sopenharmony_ci } 324060ff233Sopenharmony_ci 325060ff233Sopenharmony_ci if (name == NULL) { 326060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "name is null"); 327060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 328060ff233Sopenharmony_ci } 329060ff233Sopenharmony_ci 330060ff233Sopenharmony_ci if (strlen(name) >= TASK_NAME_MAX_LEN) { 331060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "set thread name length >= TASK_NAME_MAX_LEN"); 332060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 333060ff233Sopenharmony_ci } 334060ff233Sopenharmony_ci int32_t ret = pthread_setname_np((pthread_t)thread, name); 335060ff233Sopenharmony_ci if (ret != 0) { 336060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "Thread set name failed, ret=%{public}d", ret); 337060ff233Sopenharmony_ci return SOFTBUS_ERR; 338060ff233Sopenharmony_ci } 339060ff233Sopenharmony_ci 340060ff233Sopenharmony_ci return SOFTBUS_OK; 341060ff233Sopenharmony_ci} 342060ff233Sopenharmony_ci 343060ff233Sopenharmony_ciSoftBusThread SoftBusThreadGetSelf(void) 344060ff233Sopenharmony_ci{ 345060ff233Sopenharmony_ci return (SoftBusThread)pthread_self(); 346060ff233Sopenharmony_ci} 347060ff233Sopenharmony_ci 348060ff233Sopenharmony_ci/* cond */ 349060ff233Sopenharmony_ciint32_t SoftBusCondInit(SoftBusCond *cond) 350060ff233Sopenharmony_ci{ 351060ff233Sopenharmony_ci if (cond == NULL) { 352060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "cond is null"); 353060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 354060ff233Sopenharmony_ci } 355060ff233Sopenharmony_ci pthread_condattr_t attr = {0}; 356060ff233Sopenharmony_ci int32_t ret = pthread_condattr_init(&attr); 357060ff233Sopenharmony_ci if (ret != 0) { 358060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "pthread_condattr_init failed, ret=%{public}d", ret); 359060ff233Sopenharmony_ci return SOFTBUS_ERR; 360060ff233Sopenharmony_ci } 361060ff233Sopenharmony_ci ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); 362060ff233Sopenharmony_ci if (ret != 0) { 363060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "set clock failed, ret=%{public}d", ret); 364060ff233Sopenharmony_ci return SOFTBUS_ERR; 365060ff233Sopenharmony_ci } 366060ff233Sopenharmony_ci 367060ff233Sopenharmony_ci pthread_cond_t *tempCond = (pthread_cond_t *)SoftBusCalloc(sizeof(pthread_cond_t)); 368060ff233Sopenharmony_ci if (tempCond == NULL) { 369060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "tempCond is null"); 370060ff233Sopenharmony_ci return SOFTBUS_ERR; 371060ff233Sopenharmony_ci } 372060ff233Sopenharmony_ci ret = pthread_cond_init(tempCond, &attr); 373060ff233Sopenharmony_ci if (ret != 0) { 374060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusCondInit failed, ret=%{public}d", ret); 375060ff233Sopenharmony_ci SoftBusFree(tempCond); 376060ff233Sopenharmony_ci tempCond = NULL; 377060ff233Sopenharmony_ci return SOFTBUS_ERR; 378060ff233Sopenharmony_ci } 379060ff233Sopenharmony_ci 380060ff233Sopenharmony_ci *cond = (SoftBusCond)tempCond; 381060ff233Sopenharmony_ci return SOFTBUS_OK; 382060ff233Sopenharmony_ci} 383060ff233Sopenharmony_ci 384060ff233Sopenharmony_ciint32_t SoftBusCondSignal(SoftBusCond *cond) 385060ff233Sopenharmony_ci{ 386060ff233Sopenharmony_ci if ((cond == NULL) || ((void *)(*cond) == NULL)) { 387060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "cond is null"); 388060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 389060ff233Sopenharmony_ci } 390060ff233Sopenharmony_ci 391060ff233Sopenharmony_ci int32_t ret = pthread_cond_signal((pthread_cond_t *)*cond); 392060ff233Sopenharmony_ci if (ret != 0) { 393060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusCondSignal failed, ret=%{public}d", ret); 394060ff233Sopenharmony_ci return SOFTBUS_ERR; 395060ff233Sopenharmony_ci } 396060ff233Sopenharmony_ci 397060ff233Sopenharmony_ci return SOFTBUS_OK; 398060ff233Sopenharmony_ci} 399060ff233Sopenharmony_ci 400060ff233Sopenharmony_ciint32_t SoftBusCondBroadcast(SoftBusCond *cond) 401060ff233Sopenharmony_ci{ 402060ff233Sopenharmony_ci if ((cond == NULL) || ((void *)(*cond) == NULL)) { 403060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "cond is null"); 404060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 405060ff233Sopenharmony_ci } 406060ff233Sopenharmony_ci 407060ff233Sopenharmony_ci int32_t ret = pthread_cond_broadcast((pthread_cond_t *)*cond); 408060ff233Sopenharmony_ci if (ret != 0) { 409060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusCondBroadcast failed, ret=%{public}d", ret); 410060ff233Sopenharmony_ci return SOFTBUS_ERR; 411060ff233Sopenharmony_ci } 412060ff233Sopenharmony_ci 413060ff233Sopenharmony_ci return SOFTBUS_OK; 414060ff233Sopenharmony_ci} 415060ff233Sopenharmony_ci 416060ff233Sopenharmony_ciint32_t SoftBusCondWait(SoftBusCond *cond, SoftBusMutex *mutex, SoftBusSysTime *time) 417060ff233Sopenharmony_ci{ 418060ff233Sopenharmony_ci#define USECTONSEC 1000 419060ff233Sopenharmony_ci if ((cond == NULL) || ((void *)(*cond) == NULL)) { 420060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "cond is null"); 421060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 422060ff233Sopenharmony_ci } 423060ff233Sopenharmony_ci 424060ff233Sopenharmony_ci if ((mutex == NULL) || ((void *)(*mutex) == NULL)) { 425060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "mutex is null"); 426060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 427060ff233Sopenharmony_ci } 428060ff233Sopenharmony_ci 429060ff233Sopenharmony_ci int32_t ret; 430060ff233Sopenharmony_ci if (time == NULL) { 431060ff233Sopenharmony_ci ret = pthread_cond_wait((pthread_cond_t *)*cond, (pthread_mutex_t *)*mutex); 432060ff233Sopenharmony_ci if (ret != 0) { 433060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusCondWait failed, ret=%{public}d", ret); 434060ff233Sopenharmony_ci return SOFTBUS_ERR; 435060ff233Sopenharmony_ci } 436060ff233Sopenharmony_ci } else { 437060ff233Sopenharmony_ci struct timespec tv; 438060ff233Sopenharmony_ci tv.tv_sec = time->sec; 439060ff233Sopenharmony_ci tv.tv_nsec = time->usec * USECTONSEC; 440060ff233Sopenharmony_ci ret = pthread_cond_timedwait((pthread_cond_t *)*cond, (pthread_mutex_t *)*mutex, &tv); 441060ff233Sopenharmony_ci if (ret == ETIMEDOUT) { 442060ff233Sopenharmony_ci COMM_LOGD(COMM_ADAPTER, "SoftBusCondTimedWait timeout, ret=%{public}d", ret); 443060ff233Sopenharmony_ci return SOFTBUS_TIMOUT; 444060ff233Sopenharmony_ci } 445060ff233Sopenharmony_ci 446060ff233Sopenharmony_ci if (ret != 0) { 447060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusCondTimedWait failed, ret=%{public}d", ret); 448060ff233Sopenharmony_ci return SOFTBUS_ERR; 449060ff233Sopenharmony_ci } 450060ff233Sopenharmony_ci } 451060ff233Sopenharmony_ci 452060ff233Sopenharmony_ci return SOFTBUS_OK; 453060ff233Sopenharmony_ci} 454060ff233Sopenharmony_ci 455060ff233Sopenharmony_ciint32_t SoftBusCondDestroy(SoftBusCond *cond) 456060ff233Sopenharmony_ci{ 457060ff233Sopenharmony_ci if ((cond == NULL) || ((void *)(*cond) == NULL)) { 458060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "cond is null"); 459060ff233Sopenharmony_ci return SOFTBUS_INVALID_PARAM; 460060ff233Sopenharmony_ci } 461060ff233Sopenharmony_ci 462060ff233Sopenharmony_ci int32_t ret = pthread_cond_destroy((pthread_cond_t *)*cond); 463060ff233Sopenharmony_ci if (ret != 0) { 464060ff233Sopenharmony_ci COMM_LOGE(COMM_ADAPTER, "SoftBusCondDestroy failed, ret=%{public}d", ret); 465060ff233Sopenharmony_ci SoftBusFree((void *)*cond); 466060ff233Sopenharmony_ci *cond = (SoftBusCond)NULL; 467060ff233Sopenharmony_ci return SOFTBUS_ERR; 468060ff233Sopenharmony_ci } 469060ff233Sopenharmony_ci 470060ff233Sopenharmony_ci SoftBusFree((void *)*cond); 471060ff233Sopenharmony_ci *cond = (SoftBusCond)NULL; 472060ff233Sopenharmony_ci return SOFTBUS_OK; 473060ff233Sopenharmony_ci} 474