Skip to content

Commit

Permalink
kqueue支持
Browse files Browse the repository at this point in the history
  • Loading branch information
newNcy committed Mar 29, 2024
1 parent 255897c commit fa1132f
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 80 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ endif()
add_library(ctx ctx.h)
message("${CMAKE_SYSTEM_PROCESSOR}" )
if (CMAKE_SYSTEM_PROCESSOR MATCHES "arm64")
target_sources(ctx PRIVATE arm64.asm)
target_sources(ctx PRIVATE ctx.arm64.asm)
elseif (UNIX OR MINGW)
target_sources(ctx PRIVATE ctx.S)
else()
Expand All @@ -40,10 +40,12 @@ add_library(coroutine
add_library(aio aio.c hook.c)
target_link_libraries(aio container)

if(UNIX AND NOT APPLE)
target_sources(aio PRIVATE aio_epoll.c)
if (APPLE)
target_sources(aio PRIVATE event_kquene.c)
elseif(UNIX)
target_sources(aio PRIVATE event_epoll.c)
else()
target_sources(aio PRIVATE aio_select.c)
target_sources(aio PRIVATE event_select.c)
endif()


Expand Down
4 changes: 3 additions & 1 deletion aio.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ typedef struct
list_t * writer;
} wait_info_t;

typedef struct event_ctx_t event_ctx_t;

typedef struct aio_mgr_t
{
array_t * fired_events;
map_t * wait_map;
list_t * dead;
void * event_ctx;
event_ctx_t * event_ctx;
} aio_t;

aio_t * aio_create();
Expand Down
75 changes: 0 additions & 75 deletions aio_select.c

This file was deleted.

File renamed without changes.
File renamed without changes.
72 changes: 72 additions & 0 deletions event_kquene.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

#include "array.h"
#include <sys/event.h>
#include "aio.h"
#include "container/array.h"
#include <unistd.h>
#include <stdlib.h>


typedef struct kevent kevent_t;
struct event_ctx_t
{
int fd;
kevent_t * events;
int event_count;
int event_cap;
};

event_ctx_t * event_create()
{
event_ctx_t * ctx = (event_ctx_t*)malloc(sizeof(event_ctx_t));
ctx->fd = kqueue();
ctx->event_count = 0;
ctx->event_cap = 10;
ctx->events = (kevent_t*)malloc(sizeof(kevent_t)*ctx->event_cap);
return ctx;
}

void event_destroy(event_ctx_t * ctx)
{
free(ctx->events);
free(ctx);
}

void event_add(event_ctx_t * ctx, int fd)
{
struct kevent change;
EV_SET(&change, fd, EVFILT_READ | EVFILT_WRITE | EVFILT_EXCEPT, EV_ADD, 0, 0, NULL);
kevent(ctx->fd, &change, 1,NULL, 0, NULL);
if (ctx->event_count == ctx->event_cap) {
ctx->event_cap *= 2;
ctx->events = realloc(ctx->events, sizeof(kevent_t) * ctx->event_cap);

}
ctx->event_count ++;
}

void event_del(event_ctx_t * ctx, int fd)
{
struct kevent change;
EV_SET(&change, fd, EVFILT_READ | EVFILT_WRITE | EVFILT_EXCEPT, EV_DELETE, 0, 0, NULL);
kevent(ctx->fd, &change, 1,NULL, 0, NULL);
ctx->event_count --;
}

int event_wait(event_ctx_t * ctx, array_t * fired_events, long long ms)
{
struct timespec timeout;
timeout.tv_sec = ms/1000; // 5秒
timeout.tv_nsec = (ms%1000)*1000;
int ready = kevent(ctx->fd, NULL, 0, ctx->events, ctx->event_cap, &timeout);
array_resize(fired_events, ready);
for (int i = 0; i < ready; ++ i) {
struct kevent e = ctx->events[i];
int events = 0;
if (e.filter & EVFILT_READ) events |= IO_READ;
if (e.filter & EVFILT_WRITE) events |= IO_WRITE;
if (e.filter & EVFILT_EXCEPT) events = IO_READ | IO_WRITE;
array_set(fired_events, i, encode_event(e.ident, events));
}
return ready;
}
71 changes: 71 additions & 0 deletions event_select.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "aio.h"
#include "macros.h"
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/select.h>
#endif
#include <stdlib.h>

struct event_ctx_t
{
int max_fd;
fd_set all_fds;
};

event_ctx_t * event_create()
{
event_ctx_t * ctx = (event_ctx_t*)malloc(sizeof(event_ctx_t));
ctx->max_fd = 0;
FD_ZERO(&ctx->all_fds);
return ctx;
}

void event_destroy(event_ctx_t * ctx)
{
free(ctx);
}

void event_add(event_ctx_t * ctx, int fd)
{
FD_SET(fd, &ctx->all_fds);
if (fd > ctx->max_fd) {
ctx->max_fd = fd;
}
}

void event_del(event_ctx_t * ctx, int fd)
{
FD_CLR(fd, &ctx->all_fds);
if (ctx->max_fd == fd) {
ctx->max_fd --;
while (!FD_ISSET(ctx->max_fd, &ctx->all_fds)) {
ctx->max_fd --;
}
}
}

int event_wait(event_ctx_t * ctx, array_t * fired_events, long long ms)
{
fd_set read_fds = ctx->all_fds;
fd_set write_fds = ctx->all_fds;
fd_set execpt_fds = ctx->all_fds;
struct timeval tv;
tv.tv_sec = ms/1000;
tv.tv_usec = (ms%1000)*1000;
int ready = select(ctx->max_fd + 1, &read_fds, &write_fds, &execpt_fds, ms == -1L? NULL : &tv);
array_resize(fired_events, ready);
if (ready > 0) {
for (int i = 0; i<= ctx->max_fd; ++ i) {
int events = 0;
if (FD_ISSET(i, &read_fds)) { events |= IO_READ; }
if (FD_ISSET(i, &write_fds)) { events |= IO_WRITE; }
if (FD_ISSET(i, &execpt_fds)) { events = IO_READ | IO_WRITE; }
if (events) {
array_set(fired_events, i, encode_event(i, events));
}
}
}

return ready;
}

0 comments on commit fa1132f

Please sign in to comment.