Skip to content

Commit 4426314

Browse files
committed
impr read hook
1 parent ce4c028 commit 4426314

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

corona-c/corona.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ static void crn_gc_start_proc() {
14941494
// linfo2("wow machine thread gc %d\n", nochkid);
14951495
}
14961496

1497-
crn_machine_check_allpark();
1497+
// crn_machine_check_allpark();
14981498
}
14991499
static void crn_gc_stop_proc() {
15001500
// linfo2("gc finished %d\n", gettid());
@@ -1766,7 +1766,8 @@ void crn_init_intern() {
17661766
// GC_set_on_thread_event(crn_gc_on_thread_event);
17671767
GC_allow_register_threads();
17681768
// GC_use_threads_discovery(); // depcreated
1769-
GC_INIT();
1769+
if (!GC_is_init_called()) { GC_INIT(); }
1770+
assert(GC_is_init_called()>0);
17701771
// linfo("main thread registered: %d\n", GC_thread_is_registered()); // yes
17711772
// linfo("gcfreq=%d\n", GC_get_full_freq()); // 19
17721773
// GC_set_full_freq(5);

corona-c/hook.c

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -238,24 +238,66 @@ int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
238238
assert(1==2); // unreachable
239239
}
240240

241+
static int crn_fd_would_blocking(int fd, int iswr) {
242+
243+
fd_set read_fds;
244+
struct timeval timeout = {.tv_sec=0, .tv_usec=0};
245+
int fd_stdin = fd;//fileno(stdin); // Get file descriptor for stdin
246+
int activity;
247+
248+
// Clear the file descriptor set
249+
FD_ZERO(&read_fds);
250+
// Add stdin to the set
251+
FD_SET(fd_stdin, &read_fds);
252+
253+
// Wait for activity on the file descriptor
254+
if (iswr) {
255+
activity = select_f(fd_stdin + 1, NULL, &read_fds, NULL, &timeout);
256+
}else{
257+
activity = select_f(fd_stdin + 1, &read_fds, NULL, NULL, &timeout);
258+
}
259+
// printf("activity=%d, %s\n", activity, strerror(activity));
260+
if (activity < 0) {
261+
perror("select error");
262+
return activity;
263+
} else if (activity == 0) {
264+
printf("Timeout occurred (5 seconds). Still waiting...\n");
265+
} else {
266+
return 0; // nonblocking
267+
}
268+
return 1;
269+
}
270+
241271
ssize_t read(int fd, void *buf, size_t count)
242272
{
243273
if (!read_f) initHook();
244274
if (!crn_in_procer()) return read_f(fd, buf, count);
245275

246-
// linfo("%d fdnb=%d bufsz=%d\n", fd, fd_is_nonblocking(fd), count);
247-
fdcontext* ctx = hookcb_get_fdcontext(fd);
248-
if (ctx == nilptr) {
249-
linfo("wtf, why not found fd %d\n", fd);
250-
assert(1==2);
251-
}
252-
bool isfile = fdcontext_is_file(ctx);
253-
if (fd_is_nonblocking(fd) == 0 && !isfile) {
254-
linfo("%d fdnb=%d bufsz=%d\n", fd, fd_is_nonblocking(fd), count);
255-
assert(fd_is_nonblocking(fd) == 1);
256-
}
276+
// // linfo("%d fdnb=%d bufsz=%d\n", fd, fd_is_nonblocking(fd), count);
277+
// fdcontext* ctx = hookcb_get_fdcontext(fd);
278+
// if (ctx == nilptr) {
279+
// lwarn("wtf, why not found fd %d\n", fd);
280+
// assert(0);
281+
// }
282+
// bool isfile = fdcontext_is_file(ctx);
283+
// if (fd_is_nonblocking(fd) == 0 && !isfile) {
284+
// linfo("%d fdnb=%d bufsz=%d\n", fd, fd_is_nonblocking(fd), count);
285+
// assert(fd_is_nonblocking(fd) == 1);
286+
// }
257287
//
258288
while (1){
289+
int beblk = crn_fd_would_blocking(fd, 0);
290+
if (beblk) {
291+
bool inpoll = hookcb_getin_poll(fd, true);
292+
// linfo("read yeild %d n %d nb %d inpoll %d\n", fd, count, fd_is_nonblocking(fd), inpoll);
293+
if (inpoll) { // dont yeild inpoll fd read
294+
// hookcb_setin_poll(fd, false, true); // cannot clear flag, or unexpected yeild/suspend
295+
// return rv;
296+
}
297+
crn_procer_yield(fd, YIELD_TYPE_READ);
298+
continue;
299+
}
300+
259301
ssize_t rv = read_f(fd, buf, count);
260302
int eno = rv < 0 ? errno : 0;
261303
if (rv >= 0) {

0 commit comments

Comments
 (0)