-
Notifications
You must be signed in to change notification settings - Fork 1
/
loop.c
167 lines (148 loc) · 4.54 KB
/
loop.c
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
/*
* loop.c of arm_emulator
* Copyright (C) 2019-2020 hxdyxd <hxdyxd@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <loop.h>
#include <time.h>
#include <errno.h>
#include <config.h>
#ifdef USE_PRCTL_SET_THREAD_NAME
#include <sys/prctl.h>
#endif
#define LOG_NAME "loop"
#define DEBUG_PRINTF(...) printf("\033[0;32m" LOG_NAME "\033[0m: " __VA_ARGS__)
#define ERROR_PRINTF(...) printf("\033[1;31m" LOG_NAME "\033[0m: " __VA_ARGS__)
static void loop_prepare_callback(struct loop_t *lo)
{
for(int i=0; i<lo->callback->len; i++) {
struct loopcb_t *cb = g_array_index(lo->callback, struct loopcb_t *, i);
if(cb->prepare)
cb->prepare(cb->opaque);
}
}
static void loop_poll_callback(struct loop_t *lo)
{
for(int i=0; i<lo->callback->len; i++) {
struct loopcb_t *cb = g_array_index(lo->callback, struct loopcb_t *, i);
if(cb->poll)
cb->poll(cb->opaque);
}
}
static void loop_timer_callback(struct loop_t *lo)
{
for(int i=0; i<lo->callback->len; i++) {
struct loopcb_t *cb = g_array_index(lo->callback, struct loopcb_t *, i);
if(cb->timer)
cb->timer(cb->opaque);
}
}
static void *loop_proc(void *base)
{
struct loop_t *lo = (struct loop_t *)base;
#ifdef USE_PRCTL_SET_THREAD_NAME
prctl(PR_SET_NAME, lo->thread_name);
#endif
DEBUG_PRINTF("%s task enter success!\n", lo->thread_name);
while(lo->is_run) {
lo->poll_timeout = 2000;
g_array_set_size(lo->gpollfds, 0);
loop_prepare_callback(lo);
int r = poll((struct pollfd *)lo->gpollfds->data, lo->gpollfds->len, lo->poll_timeout);
if(r < 0) {
if(errno == EINTR)
continue;
ERROR_PRINTF("%s poll error\n", lo->thread_name);
break;
} else if(r == 0) {
loop_timer_callback(lo);
} else {
loop_poll_callback(lo);
}
lo->timer_cnt = clock()/(CLOCKS_PER_SEC/1000);
}
lo->is_run = 0;
DEBUG_PRINTF("%s task exit!\n", lo->thread_name);
return NULL;
}
int loop_add_poll(struct loop_t *lo, int fd, int events)
{
struct pollfd pfd = {
.fd = fd,
.events = events,
};
int idx = lo->gpollfds->len;
g_array_append_val(lo->gpollfds, pfd);
return idx;
}
int loop_get_revents(struct loop_t *lo, int idx)
{
return g_array_index(lo->gpollfds, struct pollfd, idx).revents;
}
void loop_register(struct loop_t *lo, const struct loopcb_t *cb)
{
g_array_append_val(lo->callback, cb);
}
int loop_init(struct loop_t *lo)
{
lo->thread_name = LOG_NAME;
lo->gpollfds = g_array_new(FALSE, FALSE, sizeof(struct pollfd));
if(!lo->gpollfds) {
ERROR_PRINTF("g array new err\n");
goto err0;
}
lo->callback = g_array_new(FALSE, FALSE, sizeof(struct loopcb_t *));
if(!lo->callback) {
ERROR_PRINTF("g array new err\n");
goto err1;
}
return 0;
g_array_free(lo->callback, TRUE);
lo->callback = NULL;
err1:
g_array_free(lo->gpollfds, TRUE);
lo->gpollfds = NULL;
err0:
return -1;
}
int loop_start(struct loop_t *lo)
{
lo->is_run = 1;
if(pthread_create(&lo->thread_id, 0, loop_proc, lo) < 0) {
ERROR_PRINTF("create thread err\n");
return -1;
}
return 0;
}
int loop_exit(struct loop_t *lo)
{
if (lo->is_run == 1) {
lo->is_run = 0;
pthread_join(lo->thread_id, 0);
} else {
ERROR_PRINTF("%s stop failed!\n", lo->thread_name);
return -1;
}
if(lo->gpollfds)
g_array_free(lo->gpollfds, TRUE);
if(lo->callback)
g_array_free(lo->callback, TRUE);
return 0;
}
struct loop_t loop_default;