Skip to content

Commit

Permalink
issue ossrs#3978: improve st_utime's default impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
suzp1984 committed Mar 7, 2024
1 parent fa8096a commit d952b60
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
3 changes: 3 additions & 0 deletions trunk/3rdparty/st-srs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ LDFLAGS += -arch $(CPU_ARCHS)
LDFLAGS += -dynamiclib -install_name /sw/lib/libst.$(MAJOR).$(DSO_SUFFIX) -compatibility_version $(MAJOR) -current_version $(VERSION)
OTHER_FLAGS = -Wall
DEFINES += -DMD_HAVE_KQUEUE -DMD_HAVE_SELECT
# https://github.com/ossrs/srs/issues/3978
# if your mac os version is less than 10.12, then add
# DEFINES += -DMD_OSX_HAS_NO_CLOCK_GETTIME
endif

ifeq ($(OS), LINUX)
Expand Down
31 changes: 21 additions & 10 deletions trunk/3rdparty/st-srs/md.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,21 @@ extern void _st_md_cxt_restore(_st_jmp_buf_t env, int val);
MD_GET_SP(_thread) = (long) (_sp); \
ST_END_MACRO

#define MD_GET_UTIME() \
struct timeval tv; \
(void) gettimeofday(&tv, NULL); \
return (tv.tv_sec * 1000000LL + tv.tv_usec)
#if defined (MD_OSX_HAS_NO_CLOCK_GETTIME)
#define MD_GET_UTIME() \
struct timeval tv; \
(void) gettimeofday(&tv, NULL); \
return (tv.tv_sec * 1000000LL + tv.tv_usec)
#else
/*
* https://github.com/ossrs/srs/issues/3978
* use clock_gettime to get the timestamp in microseconds.
*/
#define MD_GET_UTIME() \
struct timespec ts; \
clock_gettime(CLOCK_MONOTONIC, &ts); \
return (ts.tv_sec * 1000000LL + ts.tv_nsec / 1000)
#endif

#elif defined (LINUX)

Expand All @@ -129,13 +140,13 @@ extern void _st_md_cxt_restore(_st_jmp_buf_t env, int val);
#define MD_HAVE_SOCKLEN_T
/*
* All architectures and flavors of linux have the gettimeofday
* function but if you know of a faster way, use it.
* https://github.com/ossrs/srs/issues/3978
* use clock_gettime to get the timestamp in microseconds.
*/
#define MD_GET_UTIME() \
struct timeval tv; \
(void) gettimeofday(&tv, NULL); \
return (tv.tv_sec * 1000000LL + tv.tv_usec)
#define MD_GET_UTIME() \
struct timespec ts; \
clock_gettime(CLOCK_MONOTONIC, &ts); \
return (ts.tv_sec * 1000000LL + ts.tv_nsec / 1000)

#if defined(__i386__)
#define MD_GET_SP(_t) *((long *)&((_t)->context[0].__jmpbuf[4]))
Expand Down
3 changes: 3 additions & 0 deletions trunk/auto/depends.sh
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ fi
# for osx, use darwin for st, donot use epoll.
if [[ $SRS_OSX == YES ]]; then
_ST_MAKE=darwin-debug && _ST_OBJ="DARWIN_`uname -r`_DBG"
if [[ $SRS_OSX_HAS_CLOCK_GETTIME != YES ]]; then
_ST_EXTRA_CFLAGS="$_ST_EXTRA_CFLAGS -DMD_OSX_HAS_NO_CLOCK_GETTIME"
fi
fi
# for windows/cygwin
if [[ $SRS_CYGWIN64 = YES ]]; then
Expand Down
16 changes: 15 additions & 1 deletion trunk/auto/options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ SRS_CROSS_BUILD_HOST=
SRS_CROSS_BUILD_PREFIX=
# For cache build
SRS_BUILD_CACHE=YES

SRS_OSX_HAS_CLOCK_GETTIME=
#
#####################################################################################
# Toolchain for cross-build on Ubuntu for ARM or MIPS.
Expand Down Expand Up @@ -149,7 +151,19 @@ function apply_system_options() {
OS_IS_RISCV=$(gcc -dM -E - </dev/null |grep -q '#define __riscv 1' && echo YES)

# Set the os option automatically.
if [[ $OS_IS_OSX == YES ]]; then SRS_OSX=YES; fi
if [[ $OS_IS_OSX == YES ]]; then
SRS_OSX=YES;
OSX_VERSION_ARR=($(sw_vers --productVersion | tr "." "\n"))
# OSX version >= 10.12 has api clock_gettime; check man clock_gettime
if [[ ${OSX_VERSION_ARR[0]} -gt 10 ]]; then
SRS_OSX_HAS_CLOCK_GETTIME=YES
elif [[ ${OSX_VERSION_ARR[0]} -eq 10 && ${OSX_VERSION_ARR[1]} -ge 12 ]]; then
SRS_OSX_HAS_CLOCK_GETTIME=YES
else
echo "Warning: your OSX $(sw_vers --productVersion) has no api clock_gettime"
SRS_OSX_HAS_CLOCK_GETTIME=NO
fi
fi
if [[ $OS_IS_CYGWIN == YES ]]; then SRS_CYGWIN64=YES; fi

if [[ $OS_IS_OSX == YES ]]; then SRS_JOBS=$(sysctl -n hw.ncpu 2>/dev/null || echo 1); fi
Expand Down

0 comments on commit d952b60

Please sign in to comment.