reformat code and job array

This commit is contained in:
Rongsong Shen 2025-11-07 16:14:27 +08:00
parent a665ccd4fe
commit 9ff857db9f
64 changed files with 1497 additions and 2476 deletions

View file

@ -17,10 +17,7 @@
static int iomgr_update_poll(iomgr_t*, endpoint_t*, int);
static void endpoint_fini(endpoint_t*);
static int socket_create(int domain,
int type,
int port,
int backlog)
static int socket_create(int domain, int type, int port, int backlog)
{
int sock;
int opt = 1;
@ -38,7 +35,9 @@ static int socket_create(int domain,
fcntl(sock, F_SETFD, (fcntl(sock, F_GETFD) | FD_CLOEXEC));
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt,
sizeof(opt))
< 0) {
close(sock);
return -1;
}
@ -79,9 +78,7 @@ static int socket_create(int domain,
return sock;
}
static int socket_connect(int sock,
char* host,
int port)
static int socket_connect(int sock, char* host, int port)
{
struct sockaddr_in server;
struct hostent* hp;
@ -130,9 +127,7 @@ static int socket_accept(int sock)
return s;
}
int netio_write(int fd,
char* data,
int sz)
int netio_write(int fd, char* data, int sz)
{
int nc = 0;
@ -152,9 +147,7 @@ int netio_write(int fd,
return nc;
}
int netio_read(int fd,
char* data,
int sz)
int netio_read(int fd, char* data, int sz)
{
int nc = 0;
@ -182,13 +175,11 @@ int netio_read(int fd,
return nc;
}
int write_packet(int fd,
io_packet_t* pkt)
int write_packet(int fd, io_packet_t* pkt)
{
int rc;
rc = netio_write(fd, (char*)&(pkt->sz),
sizeof(pkt->sz));
rc = netio_write(fd, (char*)&(pkt->sz), sizeof(pkt->sz));
if (rc < 0) {
return -errno;
}
@ -217,13 +208,11 @@ int alloc_packet(io_packet_t* pkt)
return 1;
}
int read_packet(int fd,
io_packet_t* pkt)
int read_packet(int fd, io_packet_t* pkt)
{
int rc;
rc = netio_read(fd, (char*)&(pkt->sz),
sizeof(pkt->sz));
rc = netio_read(fd, (char*)&(pkt->sz), sizeof(pkt->sz));
if (rc < 0) {
return rc;
}
@ -260,8 +249,7 @@ static void* io_worker(void* args)
int i;
struct epoll_event events[MAX_EPOLL_EVENTS];
nfds = epoll_wait(iomgr->epoll_fd, events,
MAX_EPOLL_EVENTS, -1);
nfds = epoll_wait(iomgr->epoll_fd, events, MAX_EPOLL_EVENTS, -1);
if (nfds == -1) {
if (errno == EINTR) {
continue;
@ -326,11 +314,7 @@ static void outq_item_killer(data_item_t* it)
ioctx_deref(ctx, 1);
}
}
int endpoint_init(endpoint_t* ep,
int domain,
int type,
int port,
int backlog)
int endpoint_init(endpoint_t* ep, int domain, int type, int port, int backlog)
{
int fd = -1;
@ -340,8 +324,7 @@ int endpoint_init(endpoint_t* ep,
return -EINVAL;
}
fd = socket_create(domain, type,
port, backlog);
fd = socket_create(domain, type, port, backlog);
if (fd < 0) {
return -errno;
}
@ -354,8 +337,7 @@ int endpoint_init(endpoint_t* ep,
ep->busy = 0;
ep->needfree = 0;
dataq_init(&(ep->outq),
outq_item_killer);
dataq_init(&(ep->outq), outq_item_killer);
mylock_init(&(ep->lock));
endpoint_ref(ep);
@ -363,9 +345,7 @@ int endpoint_init(endpoint_t* ep,
return fd;
}
int endpoint_connect(endpoint_t* ep,
char* host,
int port)
int endpoint_connect(endpoint_t* ep, char* host, int port)
{
int rc = -1;
@ -378,8 +358,7 @@ int endpoint_connect(endpoint_t* ep,
return rc;
}
int endpoint_accept(endpoint_t* ep,
endpoint_t* ep2)
int endpoint_accept(endpoint_t* ep, endpoint_t* ep2)
{
int sock = -1;
@ -394,9 +373,7 @@ int endpoint_accept(endpoint_t* ep,
return sock;
}
int endpoint_set_handler(endpoint_t* ep,
msghandler_t h,
void* owner)
int endpoint_set_handler(endpoint_t* ep, msghandler_t h, void* owner)
{
if (ep->fd < 0) {
SET_ERROR(EINVAL);
@ -412,8 +389,7 @@ int endpoint_set_handler(endpoint_t* ep,
return 1;
}
int endpoint_enq(endpoint_t* ep,
data_item_t* it)
int endpoint_enq(endpoint_t* ep, data_item_t* it)
{
int rc = 0;
int flags;
@ -433,8 +409,7 @@ int endpoint_enq(endpoint_t* ep,
int rc1;
mylock_lock(&(ep->iomgr->lock));
rc1 = iomgr_update_poll(ep->iomgr,
ep, flags);
rc1 = iomgr_update_poll(ep->iomgr, ep, flags);
mylock_unlock(&(ep->iomgr->lock));
if (rc1 >= 0) {
@ -468,8 +443,7 @@ data_item_t* endpoint_deq(endpoint_t* ep)
int rc;
mylock_lock(&(ep->iomgr->lock));
rc = iomgr_update_poll(ep->iomgr,
ep, flags);
rc = iomgr_update_poll(ep->iomgr, ep, flags);
mylock_unlock(&(ep->iomgr->lock));
if (rc >= 0) {
@ -519,8 +493,7 @@ uint64_t endpoint_getseq(endpoint_t* ep)
void endpoint_block_tasks(endpoint_t* ep)
{
TRACE("%s[%ld] block tasks for endpoint %p",
__func__, pthread_self(), ep);
TRACE("%s[%ld] block tasks for endpoint %p", __func__, pthread_self(), ep);
mylock_lock(&(ep->lock));
ep->busy = 1;
mylock_unlock(&(ep->lock));
@ -528,8 +501,8 @@ void endpoint_block_tasks(endpoint_t* ep)
void endpoint_unblock_tasks(endpoint_t* ep)
{
TRACE("%s[%ld] unblock tasks for endpoint %p",
__func__, pthread_self(), ep);
TRACE("%s[%ld] unblock tasks for endpoint %p", __func__, pthread_self(),
ep);
mylock_lock(&(ep->lock));
ep->busy = 0;
mylock_unlock(&(ep->lock));
@ -543,9 +516,8 @@ int endpoint_is_task_blocked(endpoint_t* ep)
busy = ep->busy;
mylock_unlock(&(ep->lock));
TRACE("%s[%ld] endpoint %p task %s",
__func__, pthread_self(),
ep, (busy != 0) ? "blocked" : "not blocked");
TRACE("%s[%ld] endpoint %p task %s", __func__, pthread_self(), ep,
(busy != 0) ? "blocked" : "not blocked");
return (busy != 0);
}
@ -583,22 +555,17 @@ int iomgr_init(iomgr_t* iomgr)
return 0;
}
static int iomgr_update_poll(iomgr_t* iomgr,
endpoint_t* ep,
int flags)
static int iomgr_update_poll(iomgr_t* iomgr, endpoint_t* ep, int flags)
{
struct epoll_event ev;
ev.events = flags;
ev.data.ptr = ep;
return epoll_ctl(iomgr->epoll_fd,
EPOLL_CTL_MOD,
ep->fd, &ev);
return epoll_ctl(iomgr->epoll_fd, EPOLL_CTL_MOD, ep->fd, &ev);
}
int iomgr_monitor(iomgr_t* iomgr,
endpoint_t* ep)
int iomgr_monitor(iomgr_t* iomgr, endpoint_t* ep)
{
struct epoll_event ev;
int rc;
@ -611,8 +578,7 @@ int iomgr_monitor(iomgr_t* iomgr,
mylock_lock(&(ep->lock));
ep->iomgr = iomgr;
mylock_lock(&(iomgr->lock));
rc = epoll_ctl(iomgr->epoll_fd, EPOLL_CTL_ADD,
ep->fd, &ev);
rc = epoll_ctl(iomgr->epoll_fd, EPOLL_CTL_ADD, ep->fd, &ev);
mylock_unlock(&(iomgr->lock));
if (rc >= 0) {
@ -624,14 +590,12 @@ int iomgr_monitor(iomgr_t* iomgr,
return rc;
}
int iomgr_unmonitor(iomgr_t* iomgr,
endpoint_t* ep)
int iomgr_unmonitor(iomgr_t* iomgr, endpoint_t* ep)
{
int rc;
mylock_lock(&(iomgr->lock));
rc = epoll_ctl(iomgr->epoll_fd, EPOLL_CTL_DEL,
ep->fd, NULL);
rc = epoll_ctl(iomgr->epoll_fd, EPOLL_CTL_DEL, ep->fd, NULL);
if (rc < 0) {
SET_ERROR(errno);
@ -650,10 +614,7 @@ int iomgr_start(iomgr_t* iomgr)
return rc;
}
int iomgr_fini(iomgr_t* iomgr)
{
return 0;
}
int iomgr_fini(iomgr_t* iomgr) { return 0; }
int iomgr_stop(iomgr_t* iomgr)
{