Commit dee7b585 authored by 957dd's avatar 957dd

Merge branch 'feature/tank_limit' into 'master'

Feature/tank limit See merge request !53
parents f582ab9d a2d093f8
......@@ -38,7 +38,8 @@ int main(){
my_zlog_warn("此版本电压没有使用INA226");
}
int thread_rc=thread_start_init( thread_exit_time,thread_mqtt_beat,thread_open_browser,thread_mqtt_reconnect,thread_time_calculation,thread_play_mp3);
int thread_rc=thread_start_init( thread_exit_time,thread_mqtt_beat,
thread_open_browser,thread_mqtt_reconnect,thread_time_calculation,thread_play_mp3);
if(thread_rc != 0){
my_zlog_warn("多线程初始化失败");
return -7;
......
......@@ -10,14 +10,17 @@
#include "browser_open.h"
#include "device_init.h"
#include "audioplay.h"
#include "gpio_init.h"
pthread_t g_thread[6]; // 全局线程句柄数组(或传参)
#define PTHREAD_MAX 6
int g_webrtc_index=0;
pthread_t g_thread[PTHREAD_MAX]; // 全局线程句柄数组(或传参)
int g_webrtc_index=0;//判断游览器是否打开
int g_mqtt_grc=0;
void* g_args[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
void* g_args[PTHREAD_MAX] = { NULL};
int thread_start_init(ThreadFunc thread_exit_time, ThreadFunc thread_mqtt_beat,
ThreadFunc thread_open_browser, ThreadFunc thread_mqtt_reconnect,
......@@ -32,7 +35,7 @@ int thread_start_init(ThreadFunc thread_exit_time, ThreadFunc thread_mqtt_beat,
thread_play_mp3
};
for (int i = 0; i < 6; i++) {
for (int i = 0; i < PTHREAD_MAX; i++) {
if (pthread_create(&g_thread[i], NULL, thread_funcs[i], g_args[i]) != 0) {
my_zlog_error("创建线程 %d 失败", i);
return i + 1;
......@@ -168,7 +171,7 @@ void *thread_play_mp3(void* arg) {
// 等待线程结束
void thread_end_close() {
for (int i = 0; i < 6; i++) {
for (int i = 0; i < PTHREAD_MAX; i++) {
if (g_thread[i]) pthread_join(g_thread[i], NULL);
}
}
......
#ifndef PTHREAD_OPEN_H__
#define PTHREAD_OPEN_H__
typedef void *(*ThreadFunc)(void *);
int thread_start_init(ThreadFunc thread_exit_time, ThreadFunc thread_mqtt_beat,
......@@ -12,6 +13,7 @@ void *thread_open_browser(void *arg) ;//启动游览器线程
void *thread_mqtt_reconnect(void *arg) ;//mqtt自带重连线程
void *thread_time_calculation(void *arg) ;//延时计算延迟
void *thread_play_mp3(void* arg);//等待播放音频线程
void thread_end_close() ;
#endif
\ No newline at end of file
......@@ -774,6 +774,30 @@ drivers/gpio/device_init.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/gpio/device_init.c.s
.PHONY : drivers/gpio/device_init.c.s
drivers/gpio/gpio_control.o: drivers/gpio/gpio_control.c.o
.PHONY : drivers/gpio/gpio_control.o
# target to build an object file
drivers/gpio/gpio_control.c.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/gpio/gpio_control.c.o
.PHONY : drivers/gpio/gpio_control.c.o
drivers/gpio/gpio_control.i: drivers/gpio/gpio_control.c.i
.PHONY : drivers/gpio/gpio_control.i
# target to preprocess a source file
drivers/gpio/gpio_control.c.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/gpio/gpio_control.c.i
.PHONY : drivers/gpio/gpio_control.c.i
drivers/gpio/gpio_control.s: drivers/gpio/gpio_control.c.s
.PHONY : drivers/gpio/gpio_control.s
# target to generate assembly for a file
drivers/gpio/gpio_control.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/drivers/gpio/gpio_control.c.s
.PHONY : drivers/gpio/gpio_control.c.s
drivers/gpio/gpio_init.o: drivers/gpio/gpio_init.c.o
.PHONY : drivers/gpio/gpio_init.o
......@@ -1062,30 +1086,6 @@ modules/delay/delay.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/delay/delay.c.s
.PHONY : modules/delay/delay.c.s
modules/delay/device_time.o: modules/delay/device_time.c.o
.PHONY : modules/delay/device_time.o
# target to build an object file
modules/delay/device_time.c.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/delay/device_time.c.o
.PHONY : modules/delay/device_time.c.o
modules/delay/device_time.i: modules/delay/device_time.c.i
.PHONY : modules/delay/device_time.i
# target to preprocess a source file
modules/delay/device_time.c.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/delay/device_time.c.i
.PHONY : modules/delay/device_time.c.i
modules/delay/device_time.s: modules/delay/device_time.c.s
.PHONY : modules/delay/device_time.s
# target to generate assembly for a file
modules/delay/device_time.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/delay/device_time.c.s
.PHONY : modules/delay/device_time.c.s
modules/http/http_request.o: modules/http/http_request.c.o
.PHONY : modules/http/http_request.o
......@@ -1110,30 +1110,6 @@ modules/http/http_request.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/http/http_request.c.s
.PHONY : modules/http/http_request.c.s
modules/http/ota.o: modules/http/ota.c.o
.PHONY : modules/http/ota.o
# target to build an object file
modules/http/ota.c.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/http/ota.c.o
.PHONY : modules/http/ota.c.o
modules/http/ota.i: modules/http/ota.c.i
.PHONY : modules/http/ota.i
# target to preprocess a source file
modules/http/ota.c.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/http/ota.c.i
.PHONY : modules/http/ota.c.i
modules/http/ota.s: modules/http/ota.c.s
.PHONY : modules/http/ota.s
# target to generate assembly for a file
modules/http/ota.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/http/ota.c.s
.PHONY : modules/http/ota.c.s
modules/logger/mylog.o: modules/logger/mylog.c.o
.PHONY : modules/logger/mylog.o
......@@ -1861,6 +1837,9 @@ help:
@echo "... drivers/gpio/device_init.o"
@echo "... drivers/gpio/device_init.i"
@echo "... drivers/gpio/device_init.s"
@echo "... drivers/gpio/gpio_control.o"
@echo "... drivers/gpio/gpio_control.i"
@echo "... drivers/gpio/gpio_control.s"
@echo "... drivers/gpio/gpio_init.o"
@echo "... drivers/gpio/gpio_init.i"
@echo "... drivers/gpio/gpio_init.s"
......@@ -1897,15 +1876,9 @@ help:
@echo "... modules/delay/delay.o"
@echo "... modules/delay/delay.i"
@echo "... modules/delay/delay.s"
@echo "... modules/delay/device_time.o"
@echo "... modules/delay/device_time.i"
@echo "... modules/delay/device_time.s"
@echo "... modules/http/http_request.o"
@echo "... modules/http/http_request.i"
@echo "... modules/http/http_request.s"
@echo "... modules/http/ota.o"
@echo "... modules/http/ota.i"
@echo "... modules/http/ota.s"
@echo "... modules/logger/mylog.o"
@echo "... modules/logger/mylog.i"
@echo "... modules/logger/mylog.s"
......
No preview for this file type
CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_1 = 80
CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_3 = 81
CMAKE_PROGRESS_4 =
......
CMAKE_PROGRESS_1 = 90
CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_2 = 90
CMAKE_PROGRESS_3 =
CMAKE_PROGRESS_4 = 91
CMAKE_PROGRESS_5 =
......
CMAKE_PROGRESS_1 = 95
CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_2 = 95
CMAKE_PROGRESS_3 =
CMAKE_PROGRESS_4 = 96
CMAKE_PROGRESS_5 =
......
......@@ -18,8 +18,8 @@ CMAKE_PROGRESS_17 =
CMAKE_PROGRESS_18 =
CMAKE_PROGRESS_19 = 23
CMAKE_PROGRESS_20 =
CMAKE_PROGRESS_21 =
CMAKE_PROGRESS_22 = 24
CMAKE_PROGRESS_21 = 24
CMAKE_PROGRESS_22 =
CMAKE_PROGRESS_23 =
CMAKE_PROGRESS_24 = 25
CMAKE_PROGRESS_25 =
......@@ -32,8 +32,8 @@ CMAKE_PROGRESS_31 =
CMAKE_PROGRESS_32 =
CMAKE_PROGRESS_33 = 28
CMAKE_PROGRESS_34 =
CMAKE_PROGRESS_35 =
CMAKE_PROGRESS_36 = 29
CMAKE_PROGRESS_35 = 29
CMAKE_PROGRESS_36 =
CMAKE_PROGRESS_37 =
CMAKE_PROGRESS_38 = 30
CMAKE_PROGRESS_39 =
......
CMAKE_PROGRESS_1 = 85
CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_2 = 85
CMAKE_PROGRESS_3 =
CMAKE_PROGRESS_4 = 86
CMAKE_PROGRESS_5 =
......
......@@ -2,25 +2,25 @@ CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_3 = 54
CMAKE_PROGRESS_4 =
CMAKE_PROGRESS_5 = 55
CMAKE_PROGRESS_6 =
CMAKE_PROGRESS_5 =
CMAKE_PROGRESS_6 = 55
CMAKE_PROGRESS_7 =
CMAKE_PROGRESS_8 = 56
CMAKE_PROGRESS_9 =
CMAKE_PROGRESS_8 =
CMAKE_PROGRESS_9 = 56
CMAKE_PROGRESS_10 =
CMAKE_PROGRESS_11 = 57
CMAKE_PROGRESS_12 =
CMAKE_PROGRESS_11 =
CMAKE_PROGRESS_12 = 57
CMAKE_PROGRESS_13 =
CMAKE_PROGRESS_14 = 58
CMAKE_PROGRESS_15 =
CMAKE_PROGRESS_16 =
CMAKE_PROGRESS_17 = 59
CMAKE_PROGRESS_18 =
CMAKE_PROGRESS_19 = 60
CMAKE_PROGRESS_20 =
CMAKE_PROGRESS_19 =
CMAKE_PROGRESS_20 = 60
CMAKE_PROGRESS_21 =
CMAKE_PROGRESS_22 = 61
CMAKE_PROGRESS_23 =
CMAKE_PROGRESS_22 =
CMAKE_PROGRESS_23 = 61
CMAKE_PROGRESS_24 =
CMAKE_PROGRESS_25 = 62
CMAKE_PROGRESS_26 =
......@@ -30,11 +30,11 @@ CMAKE_PROGRESS_29 =
CMAKE_PROGRESS_30 =
CMAKE_PROGRESS_31 = 64
CMAKE_PROGRESS_32 =
CMAKE_PROGRESS_33 = 65
CMAKE_PROGRESS_34 =
CMAKE_PROGRESS_33 =
CMAKE_PROGRESS_34 = 65
CMAKE_PROGRESS_35 =
CMAKE_PROGRESS_36 = 66
CMAKE_PROGRESS_37 =
CMAKE_PROGRESS_36 =
CMAKE_PROGRESS_37 = 66
CMAKE_PROGRESS_38 =
CMAKE_PROGRESS_39 = 67
CMAKE_PROGRESS_40 =
......@@ -44,11 +44,11 @@ CMAKE_PROGRESS_43 =
CMAKE_PROGRESS_44 =
CMAKE_PROGRESS_45 = 69
CMAKE_PROGRESS_46 =
CMAKE_PROGRESS_47 = 70
CMAKE_PROGRESS_48 =
CMAKE_PROGRESS_47 =
CMAKE_PROGRESS_48 = 70
CMAKE_PROGRESS_49 =
CMAKE_PROGRESS_50 = 71
CMAKE_PROGRESS_51 =
CMAKE_PROGRESS_50 =
CMAKE_PROGRESS_51 = 71
CMAKE_PROGRESS_52 =
CMAKE_PROGRESS_53 = 72
CMAKE_PROGRESS_54 =
......@@ -58,11 +58,11 @@ CMAKE_PROGRESS_57 =
CMAKE_PROGRESS_58 =
CMAKE_PROGRESS_59 = 74
CMAKE_PROGRESS_60 =
CMAKE_PROGRESS_61 = 75
CMAKE_PROGRESS_62 =
CMAKE_PROGRESS_61 =
CMAKE_PROGRESS_62 = 75
CMAKE_PROGRESS_63 =
CMAKE_PROGRESS_64 = 76
CMAKE_PROGRESS_65 =
CMAKE_PROGRESS_64 =
CMAKE_PROGRESS_65 = 76
CMAKE_PROGRESS_66 =
CMAKE_PROGRESS_67 = 77
CMAKE_PROGRESS_68 =
......@@ -72,5 +72,5 @@ CMAKE_PROGRESS_71 =
CMAKE_PROGRESS_72 =
CMAKE_PROGRESS_73 = 79
CMAKE_PROGRESS_74 =
CMAKE_PROGRESS_75 = 80
CMAKE_PROGRESS_75 =
#include "car0102_control.h"
#include "common.h"
#include "gpio_init.h"
#include "device_init.h"
#include "gpio_control.h"
void car0102_calculate_L_R(int angle) {//将角度转化为对应的舵机pwm值
......@@ -16,15 +18,15 @@ void car0102_calculate_L_R(int angle) {//将角度转化为对应的舵机pwm值
pwmWrite(PWM_PIN_CHANGE,val);
}
void car0102_speed_stop() {
//pin_value(26,0);
//device_gpio_control(g_device_type,26,0);
pwmWrite(PWM_PIN_SPEED,0);
car0102_calculate_L_R(90);
}
void car0102_mode_1_flont(unsigned char gval) {
pin_value(26,1);
device_gpio_control(g_device_type,26,1);
if(gval == 0) {
pin_value(26,0);
device_gpio_control(g_device_type,26,0);
pwmWrite(PWM_PIN_SPEED, 0);
}else if (gval < 50) {
pwmWrite(PWM_PIN_SPEED, 1000);
......@@ -68,7 +70,7 @@ void car0102_mode_1_flont(unsigned char gval) {
void car0102_mode_2_back(unsigned char gval) {
int k = 5;
int b = 100;
pin_value(26,0);
device_gpio_control(g_device_type,26,0);
if (gval < 50) {
pwmWrite(PWM_PIN_SPEED, 0);
} else if (gval <= 55) {
......
......@@ -110,8 +110,8 @@ void tank0203_change(unsigned char *buf) {
tank0203_mode_lift_back(0);
//tank0203_mode_lift_back(s_val_2-20);
}else {
tank0203_mode_lift_back(val+20);
tank0203_mode_right_flont(val+20);
tank0203_mode_lift_back(val+30);
tank0203_mode_right_flont(val+30);
}
}
......@@ -123,8 +123,8 @@ void tank0203_change(unsigned char *buf) {
tank0203_mode_right_back(0);
}
else {
tank0203_mode_lift_flont(val+20);
tank0203_mode_right_back(val+20);
tank0203_mode_lift_flont(val+30);
tank0203_mode_right_back(val+30);
}
}
......@@ -134,8 +134,8 @@ void tank0203_change(unsigned char *buf) {
tank0203_mode_right_flont(0);
}
else {
tank0203_mode_lift_flont(val+20);
tank0203_mode_right_back(val+20);
tank0203_mode_lift_flont(val+30);
tank0203_mode_right_back(val+30);
}
}
......
......@@ -3,6 +3,7 @@
#include "gpio_init.h"
#include "pthrpoll.h"
#include "tank_angle.h"
#include "gpio_control.h"
int tank_shot_back_stop(unsigned char pin,unsigned char val);
......@@ -30,45 +31,9 @@ void tank_shot_back(unsigned char gval) {
pwmWrite(PWM_PIN_CHANGE, change_1);
}
// if (gval < 50) {
// pwmWrite(PWM_PIN_CHANGE, 75);
// } else if (gval <= 55) {
// pwmWrite(PWM_PIN_CHANGE, 71);
// } else if (gval <= 60) {
// pwmWrite(PWM_PIN_CHANGE, 70);
// } else if (gval <= 65) {
// pwmWrite(PWM_PIN_CHANGE, 70);
// } else if (gval <= 70) {
// pwmWrite(PWM_PIN_CHANGE, 70);
// } else if (gval <= 75) {
// pwmWrite(PWM_PIN_CHANGE, 69);
// } else if (gval <= 90) {
// pwmWrite(PWM_PIN_CHANGE, 68);
// } else if (gval <= 100) {
// pwmWrite(PWM_PIN_CHANGE, 68);
// } else if (gval <= 110) {
// pwmWrite(PWM_PIN_CHANGE, 66);
// } else if (gval <= 120) {
// pwmWrite(PWM_PIN_CHANGE, 65);
// } else if (gval <= 130) {
// pwmWrite(PWM_PIN_CHANGE, 64);
// } else if (gval <= 140) {
// pwmWrite(PWM_PIN_CHANGE, 62);
// } else if (gval <= 150) {
// pwmWrite(PWM_PIN_CHANGE, 60);
// } else if (gval <= 160) {
// pwmWrite(PWM_PIN_CHANGE, 59);
// } else if (gval <= 170) {
// pwmWrite(PWM_PIN_CHANGE, 58);
// } else if (gval <= 180) {
// pwmWrite(PWM_PIN_CHANGE, 57);
// } else if (gval <= 190) {
// pwmWrite(PWM_PIN_CHANGE, 56);
// } else if (gval <= 200) {
// pwmWrite(PWM_PIN_CHANGE, 55);
// }
}
const tank_common_back tank_common_config_t[]={
{
.device_id = DEVICE_TANK0202,
......@@ -90,7 +55,10 @@ const tank_common_back tank_common_config_t[]={
};
void tank_shot_back_stop_task_function(void *arg) {//多线程处理坦克发射后退线程池
if(arg!= NULL){
free(arg);
}
while(1){
long long interval=shot_device_time_start-shot_device_time_end;
if(g_device_delay_count > g_tank_common_config_t->back_time&&g_device_delay_count < (g_tank_common_config_t->back_time+30))
......@@ -104,7 +72,7 @@ void tank_shot_back_stop_task_function(void *arg) {//多线程处理坦克发射
}
}
free(arg);
}
ThreadPool_t *pool_tank_t;
......@@ -113,7 +81,7 @@ void tank_shot_pthrpoll_task_init(){
int *arg = malloc(sizeof(int));
*arg = 1;
pool_tank_t=thread_pool_init(1,1);
thread_pool_add_task(pool_tank_t, tank_shot_back_stop_task_function, &arg);
thread_pool_add_task(pool_tank_t, tank_shot_back_stop_task_function, arg);
my_zlog_debug("线程池打开");
}
......@@ -143,9 +111,10 @@ int tank_shot_back_stop(unsigned char pin,unsigned char val){
shot_device_time_end=get_current_time_millis();
}
/*销毁坦克使用的线程池,让其正常销毁,只有在tank0202设备号下才有用*/
/*销毁坦克使用的线程池,让其正常销毁,只有在tank相关设备号下才有用,最后销毁都会到device——common.h中*/
void tank_thread_close(){
thread_pool_destroy(pool_tank_t);
thread_pool_destroy(g_pool_device_gpio_control_t);
}
void tank_shot_stop_control(int device_id,unsigned char pin,unsigned char val) {
......
......@@ -3,9 +3,11 @@
#include "common.h"
#include "gpio_init.h"
int g_device_delay_count =0;
int abnormal_pin_pwm_stop=0;
int g_device_type =0;
int g_device_type =0; //设备类型,比如tank0202、car0101
const deviceconfig_t device_configs[] = {
// car0101配置
......
#ifndef DEVICE_INIT_H__
#define DEVICE_INIT_H__
extern int g_device_type;
extern int g_device_type;//设备类型,比如tank0202、car0101
extern int g_device_delay_count ;
extern int g_device_delay_count ; //设备延时函数
typedef struct {
int device_id; // 设备ID (101, 102等)
......
#include "common.h"
#include "gpio_control.h"
#include "tank_angle.h"
#include "gpio_init.h"
#include "device_init.h"
#include "devcontrol_common.h"
#include "http_request.h"
#include "delay.h"
const gpiocontrol_t *gpio_control_config_t = NULL ;
ThreadPool_t *g_pool_device_gpio_control_t;
static bool s_poll_tank_index =0;
void public_pin_value(int pin,int value);
void public_pwm_value(int pin ,int value);
void tank0202_pwm_value(int pin,int value);
void tank0203_pwm_value(int pin,int value);
void tank0206_pwm_value(int pin,int value);
void tank_angle_limit_function(void *arg_gpio){
if (arg_gpio != NULL) {
free(arg_gpio);
}
printf("limit task started.\n");
while(1){
int limit_status = angle_limit();
if(limit_status==1) {
device_gpio_control(g_device_type,5,0);
my_zlog_debug("lift limit stop");
}
else if(limit_status==2) {
device_gpio_control(g_device_type,7,0);
my_zlog_debug("right limit stop");
}
else if(limit_status==0) {
delay_ms(5);
my_zlog_debug("limit stop");
}
}
free(arg_gpio);
}
void device_gpio_control_threadpoll_init(){
int *arg_gpio = malloc(sizeof(int));
my_zlog_info("device_gpio_control_threadpoll_init start\n");
*arg_gpio = 2;
g_pool_device_gpio_control_t = thread_pool_init(1,1);
thread_pool_add_task(g_pool_device_gpio_control_t, tank_angle_limit_function, arg_gpio);
}
const gpiocontrol_t gpio_configs[] = {
{
.device_id = DEVICE_CAR0101,
.device_pin_value =public_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_CAR0102,
.device_pin_value =public_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_CAR0103,
.device_pin_value =public_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_CAR0104,
.device_pin_value =public_pin_value,
.device_pwm_value =public_pwm_value
},
{
.device_id = DEVICE_TANK0202,
.device_pin_value =public_pin_value,
.device_pwm_value =tank0202_pwm_value,
.device_gpio_pthread_create=device_gpio_control_threadpoll_init
},
{
.device_id = DEVICE_TANK0203,
.device_pin_value =public_pin_value,
.device_pwm_value =tank0203_pwm_value,
.device_gpio_pthread_create=device_gpio_control_threadpoll_init
},
{
.device_id = DEVICE_TANK0206,
.device_pin_value =public_pin_value,
.device_pwm_value =tank0203_pwm_value
},
{
.device_id = DEVICE_PAO_PTZ0401,
.device_pin_value =public_pin_value,
.device_pwm_value =public_pwm_value
},
};
/*
设备控制代码
*/
void device_gpio_control(int device_id,int pin,int val) {
if (!gpio_control_config_t || gpio_control_config_t->device_id != device_id) {
// 查找设备配置
for(int i = 0; gpio_configs[i].device_id != -1; i++) {
if(gpio_configs[i].device_id == device_id) {
gpio_control_config_t = &gpio_configs[i];
break;
}
}
if(gpio_control_config_t&&
(device_id ==DEVICE_TANK0202 || device_id ==DEVICE_TANK0203))
{
my_zlog_info("线程函数:%d\n", device_id);
gpio_control_config_t->device_gpio_pthread_create(); //创建线程,线程关闭在tank.common.h中何tank需要的其他线程关闭
}
}
if(!gpio_control_config_t) {
my_zlog_error("Error: Device gpio stop ID %d not found!", device_id);
return;
}
gpio_control_config_t->device_pin_value(pin,val);
gpio_control_config_t->device_pwm_value(pin,val);
}
void public_pin_value(int pin,int value) { //引脚控制
for(int i = 0 ; i <= g_gpiocount ; i++) {
if(pin == g_gpiowpi[i]) {
break;
}if(i == g_gpiocount) {
return ;
}
}
if(value==1) {
digitalWrite(pin, HIGH);
my_zlog_debug("pin:%d,%d",pin,HIGH);
}else if(value==0) {
digitalWrite(pin, LOW);
my_zlog_debug("pin:%d,%d",pin,LOW);
}
}
void public_pwm_value(int pin ,int value){
for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
if(pin == g_gpioPwm[i]) {
break;
}
if(i == g_gpio_softpwmcount) {
return ;
}
}
int shot_speed = (int)(g_shot_speed_value*100);
if(value==1) {
if(pin == 27){
softPwmWrite(pin, shot_speed);
my_zlog_debug("pwm:%d,%d",pin,shot_speed);
} else {
softPwmWrite(pin, shot_speed);
my_zlog_debug("pwm:%d,%d",pin,shot_speed);
}
}else if(value==0) {
softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin);
}
my_zlog_debug("public pwm");
}
void tank0202_pwm_value(int pin,int value) { //软件陪我们控制调速
for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
if(pin == g_gpioPwm[i]) {
break;
}
if(i == g_gpio_softpwmcount) {
return ;
}
}
if(value==1) {
if(pin == 27){
softPwmWrite(pin, 50);
} else {
softPwmWrite(pin, 30);
my_zlog_debug("pwm:%d",pin);
}
}else if(value==0) {
softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin);
}
my_zlog_debug("tank0202 pwm");
}
void tank0203_pwm_value(int pin,int value) { //软件陪我们控制调速
for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
if(pin == g_gpioPwm[i]) {
break;
}
if(i == g_gpio_softpwmcount) {
return ;
}
}
if(value==1) {
if(pin == 27){
softPwmWrite(pin, 45);
} else {
softPwmWrite(pin, 30);
my_zlog_debug("pwm:%d",pin);
}
}else if(value==0) {
softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin);
}
my_zlog_debug("tank0203 pwm");
}
void tank0206_pwm_value(int pin,int value) { //软件陪我们控制调速
for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
if(pin == g_gpioPwm[i]) {
break;
}
if(i == g_gpio_softpwmcount) {
return ;
}
}
if(value==1) {
if(pin == 27){
softPwmWrite(pin, 45);
} else {
softPwmWrite(pin, 35);
my_zlog_debug("pwm:%d",pin);
}
}else if(value==0) {
softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin);
}
my_zlog_debug("tank0206 pwm");
}
#ifndef GPIO_CONTROL_H__
#define GPIO_CONTROL_H__
#include "pthrpoll.h"
typedef struct {
int device_id; // 设备ID (101, 102等)备名称
void (*device_pwm_value)(int pin,int val); // PWM初始化函数指针
void (*device_pin_value)(int pin,int value);// 速度控制函数指针
void (*device_gpio_pthread_create)(void);
} gpiocontrol_t;
void device_gpio_control(int device_id,int pin,int val);
extern ThreadPool_t *g_pool_device_gpio_control_t;
#endif
\ No newline at end of file
......@@ -4,6 +4,7 @@
#include "device_init.h"
#include "devcontrol_common.h"
#define MIN_DUTY 0 // 最小占空比
#define MAX_DUTY 100 // 最大占空比
......@@ -52,70 +53,6 @@ void pwm_all_default() {//全部至低电平,车和坦克共用
}
}
void pwm_value(int pin,int value) { //软件陪我们控制调速
for(int i = 0 ; i <= g_gpio_softpwmcount ; i++) {
if(pin == g_gpioPwm[i]) {
break;
}
if(i == g_gpio_softpwmcount) {
return ;
}
}
int shot_speed = (int)(g_shot_speed_value*100);
if(value==1) {
if(pin == 27){
if(g_device_type == DEVICE_TANK0202) softPwmWrite(pin, 50);
else if(g_device_type == DEVICE_TANK0203) softPwmWrite(pin, 50);
else if(g_device_type == DEVICE_TANK0206) softPwmWrite(pin, 45);
else {
softPwmWrite(pin, shot_speed);
my_zlog_debug("pwm:%d,%d",pin,shot_speed);
}
} else {
if(g_device_type == DEVICE_TANK0202) {
softPwmWrite(pin, 35);
if(angele_limit() == 1) softPwmWrite(5, 0);
if(angele_limit() == 2) softPwmWrite(7, 0);
}
else if(g_device_type == DEVICE_TANK0203) {
softPwmWrite(pin, 30);
if(angele_limit() == 1) softPwmWrite(5, 0);
if(angele_limit() == 2) softPwmWrite(7, 0);
}
else if(g_device_type == DEVICE_TANK0206) softPwmWrite(pin, 35);
else{
softPwmWrite(pin, shot_speed);
my_zlog_debug("pwm:%d,%d",pin,shot_speed);
}
my_zlog_debug("pwm:%d",pin);
}
}else if(value==0) {
softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin);
}
}
void pin_value(int pin,int value) { //引脚控制
for(int i = 0 ; i <= g_gpiocount ; i++) {
if(pin == g_gpiowpi[i]) {
break;
}if(i == g_gpiocount) {
return ;
}
}
if(value==1) {
digitalWrite(pin, HIGH);
my_zlog_debug("pin:%d,%d",pin,HIGH);
}else if(value==0) {
digitalWrite(pin, LOW);
my_zlog_debug("pin:%d,%d",pin,LOW);
}
}
/*物理pwm初始化*/
void physics_pwm_init() {
......
......@@ -9,6 +9,12 @@
#define PWM_PIN_SPEED 21
#define PWM_PIN_CHANGE 2
extern int g_gpioPwm[30];//软件控制
extern int g_gpiowpi[40]; //能使用高低引脚和其他引脚
extern int g_gpiocount;
extern int g_gpio_softpwmcount;
void init_gpiowpi(const int *values_pin);//gpio引脚初始化
void init_gpiopwm(const int *values_pwm);
......
......@@ -2,9 +2,9 @@
#include "common.h"
#include "ads1115.h"
#define LIFT_LIMIT 165
#define LIFT_LIMIT 170
#define MIDDLE_LIMIT 180
#define RIGHT_LIMIT 195
#define RIGHT_LIMIT 205
double tank_angle(){
double angle=0;
......@@ -19,7 +19,7 @@ double tank_angle(){
int angele_limit(){
int angle_limit(){
int tank_limit_angele = tank_angle();
if(tank_limit_angele >=LIFT_LIMIT && tank_limit_angele<=MIDDLE_LIMIT) return 1;
else if(tank_limit_angele >=MIDDLE_LIMIT && tank_limit_angele<=RIGHT_LIMIT) return 2;
......
......@@ -3,6 +3,6 @@
double tank_angle();
int angele_limit();
int angle_limit();
#endif
\ No newline at end of file
......@@ -2,19 +2,20 @@
#include "warn.h"
#include "http_request.h"
#include "mylog.h"
#include "gpio_init.h"
#include "gpio_control.h"
#include "device_init.h"
#include "devcontrol_common.h"
//最高警报,最低电压报警
int alarm_highest(int index) {
if(index == 1) {
pin_value(20,1);//紫灯
device_gpio_control(g_device_type,20,1);//紫灯
if(g_enable_buzzer_value) {
pin_value(22,1);//蜂鸣器
device_gpio_control(g_device_type,22,1);//蜂鸣器
}
}else if(index == 0) {
pin_value(20,0);//紫灯
pin_value(22,0);//蜂鸣器
device_gpio_control(g_device_type,20,0);//紫灯
device_gpio_control(g_device_type,22,0);//蜂鸣器
}
return index ;
......@@ -23,9 +24,9 @@ int alarm_highest(int index) {
//比较小警报
int alarm_low(int index) {
if(index == 1) {
pin_value(16,1);//黄灯
device_gpio_control(g_device_type,16,1);//黄灯
} else if(index == 0) {
pin_value(16,0);//黄灯
device_gpio_control(g_device_type,16,0);//黄灯
}
return index ;
......
......@@ -37,7 +37,6 @@
#include "delay.h"
#include "mylog.h"
#include "device_time.h"
// ========== 香橙派优化宏 ==========
......
......@@ -13,15 +13,15 @@
#include "tank_common.h"
#include "common.h"
#define DEVICE_CAR0101 101
#define DEVICE_CAR0102 102
#define DEVICE_CAR0103 103
#define DEVICE_CAR0104 104
#define DEVICE_TANK0201 201
#define DEVICE_TANK0202 202
#define DEVICE_TANK0203 203
#define DEVICE_TANK0206 206
#define DEVICE_PAO_PTZ0401 401
#define DEVICE_CAR0101 101 //莽牛大车
#define DEVICE_CAR0102 102 //莽牛小车
#define DEVICE_CAR0103 103 //推土机
#define DEVICE_CAR0104 104 //挖机
#define DEVICE_TANK0201 201 //废弃坦克
#define DEVICE_TANK0202 202 //
#define DEVICE_TANK0203 203 //M1A2美国坦克
#define DEVICE_TANK0206 206 //可以喷水坦克
#define DEVICE_PAO_PTZ0401 401 //云台
typedef struct {
int device_id; // 设备ID (101, 102等)
......
......@@ -20,32 +20,6 @@ int opencamsh_china(){
}
// int opencamsh_china() {
// const char* url = "https://jywy.yd-ss.com?dev=";
// char urls[50];
// sprintf(urls, "%s%s", url, mqtt_topic_pure_number());
// // 使用nice和taskset提高优先级并绑定CPU核心
// sprintf(gwebcam, "su - orangepi -c \"nice -n -10 taskset -c 1 chromium-browser --use-fake-ui-for-media-stream "
// "--use-gl=egl "
// "--enable-features=WebRTC-H264WithOpenH264FFmpeg "
// "--disable-features=WebRTC-IPv6OnWiFi "
// "--enable-gpu-rasterization "
// "--enable-zero-copy "
// "--disable-quic "
// "--disable-gpu-compositing "
// "--autoplay-policy=no-user-gesture-required "
// "--window-size=800,600 --start-maximized "
// "%s\"", urls);
// system(gwebcam);
// my_zlog_debug("%s", gwebcam);
// my_zlog_debug("open cam");
// return 0;
// }
int opencamsh_abroad(){
const char* url = "https://video.luckycar.top/?dev=";
char urls[50];
......
#include "device_time.h"
int g_device_delay_count =0 ;
\ No newline at end of file
#ifndef DEVICE_TIME_H
#define DEVICE_TIME_H
extern int g_device_delay_count ;
#endif
\ No newline at end of file
......@@ -4,7 +4,7 @@
#include"common.h"// 用于存储HTTP响应数据的结构体
/*2为关闭请求,1为打开*/
#define HTTP_REQUEST_INDEX 2
#define HTTP_REQUEST_INDEX 1
struct MemoryStruct {
char *memory;
......
#include "ota.h"
#include "common.h"
int ota_init(){
}
\ No newline at end of file
#ifndef OTA_H
#define OTA_H
#endif
\ No newline at end of file
......@@ -18,6 +18,7 @@
#include "gpio_init.h"
#include "device_init.h"
#include "tank_angle.h"
#include "gpio_control.h"
int g_heartbeat_count=0;
......@@ -164,9 +165,8 @@ void message_4(cJSON *body){//message 为4时候
my_zlog_debug("pinTemp:%d",g_valt[1]);
my_zlog_debug("valTemp:%d",g_valt[2]);
pin_value(g_valt[1],g_valt[2]);
pwm_value(g_valt[1],g_valt[2]);
device_gpio_control(g_device_type,g_valt[1],g_valt[2]);
tank_shot_stop_control(g_device_type,g_valt[1],g_valt[2]);
}
......
#include "pthrpoll.h"
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
// 初始化任务队列
static int task_queue_init(TaskQueue *queue) {
......@@ -16,6 +20,7 @@ static int task_queue_init(TaskQueue *queue) {
}
// 向任务队列中添加任务
// 注意:现在不负责复制参数,调用者需要保证 argument 的生命周期
static int task_queue_add(TaskQueue *queue, void (*function)(void *), void *argument) {
Task *task = (Task *)malloc(sizeof(Task));
if (task == NULL) {
......@@ -36,6 +41,7 @@ static int task_queue_add(TaskQueue *queue, void (*function)(void *), void *argu
}
queue->size++;
// 唤醒一个等待的线程
pthread_cond_signal(&queue->cond);
pthread_mutex_unlock(&queue->mutex);
return 0;
......@@ -45,10 +51,12 @@ static int task_queue_add(TaskQueue *queue, void (*function)(void *), void *argu
static Task *task_queue_remove(TaskQueue *queue, ThreadPool_t *pool) {
pthread_mutex_lock(&queue->mutex);
// 如果队列为空且线程池未关闭,则等待
while (queue->size == 0 && !pool->shutdown) {
pthread_cond_wait(&queue->cond, &queue->mutex);
}
// 如果线程池已关闭且队列为空,则线程可以退出了
if (pool->shutdown && queue->size == 0) {
pthread_mutex_unlock(&queue->mutex);
return NULL;
......@@ -70,65 +78,29 @@ static Task *task_queue_remove(TaskQueue *queue, ThreadPool_t *pool) {
static void *worker_thread(void *arg) {
ThreadPool_t *pool = (ThreadPool_t *)arg;
pthread_mutex_lock(&pool->mutex);
pool->active_threads++;
pthread_mutex_unlock(&pool->mutex);
while (1) {
// 从队列获取任务,如果返回 NULL,说明需要退出
Task *task = task_queue_remove(&pool->task_queue, pool);
if (task == NULL) {
break; // 收到关闭信号且队列为空
break;
}
// 执行任务
task->function(task->argument);
free(task->argument); // 释放任务参数
free(task); // 释放任务本身
// 通知回收线程可能有空闲线程
pthread_cond_signal(&pool->reaper_cond);
// 释放任务结构体本身
free(task);
// 注意:task->argument 的释放由任务函数内部或调用者负责
}
// 线程退出前,减少存活线程计数
pthread_mutex_lock(&pool->mutex);
pool->active_threads--;
pool->live_threads--;
pthread_mutex_unlock(&pool->mutex);
return NULL;
}
// 回收线程函数
static void *reaper_thread(void *arg) {
ThreadPool_t *pool = (ThreadPool_t *)arg;
while (!pool->shutdown) {
pthread_mutex_lock(&pool->mutex);
// 检查是否需要回收线程
while (pool->active_threads <= pool->min_threads ||
(pool->task_queue.size > 0 && pool->active_threads <= pool->task_queue.size)) {
pthread_cond_wait(&pool->reaper_cond, &pool->mutex);
if (pool->shutdown) {
pthread_mutex_unlock(&pool->mutex);
return NULL;
}
}
// 计算可以回收的线程数量
int excess_threads = pool->active_threads - pool->min_threads;
if (excess_threads > 0 && pool->active_threads > pool->min_threads) {
// 通过添加空任务来让工作线程退出
for (int i = 0; i < excess_threads; i++) {
task_queue_add(&pool->task_queue, NULL, NULL);
}
}
pthread_mutex_unlock(&pool->mutex);
sleep(1); // 避免过于频繁检查
}
return NULL;
}
// 初始化线程池
// 初始化线程池 (修正版,非单例)
ThreadPool_t *thread_pool_init(int min_threads, int max_threads) {
if (min_threads <= 0 || max_threads <= 0 || min_threads > max_threads) {
errno = EINVAL;
......@@ -140,26 +112,26 @@ ThreadPool_t *thread_pool_init(int min_threads, int max_threads) {
return NULL;
}
pool->threads = (pthread_t *)malloc(max_threads * sizeof(pthread_t));
if (pool->threads == NULL) {
if (task_queue_init(&pool->task_queue) != 0) {
free(pool);
return NULL;
}
if (task_queue_init(&pool->task_queue) != 0) {
free(pool->threads);
pool->threads = (pthread_t *)malloc(max_threads * sizeof(pthread_t));
if (pool->threads == NULL) {
pthread_mutex_destroy(&pool->task_queue.mutex);
pthread_cond_destroy(&pool->task_queue.cond);
free(pool);
return NULL;
}
pool->min_threads = min_threads;
pool->max_threads = max_threads;
pool->thread_count = min_threads;
pool->active_threads = 0;
pool->live_threads = 0; // 初始为0,创建一个增加一个
pool->busy_threads = 0; // 可以增加这个来跟踪繁忙线程(本次简化实现中未使用)
pool->shutdown = 0;
if (pthread_mutex_init(&pool->mutex, NULL) != 0 ||
pthread_cond_init(&pool->reaper_cond, NULL) != 0) {
if (pthread_mutex_init(&pool->mutex, NULL) != 0) {
pthread_mutex_destroy(&pool->task_queue.mutex);
pthread_cond_destroy(&pool->task_queue.cond);
free(pool->threads);
......@@ -167,43 +139,14 @@ ThreadPool_t *thread_pool_init(int min_threads, int max_threads) {
return NULL;
}
// 创建工作线程
// 创建核心(最小)数量的工作线程
for (int i = 0; i < min_threads; i++) {
if (pthread_create(&pool->threads[i], NULL, worker_thread, pool) != 0) {
// 创建线程失败,关闭已创建的线程
pool->shutdown = 1;
pthread_cond_broadcast(&pool->task_queue.cond);
for (int j = 0; j < i; j++) {
pthread_join(pool->threads[j], NULL);
}
pthread_mutex_destroy(&pool->mutex);
pthread_cond_destroy(&pool->reaper_cond);
pthread_mutex_destroy(&pool->task_queue.mutex);
pthread_cond_destroy(&pool->task_queue.cond);
free(pool->threads);
free(pool);
// 如果创建失败,销毁已创建的资源并返回
thread_pool_destroy(pool);
return NULL;
}
}
// 创建回收线程
if (pthread_create(&pool->reaper_thread, NULL, reaper_thread, pool) != 0) {
pool->shutdown = 1;
pthread_cond_broadcast(&pool->task_queue.cond);
for (int i = 0; i < min_threads; i++) {
pthread_join(pool->threads[i], NULL);
}
pthread_mutex_destroy(&pool->mutex);
pthread_cond_destroy(&pool->reaper_cond);
pthread_mutex_destroy(&pool->task_queue.mutex);
pthread_cond_destroy(&pool->task_queue.cond);
free(pool->threads);
free(pool);
return NULL;
pool->live_threads++;
}
return pool;
......@@ -221,23 +164,19 @@ int thread_pool_add_task(ThreadPool_t *pool, void (*function)(void *), void *arg
return -1;
}
// 如果任务队列过长且可以创建更多线程,则创建新线程
if (pool->task_queue.size > pool->active_threads &&
pool->thread_count < pool->max_threads) {
if (pthread_create(&pool->threads[pool->thread_count], NULL, worker_thread, pool) == 0) {
pool->thread_count++;
// 动态扩容:如果当前任务数大于存活线程数,且尚未达到最大线程数
// 这是一个简单的扩容策略,可以根据需要调整
if (pool->task_queue.size > 0 && pool->live_threads < pool->max_threads) {
// 尝试创建一个新线程
if (pthread_create(&pool->threads[pool->live_threads], NULL, worker_thread, pool) == 0) {
pool->live_threads++;
}
// 如果创建失败,也没关系,现有线程会处理任务
}
pthread_mutex_unlock(&pool->mutex);
// 复制参数,确保生命周期
void *arg_copy = malloc(sizeof(*argument));
if (arg_copy == NULL) {
return -1;
}
*(int *)arg_copy = *(int *)argument;
return task_queue_add(&pool->task_queue, function, arg_copy);
// 直接添加任务,不再复制参数
return task_queue_add(&pool->task_queue, function, argument);
}
// 销毁线程池
......@@ -247,33 +186,38 @@ void thread_pool_destroy(ThreadPool_t *pool) {
}
pthread_mutex_lock(&pool->mutex);
// 如果已经调用过销毁,直接返回
if (pool->shutdown) {
pthread_mutex_unlock(&pool->mutex);
return;
}
pool->shutdown = 1;
pthread_mutex_unlock(&pool->mutex);
// 唤醒所有线程
// 唤醒所有可能在等待任务的线程,让他们检查 shutdown 标志并退出
pthread_cond_broadcast(&pool->task_queue.cond);
pthread_cond_signal(&pool->reaper_cond);
// 等待所有线程退出
for (int i = 0; i < pool->thread_count; i++) {
// 等待所有存活的线程退出
// live_threads 在这里是创建过的线程数,因为我们去掉了回收功能
for (int i = 0; i < pool->live_threads; i++) {
pthread_join(pool->threads[i], NULL);
}
pthread_join(pool->reaper_thread, NULL);
// 清理剩余任务
// 清理任务队列中未执行的任务
Task *task = pool->task_queue.head;
while (task != NULL) {
Task *next = task->next;
if (task->argument) free(task->argument);
// 注意:这里我们只释放任务结构体
// 参数 argument 的内存应该由调用者在确认线程池销毁后自己管理
// 或者设计一个统一的清理回调
free(task);
task = next;
}
// 释放资源
// 释放所有资源
free(pool->threads);
pthread_mutex_destroy(&pool->task_queue.mutex);
pthread_cond_destroy(&pool->task_queue.cond);
pthread_mutex_destroy(&pool->mutex);
pthread_cond_destroy(&pool->reaper_cond);
free(pool);
}
\ No newline at end of file
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#ifndef PTHRPOLL_H
#define PTHRPOLL_H
#include "common.h"
// 为了让头文件自给自足,直接包含它所需要的依赖
#include <common.h>
// 任务结构体
typedef struct Task {
void (*function)(void *);
void *argument;
struct Task *next;
void (*function)(void *); // 函数指针,指向要执行的任务函数
void *argument; // 传递给任务函数的参数
struct Task *next; // 指向下一个任务的指针,构成链表
} Task;
// 任务队列结构体
typedef struct {
Task *head;
Task *tail;
int size;
pthread_mutex_t mutex;
pthread_cond_t cond;
Task *head; // 队列头部
Task *tail; // 队列尾部
int size; // 队列中任务的数量
pthread_mutex_t mutex; // 用于保护任务队列的互斥锁
pthread_cond_t cond; // 用于线程同步的条件变量
} TaskQueue;
// 线程池结构体
// 线程池结构体 (此结构体已被简化和明确化)
typedef struct {
pthread_t *threads;
pthread_t reaper_thread; // 新增:回收线程
int thread_count;
int min_threads; // 新增:最小线程数
int max_threads; // 新增:最大线程数
int active_threads; // 新增:活跃线程数
TaskQueue task_queue;
int shutdown;
pthread_mutex_t mutex;
pthread_cond_t reaper_cond; // 新增:回收线程条件变量
pthread_t *threads; // 存放线程ID的数组
int min_threads; // 线程池中最小线程数
int max_threads; // 线程池中最大线程数
// --- 字段已重命名和简化 ---
int live_threads; // 当前存活的线程数
int busy_threads; // 当前正在忙碌(执行任务)的线程数(为未来功能增强预留)
TaskQueue task_queue; // 任务队列
int shutdown; // 线程池关闭标志(1表示关闭,0表示运行)
pthread_mutex_t mutex; // 用于保护线程池级别变量(如线程计数)的互斥锁
// --- 已移除的字段 ---
// pthread_t reaper_thread; // 已移除:回收线程的逻辑复杂且存在缺陷。
// pthread_cond_t reaper_cond; // 已移除:没有回收线程后不再需要。
// 'thread_count' 和 'active_threads' 的概念现在由 'live_threads' 更清晰地表示。
} ThreadPool_t;
// 初始化线程池
/**
* @brief 初始化线程池。
* @param min_threads 线程池中保持存活的最小线程数。
* @param max_threads 线程池可以扩展到的最大线程数。
* @return 成功时返回新创建的线程池指针,失败时返回 NULL。
*/
ThreadPool_t *thread_pool_init(int min_threads, int max_threads);
// 向线程池添加任务
/**
* @brief 向线程池的任务队列中添加一个新任务。
* @param pool 要添加任务的线程池。
* @param function 要执行的任务函数指针。
* @param argument 传递给任务函数的参数。
* 注意:调用者负责管理此参数的内存。
* 如果参数是动态分配的,任务函数内部应负责释放它。
* @return 成功返回 0,失败返回 -1。
*/
int thread_pool_add_task(ThreadPool_t *pool, void (*function)(void *), void *argument);
// 销毁线程池
/**
* @brief 关闭并清理线程池资源。
* 此函数会等待所有当前正在运行的任务执行完毕,但不会执行队列中剩余的任务。
* @param pool 要销毁的线程池。
*/
void thread_pool_destroy(ThreadPool_t *pool);
#endif //THREAD_POOL_H
\ No newline at end of file
#endif // PTHRPOLL_H
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment