-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimingTimeOutManager.c
More file actions
185 lines (165 loc) · 4.74 KB
/
TimingTimeOutManager.c
File metadata and controls
185 lines (165 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* TimingTimeOutManager.c
*
* Created on: 2022年5月6日
* Author: ZhouXin
*/
#include "TimingTimeOutManager.h"
#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!\n", __func__);return;}
static void* pthread_func (void* argv)
{
TTOMStatus_e status = TTOM_STOP;
TTOMPrivInfo_s *info = (TTOMPrivInfo_s*)argv;
unsigned int count = 0;
pthread_detach(pthread_self());
while (TTOM_TASK_RUNNING == info->task_running)
{
//! An external API can control the completion of a task
if(info->process_status == TTOM_STOP)
{
status = TTOM_STOP;
}
if(TTOM_STOP == status)
{
info->process_status = TTOM_STOP;
sem_wait(&info->s1);//阻塞在这里
count = info->time_out / info->timing;
//! count must greater than zero
status = (count > 0) ? TTOM_START : TTOM_STOP;
continue;
}
else if(TTOM_START == status)
{
if(NULL != info->timing_call_back_func)
{
TTOMResult_e ret = info->timing_call_back_func(info->timing_call_back_argv);
//! If the expected result is obtained during scheduled invocation, the task is automatically terminated
if(TTOM_SUEECSS == ret)
{
status = TTOM_STOP;
if(NULL != info->success_call_back_func)
{
info->success_call_back_func(info->success_call_back_argv);
}
continue;
}
}
count--;
//! If the expected result is not obtained during the scheduled call until the timeout, perform the following steps
if(count == 0)
{
status = TTOM_STOP;
if(NULL != info->time_out_call_back_func)
{
info->time_out_call_back_func(info->time_out_call_back_argv);
}
continue;
}
}
sleep (info->timing);
}
pthread_exit(NULL);
}
void TTOM_Init(TTOMPrivInfo_s* info, unsigned int time_out, unsigned int timing)
{
return_if_fail (info != NULL);
pthread_t pt = 0;
int ret = 0;
if(info->task_running != TTOM_TASK_RUNNING)
{
info->task_running = TTOM_TASK_RUNNING;
info->process_status = TTOM_STOP;
info->time_out = time_out;
info->timing = timing;
info->timing_call_back_func = NULL;
info->time_out_call_back_func = NULL;
info->success_call_back_func = NULL;
info->timing_call_back_argv = NULL;
info->time_out_call_back_argv = NULL;
info->success_call_back_argv = NULL;
sem_init (&info->s1, 0, 0);
ret = pthread_create (&pt, NULL, pthread_func, (void*)info);
if (ret != 0)
{
info->task_running = !TTOM_TASK_RUNNING;
sem_destroy(&info->s1);
printf("%s: pthread_create error\n", __func__);
}
}
}
void TTOM_SetTimingCallBackFunc(TTOMPrivInfo_s* info, TTOMCallBack timing_call_back_func, void* timing_call_back_argv)
{
return_if_fail (info != NULL);
if(info->process_status != TTOM_START)
{
info->timing_call_back_func = timing_call_back_func;
info->timing_call_back_argv = timing_call_back_argv;
}
}
void TTOM_SetSuccessCallBackFunc(TTOMPrivInfo_s* info, TTOMCallBack success_call_back_func, void* success_call_back_argv)
{
return_if_fail (info != NULL);
if(info->process_status != TTOM_START)
{
info->success_call_back_func = success_call_back_func;
info->success_call_back_argv = success_call_back_argv;
}
}
void TTOM_SetTimeOutCallBackFunc(TTOMPrivInfo_s* info, TTOMCallBack time_out_call_back_func, void* time_out_call_back_argv)
{
return_if_fail (info != NULL);
if(info->process_status != TTOM_START)
{
info->time_out_call_back_func = time_out_call_back_func;
info->time_out_call_back_argv = time_out_call_back_argv;
}
}
void TTOM_DeInit(TTOMPrivInfo_s* info)
{
return_if_fail (info != NULL);
info->task_running = !TTOM_TASK_RUNNING;
info->timing_call_back_func = NULL;
info->time_out_call_back_func = NULL;
info->timing_call_back_argv = NULL;
info->time_out_call_back_argv = NULL;
info->success_call_back_func = NULL;
info->success_call_back_argv = NULL;
sem_destroy(&info->s1);
if(info->process_status != TTOM_STOP)
{
info->process_status = TTOM_STOP;
}
}
void TTOM_Stop(TTOMPrivInfo_s* info)
{
return_if_fail (info != NULL);
info->process_status = TTOM_STOP;
}
void TTOM_Start(TTOMPrivInfo_s* info)
{
return_if_fail (info != NULL);
if(info->process_status != TTOM_START)
{
info->process_status = TTOM_START;
sem_post(&info->s1);
}
}
void TTOM_Start2(TTOMPrivInfo_s* info, unsigned int time_out, unsigned int timing)
{
return_if_fail (info != NULL);
if(info->process_status != TTOM_START)
{
info->time_out = time_out;
info->timing = timing;
info->process_status = TTOM_START;
sem_post(&info->s1);
}
}
TTOMStatus_e TTOM_Get_Status(TTOMPrivInfo_s* info)
{
if(NULL == info)
{
return TTOM_STOP;
}
return info->process_status;
}