Skip to content

Commit

Permalink
optimize code [4]
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Feb 23, 2024
1 parent f10f815 commit 19568ef
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 75 deletions.
4 changes: 2 additions & 2 deletions include/swoole.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ struct Address;
} // namespace network
class AsyncThreads;
#ifdef SW_USE_IOURING
class AsyncIOUring;
class AsyncIouring;
#endif
namespace async {
class ThreadPool;
Expand Down Expand Up @@ -668,7 +668,7 @@ struct ThreadGlobal {
Timer *timer;
AsyncThreads *async_threads;
#ifdef SW_USE_IOURING
AsyncIOUring *async_iouring;
AsyncIouring *async_iouring;
#endif
uint32_t signal_listener_num;
uint32_t co_signal_listener_num;
Expand Down
6 changes: 3 additions & 3 deletions include/swoole_async.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class AsyncThreads {
};

#ifdef SW_USE_IOURING
class AsyncIOUring {
class AsyncIouring {
private:
int ring_fd;
uint64_t task_num = 0;
Expand Down Expand Up @@ -165,8 +165,8 @@ class AsyncIOUring {
}

public:
AsyncIOUring(Reactor *reactor_);
~AsyncIOUring();
AsyncIouring(Reactor *reactor_);
~AsyncIouring();

enum opcodes {
SW_IORING_OP_OPENAT = IORING_OP_OPENAT,
Expand Down
4 changes: 2 additions & 2 deletions include/swoole_coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ namespace coroutine {
bool async(async::Handler handler, AsyncEvent &event, double timeout = -1);
bool async(const std::function<void(void)> &fn, double timeout = -1);
#ifdef SW_USE_IOURING
int async(AsyncIOUring::opcodes opcode,
int async(AsyncIouring::opcodes opcode,
const char *pathname,
const char *pathname2 = nullptr,
mode_t mode = 0,
int flags = 0,
struct statx *statxbuf = nullptr,
double timeout = -1);
int async(AsyncIOUring::opcodes opcode,
int async(AsyncIouring::opcodes opcode,
int fd,
void *rbuf = nullptr,
const void *wbuf = nullptr,
Expand Down
4 changes: 2 additions & 2 deletions include/swoole_file_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#ifdef SW_USE_IOURING
#define open(pathname, flags, mode) swoole_coroutine_iouring_open(pathname, flags, mode)
#define _close(fd) swoole_coroutine_iouring_close_file(fd)
#define close_file(fd) swoole_coroutine_iouring_close_file(fd)
#define read(fd, buf, count) swoole_coroutine_iouring_read(fd, buf, count)
#define write(fd, buf, count) swoole_coroutine_iouring_write(fd, buf, count)
#define rename(oldpath, newpath) swoole_coroutine_iouring_rename(oldpath, newpath)
Expand All @@ -35,7 +35,7 @@
#define fdatasync(fd) swoole_coroutine_iouring_fdatasync(fd)
#else
#define open(pathname, flags, mode) swoole_coroutine_open(pathname, flags, mode)
#define _close(fd) swoole_coroutine_close_file(fd)
#define close_file(fd) swoole_coroutine_close_file(fd)
#define read(fd, buf, count) swoole_coroutine_read(fd, buf, count)
#define write(fd, buf, count) swoole_coroutine_write(fd, buf, count)
#define lseek(fd, offset, whence) swoole_coroutine_lseek(fd, offset, whence)
Expand Down
33 changes: 13 additions & 20 deletions src/coroutine/iouring.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "swoole_coroutine_system.h"

#ifdef SW_USE_IOURING
using swoole::AsyncIOUring;
using swoole::AsyncIouring;
using swoole::Coroutine;
using swoole::coroutine::async;

Expand All @@ -33,51 +33,44 @@ int swoole_coroutine_iouring_open(const char *pathname, int flags, mode_t mode)
if (sw_unlikely(is_no_coro())) {
return open(pathname, flags, mode);
}
return async(AsyncIOUring::SW_IORING_OP_OPENAT, pathname, nullptr, mode, flags);
}

int swoole_coroutine_iouring_close_file(int fd) {
if (sw_unlikely(is_no_coro())) {
return close(fd);
}
return async(AsyncIOUring::SW_IORING_OP_CLOSE, fd);
return async(AsyncIouring::SW_IORING_OP_OPENAT, pathname, nullptr, mode, flags);
}

ssize_t swoole_coroutine_iouring_read(int sockfd, void *buf, size_t count) {
if (sw_unlikely(is_no_coro())) {
return read(sockfd, buf, count);
}

return async(AsyncIOUring::SW_IORING_OP_READ, sockfd, buf, nullptr, nullptr, count);
return async(AsyncIouring::SW_IORING_OP_READ, sockfd, buf, nullptr, nullptr, count);
}

ssize_t swoole_coroutine_iouring_write(int sockfd, const void *buf, size_t count) {
if (sw_unlikely(is_no_coro())) {
return write(sockfd, buf, count);
}

return async(AsyncIOUring::SW_IORING_OP_WRITE, sockfd, nullptr, buf, nullptr, count);;
return async(AsyncIouring::SW_IORING_OP_WRITE, sockfd, nullptr, buf, nullptr, count);;
}

int swoole_coroutine_iouring_rename(const char *oldpath, const char *newpath) {
if (sw_unlikely(is_no_coro())) {
return rename(oldpath, newpath);
}
return async(AsyncIOUring::SW_IORING_OP_RENAMEAT, oldpath, newpath);
return async(AsyncIouring::SW_IORING_OP_RENAMEAT, oldpath, newpath);
}

int swoole_coroutine_iouring_mkdir(const char *pathname, mode_t mode) {
if (sw_unlikely(is_no_coro())) {
return mkdir(pathname, mode);
}
return async(AsyncIOUring::SW_IORING_OP_MKDIRAT, pathname, nullptr, mode);
return async(AsyncIouring::SW_IORING_OP_MKDIRAT, pathname, nullptr, mode);
}

int swoole_coroutine_iouring_unlink(const char *pathname) {
if (sw_unlikely(is_no_coro())) {
return unlink(pathname);
}
return async(AsyncIOUring::SW_IORING_OP_UNLINK_FILE, pathname);
return async(AsyncIouring::SW_IORING_OP_UNLINK_FILE, pathname);
}

void swoole_statx_to_stat(const struct statx *statxbuf, struct stat *statbuf) {
Expand Down Expand Up @@ -105,7 +98,7 @@ int swoole_coroutine_iouring_fstat(int fd, struct stat *statbuf) {
}

struct statx statxbuf = {};
int retval = async(AsyncIOUring::SW_IORING_OP_FSTAT, fd, nullptr, nullptr, &statxbuf);
int retval = async(AsyncIouring::SW_IORING_OP_FSTAT, fd, nullptr, nullptr, &statxbuf);
swoole_statx_to_stat(&statxbuf, statbuf);
return retval;
}
Expand All @@ -116,7 +109,7 @@ int swoole_coroutine_iouring_stat(const char *path, struct stat *statbuf) {
}

struct statx statxbuf = {};
int retval = async(AsyncIOUring::SW_IORING_OP_LSTAT, path, nullptr, 0, 0, &statxbuf);
int retval = async(AsyncIouring::SW_IORING_OP_LSTAT, path, nullptr, 0, 0, &statxbuf);
swoole_statx_to_stat(&statxbuf, statbuf);
return retval;
}
Expand All @@ -127,7 +120,7 @@ int swoole_coroutine_iouring_lstat(const char *path, struct stat *statbuf) {
}

struct statx statxbuf = {};
int retval = async(AsyncIOUring::SW_IORING_OP_LSTAT, path, nullptr, 0, 0, &statxbuf);
int retval = async(AsyncIouring::SW_IORING_OP_LSTAT, path, nullptr, 0, 0, &statxbuf);
swoole_statx_to_stat(&statxbuf, statbuf);
return retval;
}
Expand All @@ -137,22 +130,22 @@ int swoole_coroutine_iouring_rmdir(const char *pathname) {
return rmdir(pathname);
}

return async(AsyncIOUring::SW_IORING_OP_UNLINK_DIR, pathname);
return async(AsyncIouring::SW_IORING_OP_UNLINK_DIR, pathname);
}

int swoole_coroutine_iouring_fsync(int fd) {
if (sw_unlikely(is_no_coro())) {
return fsync(fd);
}

return async(AsyncIOUring::SW_IORING_OP_FSYNC, fd);
return async(AsyncIouring::SW_IORING_OP_FSYNC, fd);
}

int swoole_coroutine_iouring_fdatasync(int fd) {
if (sw_unlikely(is_no_coro())) {
return fdatasync(fd);
}

return async(AsyncIOUring::SW_IORING_OP_FDATASYNC, fd);
return async(AsyncIouring::SW_IORING_OP_FDATASYNC, fd);
}
#endif
32 changes: 16 additions & 16 deletions src/coroutine/system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ void System::init_reactor(Reactor *reactor) {

reactor->set_handler(SW_FD_AIO | SW_EVENT_READ, AsyncThreads::callback);
#ifdef SW_USE_IOURING
reactor->set_handler(SW_FD_IOURING | SW_EVENT_READ, AsyncIOUring::callback);
reactor->set_handler(SW_FD_IOURING | SW_EVENT_READ, AsyncIouring::callback);
#endif
}

Expand Down Expand Up @@ -699,15 +699,15 @@ bool async(const std::function<void(void)> &fn, double timeout) {
}

#ifdef SW_USE_IOURING
int async(AsyncIOUring::opcodes opcode,
int async(AsyncIouring::opcodes opcode,
const char *pathname,
const char *pathname2,
mode_t mode,
int flags,
struct statx *statxbuf,
double timeout) {
if (SwooleTG.async_iouring == nullptr) {
SwooleTG.async_iouring = new AsyncIOUring(SwooleTG.reactor);
SwooleTG.async_iouring = new AsyncIouring(SwooleTG.reactor);
SwooleTG.async_iouring->add_event();
}

Expand All @@ -724,16 +724,16 @@ int async(AsyncIOUring::opcodes opcode,
event.statxbuf = statxbuf;

bool result = false;
AsyncIOUring *iouring = SwooleTG.async_iouring;
if (opcode == AsyncIOUring::SW_IORING_OP_OPENAT) {
AsyncIouring *iouring = SwooleTG.async_iouring;
if (opcode == AsyncIouring::SW_IORING_OP_OPENAT) {
result = iouring->open(&event);
} else if (opcode == AsyncIOUring::SW_IORING_OP_MKDIRAT) {
} else if (opcode == AsyncIouring::SW_IORING_OP_MKDIRAT) {
result = iouring->mkdir(&event);
} else if (opcode == AsyncIOUring::SW_IORING_OP_UNLINK_FILE || opcode == AsyncIOUring::SW_IORING_OP_UNLINK_DIR) {
} else if (opcode == AsyncIouring::SW_IORING_OP_UNLINK_FILE || opcode == AsyncIouring::SW_IORING_OP_UNLINK_DIR) {
result = iouring->unlink(&event);
} else if (opcode == AsyncIOUring::SW_IORING_OP_RENAMEAT) {
} else if (opcode == AsyncIouring::SW_IORING_OP_RENAMEAT) {
result = iouring->rename(&event);
} else if (opcode == AsyncIOUring::SW_IORING_OP_FSTAT || opcode == AsyncIOUring::SW_IORING_OP_LSTAT) {
} else if (opcode == AsyncIouring::SW_IORING_OP_FSTAT || opcode == AsyncIouring::SW_IORING_OP_LSTAT) {
result = iouring->statx(&event);
}

Expand All @@ -744,15 +744,15 @@ int async(AsyncIOUring::opcodes opcode,
return event.retval;
}

int async(AsyncIOUring::opcodes opcode,
int async(AsyncIouring::opcodes opcode,
int fd,
void *rbuf,
const void *wbuf,
struct statx *statxbuf,
size_t count,
double timeout) {
if (SwooleTG.async_iouring == nullptr) {
SwooleTG.async_iouring = new AsyncIOUring(SwooleTG.reactor);
SwooleTG.async_iouring = new AsyncIouring(SwooleTG.reactor);
SwooleTG.async_iouring->add_event();
}

Expand All @@ -769,14 +769,14 @@ int async(AsyncIOUring::opcodes opcode,
event.count = count;

bool result = false;
AsyncIOUring *iouring = SwooleTG.async_iouring;
if (opcode == AsyncIOUring::SW_IORING_OP_READ || opcode == AsyncIOUring::SW_IORING_OP_WRITE) {
AsyncIouring *iouring = SwooleTG.async_iouring;
if (opcode == AsyncIouring::SW_IORING_OP_READ || opcode == AsyncIouring::SW_IORING_OP_WRITE) {
result = iouring->wr(&event);
} else if (opcode == AsyncIOUring::SW_IORING_OP_CLOSE) {
} else if (opcode == AsyncIouring::SW_IORING_OP_CLOSE) {
result = iouring->close(&event);
} else if (opcode == AsyncIOUring::SW_IORING_OP_FSTAT) {
} else if (opcode == AsyncIouring::SW_IORING_OP_FSTAT) {
result = iouring->statx(&event);
} else if (opcode == AsyncIOUring::SW_IORING_OP_FSYNC || opcode == AsyncIOUring::SW_IORING_OP_FDATASYNC) {
} else if (opcode == AsyncIouring::SW_IORING_OP_FSYNC || opcode == AsyncIouring::SW_IORING_OP_FDATASYNC) {
result = iouring->fsync(&event);
}

Expand Down
Loading

0 comments on commit 19568ef

Please sign in to comment.