mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 10:57:34 +08:00
Optimize codes and delete unused codes.
This commit is contained in:
parent
c0df7d2aa9
commit
2b9bb710c0
@ -68,13 +68,11 @@ static void handle_poll_read(EVENT *ev, FILE_EVENT *fe, POLLFD *pfd)
|
||||
static void read_callback(EVENT *ev, FILE_EVENT *fe)
|
||||
{
|
||||
POLLFD *pfd;
|
||||
//RING_ITER iter;
|
||||
RING *iter = fe->pfds.succ, *next = iter;
|
||||
|
||||
event_del_read(ev, fe);
|
||||
SET_READABLE(fe);
|
||||
|
||||
#if 1
|
||||
// Walk througth the RING list, handle each poll event, and one RING
|
||||
// node maybe be detached after it has been handled without any poll
|
||||
// event bound with it again.
|
||||
@ -85,15 +83,6 @@ static void read_callback(EVENT *ev, FILE_EVENT *fe)
|
||||
handle_poll_read(ev, fe, pfd);
|
||||
}
|
||||
}
|
||||
#else
|
||||
ring_foreach(iter, &fe->pfds) {
|
||||
pfd = ring_to_appl(iter.ptr, POLLFD, me);
|
||||
if (pfd->pfd->events & POLLIN) {
|
||||
handle_poll_read(ev, fe, pfd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@ -134,13 +123,11 @@ static void handle_poll_write(EVENT *ev, FILE_EVENT *fe, POLLFD *pfd)
|
||||
static void write_callback(EVENT *ev, FILE_EVENT *fe)
|
||||
{
|
||||
POLLFD *pfd;
|
||||
//RING_ITER iter;
|
||||
RING *iter = fe->pfds.succ, *next = iter;
|
||||
|
||||
event_del_write(ev, fe);
|
||||
SET_WRITABLE(fe);
|
||||
|
||||
#if 1
|
||||
for (; iter != &fe->pfds; iter = next) {
|
||||
next = next->succ;
|
||||
pfd = ring_to_appl(iter, POLLFD, me);
|
||||
@ -148,15 +135,6 @@ static void write_callback(EVENT *ev, FILE_EVENT *fe)
|
||||
handle_poll_write(ev, fe, pfd);
|
||||
}
|
||||
}
|
||||
#else
|
||||
ring_foreach(iter, &fe->pfds) {
|
||||
pfd = ring_to_appl(iter.ptr, POLLFD, me);
|
||||
if (pfd->pfd->events & POLLOUT) {
|
||||
handle_poll_write(ev, fe, pfd);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@ -247,18 +225,6 @@ static void poll_event_clean(EVENT *ev, POLL_EVENT *pe)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This callback will be called from event_process_poll() in event.c and the
|
||||
* fiber blocked after calling acl_fiber_switch() in acl_fiber_poll() will
|
||||
* wakeup and continue to run.
|
||||
*/
|
||||
static void poll_callback(EVENT *ev fiber_unused, POLL_EVENT *pe)
|
||||
{
|
||||
if (pe->fiber->status != FIBER_STATUS_READY) {
|
||||
acl_fiber_ready(pe->fiber);
|
||||
}
|
||||
}
|
||||
|
||||
static POLLFD *pollfd_alloc(POLL_EVENT *pe, struct pollfd *fds, nfds_t nfds)
|
||||
{
|
||||
POLLFD *pfds = (POLLFD *) mem_malloc(nfds * sizeof(POLLFD));
|
||||
@ -328,6 +294,18 @@ static void pollfds_copy(struct pollfd *fds, const pollfds *pfds)
|
||||
memcpy(fds, pfds->fds, sizeof(struct pollfd) * pfds->nfds);
|
||||
}
|
||||
|
||||
/**
|
||||
* This callback will be called from event_process_poll() in event.c and the
|
||||
* fiber blocked after calling acl_fiber_switch() in acl_fiber_poll() will
|
||||
* wakeup and continue to run.
|
||||
*/
|
||||
static void poll_callback(EVENT *ev fiber_unused, POLL_EVENT *pe)
|
||||
{
|
||||
if (pe->fiber->status != FIBER_STATUS_READY) {
|
||||
acl_fiber_ready(pe->fiber);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // SHARE_STACK
|
||||
|
||||
int WINAPI acl_fiber_poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
||||
@ -363,6 +341,8 @@ int WINAPI acl_fiber_poll(struct pollfd *fds, nfds_t nfds, int timeout)
|
||||
old_timeout = ev->timeout;
|
||||
|
||||
#ifdef SHARE_STACK
|
||||
// In shared stack mode, the fds input must be save to the dynamic
|
||||
// memory to avoid memory collision accessed by different fibers.
|
||||
if (curr->oflag & ACL_FIBER_ATTR_SHARE_STACK) {
|
||||
pfds = pollfds_save(fds, nfds);
|
||||
pe = (POLL_EVENT *) mem_malloc(sizeof(POLL_EVENT));
|
||||
|
101
lib_fiber/unit_test/io/poll.cpp
Normal file
101
lib_fiber/unit_test/io/poll.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
#include "stdafx.h"
|
||||
#include "test_io.h"
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/eventfd.h>
|
||||
|
||||
static int read_wait(int fd, int timeo)
|
||||
{
|
||||
struct pollfd pfd;
|
||||
|
||||
printf(">>>%s: fiber-%d, pfd=%p\r\n", __FUNCTION__, acl::fiber::self(), &pfd);
|
||||
memset(&pfd, 0, sizeof(pfd));
|
||||
pfd.fd = fd;
|
||||
pfd.events = POLLIN;
|
||||
|
||||
int n = poll(&pfd, 1, timeo * 1000);
|
||||
if (n < 0) {
|
||||
printf("poll error: %s\r\n", acl::last_serror());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (n == 0) {
|
||||
printf("poll read timeout: %s\r\n", acl::last_serror());
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("%s: fd=%d is readable!\r\n", __FUNCTION__, fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool fiber_read(int fd, long long& out)
|
||||
{
|
||||
if (read_wait(fd, 2) <= 0) {
|
||||
printf("read_wait error for fd=%d\r\n", fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
long long n;
|
||||
ssize_t ret = read(fd, &n, sizeof(n));
|
||||
if (ret != sizeof(n)) {
|
||||
printf("read from eventfd %d error %s\r\n",
|
||||
fd, acl::last_serror());
|
||||
return false;
|
||||
} else {
|
||||
out = n;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
#endif // __linux__
|
||||
|
||||
int test_poll(AUT_LINE *test_line acl_unused, void *arg acl_unused)
|
||||
{
|
||||
#ifdef __linux__
|
||||
int fd = eventfd(0, 0);
|
||||
|
||||
if (fd == -1) {
|
||||
printf("create eventfd error %s\r\n", acl::last_serror());
|
||||
return -1;
|
||||
}
|
||||
|
||||
long long out = 0, in = 1000000;
|
||||
int shared_stack = 0;
|
||||
|
||||
AUT_INT(test_line, "shared_stack", shared_stack, 0);
|
||||
|
||||
if (shared_stack) {
|
||||
printf(">>>fiber's stack shared\r\n");
|
||||
go_share(8000) [=, &out] {
|
||||
(void) fiber_read(fd, out);
|
||||
};
|
||||
} else {
|
||||
printf(">>>fiber's stack no-shared\r\n");
|
||||
go[=, &out] {
|
||||
(void) fiber_read(fd, out);
|
||||
};
|
||||
}
|
||||
|
||||
go[=] {
|
||||
long long n = in;
|
||||
ssize_t ret = write(fd, &n, sizeof(n));
|
||||
if (ret != sizeof(n)) {
|
||||
printf("write to eventfd %d error %s\r\n",
|
||||
fd, acl::last_serror());
|
||||
}
|
||||
};
|
||||
|
||||
acl::fiber::schedule();
|
||||
|
||||
if (out == in) {
|
||||
printf("Ok, the result read from eventfd: %lld\r\n", out);
|
||||
return 0;
|
||||
} else {
|
||||
printf("Err, the result is %lld, but need %lld\r\n", out, in);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
printf("eventfd only be supported on Linux\r\n");
|
||||
return 0;
|
||||
#endif
|
||||
}
|
@ -5,3 +5,6 @@ void io_register(void);
|
||||
|
||||
/* In eventfd.cpp */
|
||||
int test_eventfd(AUT_LINE *test_line, void *arg);
|
||||
|
||||
/* In poll.cpp */
|
||||
int test_poll(AUT_LINE *test_line, void *arg);
|
||||
|
@ -6,5 +6,8 @@ static AUT_FN_ITEM __test_fn_tab[] = {
|
||||
/* In eventfd.cpp */
|
||||
{ "test_eventfd", "test_eventfd", test_eventfd, NULL, 0 },
|
||||
|
||||
/* In poll.cpp */
|
||||
{ "test_poll", "test_poll", test_poll, NULL, 0 },
|
||||
|
||||
{ NULL, NULL, NULL, NULL, 0 },
|
||||
};
|
||||
|
@ -9,3 +9,5 @@ tbox_mixed_consume|0|0|threads_consumer=2,threads_producer=2,threads_consumer_al
|
||||
file_load|0|0|filename=main.cpp
|
||||
file_load|0|0|filename=Makefile.in,show=1
|
||||
test_eventfd|0|0|
|
||||
test_poll|0|0|shared_stack=0
|
||||
test_poll|0|0|shared_stack=1
|
||||
|
Loading…
Reference in New Issue
Block a user