Skip to content

Commit

Permalink
Fix #11, Support OSX for Apple Darwin, macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Mar 28, 2020
1 parent f9a13a8 commit 91d530e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ DARWIN_*_DBG
LINUX_*_DBG
obj
st.pc
.idea
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ OTHER_FLAGS = -Wall
endif

ifeq ($(OS), DARWIN)
EXTRA_OBJS = $(TARGETDIR)/md_darwin.o
LD = cc
SFLAGS = -fPIC -fno-common
DSO_SUFFIX = dylib
Expand All @@ -139,8 +140,8 @@ CFLAGS += -arch ppc
LDFLAGS += -arch ppc
endif
ifeq ($(INTEL), yes)
CFLAGS += -arch i386 -arch x86_64
LDFLAGS += -arch i386 -arch x86_64
CFLAGS += -arch x86_64
LDFLAGS += -arch x86_64
endif
LDFLAGS += -dynamiclib -install_name /sw/lib/libst.$(MAJOR).$(DSO_SUFFIX) -compatibility_version $(MAJOR) -current_version $(VERSION)
OTHER_FLAGS = -Wall
Expand Down Expand Up @@ -313,7 +314,9 @@ endif
# for SRS
# disable examples for ubuntu crossbuild failed.
# @see https://github.com/winlinvip/simple-rtmp-server/issues/308
ifeq ($(OS), LINUX)
EXAMPLES =
endif

ifeq ($(OS), DARWIN)
LINKNAME = libst.$(DSO_SUFFIX)
Expand Down Expand Up @@ -369,10 +372,13 @@ $(HEADER): public.h
$(TARGETDIR)/md.o: md.S
$(CC) $(CFLAGS) -c $< -o $@

$(TARGETDIR)/md_darwin.o: md_darwin.S
$(CC) $(CFLAGS) -c $< -o $@

$(TARGETDIR)/%.o: %.c common.h md.h
$(CC) $(CFLAGS) -c $< -o $@

examples::
examples: $(SLIBRARY)
@echo Making $@
@cd $@; $(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)" OS="$(OS)" TARGETDIR="$(TARGETDIR)"

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The branch [srs](https://github.com/ossrs/state-threads/tree/srs) will be patche
- [x] Patch [st.osx10.14.build.patch](https://github.com/ossrs/srs/blob/2.0release/trunk/3rdparty/patches/6.st.osx10.14.build.patch), for osx 10.14 build.
- [x] Support macro `MD_ST_NO_ASM` to disable ASM, [#8](https://github.com/ossrs/state-threads/issues/8).
- [x] Merge patch [srs#1282](https://github.com/ossrs/srs/issues/1282#issuecomment-445539513) to support aarch64, [#9](https://github.com/ossrs/state-threads/issues/9).
- [x] Support OSX for Apple Darwin, macOS, [#11](https://github.com/ossrs/state-threads/issues/11).

## Docs

Expand Down
32 changes: 18 additions & 14 deletions md.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,30 @@
#define MD_ALWAYS_UNSERIALIZED_ACCEPT
#define MD_HAVE_SOCKLEN_T

#define MD_SETJMP(env) _setjmp(env)
#define MD_LONGJMP(env, val) _longjmp(env, val)
#define MD_USE_BUILTIN_SETJMP

#if defined(__ppc__)
#define MD_JB_SP 0
#elif defined(__i386__)
#define MD_JB_SP 9
#elif defined(__x86_64__)
#define MD_JB_SP 4
#if defined(__amd64__) || defined(__x86_64__)
#define JB_SP 12
#define MD_GET_SP(_t) *((long *)&((_t)->context[JB_SP]))
#else
#error Unknown CPU architecture
#endif

#define MD_INIT_CONTEXT(_thread, _sp, _main) \
ST_BEGIN_MACRO \
if (MD_SETJMP((_thread)->context)) \
_main(); \
*((long *)&((_thread)->context[MD_JB_SP])) = (long) (_sp); \
#define MD_INIT_CONTEXT(_thread, _sp, _main) \
ST_BEGIN_MACRO \
if (MD_SETJMP((_thread)->context)) \
_main(); \
MD_GET_SP(_thread) = (long) (_sp); \
ST_END_MACRO

#if defined(MD_USE_BUILTIN_SETJMP)
#define MD_SETJMP(env) _st_md_cxt_save(env)
#define MD_LONGJMP(env, val) _st_md_cxt_restore(env, val)

extern int _st_md_cxt_save(jmp_buf env);
extern void _st_md_cxt_restore(jmp_buf env, int val);
#endif

#define MD_GET_UTIME() \
struct timeval tv; \
(void) gettimeofday(&tv, NULL); \
Expand Down
76 changes: 76 additions & 0 deletions md_darwin.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

/* If user disable the ASM, such as avoiding bugs in ASM, donot compile it. */
#if !defined(MD_ST_NO_ASM)

#if defined(__amd64__) || defined(__x86_64__)

/****************************************************************/

/*
* Internal __jmp_buf layout
*/
#define JB_RBX 0
#define JB_RBP 1
#define JB_R12 2 /* Backup IP, https://www.cnblogs.com/Five100Miles/p/8458561.html */
#define JB_R13 3 /* Backup SP, https://www.cnblogs.com/Five100Miles/p/8458561.html */
#define JB_R14 4 /* Backup LR, https://www.cnblogs.com/Five100Miles/p/8458561.html */
#define JB_R15 5 /* Backup PC, https://www.cnblogs.com/Five100Miles/p/8458561.html */
#define JB_RSP 6
#define JB_PC 7

.file "md_darwin.S"
.text

/* _st_md_cxt_save(__jmp_buf env) */ /* The env is rdi, http://blog.chinaunix.net/uid-20157960-id-1974354.html */
.globl __st_md_cxt_save
.align 16
__st_md_cxt_save:
/*
* Save registers.
*/
movq %rbx, (JB_RBX*8)(%rdi) /* Save rbx to env[0], *(int64_t*)(rdi+0)=rbx */
movq %rbp, (JB_RBP*8)(%rdi) /* Save rbp to env[1], *(int64_t*)(rdi+1)=rbp */
movq %r12, (JB_R12*8)(%rdi) /* Save r12 to env[2], *(int64_t*)(rdi+2)=r12 */
movq %r13, (JB_R13*8)(%rdi) /* Save r13 to env[3], *(int64_t*)(rdi+3)=r13 */
movq %r14, (JB_R14*8)(%rdi) /* Save r14 to env[4], *(int64_t*)(rdi+4)=r14 */
movq %r15, (JB_R15*8)(%rdi) /* Save r15 to env[5], *(int64_t*)(rdi+5)=r15 */
/* Save SP */
leaq 8(%rsp), %rdx /* Save *(int64_t*)(rsp+8) to rdx, https://my.oschina.net/guonaihong/blog/508907 */
movq %rdx, (JB_RSP*8)(%rdi) /* Save rdx(rsp) to env[6], *(int64_t*)(rdi+6)=rdx */
/* Save PC we are returning to */
movq (%rsp), %rax /* Save PC(parent function address) %(rsp) to rax */
movq %rax, (JB_PC*8)(%rdi) /* Save rax(PC) to env[7], *(int64_t*)(rdi+7)=rax */
xorq %rax, %rax /* Reset rax to 0 */
ret


/****************************************************************/

/* _st_md_cxt_restore(__jmp_buf env, int val) */ /* The env is rdi, val is esi/rsi, http://blog.chinaunix.net/uid-20157960-id-1974354.html */
.globl __st_md_cxt_restore
.align 16
__st_md_cxt_restore:
/*
* Restore registers.
*/
movq (JB_RBX*8)(%rdi), %rbx /* Load rbx from env[0] */
movq (JB_RBP*8)(%rdi), %rbp /* Load rbp from env[1] */
movq (JB_R12*8)(%rdi), %r12 /* Load r12 from env[2] */
movq (JB_R13*8)(%rdi), %r13 /* Load r13 from env[3] */
movq (JB_R14*8)(%rdi), %r14 /* Load r14 from env[4] */
movq (JB_R15*8)(%rdi), %r15 /* Load r15 from env[5] */
/* Set return value */ /* The esi is param1 val, the eax is return value */
test %esi, %esi /* if (!val) { */
mov $01, %eax /* val=1; */
cmove %eax, %esi /* } */
mov %esi, %eax /* return val; */
movq (JB_PC*8)(%rdi), %rdx /* Load rdx(PC) from env[7] */
movq (JB_RSP*8)(%rdi), %rsp /* Load rsp from env[6] */
/* Jump to saved PC */
jmpq *%rdx /* Jump to rdx(PC) */

/****************************************************************/

#endif

#endif

1 comment on commit 91d530e

@winlinvip
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.