Debian Codegraph @

srtcore/sync_posix.cpp

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2019 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10 #include "platform_sys.h"
11
12 #include <iomanip>
13 #include <math.h>
14 #include <stdexcept>
15 #include "sync.h"
16 #include "utilities.h"
17 #include "udt.h"
18 #include "srt.h"
19 #include "srt_compat.h"
20 #include "logging.h"
21 #include "common.h"
22
23 #if defined(_WIN32)
24 #include "win/wintime.h"
25 #include <sys/timeb.h>
26 #elif TARGET_OS_MAC
27 #include <mach/mach_time.h>
28 #endif
29
30 namespace srt_logging
31 {
32 extern Logger inlog;
33 }
34 using namespace srt_logging;
35
36 namespace srt
37 {
38 namespace sync
39 {
40
41 static void rdtsc(uint64_t& x)
42 {
43 #if SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_IA32_RDTSC
44 uint32_t lval, hval;
45 // asm volatile ("push %eax; push %ebx; push %ecx; push %edx");
46 // asm volatile ("xor %eax, %eax; cpuid");
47 asm volatile("rdtsc" : "=a"(lval), "=d"(hval));
48 // asm volatile ("pop %edx; pop %ecx; pop %ebx; pop %eax");
49 x = hval;
50 x = (x << 32) | lval;
51 #elif SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_IA64_ITC
52 asm("mov %0=ar.itc" : "=r"(x)::"memory");
53 #elif SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_AMD64_RDTSC
54 uint32_t lval, hval;
55 asm volatile("rdtsc" : "=a"(lval), "=d"(hval));
56 x = hval;
57 x = (x << 32) | lval;
58 #elif SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_WINQPC
59 // This function should not fail, because we checked the QPC
60 // when calling to QueryPerformanceFrequency. If it failed,
61 // the m_bUseMicroSecond was set to true.
62 QueryPerformanceCounter((LARGE_INTEGER*)&x);
63 #elif SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_MACH_ABSTIME
64 x = mach_absolute_time();
65 #elif SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_GETTIME_MONOTONIC
66 // get_cpu_frequency() returns 1 us accuracy in this case
67 timespec tm;
68 clock_gettime(CLOCK_MONOTONIC, &tm);
69 x = tm.tv_sec * uint64_t(1000000) + (tm.tv_nsec / 1000);
70 #elif SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_POSIX_GETTIMEOFDAY
71 // use system call to read time clock for other archs
72 timeval t;
73 gettimeofday(&t, 0);
74 x = t.tv_sec * uint64_t(1000000) + t.tv_usec;
75 #else
76 #error Wrong SRT_SYNC_CLOCK
77 #endif
78 }
79
80 static int64_t get_cpu_frequency()
81 {
82 int64_t frequency = 1; // 1 tick per microsecond.
83
84 #if SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_WINQPC
85 LARGE_INTEGER ccf; // in counts per second
86 if (QueryPerformanceFrequency(&ccf))
87 {
88 frequency = ccf.QuadPart / 1000000; // counts per microsecond
89 if (frequency == 0)
90 {
91 LOGC(inlog.Warn, log << "Win QPC frequency of " << ccf.QuadPart
92 << " counts/s is below the required 1 us accuracy. Please consider using C++11 timing (-DENABLE_STDCXX_SYNC=ON) instead.");
93 frequency = 1; // set back to 1 to avoid division by zero.
94 }
95 }
96 else
97 {
98 // Can't throw an exception, it won't be handled.
99 LOGC(inlog.Error, log << "IPE: QueryPerformanceFrequency failed with " << GetLastError());
100 }
101
102 #elif SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_MACH_ABSTIME
103 mach_timebase_info_data_t info;
104 mach_timebase_info(&info);
105 frequency = info.denom * int64_t(1000) / info.numer;
106
107 #elif SRT_SYNC_CLOCK >= SRT_SYNC_CLOCK_AMD64_RDTSC && SRT_SYNC_CLOCK <= SRT_SYNC_CLOCK_IA64_ITC
108 // SRT_SYNC_CLOCK_AMD64_RDTSC or SRT_SYNC_CLOCK_IA32_RDTSC or SRT_SYNC_CLOCK_IA64_ITC
109 uint64_t t1, t2;
110
111 rdtsc(t1);
112 timespec ts;
113 ts.tv_sec = 0;
114 ts.tv_nsec = 100000000;
115 nanosleep(&ts, NULL);
116 rdtsc(t2);
117
118 // CPU clocks per microsecond
119 frequency = int64_t(t2 - t1) / 100000;
120 #endif
121
122 return frequency;
123 }
124
125 static int count_subsecond_precision(int64_t ticks_per_us)
126 {
127 int signs = 6; // starting from 1 us
128 while (ticks_per_us /= 10) ++signs;
129 return signs;
130 }
131
132 const int64_t s_clock_ticks_per_us = get_cpu_frequency();
133
134 const int s_clock_subsecond_precision = count_subsecond_precision(s_clock_ticks_per_us);
135
136 int clockSubsecondPrecision() { return s_clock_subsecond_precision; }
137
138 } // namespace sync
139 } // namespace srt
140
141 ////////////////////////////////////////////////////////////////////////////////
142 //
143 // Sync utilities section
144 //
145 ////////////////////////////////////////////////////////////////////////////////
146
147 static timespec us_to_timespec(const uint64_t time_us)
148 {
149 timespec timeout;
150 timeout.tv_sec = time_us / 1000000;
151 timeout.tv_nsec = (time_us % 1000000) * 1000;
152 return timeout;
153 }
154
155 ////////////////////////////////////////////////////////////////////////////////
156 //
157 // TimePoint section
158 //
159 ////////////////////////////////////////////////////////////////////////////////
160
161 template <>
162 srt::sync::Duration<srt::sync::steady_clock> srt::sync::TimePoint<srt::sync::steady_clock>::time_since_epoch() const
163 {
164 return srt::sync::Duration<srt::sync::steady_clock>(m_timestamp);
165 }
166
167 srt::sync::TimePoint<srt::sync::steady_clock> srt::sync::steady_clock::now()
168 {
169 uint64_t x = 0;
170 rdtsc(x);
171 return TimePoint<steady_clock>(x);
172 }
173
174 int64_t srt::sync::count_microseconds(const steady_clock::duration& t)
175 {
176 return t.count() / s_clock_ticks_per_us;
177 }
178
179 int64_t srt::sync::count_milliseconds(const steady_clock::duration& t)
180 {
181 return t.count() / s_clock_ticks_per_us / 1000;
182 }
183
184 int64_t srt::sync::count_seconds(const steady_clock::duration& t)
185 {
186 return t.count() / s_clock_ticks_per_us / 1000000;
187 }
188
189 srt::sync::steady_clock::duration srt::sync::microseconds_from(int64_t t_us)
190 {
191 return steady_clock::duration(t_us * s_clock_ticks_per_us);
192 }
193
194 srt::sync::steady_clock::duration srt::sync::milliseconds_from(int64_t t_ms)
195 {
196 return steady_clock::duration((1000 * t_ms) * s_clock_ticks_per_us);
197 }
198
199 srt::sync::steady_clock::duration srt::sync::seconds_from(int64_t t_s)
200 {
201 return steady_clock::duration((1000000 * t_s) * s_clock_ticks_per_us);
202 }
203
204 srt::sync::Mutex::Mutex()
205 {
206 const int err = pthread_mutex_init(&m_mutex, 0);
207 if (err)
208 {
209 throw CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0);
210 }
211 }
212
213 srt::sync::Mutex::~Mutex()
214 {
215 pthread_mutex_destroy(&m_mutex);
216 }
217
218 int srt::sync::Mutex::lock()
219 {
220 return pthread_mutex_lock(&m_mutex);
221 }
222
223 int srt::sync::Mutex::unlock()
224 {
225 return pthread_mutex_unlock(&m_mutex);
226 }
227
228 bool srt::sync::Mutex::try_lock()
229 {
230 return (pthread_mutex_trylock(&m_mutex) == 0);
231 }
232
233 srt::sync::UniqueLock::UniqueLock(Mutex& m)
234 : m_Mutex(m)
235 {
236 m_iLocked = m_Mutex.lock();
237 }
238
239 srt::sync::UniqueLock::~UniqueLock()
240 {
241 if (m_iLocked == 0)
242 {
243 unlock();
244 }
245 }
246
247 void srt::sync::UniqueLock::lock()
248 {
249 if (m_iLocked != -1)
250 throw CThreadException(MJ_SYSTEMRES, MN_THREAD, 0);
251
252 m_iLocked = m_Mutex.lock();
253 }
254
255 void srt::sync::UniqueLock::unlock()
256 {
257 if (m_iLocked != 0)
258 throw CThreadException(MJ_SYSTEMRES, MN_THREAD, 0);
259
260 m_Mutex.unlock();
261 m_iLocked = -1;
262 }
263
264 srt::sync::Mutex* srt::sync::UniqueLock::mutex()
265 {
266 return &m_Mutex;
267 }
268
269 ////////////////////////////////////////////////////////////////////////////////
270 //
271 // Condition section (based on pthreads)
272 //
273 ////////////////////////////////////////////////////////////////////////////////
274
275 namespace srt
276 {
277 namespace sync
278 {
279
280 Condition::Condition()
281 #ifdef _WIN32
282 : m_cv(PTHREAD_COND_INITIALIZER)
283 #endif
284 {}
285
286 Condition::~Condition() {}
287
288 void Condition::init()
289 {
290 pthread_condattr_t* attr = NULL;
291 #if SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_GETTIME_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK
292 pthread_condattr_t CondAttribs;
293 pthread_condattr_init(&CondAttribs);
294 if (pthread_condattr_setclock(&CondAttribs, CLOCK_MONOTONIC) != 0)
295 {
296 pthread_condattr_destroy(&CondAttribs);
297 LOGC(inlog.Fatal, log << "IPE: pthread_condattr_setclock failed to set up a monotonic clock for a CV");
298 }
299 attr = &CondAttribs;
300 #endif
301 const int res = pthread_cond_init(&m_cv, attr);
302 #if SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_GETTIME_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK
303 if (attr != NULL)
304 {
305 pthread_condattr_destroy(attr);
306 }
307 #endif
308 if (res != 0)
309 throw std::runtime_error("pthread_cond_init monotonic failed");
310 }
311
312 void Condition::reset()
313 {
314
315 pthread_cond_t temp = PTHREAD_COND_INITIALIZER;
316 memcpy(&m_cv, (void *) &temp, sizeof(m_cv));
317 init();
318 }
319
320 void Condition::destroy()
321 {
322 pthread_cond_destroy(&m_cv);
323 }
324
325 void Condition::wait(UniqueLock& lock)
326 {
327 pthread_cond_wait(&m_cv, &lock.mutex()->ref());
328 }
329
330 bool Condition::wait_for(UniqueLock& lock, const steady_clock::duration& rel_time)
331 {
332 timespec timeout;
333 #if SRT_SYNC_CLOCK == SRT_SYNC_CLOCK_GETTIME_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK
334 clock_gettime(CLOCK_MONOTONIC, &timeout);
335 const uint64_t now_us = timeout.tv_sec * uint64_t(1000000) + (timeout.tv_nsec / 1000);
336 #else
337 timeval now;
338 gettimeofday(&now, 0);
339 const uint64_t now_us = now.tv_sec * uint64_t(1000000) + now.tv_usec;
340 #endif
341 timeout = us_to_timespec(now_us + count_microseconds(rel_time));
342 return pthread_cond_timedwait(&m_cv, &lock.mutex()->ref(), &timeout) != ETIMEDOUT;
343 }
344
345 bool Condition::wait_until(UniqueLock& lock, const steady_clock::time_point& timeout_time)
346 {
347 // This will work regardless as to which clock is in use. The time
348 // should be specified as steady_clock::time_point, so there's no
349 // question of the timer base.
350 const steady_clock::time_point now = steady_clock::now();
351 if (now >= timeout_time)
352 return false; // timeout
353
354 // wait_for() is used because it will be converted to pthread-frienly timeout_time inside.
355 return wait_for(lock, timeout_time - now);
356 }
357
358 void Condition::notify_one()
359 {
360 pthread_cond_signal(&m_cv);
361 }
362
363 void Condition::notify_all()
364 {
365 pthread_cond_broadcast(&m_cv);
366 }
367
368 }; // namespace sync
369 }; // namespace srt
370
371
372 ////////////////////////////////////////////////////////////////////////////////
373 //
374 // CThread class
375 //
376 ////////////////////////////////////////////////////////////////////////////////
377
378 srt::sync::CThread::CThread()
379 {
380 m_thread = pthread_t();
381 m_pid = getpid();
382 }
383
384 srt::sync::CThread::CThread(void *(*start_routine) (void *), void *arg)
385 {
386 create(start_routine, arg);
387 }
388
389 #if HAVE_FULL_CXX11
390 srt::sync::CThread& srt::sync::CThread::operator=(CThread&& other)
391 #else
392 srt::sync::CThread& srt::sync::CThread::operator=(CThread& other)
393 #endif
394 {
395 if (joinable())
396 {
397 // If the thread has already terminated, then
398 // pthread_join() returns immediately.
399 // But we have to check it has terminated before replacing it.
400 LOGC(inlog.Error, log << "IPE: Assigning to a thread that is not terminated!");
401
402 #ifndef DEBUG
403 #if !defined(__ANDROID__) && !defined(__OHOS__)
404 // In case of production build the hanging thread should be terminated
405 // to avoid hang ups and align with C++11 implementation.
406 // There is no pthread_cancel on Android. See #1476. This error should not normally
407 // happen, but if it happen, then detaching the thread.
408 pthread_cancel(m_thread);
409 #endif // __ANDROID__ __OHOS__
410 #else
411 join();
412 #endif
413 }
414 // Move thread handler from other
415 m_thread = other.m_thread;
416 m_pid = other.m_pid;
417 other.m_thread = pthread_t();
418 return *this;
419 }
420
421 #if !HAVE_FULL_CXX11
422 void srt::sync::CThread::create_thread(void *(*start_routine) (void *), void *arg)
423 {
424 SRT_ASSERT(!joinable());
425 create(start_routine, arg);
426 }
427 #endif
428
429 bool srt::sync::CThread::joinable() const
430 {
431 return m_pid == getpid() && !pthread_equal(m_thread, pthread_t());
432 }
433
434 void srt::sync::CThread::join()
435 {
436 if (joinable())
437 {
438 void *retval;
439 const int ret SRT_ATR_UNUSED = pthread_join(m_thread, &retval);
440 if (ret != 0)
441 {
442 LOGC(inlog.Error, log << "pthread_join failed with " << ret << " (" << m_thread << ")");
443 }
444 #ifdef HEAVY_LOGGING
445 else
446 {
447 HLOGC(inlog.Debug, log << "pthread_join SUCCEEDED");
448 }
449 #endif
450 // After joining, joinable should be false
451 }
452 m_thread = pthread_t();
453 return;
454 }
455
456 void srt::sync::CThread::create(void *(*start_routine) (void *), void *arg)
457 {
458 const int st = pthread_create(&m_thread, NULL, start_routine, arg);
459 if (st != 0)
460 {
461 LOGC(inlog.Error, log << "pthread_create failed with " << st);
462 throw CThreadException(MJ_SYSTEMRES, MN_THREAD, 0);
463 }
464 m_pid = getpid();
465 }
466
467
468 ////////////////////////////////////////////////////////////////////////////////
469 //
470 // CThreadError class - thread local storage error wrapper
471 //
472 ////////////////////////////////////////////////////////////////////////////////
473 namespace srt {
474 namespace sync {
475
476 class CThreadError
477 {
478 public:
479 CThreadError()
480 {
481 pthread_key_create(&m_ThreadSpecKey, ThreadSpecKeyDestroy);
482
483 // This is a global object and as such it should be called in the
484 // main application thread or at worst in the thread that has first
485 // run `srt_startup()` function and so requested the SRT library to
486 // be dynamically linked. Most probably in this very thread the API
487 // errors will be reported, so preallocate the ThreadLocalSpecific
488 // object for this error description.
489
490 // This allows std::bac_alloc to crash the program during
491 // the initialization of the SRT library (likely it would be
492 // during the DL constructor, still way before any chance of
493 // doing any operations here). This will prevent SRT from running
494 // into trouble while trying to operate.
495 CUDTException* ne = new CUDTException();
496 pthread_setspecific(m_ThreadSpecKey, ne);
497 }
498
499 ~CThreadError()
500 {
501 // Likely all objects should be deleted in all
502 // threads that have exited, but std::this_thread didn't exit
503 // yet :).
504 ThreadSpecKeyDestroy(pthread_getspecific(m_ThreadSpecKey));
505 pthread_key_delete(m_ThreadSpecKey);
506 }
507
508 void set(const CUDTException& e)
509 {
510 CUDTException* cur = get();
511 // If this returns NULL, it means that there was an unexpected
512 // memory allocation error. Simply ignore this request if so
513 // happened, and then when trying to get the error description
514 // the application will always get the memory allocation error.
515
516 // There's no point in doing anything else here; lack of memory
517 // must be prepared for prematurely, and that was already done.
518 if (!cur)
519 return;
520
521 *cur = e;
522 }
523
524 /*[[nullable]]*/ CUDTException* get()
525 {
526 if (!pthread_getspecific(m_ThreadSpecKey))
527 {
528 // This time if this can't be done due to memory allocation
529 // problems, just allow this value to be NULL, which during
530 // getting the error description will redirect to a memory
531 // allocation error.
532
533 // It would be nice to somehow ensure that this object is
534 // created in every thread of the application using SRT, but
535 // POSIX thread API doesn't contain any possibility to have
536 // a creation callback that would apply to every thread in
537 // the application (as it is for C++11 thread_local storage).
538 CUDTException* ne = new(std::nothrow) CUDTException();
539 pthread_setspecific(m_ThreadSpecKey, ne);
540 return ne;
541 }
542 return (CUDTException*)pthread_getspecific(m_ThreadSpecKey);
543 }
544
545 static void ThreadSpecKeyDestroy(void* e)
546 {
547 delete (CUDTException*)e;
548 }
549
550 private:
551 pthread_key_t m_ThreadSpecKey;
552 };
553
554 // Threal local error will be used by CUDTUnited
555 // that has a static scope
556
557 // This static makes this object file-private access so that
558 // the access is granted only for the accessor functions.
559 static CThreadError s_thErr;
560
561 void SetThreadLocalError(const CUDTException& e)
562 {
563 s_thErr.set(e);
564 }
565
566 CUDTException& GetThreadLocalError()
567 {
568 // In POSIX version we take into account the possibility
569 // of having an allocation error here. Therefore we need to
570 // allow this value to return NULL and have some fallback
571 // for that case. The dynamic memory allocation failure should
572 // be the only case as to why it is unable to get the pointer
573 // to the error description.
574 static CUDTException resident_alloc_error (MJ_SYSTEMRES, MN_MEMORY);
575 CUDTException* curx = s_thErr.get();
576 if (!curx)
577 return resident_alloc_error;
578 return *curx;
579 }
580
581 } // namespace sync
582 } // namespace srt
583