Skip to content

Commit

Permalink
Removed compat code for clock_gettime. This function wasn't used in S…
Browse files Browse the repository at this point in the history
…RT. (#236)
  • Loading branch information
ethouris authored and rndi committed Jan 24, 2018
1 parent e0013dd commit 127bdd6
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 231 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,9 @@ target_link_libraries (${TARGET_srt} PUBLIC ${PTHREAD_LIBRARY} ${DEPENDS_srt})
set (SRT_LIBS_PRIVATE ${SRT_LIBS_PRIVATE} ${PTHREAD_LIBRARY})
target_include_directories(${TARGET_srt} PUBLIC ${PTHREAD_INCLUDE_DIR} ${SRT_SRC_SRTCORE_DIR})

# Not sure why librt is required, but somehow only on Linux
if ( LINUX )
target_link_libraries(${TARGET_srt} PUBLIC rt)
target_link_libraries(${TARGET_srt} PUBLIC dl)
# Unsure why libdl was required, keeping for the time being.
#target_link_libraries(${TARGET_srt} PUBLIC dl)
set (IFNEEDED_SRT_LDFLAGS -pthread)
endif()

Expand Down
2 changes: 1 addition & 1 deletion apps/srt-live-transmit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ void TestLogHandler(void* opaque, int level, const char* file, int line, const c
time_t now;
time(&now);
char buf[1024];
struct tm local = LocalTime(now);
struct tm local = SysLocalTime(now);
size_t pos = strftime(buf, 1024, "[%c ", &local);

#ifdef _MSC_VER
Expand Down
101 changes: 0 additions & 101 deletions common/srt_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,107 +38,6 @@ written by
#include <windows.h>
#endif

#if defined(__MACH__)

#include <assert.h>
#include <sys/time.h>
#include <CoreServices/CoreServices.h>
#include <pthread.h>
#include <mach/mach.h>
#include <mach/clock.h>
#include <mach/mach_time.h>
#include <AvailabilityMacros.h>


int OSX_clock_gettime(clockid_t clock_id, struct timespec * ts)
{
int result = -1;

if (ts == NULL)
{
errno = EFAULT;
return -1;
}

switch (clock_id)
{
case CLOCK_REALTIME :
{
struct timeval tv;

result = gettimeofday(&tv, NULL);
if (result == 0)
{
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
}
}
break;

case CLOCK_MONOTONIC :
{
mach_timebase_info_data_t timebase_info;
memset(&timebase_info, 0, sizeof(timebase_info));
static const uint64_t BILLION = UINT64_C(1000000000);

(void)mach_timebase_info(&timebase_info);
if (timebase_info.numer <= 0
|| timebase_info.denom <= 0)
{
result = -1;
errno = EINVAL;
}
else
{
uint64_t monotonic_nanoseconds = mach_absolute_time()
* timebase_info.numer / timebase_info.denom;

ts->tv_sec = monotonic_nanoseconds / BILLION;
ts->tv_nsec = monotonic_nanoseconds % BILLION;
if (ts->tv_sec < 0
|| ts->tv_nsec < 0)
{
result = -1;
errno = EINVAL;
}
else
{
result = 0;
}
}
}
break;

#if 0
// Warning This is probably slow!!
case CLOCK_CALENDAR :
{
clock_serv_t cclock;
mach_timespec_t mts;

host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;

result = 0;
}
break;
#endif

default :
{
result = -1;
errno = EINVAL;
}
}

return result;
}

#endif // (__MACH__)


extern const char * SysStrError(int errnum, char * buf, size_t buflen)
{
Expand Down
125 changes: 1 addition & 124 deletions common/srt_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,129 +51,6 @@ extern "C" {
#endif


// MacOS X has a monotonic clock, but it's not POSIX.
#if defined(__MACH__)

#include <AvailabilityMacros.h>
#include <time.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>

// NOTE: clock_gettime() and clock_res() were added in OSX-10.12, IOS-10.0,
// TVOS-10.0, and WATCHOS-3.0. But XCode8 makes this very difficult to
// conditionally check for availability of the function. It will create
// a weak binding to clock_gettime() and clock_res() which will not be
// resolved at runtime unless actually run under OSX-10.12 even if
// -mmacosx-version-min= or MACOSX_DEPLOYMENT_TARGET= are less than 10.12.
// This is particularly problematic because the weak symbol will be NULL
// and cause an application crash when calling either of these functions.
// Attempting to work around this issue.
// See:
// https://bugs.erlang.org/browse/ERL-256
// https://curl.haxx.se/mail/lib-2016-09/0043.html

#if defined(CLOCK_REALTIME)
#define __SRT_OSX_CLOCK_GETTIME_AVAILABILITY 1
#else
typedef enum
{
CLOCK_REALTIME = 0,
CLOCK_MONOTONIC = 6,
// CONSIDER: CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID.
// Mentioned in Linux manpage, but not described as Linux-specific.
} clockid_t;
#endif

// OS-X does not have clock_gettime(2). This implements work arounds.
// https://developer.apple.com/library/mac/qa/qa1398/_index.html
// http://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x

int OSX_clock_gettime(clockid_t clock_id, struct timespec * ts);

static inline int OSX_clock_getres(clockid_t clock_id, struct timespec * ts)
{
if (0)
{
(void)clock_id;
}

ts->tv_sec = 0;
ts->tv_nsec = 1;

return 0;
}

static inline int OSXCall_clock_gettime(clockid_t clock_id, struct timespec * ts)
{
#if defined(__SRT_OSX_CLOCK_GETTIME_AVAILABILITY) \
&& (__SRT_OSX_CLOCK_GETTIME_AVAILABILITY == 1)
if (&clock_gettime != NULL)
{
return clock_gettime(clock_id, ts);
}
#endif

return OSX_clock_gettime(clock_id, ts);
}

static inline int OSXCall_clock_getres(clockid_t clock_id, struct timespec * ts)
{
#if defined(__SRT_OSX_CLOCK_GETTIME_AVAILABILITY) \
&& (__SRT_OSX_CLOCK_GETTIME_AVAILABILITY == 1)
if (&clock_getres != NULL)
{
return clock_getres(clock_id, ts);
}
#endif

return OSX_clock_getres(clock_id, ts);
}

#define SysClockGetTime OSXCall_clock_gettime
#define SysClockGetRes OSXCall_clock_getres

static inline int pthread_condattr_setclock(
pthread_condattr_t * attr, clockid_t clock_id)
{
if (0)
{
(void)attr;
(void)clock_id;
}

errno = ENOSYS;

return -1;
}

static inline size_t SysStrnlen(const char * s, size_t maxlen)
{
const char* pos = (const char*) memchr(s, 0, maxlen);
return pos ? pos - s : maxlen;
}

#if !defined(MAC_OS_X_VERSION_MAX_ALLOWED)
#error "FIXME!!"
#endif
#if (MAC_OS_X_VERSION_MAX_ALLOWED <= 101200)
#if defined(strnlen)
#undef strnlen
#endif
#define strnlen(s, maxlen) SysStrnlen(s, maxlen)
#endif

#endif // (__MACH__)


#ifndef SysClockGetTime
#define SysClockGetTime clock_gettime
#endif

#ifndef SysClockGetRes
#define SysClockGetRes clock_res
#endif

/* Ensures that we store the error in the buffer and return the bufer. */
SRT_API const char * SysStrError(int errnum, char * buf, size_t buflen);

Expand All @@ -191,7 +68,7 @@ inline std::string SysStrError(int errnum)
return SysStrError(errnum, buf, 1024);
}

inline struct tm LocalTime(time_t tt)
inline struct tm SysLocalTime(time_t tt)
{
struct tm tms;
memset(&tms, 0, sizeof tms);
Expand Down
4 changes: 2 additions & 2 deletions srtcore/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ std::string logging::FormatTime(uint64_t time)
time_t usec = time%1000000;

time_t tt = sec;
struct tm tm = LocalTime(tt);
struct tm tm = SysLocalTime(tt);

char tmp_buf[512];
#ifdef WIN32
Expand Down Expand Up @@ -872,7 +872,7 @@ void logging::LogDispatcher::CreateLogLinePrefix(std::ostringstream& serr)
timeval tv;
gettimeofday(&tv, 0);
time_t t = tv.tv_sec;
struct tm tm = LocalTime(t);
struct tm tm = SysLocalTime(t);

// Nice to have %T as "standard time format" for logs,
// but it's Single Unix Specification and doesn't exist
Expand Down

0 comments on commit 127bdd6

Please sign in to comment.