AJA NTV2 SDK  17.5.0.1242
NTV2 SDK 17.5.0.1242
pthreadsextra.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
8 #include "pthreadsextra.h"
9 #include <sys/time.h>
11 
12 int clock_gettime(clockid_t clk_id, struct timespec *tp)
13 {
14  AJA_UNUSED(clk_id);
15 
16  struct timeval tv;
17  gettimeofday(&tv, NULL);
18  tp->tv_sec = tv.tv_sec;
19  tp->tv_nsec = tv.tv_usec*1000;
20 
21  return 0;
22 
23 }
24 
25 static bool times_up(const struct timespec *timeout)
26 {
27  // get the current time of day
28  struct timespec ts;
30 
31  if(ts.tv_sec >= timeout->tv_sec &&
32  ts.tv_nsec >= timeout->tv_nsec)
33  {
34  return true;
35  }
36  else
37  {
38  return false;
39  }
40 }
41 
42 // see: http://lists.apple.com/archives/xcode-users/2007/Apr/msg00331.html
43 int pthread_mutex_timedlock(pthread_mutex_t *__restrict mutex, const struct timespec *__restrict abs_timeout)
44 {
45  int result;
46  do
47  {
48  result = pthread_mutex_trylock(mutex);
49  if (result == EBUSY)
50  {
51  timespec ts;
52  ts.tv_sec = 0;
53  ts.tv_nsec = 10000000;
54 
55  /* Sleep for 10,000,000 nanoseconds before trying again. */
56  int status = -1;
57  while (status == -1)
58  status = nanosleep(&ts, &ts);
59  }
60  else
61  break;
62 
63  if(times_up(abs_timeout))
64  {
65  result = ETIMEDOUT;
66  }
67  }
68  while (result != 0);
69 
70  return result;
71 }
72 
73 pid_t gettid(void)
74 {
75  return getpid();
76 }
NULL
#define NULL
Definition: ntv2caption608types.h:19
AJA_UNUSED
#define AJA_UNUSED(_x_)
Definition: types.h:424
times_up
static bool times_up(const struct timespec *timeout)
Definition: pthreadsextra.cpp:25
CLOCK_REALTIME
#define CLOCK_REALTIME
Definition: pthreadsextra.h:21
gettid
pid_t gettid(void)
Definition: pthreadsextra.cpp:73
pthreadsextra.h
Declares extra symbols to make the Mac threads implementation look more like Unix.
threadimpl.h
Declares the AJAThreadImpl class.
clockid_t
int clockid_t
Definition: pthreadsextra.h:18
clock_gettime
int clock_gettime(clockid_t clk_id, struct timespec *tp)
Definition: pthreadsextra.cpp:12
pthread_mutex_timedlock
int pthread_mutex_timedlock(pthread_mutex_t *__restrict mutex, const struct timespec *__restrict abs_timeout)
Definition: pthreadsextra.cpp:43