Skip to content

Commit

Permalink
改进线程同步逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
sfxfs committed Nov 21, 2023
1 parent c12770e commit 076bfc1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
11 changes: 8 additions & 3 deletions user/control/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ void *control_thread(void *arg)
rov_info_t *info = (rov_info_t *)arg;
for (;;)
{
pthread_mutex_lock(&info->system.device.power_output_mtx);
pthread_cond_wait(&info->system.server.recv_cmd_cond, &info->system.device.power_output_mtx);
pthread_mutex_lock(&info->system.control.recv_cmd_mtx);
pthread_cond_wait(&info->system.control.recv_cmd_cond, &info->system.control.recv_cmd_mtx);
log_d("cmd recv, ask for propeller");
if (info->control.flag.debug_mode == false)
{
rov_manual_control(info);
Expand All @@ -30,7 +31,8 @@ void *control_thread(void *arg)
{
rov_debug_control(info);
}
pthread_mutex_unlock(&info->system.device.power_output_mtx);
pthread_cond_signal(&info->system.device.power_output_cond);
pthread_mutex_unlock(&info->system.control.recv_cmd_mtx);
}
return NULL;
}
Expand All @@ -42,6 +44,9 @@ void *control_thread(void *arg)
*/
int rov_control_run(struct rov_info* info)
{
pthread_mutex_init(&info->system.control.recv_cmd_mtx, NULL);
pthread_cond_init(&info->system.control.recv_cmd_cond, NULL);

log_i("starting thread");
if (pthread_create(&info->system.control.main_tid, NULL, control_thread, info) != 0)
{
Expand Down
4 changes: 3 additions & 1 deletion user/data-type/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
typedef struct sys_device
{
pthread_t main_tid;
pthread_cond_t power_output_cond;
pthread_mutex_t power_output_mtx;
} sys_device_t;

Expand All @@ -19,13 +20,14 @@ typedef struct sys_server
{
pthread_t loss_status_check_tid;
pthread_mutex_t loss_status_mtx;
pthread_cond_t recv_cmd_cond;
struct server_config config;
} sys_server_t;

typedef struct sys_control
{
pthread_t main_tid;
pthread_mutex_t recv_cmd_mtx;
pthread_cond_t recv_cmd_cond;
} sys_control_t;

typedef struct system
Expand Down
5 changes: 5 additions & 0 deletions user/device/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ void *propeller_thread(void *arg)
for (;;)
{
pthread_mutex_lock(&info->system.device.power_output_mtx);
pthread_cond_wait(&info->system.device.power_output_cond, &info->system.device.power_output_mtx);

log_d("write to propeller");
write_to_propeller(&info->propeller);

pthread_mutex_unlock(&info->system.device.power_output_mtx);
}

Expand All @@ -68,6 +72,7 @@ void *propeller_thread(void *arg)
int rov_device_run(struct rov_info* info)
{
pthread_mutex_init(&info->system.device.power_output_mtx, NULL);
pthread_cond_init(&info->system.device.power_output_cond, NULL);

log_i("starting pwm controller");
if (pwm_controller_init(PCA9685_ADDRESS_A000000, info->propeller.pwm_freq_calibration) < 0)
Expand Down
8 changes: 7 additions & 1 deletion user/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ extern void uvm_loop (void);

static void exit_uvm(int sig)
{
printf("info: closing uv-master app...\n");
printf("\ninfo: closing uv-master app...\n");
uvm_deinit();
printf( " __ __ \n"
" / ) / ) /\n"
" /--< __ , _ /--< __ , _ / \n"
"/___/_/ (_/_</_ /___/_/ (_/_</_ ' \n"
" / / o \n"
" ' ' \n");
exit(EXIT_SUCCESS);
}

Expand Down
6 changes: 5 additions & 1 deletion user/server/handler/control.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#define LOG_TAG "server.handler.ctl"

#include "elog.h"
#include "config/parameters/utils.h"
#include "data_define.h"

Expand Down Expand Up @@ -33,7 +36,8 @@ static cJSON *move_analysis(cJSON* params, struct rov_info* info, move_mode_t mo
break;
}

pthread_cond_signal(&info->system.server.recv_cmd_cond);
log_d("rocket cmd recv, sending cond signal");
pthread_cond_signal(&info->system.control.recv_cmd_cond);

return cJSON_CreateNull();
}
Expand Down
2 changes: 1 addition & 1 deletion user/server/handler/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ cJSON *set_propeller_values_handler(jrpc_context *ctx, cJSON *params, cJSON *id)
info->propeller.front_left.power_debug = cjson_value_analysis_int(params, "front_left");
info->propeller.front_right.power_debug = cjson_value_analysis_int(params, "front_right");

pthread_cond_signal(&info->system.server.recv_cmd_cond);
pthread_cond_signal(&info->system.control.recv_cmd_cond);

return cJSON_CreateNull();
}
Expand Down
5 changes: 2 additions & 3 deletions user/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ int loss_ctl_timer_init(struct rov_info* info)
*/
int jsonrpc_server_run(struct rov_info* info, uint8_t debug_level)
{
pthread_cond_init(&info->system.server.recv_cmd_cond, NULL);

o = onion_new(O_DETACH_LISTEN | O_NO_SIGTERM);
if (o == NULL)
{
Expand Down Expand Up @@ -196,7 +194,8 @@ int jsonrpc_server_stop()
{
log_i("stop service");

mln_event_free(ev);//释放事件模块
if (ev != NULL)
mln_event_free(ev);//释放事件模块
onion_free(o);

return 0;
Expand Down

0 comments on commit 076bfc1

Please sign in to comment.