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 THREAD_POOL_H 17 #define THREAD_POOL_H 18 19 #include <stdbool.h> 20 #include <stdint.h> 21 #include "softbus_adapter_thread.h" 22 23 #ifdef __cplusplus 24 #if __cplusplus 25 extern "C" { 26 #endif 27 #endif 28 typedef enum { 29 ONCE = 0, 30 PERSISTENT = 1, 31 } JobMode; 32 33 typedef int32_t (*JobTask)(void *arg); 34 struct Job { 35 int32_t (*callbackFunction)(void *arg); 36 void *arg; 37 struct Job *next; 38 JobMode jobMode; 39 SoftBusMutex mutex; 40 uintptr_t handle; 41 bool runnable; 42 }; 43 44 typedef struct Job Job; 45 46 typedef struct { 47 int32_t threadNum; 48 int32_t queueMaxNum; 49 Job *head; 50 Job *tail; 51 SoftBusThread *pthreads; 52 SoftBusMutex mutex; 53 SoftBusCond queueEmpty; 54 SoftBusCond queueNotEmpty; 55 SoftBusCond queueNotFull; 56 int32_t queueCurNum; 57 int32_t queueClose; 58 int32_t poolClose; 59 } ThreadPool; 60 61 ThreadPool* ThreadPoolInit(int32_t threadNum, int32_t queueMaxNum); 62 63 int32_t ThreadPoolAddJob(ThreadPool *pool, int32_t (*callbackFunction)(void *arg), 64 void *arg, JobMode jobMode, uintptr_t handle); 65 66 int32_t ThreadPoolRemoveJob(ThreadPool *pool, uintptr_t handle); 67 68 int32_t ThreadPoolDestroy(ThreadPool *pool); 69 70 #ifdef __cplusplus 71 #if __cplusplus 72 } 73 #endif /* __cplusplus */ 74 #endif /* __cplusplus */ 75 76 #endif /* THREAD_POOL_H */