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 struct Job { 34 int32_t (*callbackFunction)(void *arg); 35 void *arg; 36 struct Job *next; 37 JobMode jobMode; 38 SoftBusMutex mutex; 39 uintptr_t handle; 40 bool runnable; 41 }; 42 43 typedef struct Job Job; 44 45 typedef struct { 46 int32_t threadNum; 47 int32_t queueMaxNum; 48 Job *head; 49 Job *tail; 50 SoftBusThread *pthreads; 51 SoftBusMutex mutex; 52 SoftBusCond queueEmpty; 53 SoftBusCond queueNotEmpty; 54 SoftBusCond queueNotFull; 55 int32_t queueCurNum; 56 int32_t queueClose; 57 int32_t poolClose; 58 } ThreadPool; 59 60 ThreadPool* ThreadPoolInit(int32_t threadNum, int32_t queueMaxNum); 61 62 int32_t ThreadPoolAddJob(ThreadPool *pool, int32_t (*callbackFunction)(void *arg), 63 void *arg, JobMode jobMode, uintptr_t handle); 64 65 int32_t ThreadPoolRemoveJob(ThreadPool *pool, uintptr_t handle); 66 67 int32_t ThreadPoolDestroy(ThreadPool *pool); 68 69 #ifdef __cplusplus 70 #if __cplusplus 71 } 72 #endif /* __cplusplus */ 73 #endif /* __cplusplus */ 74 75 #endif /* THREAD_POOL_H */