Skip to content

Commit

Permalink
Use syscall other dysym to avoid being blocked when use tcmalloc to d…
Browse files Browse the repository at this point in the history
…ebug app.
  • Loading branch information
zhengshuxin committed Oct 30, 2023
1 parent 3b9fc38 commit d4dfe8c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib_fiber/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ ifeq ($(findstring Linux, $(UNIXNAME)), Linux)
ifeq ($(debug_stack), yes)
CFLAGS += -DDEBUG_STACK
endif

ifeq ($(USE_TCMALLOC), yes)
CFLAGS += -DUSE_TCMALLOC
endif
ifeq ($(USE_TCMALLOC), YES)
CFLAGS += -DUSE_TCMALLOC
endif
endif

# For CYGWIN
Expand Down
45 changes: 41 additions & 4 deletions lib_fiber/c/src/hook/io.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#include "stdafx.h"

#if defined(SYS_UNIX) && defined(USE_TCMALLOC)
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#endif

#include "common.h"

#include "fiber.h"
Expand Down Expand Up @@ -39,24 +46,34 @@ int WINAPI acl_fiber_close(socket_t fd)
FILE_EVENT *fe;
EVENT *ev;


# ifndef USE_TCMALLOC
if (sys_close == NULL) {
hook_once();
if (sys_close == NULL) {
msg_error("%s: sys_close NULL", __FUNCTION__);
return -1;
}
}
# endif

if (!var_hook_sys_api) {
# ifdef USE_TCMALLOC
return syscall(SYS_close, fd);
# else
// In thread mode, we only need to free the fe, because
// no fiber was bound with the fe.
fe = fiber_file_get(fd);
if (fe) {
fiber_file_free(fe);
}

return (*sys_close)(fd);
# endif
}

# ifdef USE_TCMALLOC
if (sys_close == NULL) {
hook_once();
}
# endif

if (IS_INVALID(fd)) {
msg_error("%s(%d): invalid fd=%u", __FUNCTION__, __LINE__, fd);
return -1;
Expand Down Expand Up @@ -131,13 +148,23 @@ ssize_t acl_fiber_read(socket_t fd, void *buf, size_t count)
return -1;
}

# ifdef USE_TCMALLOC
if (!var_hook_sys_api) {
return syscall(SYS_read, fd, buf, count);
}

if (sys_read == NULL) {
hook_once();
}
# else
if (sys_read == NULL) {
hook_once();
}

if (!var_hook_sys_api) {
return (*sys_read)(fd, buf, count);
}
# endif

fe = fiber_file_open(fd);
return fiber_read(fe, buf, count);
Expand Down Expand Up @@ -293,13 +320,23 @@ ssize_t acl_fiber_write(socket_t fd, const void *buf, size_t count)
return -1;
}

# ifdef USE_TCMALLOC
if (!var_hook_sys_api) {
return syscall(SYS_write, fd, buf, count);
}

if (sys_write == NULL) {
hook_once();
}
# else
if (sys_write == NULL) {
hook_once();
}

if (!var_hook_sys_api) {
return (*sys_write)(fd, buf, count);
}
# endif

fe = fiber_file_open(fd);
return fiber_write(fe, buf, count);
Expand Down

0 comments on commit d4dfe8c

Please sign in to comment.