Skip to content

Commit f92fc15

Browse files
committed
bugfix: in freebsd, libthr may call calloc before globalinit
1 parent c10e541 commit f92fc15

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

platform.mk

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ linux freebsd : SKYNET_LIBS += -lrt
3333

3434
# Turn off jemalloc and malloc hook on macosx and freebsd
3535

36-
freebsd macosx : MALLOC_STATICLIB :=
37-
freebsd macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC
36+
macosx : MALLOC_STATICLIB :=
37+
macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC
3838

3939
linux macosx freebsd :
4040
$(MAKE) all PLAT=$@ SKYNET_LIBS="$(SKYNET_LIBS)" SHARED="$(SHARED)" EXPORT="$(EXPORT)" MALLOC_STATICLIB="$(MALLOC_STATICLIB)" SKYNET_DEFINES="$(SKYNET_DEFINES)"

skynet-src/skynet_server.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ struct skynet_context {
5252

5353
struct skynet_node {
5454
int total;
55+
int init;
5556
uint32_t monitor_exit;
5657
pthread_key_t handle_key;
5758
};
@@ -75,8 +76,13 @@ context_dec() {
7576

7677
uint32_t
7778
skynet_current_handle(void) {
78-
void * handle = pthread_getspecific(G_NODE.handle_key);
79-
return (uint32_t)(uintptr_t)handle;
79+
if (G_NODE.init) {
80+
void * handle = pthread_getspecific(G_NODE.handle_key);
81+
return (uint32_t)(uintptr_t)handle;
82+
} else {
83+
uintptr_t v = (uint32_t)(-THREAD_MAIN);
84+
return v;
85+
}
8086
}
8187

8288
static void
@@ -650,6 +656,7 @@ void
650656
skynet_globalinit(void) {
651657
G_NODE.total = 0;
652658
G_NODE.monitor_exit = 0;
659+
G_NODE.init = 1;
653660
if (pthread_key_create(&G_NODE.handle_key, NULL)) {
654661
fprintf(stderr, "pthread_key_create failed");
655662
exit(1);

0 commit comments

Comments
 (0)