vdr 2.8.2
thread.c
Go to the documentation of this file.
1/*
2 * thread.c: A simple thread base class
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * $Id: thread.c 5.7 2026/05/23 21:03:38 kls Exp $
8 */
9
10#include "thread.h"
11#include <cxxabi.h>
12#include <dlfcn.h>
13#include <errno.h>
14#include <execinfo.h>
15#include <linux/unistd.h>
16#include <malloc.h>
17#include <stdarg.h>
18#include <stdlib.h>
19#include <sys/prctl.h>
20#include <sys/resource.h>
21#include <sys/syscall.h>
22#include <sys/time.h>
23#include <sys/wait.h>
24#include <unistd.h>
25#include "tools.h"
26
27#define ABORT { dsyslog("ABORT!"); cBackTrace::BackTrace(); abort(); }
28
29//#define DEBUG_LOCKING // uncomment this line to activate debug output for locking
30#define DEBUG_LOCKSEQ // uncomment this line to activate debug output for invalid locking sequence
31//#define DEBUG_LOCKCALL // uncomment this line to activate caller information with DEBUG_LOCKSEQ (WARNING: expensive operation, use only when actually debugging the locking sequence!)
32
33#ifdef DEBUG_LOCKING
34#define dbglocking(a...) fprintf(stderr, a)
35#else
36#define dbglocking(a...)
37#endif
38
39static bool GetAbsTime(struct timespec *Abstime, int MillisecondsFromNow)
40{
41 struct timeval now;
42 if (gettimeofday(&now, NULL) == 0) { // get current time
43 MillisecondsFromNow = max(MillisecondsFromNow, 3); // // making sure the time is >2ms to avoid possible busy waits
44 now.tv_sec += MillisecondsFromNow / 1000; // add full seconds
45 now.tv_usec += (MillisecondsFromNow % 1000) * 1000; // add microseconds
46 if (now.tv_usec >= 1000000) { // take care of an overflow
47 now.tv_sec++;
48 now.tv_usec -= 1000000;
49 }
50 Abstime->tv_sec = now.tv_sec; // seconds
51 Abstime->tv_nsec = now.tv_usec * 1000; // nano seconds
52 return true;
53 }
54 return false;
55}
56
57// --- cCondWait -------------------------------------------------------------
58
60{
61 signaled = false;
62 pthread_mutex_init(&mutex, NULL);
63 pthread_cond_init(&cond, NULL);
64}
65
67{
68 pthread_cond_broadcast(&cond); // wake up any sleepers
69 pthread_cond_destroy(&cond);
70 pthread_mutex_destroy(&mutex);
71}
72
73void cCondWait::SleepMs(int TimeoutMs)
74{
75 cCondWait w;
76 w.Wait(max(TimeoutMs, 3)); // making sure the time is >2ms to avoid a possible busy wait
77}
78
79bool cCondWait::Wait(int TimeoutMs)
80{
81 pthread_mutex_lock(&mutex);
82 if (!signaled) {
83 if (TimeoutMs) {
84 struct timespec abstime;
85 if (GetAbsTime(&abstime, TimeoutMs)) {
86 while (!signaled) {
87 if (pthread_cond_timedwait(&cond, &mutex, &abstime) == ETIMEDOUT)
88 break;
89 }
90 }
91 }
92 else
93 pthread_cond_wait(&cond, &mutex);
94 }
95 bool r = signaled;
96 signaled = false;
97 pthread_mutex_unlock(&mutex);
98 return r;
99}
100
102{
103 pthread_mutex_lock(&mutex);
104 signaled = true;
105 pthread_cond_broadcast(&cond);
106 pthread_mutex_unlock(&mutex);
107}
108
109// --- cCondVar --------------------------------------------------------------
110
112{
113 pthread_cond_init(&cond, 0);
114}
115
117{
118 pthread_cond_broadcast(&cond); // wake up any sleepers
119 pthread_cond_destroy(&cond);
120}
121
123{
124 if (Mutex.locked) {
125 int locked = Mutex.locked;
126 Mutex.locked = 0; // have to clear the locked count here, as pthread_cond_wait
127 // does an implicit unlock of the mutex
128 Mutex.lockThreadId = 0;
129 pthread_cond_wait(&cond, &Mutex.mutex);
130 Mutex.lockThreadId = cThread::ThreadId(); // pthread_cond_wait re-locked the mutex without going through cMutex::Lock()
131 Mutex.locked = locked;
132 }
133}
134
135bool cCondVar::TimedWait(cMutex &Mutex, int TimeoutMs)
136{
137 bool r = true; // true = condition signaled, false = timeout
138
139 if (Mutex.locked) {
140 struct timespec abstime;
141 if (GetAbsTime(&abstime, TimeoutMs)) {
142 int locked = Mutex.locked;
143 Mutex.locked = 0; // have to clear the locked count here, as pthread_cond_timedwait
144 // does an implicit unlock of the mutex.
145 Mutex.lockThreadId = 0;
146 if (pthread_cond_timedwait(&cond, &Mutex.mutex, &abstime) == ETIMEDOUT)
147 r = false;
148 Mutex.lockThreadId = cThread::ThreadId(); // pthread_cond_timedwait re-locked the mutex without going through cMutex::Lock()
149 Mutex.locked = locked;
150 }
151 }
152 return r;
153}
154
156{
157 pthread_cond_broadcast(&cond);
158}
159
160// --- cRwLock ---------------------------------------------------------------
161
162cRwLock::cRwLock(bool PreferWriter)
163{
164 locked = 0;
166 pthread_rwlockattr_t attr;
167 pthread_rwlockattr_init(&attr);
168 pthread_rwlockattr_setkind_np(&attr, PreferWriter ? PTHREAD_RWLOCK_PREFER_WRITER_NP : PTHREAD_RWLOCK_PREFER_READER_NP);
169 pthread_rwlock_init(&rwlock, &attr);
170}
171
173{
174 pthread_rwlock_destroy(&rwlock);
175}
176
177bool cRwLock::Lock(bool Write, int TimeoutMs)
178{
179 int Result = 0;
180 struct timespec abstime;
181 if (TimeoutMs) {
182 if (!GetAbsTime(&abstime, TimeoutMs))
183 TimeoutMs = 0;
184 }
185 if (Write) {
186 Result = TimeoutMs ? pthread_rwlock_timedwrlock(&rwlock, &abstime) : pthread_rwlock_wrlock(&rwlock);
187 if (Result == 0)
189 }
190 else if (writeLockThreadId == cThread::ThreadId()) {
191 locked++; // there can be any number of stacked read locks, so we keep track here
192 Result = 0; // acquiring a read lock while holding a write lock within the same thread is OK
193 }
194 else
195 Result = TimeoutMs ? pthread_rwlock_timedrdlock(&rwlock, &abstime) : pthread_rwlock_rdlock(&rwlock);
196 return Result == 0;
197}
198
200{
201 if (writeLockThreadId == cThread::ThreadId()) { // this is the thread that obtained the initial write lock
202 if (locked) { // this is the unlock of a read lock within the write lock
203 locked--;
204 return;
205 }
206 }
208 pthread_rwlock_unlock(&rwlock);
209}
210
211// --- cMutex ----------------------------------------------------------------
212
214{
215 lockThreadId = 0;
216 locked = 0;
217 pthread_mutexattr_t attr;
218 pthread_mutexattr_init(&attr);
219 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
220 pthread_mutex_init(&mutex, &attr);
221}
222
224{
225 pthread_mutex_destroy(&mutex);
226}
227
228void cMutex::Lock(void)
229{
230 // PTHREAD_MUTEX_ERRORCHECK_NP rejects recursive entry from the same thread; handle the
231 // nested cMutexLock case (documented in thread.h) here, like cRwLock does below:
232 tThreadId ThisThreadId = cThread::ThreadId();
233 if (lockThreadId == ThisThreadId && locked) {
234 locked++;
235 return;
236 }
237 pthread_mutex_lock(&mutex);
238 lockThreadId = ThisThreadId;
239 locked++;
240}
241
243{
244 if (!--locked) {
245 lockThreadId = 0;
246 pthread_mutex_unlock(&mutex);
247 }
248}
249
250// --- cThread ---------------------------------------------------------------
251
253
254cThread::cThread(const char *Description, bool LowPriority)
255{
256 active = running = false;
257 childTid = 0;
258 childThreadId = 0;
259 description = NULL;
260 if (Description)
261 SetDescription("%s", Description);
262 lowPriority = LowPriority;
263}
264
266{
267 Cancel(); // just in case the derived class didn't call it
268 free(description);
269}
270
271void cThread::SetPriority(int Priority)
272{
273 if (setpriority(PRIO_PROCESS, 0, Priority) < 0)
274 LOG_ERROR;
275}
276
277void cThread::SetIOPriority(int Priority)
278{
279 if (syscall(SYS_ioprio_set, 1, 0, (Priority & 0xff) | (3 << 13)) < 0) // idle class
280 LOG_ERROR;
281}
282
283void cThread::SetDescription(const char *Description, ...)
284{
285 free(description);
286 description = NULL;
287 if (Description) {
288 va_list ap;
289 va_start(ap, Description);
290 description = strdup(cString::vsprintf(Description, ap));
291 va_end(ap);
292 }
293}
294
296{
297 Thread->childThreadId = ThreadId();
298 if (Thread->description) {
299 dsyslog("%s thread started (pid=%d, tid=%d, prio=%s)", Thread->description, getpid(), Thread->childThreadId, Thread->lowPriority ? "low" : "high");
300#ifdef PR_SET_NAME
301 if (prctl(PR_SET_NAME, Thread->description, 0, 0, 0) < 0)
302 esyslog("%s thread naming failed (pid=%d, tid=%d)", Thread->description, getpid(), Thread->childThreadId);
303#endif
304 }
305 if (Thread->lowPriority) {
306 Thread->SetPriority(19);
307 Thread->SetIOPriority(7);
308 }
309 Thread->Action();
310 if (Thread->description)
311 dsyslog("%s thread ended (pid=%d, tid=%d)", Thread->description, getpid(), Thread->childThreadId);
312 Thread->running = false;
313 Thread->active = false;
314 return NULL;
315}
316
317#define THREAD_STOP_TIMEOUT 3000 // ms to wait for a thread to stop before newly starting it
318#define THREAD_STOP_SLEEP 30 // ms to sleep while waiting for a thread to stop
319
321{
322 if (!running) {
323 if (active) {
324 // Wait until the previous incarnation of this thread has completely ended
325 // before starting it newly:
326 cTimeMs RestartTimeout;
327 while (!running && active && RestartTimeout.Elapsed() < THREAD_STOP_TIMEOUT)
329 }
330 if (!active) {
331 active = running = true;
332 if (pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this) == 0) {
333 pthread_detach(childTid); // auto-reap
334 }
335 else {
336 LOG_ERROR;
337 active = running = false;
338 return false;
339 }
340 }
341 }
342 return true;
343}
344
346{
347 if (active) {
348 //
349 // Single UNIX Spec v2 says:
350 //
351 // The pthread_kill() function is used to request
352 // that a signal be delivered to the specified thread.
353 //
354 // As in kill(), if sig is zero, error checking is
355 // performed but no signal is actually sent.
356 //
357 int err;
358 if ((err = pthread_kill(childTid, 0)) != 0) {
359 if (err != ESRCH)
360 LOG_ERROR;
361 childTid = 0;
362 active = running = false;
363 }
364 else
365 return true;
366 }
367 return false;
368}
369
370void cThread::Cancel(int WaitSeconds)
371{
372 running = false;
373 if (active && WaitSeconds > -1) {
374 if (WaitSeconds > 0) {
375 cTimeMs t(WaitSeconds * 1000);
376 while (!t.TimedOut()) {
377 if (!Active())
378 return;
380 }
381 esyslog("ERROR: %s thread %d won't end (waited %d seconds) - canceling it...", description ? description : "", childThreadId, WaitSeconds);
382 }
383 pthread_cancel(childTid);
384 childTid = 0;
385 active = false;
386 }
387}
388
390{
391 return syscall(__NR_gettid);
392}
393
395{
396 if (mainThreadId == 0)
398 else
399 esyslog("ERROR: attempt to set main thread id to %d while it already is %d", ThreadId(), mainThreadId);
400}
401
402// --- cMutexLock ------------------------------------------------------------
403
405{
406 mutex = NULL;
407 locked = false;
408 Lock(Mutex);
409}
410
412{
413 if (mutex && locked)
414 mutex->Unlock();
415}
416
418{
419 if (Mutex && !mutex) {
420 mutex = Mutex;
421 Mutex->Lock();
422 locked = true;
423 return true;
424 }
425 return false;
426}
427
428// --- cThreadLock -----------------------------------------------------------
429
431{
432 thread = NULL;
433 locked = false;
434 Lock(Thread);
435}
436
438{
439 if (thread && locked)
440 thread->Unlock();
441}
442
444{
445 if (Thread && !thread) {
446 thread = Thread;
447 Thread->Lock();
448 locked = true;
449 return true;
450 }
451 return false;
452}
453
454// --- cBackTrace ------------------------------------------------------------
455
456#define BT_BUF_SIZE 100
457
459{
460 char *Module = s;
461 char *Function = NULL;
462 char *Offset = NULL;
463 char *Address = NULL;
464 // separate the string:
465 for (char *q = Module; *q; q++) {
466 if (*q == '(') {
467 *q = 0;
468 Function = q + 1;
469 }
470 else if (*q == '+') {
471 *q = 0;
472 Offset = q + 1;
473 }
474 else if (*q == ')')
475 *q = 0;
476 else if (*q == '[')
477 Address = q + 1;
478 else if (*q == ']') {
479 *q = 0;
480 break;
481 }
482 }
483 // demangle the function name:
484 char *DemangledFunction = NULL;
485 if (Function) {
486 int status;
487 DemangledFunction = abi::__cxa_demangle(Function, NULL, 0, &status);
488 if (DemangledFunction)
489 Function = DemangledFunction;
490 if (!*Function)
491 Function = NULL;
492 }
493 cString d = cString::sprintf("%s%s%s", Module, Function ? " " : "", Function ? Function : "");
494 // convert string address to numbers:
495 unsigned long long addr = Address ? strtoull(Address, NULL, 0) : 0;
496 unsigned long long offs = Offset ? strtoull(Offset, NULL, 0) : 0;
497 // for shared libraries we need get the offset inside the library:
498 if (Function) {
499 // check whether the module name ends with ".so*":
500 char *e = Module;
501 char *p = NULL;
502 while (e = strstr(e, ".so"))
503 p = e++;
504 if (p && !strchr(p, '/')) {
505 Dl_info dlinfo;
506 if (dladdr(reinterpret_cast<void*>(addr), &dlinfo)) {
507 if ((strcmp(Module, dlinfo.dli_fname) == 0) && dlinfo.dli_fbase) {
508 unsigned long long base = reinterpret_cast<unsigned long long>(dlinfo.dli_fbase);
509 addr -= base;
510 addr &= 0x0FFFFFFFF; // to make it work on both 32 and 64 bit systems
511 }
512 }
513 }
514 }
515 // determine the file name and line number:
516 cString cmd = cString::sprintf("addr2line --functions --demangle --inlines --basename --exe=%s 0x%llx", Module, Function ? addr : offs);
517 cPipe p;
518 if (p.Open(cmd, "r")) {
519 int n = 0;
520 cReadLine rl;
521 while (char *l = rl.Read(p)) {
522 if (n == 0) {
523 if (Function && strcmp(l, Function))
524 d = cString::sprintf("%s calling %s", *d, l);
525 }
526 else
527 d = cString::sprintf("%s at %s", *d, l);
528 n++;
529 }
530 p.Close();
531 }
532 free(DemangledFunction);
533 return d;
534}
535
536void cBackTrace::BackTrace(cStringList &StringList, int Level, bool Mangled)
537{
538 void *b[BT_BUF_SIZE];
539 int n = backtrace(b, BT_BUF_SIZE);
540 if (char **s = backtrace_symbols(b, n)) {
541 for (int i = max(Level, 0) + 1; i < n; i++) // 1 is the call to this function itself
542 StringList.Append(strdup(Mangled ? s[i] : *Demangle(s[i])));
543 free(s);
544 }
545}
546
547void cBackTrace::BackTrace(FILE *f, int Level, bool Mangled)
548{
549 cStringList sl;
550 BackTrace(sl, Level + 1, Mangled); // 1 is the call to this function itself
551 for (int i = 0; i < sl.Size(); i++) {
552 if (f)
553 fprintf(f, "%s\n", sl[i]);
554 else
555 dsyslog("%s", sl[i]);
556 }
557}
558
559cString cBackTrace::GetCaller(int Level, bool Mangled)
560{
561 cString Caller;
562 Level = max(Level, 0) + 1; // 1 is the call to this function itself
563 void *b[BT_BUF_SIZE];
564 int n = backtrace(b, BT_BUF_SIZE);
565 if (char **s = backtrace_symbols(b, n)) {
566 if (Level < n)
567 Caller = Mangled ? s[Level] : *Demangle(s[Level]);
568 free(s);
569 }
570 return Caller;
571}
572
573// --- cStateLockLog ---------------------------------------------------------
574
575#ifdef DEBUG_LOCKSEQ
576#define SLL_SIZE 20 // the number of log entries
577#define SLL_LENGTH 512 // the maximum length of log entries
578#define SLL_THREADS 20 // the maximum number of threads holding locks at the same time (typically well below 10)
579#define SLL_MAX_LIST 9 // max. number of lists to log
580#define SLL_WRITE_FLAG 0x80000000
581#define SLL_LOCK_FLAG 0x40000000
582
584private:
591#ifdef DEBUG_LOCKCALL
592 char logCaller[SLL_SIZE][SLL_LENGTH];
593#endif
595 bool dumped;
596 void Dump(const char *Name, tThreadId ThreadId);
597public:
598 cStateLockLog(void);
599 void Check(const char *Name, bool Lock, bool Write = false);
600 };
601
603{
604 memset(logThreadIds, 0, sizeof(logThreadIds));
605 memset(logFlags, 0, sizeof(logFlags));
606 memset(logCounter, 0, sizeof(logCounter));
607#ifdef DEBUG_LOCKCALL
608 memset(logCaller, 0, sizeof(logCaller));
609#endif
610 logIndex = 0;
611 dumped = false;
612}
613
614void cStateLockLog::Dump(const char *Name, tThreadId ThreadId)
615{
616 dsyslog("--- begin invalid lock sequence report");
617 dsyslog("TID T C R DR S ST");
618 int LastFlags = 0;
619 for (int i = 0; i < SLL_SIZE; i++) {
620 if (tThreadId tid = logThreadIds[logIndex]) {
621 char msg[SLL_LENGTH];
622 char *q = msg;
623 q += sprintf(q, "%5d", tid);
624 int Flags = logFlags[logIndex];
625 bool Write = Flags & SLL_WRITE_FLAG;
626 bool Lock = Flags & SLL_LOCK_FLAG;
627 Flags &= ~(SLL_WRITE_FLAG | SLL_LOCK_FLAG);
628 int Changed = LastFlags ^ Flags;
629 LastFlags = Flags;
630 for (int i = 0; i <= SLL_MAX_LIST; i++) {
631 char c = '-';
632 int b = 1 << i;
633 if ((Flags & b) != 0)
634 c = '*';
635 if ((Changed & b) != 0)
636 c = Lock ? Write ? 'W' : 'R' : 'U';
637 q += sprintf(q, " %c", c);
638 }
639 q += sprintf(q, " %c", Lock ? 'L' : 'U');
640#ifdef DEBUG_LOCKCALL
641 if (*logCaller[logIndex]) {
642 *q++ = ' ';
643 strn0cpy(q, *cBackTrace::Demangle(logCaller[logIndex]), sizeof(msg) - (q - msg));
644 }
645#endif
646 dsyslog("%s", msg);
647 }
648 if (++logIndex >= SLL_SIZE)
649 logIndex = 0;
650 }
651 dsyslog("%5d invalid lock sequence: %s", ThreadId, Name);
652 dsyslog("full backtrace:");
653 cBackTrace::BackTrace(NULL, 2);
654 dsyslog("--- end invalid lock sequence report");
655 dsyslog("--- THERE WILL BE NO FURTHER REPORTS UNTIL VDR IS RESTARTED!");
656 fprintf(stderr, "invalid lock sequence at %s\n", *DayDateTime(time(NULL)));
657}
658
659void cStateLockLog::Check(const char *Name, bool Lock, bool Write)
660{
661 if (!dumped && Name) {
662 int n = *Name - '0' - 1;
663 if (0 <= n && n < SLL_MAX_LIST) {
664 int b = 1 << n;
665 cMutexLock MutexLock(&mutex);
666 tThreadId ThreadId = cThread::ThreadId();
667 int Index = -1;
668 int AvailableIndex = -1;
669 for (int i = 0; i < threadIds.Size(); i++) {
670 if (ThreadId == threadIds[i]) {
671 Index = i;
672 break;
673 }
674 if (threadIds[i] == 0)
675 AvailableIndex = i;
676 }
677 if (Index < 0) {
678 if (AvailableIndex < 0) {
679 Index = threadIds.Size();
680 threadIds.Append(ThreadId);
681 flags.Append(0);
682 }
683 else {
684 Index = AvailableIndex;
685 threadIds[Index] = ThreadId;
686 }
687 }
688 if (Index >= SLL_THREADS) {
689 // should never happen!
690 esyslog("ERROR: too many threads holding list locks at the same time - stopped logging locks!");
691 dumped = true;
692 return;
693 }
694 bool DoDump = false;
695 if (Lock) {
696 if ((flags[Index] & ~b) < b) // thread holds only "smaller" locks -> OK
697 ;
698 else if ((flags[Index] & b) == 0) // thread already holds "bigger" locks, so it may only re-lock one that it already has!
699 DoDump = true;
700 logCounter[Index][n]++;
701 flags[Index] |= b;
702 }
703 else if (--logCounter[Index][n] == 0)
704 flags[Index] &= ~b;
705 logThreadIds[logIndex] = ThreadId;
706 logFlags[logIndex] = flags[Index] | (Write ? SLL_WRITE_FLAG : 0) | (Lock ? SLL_LOCK_FLAG : 0);
707 if (flags[Index] == 0)
708 threadIds[Index] = 0;
709#ifdef DEBUG_LOCKCALL
710 strn0cpy(logCaller[logIndex], cBackTrace::GetCaller(Lock ? 3 : 5, true), SLL_LENGTH);
711#endif
712 if (++logIndex >= SLL_SIZE)
713 logIndex = 0;
714 if (DoDump) {
715 Dump(Name, ThreadId);
716 dumped = true;
717 }
718 }
719 }
720}
721
723
724#define dbglockseq(n, l, w) StateLockLog.Check(n, l, w)
725#else
726#define dbglockseq(n, l, w)
727#endif // DEBUG_LOCKSEQ
728
729// --- cStateLock ------------------------------------------------------------
730
731cStateLock::cStateLock(const char *Name)
732:rwLock(true)
733{
734 name = Name;
735 threadId = 0;
736 state = 0;
738 syncStateKey = NULL;
739}
740
741bool cStateLock::Lock(cStateKey &StateKey, bool Write, int TimeoutMs)
742{
743 dbglocking("%5d %-12s %10p lock state = %d/%d write = %d timeout = %d\n", cThread::ThreadId(), name, &StateKey, state, StateKey.state, Write, TimeoutMs);
744 StateKey.timedOut = false;
745 if (StateKey.stateLock) {
746 esyslog("ERROR: StateKey already in use in call to cStateLock::Lock() (tid=%d, lock=%s)", StateKey.stateLock->threadId, name);
747 ABORT;
748 return false;
749 }
750 if (rwLock.Lock(Write, TimeoutMs)) {
751 dbglockseq(name, true, Write);
752 StateKey.stateLock = this;
753 if (Write) {
754 dbglocking("%5d %-12s %10p locked write\n", cThread::ThreadId(), name, &StateKey);
756 StateKey.write = true;
757 return true;
758 }
759 else if (state != StateKey.state) {
760 dbglocking("%5d %-12s %10p locked read\n", cThread::ThreadId(), name, &StateKey);
761 return true;
762 }
763 else {
764 dbglocking("%5d %-12s %10p state unchanged\n", cThread::ThreadId(), name, &StateKey);
765 StateKey.stateLock = NULL;
766 dbglockseq(name, false, false);
767 rwLock.Unlock();
768 }
769 }
770 else if (TimeoutMs) {
771 dbglocking("%5d %-12s %10p timeout\n", cThread::ThreadId(), name, &StateKey);
772 StateKey.timedOut = true;
773 }
774 else if (threadId == cThread::ThreadId()) {
775 static bool DoubleWriteLockReported = false;
776 if (!DoubleWriteLockReported) {
777 dsyslog("WARNING: attempt to acquire write lock while already holding a write lock in the same thread - this may crash! (backtrace follows)");
779 DoubleWriteLockReported = true;
780 }
781 }
782 return false;
783}
784
785void cStateLock::Unlock(cStateKey &StateKey, bool IncState)
786{
787 dbglocking("%5d %-12s %10p unlock state = %d/%d inc = %d\n", cThread::ThreadId(), name, &StateKey, state, StateKey.state, IncState);
788 if (StateKey.stateLock != this) {
789 esyslog("ERROR: cStateLock::Unlock() called with an unused key (tid=%d, lock=%s)", threadId, name);
790 ABORT;
791 return;
792 }
793 if (StateKey.write && threadId != cThread::ThreadId()) {
794 esyslog("ERROR: cStateLock::Unlock() called without holding a write lock (tid=%d, lock=%s)", threadId, name);
795 ABORT;
796 return;
797 }
798 if (StateKey.write && (IncState && explicitModify != emArmed || explicitModify == emEnabled)) {
799 if (syncStateKey && syncStateKey->state == state)
800 syncStateKey->state++;
801 state++;
802 }
803 StateKey.state = state;
804 StateKey.stateLock = NULL;
805 if (StateKey.write) {
806 StateKey.write = false;
807 threadId = 0;
809 syncStateKey = NULL;
810 }
811 dbglockseq(name, false, false);
812 rwLock.Unlock();
813}
814
816{
817 dbglocking("%5d %-12s %10p SetSyncStateKey\n", cThread::ThreadId(), name, &StateKey);
818 if (threadId != cThread::ThreadId()) {
819 esyslog("ERROR: cStateLock::SetSyncStateKey() called without holding a write lock (tid=%d, lock=%s)", threadId, name);
820 ABORT;
821 return;
822 }
823 if (StateKey.stateLock == this) {
824 esyslog("ERROR: cStateLock::SetSyncStateKey() called with locked key (tid=%d, lock=%s)", threadId, name);
825 ABORT;
826 return;
827 }
828 if (syncStateKey) {
829 esyslog("ERROR: cStateLock::SetSyncStateKey() called twice (tid=%d, lock=%s)", threadId, name);
830 ABORT;
831 return;
832 }
833 syncStateKey = &StateKey;
834}
835
837{
838 if (threadId != cThread::ThreadId()) {
839 esyslog("ERROR: cStateLock::SetExplicitModify() called without holding a write lock (tid=%d, lock=%s)", threadId, name);
840 ABORT;
841 return;
842 }
843 if (explicitModify != emDisabled) {
844 esyslog("ERROR: cStateLock::SetExplicitModify() called twice (tid=%d, lock=%s)", threadId, name);
845 ABORT;
846 return;
847 }
849}
850
852{
853 if (threadId != cThread::ThreadId()) {
854 esyslog("ERROR: cStateLock::SetModified() called without holding a write lock (tid=%d, lock=%s)", threadId, name);
855 ABORT;
856 return;
857 }
859}
860
861// --- cStateKey -------------------------------------------------------------
862
863cStateKey::cStateKey(bool IgnoreFirst)
864{
865 stateLock = NULL;
866 write = false;
867 state = 0;
868 if (!IgnoreFirst)
869 Reset();
870}
871
873{
874 if (stateLock) {
875 esyslog("ERROR: cStateKey::~cStateKey() called without releasing the lock first (tid=%d, lock=%s, key=%p)", stateLock->threadId, stateLock->name, this);
876 ABORT;
877 }
878}
879
881{
882 state = -1; // lock and key are initialized differently, to make the first check return true
883}
884
885void cStateKey::Remove(bool IncState)
886{
887 if (stateLock)
888 stateLock->Unlock(*this, IncState);
889 else {
890 esyslog("ERROR: cStateKey::Remove() called without holding a lock (key=%p)", this);
891 ABORT;
892 }
893}
894
896{
897 if (!stateLock) {
898 esyslog("ERROR: cStateKey::StateChanged() called without holding a lock (tid=%d, key=%p)", cThread::ThreadId(), this);
899 ABORT;
900 }
901 else if (write)
902 return state != stateLock->state;
903 else
904 return true;
905}
906
907// --- cIoThrottle -----------------------------------------------------------
908
910int cIoThrottle::count = 0;
911
913{
914 active = false;
915}
916
921
923{
924 if (!active) {
925 mutex.Lock();
926 count++;
927 active = true;
928 dsyslog("i/o throttle activated, count = %d (tid=%d)", count, cThread::ThreadId());
929 mutex.Unlock();
930 }
931}
932
934{
935 if (active) {
936 mutex.Lock();
937 count--;
938 active = false;
939 dsyslog("i/o throttle released, count = %d (tid=%d)", count, cThread::ThreadId());
940 mutex.Unlock();
941 }
942}
943
945{
946 return count > 0;
947}
948
949// --- cPipe -----------------------------------------------------------------
950
951// cPipe::Open() and cPipe::Close() are based on code originally received from
952// Andreas Vitting <Andreas@huji.de>
953
955{
956 pid = -1;
957 f = NULL;
958}
959
961{
962 Close();
963}
964
965bool cPipe::Open(const char *Command, const char *Mode)
966{
967 int fd[2];
968
969 if (pipe(fd) < 0) {
970 LOG_ERROR_STR(Command);
971 return false;
972 }
973 if ((pid = fork()) < 0) { // fork failed
974 LOG_ERROR_STR(Command);
975 close(fd[0]);
976 close(fd[1]);
977 return false;
978 }
979
980 const char *mode = "w";
981 int iopipe = 0;
982
983 if (pid > 0) { // parent process
984 if (strcmp(Mode, "r") == 0) {
985 mode = "r";
986 iopipe = 1;
987 }
988 close(fd[iopipe]);
989 if ((f = fdopen(fd[1 - iopipe], mode)) == NULL) {
990 LOG_ERROR_STR(Command);
991 close(fd[1 - iopipe]);
992 }
993 return f != NULL;
994 }
995 else { // child process
996 int iofd = STDOUT_FILENO;
997 if (strcmp(Mode, "w") == 0) {
998 iopipe = 1;
999 iofd = STDIN_FILENO;
1000 }
1001 close(fd[iopipe]);
1002 if (dup2(fd[1 - iopipe], iofd) == -1) { // now redirect
1003 close(fd[1 - iopipe]);
1004 _exit(-1);
1005 }
1006 else {
1007 int MaxPossibleFileDescriptors = getdtablesize();
1008 for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
1009 close(i); //close all dup'ed filedescriptors
1010 if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1) {
1011 close(fd[1 - iopipe]);
1012 _exit(-1);
1013 }
1014 }
1015 _exit(0);
1016 }
1017}
1018
1020{
1021 int ret = -1;
1022
1023 if (f) {
1024 fclose(f);
1025 f = NULL;
1026 }
1027
1028 if (pid > 0) {
1029 int status = 0;
1030 int i = 5;
1031 while (i > 0) {
1032 ret = waitpid(pid, &status, WNOHANG);
1033 if (ret < 0) {
1034 if (errno != EINTR && errno != ECHILD) {
1035 LOG_ERROR;
1036 break;
1037 }
1038 }
1039 else if (ret == pid)
1040 break;
1041 i--;
1042 cCondWait::SleepMs(100);
1043 }
1044 if (!i) {
1045 kill(pid, SIGKILL);
1046 ret = -1;
1047 }
1048 else if (ret == -1 || !WIFEXITED(status))
1049 ret = -1;
1050 pid = -1;
1051 }
1052
1053 return ret;
1054}
1055
1056// --- SystemExec ------------------------------------------------------------
1057
1058int SystemExec(const char *Command, bool Detached)
1059{
1060 pid_t pid;
1061
1062 if ((pid = fork()) < 0) { // fork failed
1063 LOG_ERROR_STR(Command);
1064 return -1;
1065 }
1066
1067 if (pid > 0) { // parent process
1068 int status = 0;
1069 if (waitpid(pid, &status, 0) < 0) {
1070 LOG_ERROR_STR(Command);
1071 return -1;
1072 }
1073 return status;
1074 }
1075 else { // child process
1076 if (Detached) {
1077 // Fork again and let first child die - grandchild stays alive without parent
1078 if (fork() > 0)
1079 _exit(0);
1080 // Start a new session
1081 pid_t sid = setsid();
1082 if (sid < 0)
1083 _exit(-1);
1084 // close STDIN and re-open as /dev/null
1085 int devnull = open("/dev/null", O_RDONLY);
1086 if (devnull < 0 || dup2(devnull, 0) < 0)
1087 _exit(-1);
1088 }
1089 int MaxPossibleFileDescriptors = getdtablesize();
1090 for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
1091 close(i); //close all dup'ed filedescriptors
1092 if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1)
1093 _exit(-1);
1094 _exit(0);
1095 }
1096}
static void BackTrace(cStringList &StringList, int Level=0, bool Mangled=false)
Produces a backtrace and stores it in the given StringList.
Definition thread.c:536
static cString GetCaller(int Level=0, bool Mangled=false)
Returns the caller at the given Level (or the immediate caller, if Level is 0).
Definition thread.c:559
static cString Demangle(char *s)
Demangles the function name in the given string and returns the converted version of s.
Definition thread.c:458
void Wait(cMutex &Mutex)
Definition thread.c:122
cCondVar(void)
Definition thread.c:111
bool TimedWait(cMutex &Mutex, int TimeoutMs)
Definition thread.c:135
void Broadcast(void)
Definition thread.c:155
pthread_cond_t cond
Definition thread.h:46
~cCondVar()
Definition thread.c:116
pthread_cond_t cond
Definition thread.h:22
bool signaled
Definition thread.h:23
cCondWait(void)
Definition thread.c:59
~cCondWait()
Definition thread.c:66
bool Wait(int TimeoutMs=0)
Waits at most TimeoutMs milliseconds for a call to Signal(), or forever if TimeoutMs is 0.
Definition thread.c:79
void Signal(void)
Signals a caller of Wait() that the condition it is waiting for is met.
Definition thread.c:101
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
Definition thread.c:73
pthread_mutex_t mutex
Definition thread.h:21
cIoThrottle(void)
Definition thread.c:912
static int count
Definition thread.h:271
void Activate(void)
Activates the global I/O throttling mechanism.
Definition thread.c:922
~cIoThrottle()
Definition thread.c:917
void Release(void)
Releases the global I/O throttling mechanism.
Definition thread.c:933
bool active
Definition thread.h:272
static bool Engaged(void)
Returns true if any I/O throttling object is currently active.
Definition thread.c:944
static cMutex mutex
Definition thread.h:270
cMutexLock(cMutex *Mutex=NULL)
Definition thread.c:404
bool Lock(cMutex *Mutex)
Definition thread.c:417
~cMutexLock()
Definition thread.c:411
cMutex * mutex
Definition thread.h:144
bool locked
Definition thread.h:145
void Lock(void)
Definition thread.c:228
tThreadId lockThreadId
Definition thread.h:71
pthread_mutex_t mutex
Definition thread.h:70
cMutex(void)
Definition thread.c:213
~cMutex()
Definition thread.c:223
int locked
Definition thread.h:72
void Unlock(void)
Definition thread.c:242
pid_t pid
Definition thread.h:295
int Close(void)
Definition thread.c:1019
FILE * f
Definition thread.h:296
bool Open(const char *Command, const char *Mode)
Definition thread.c:965
cPipe(void)
Definition thread.c:954
~cPipe()
Definition thread.c:960
char * Read(FILE *f)
Definition tools.c:1548
int locked
Definition thread.h:58
tThreadId writeLockThreadId
Definition thread.h:59
pthread_rwlock_t rwlock
Definition thread.h:57
cRwLock(bool PreferWriter=false)
Definition thread.c:162
bool Lock(bool Write, int TimeoutMs=0)
Definition thread.c:177
void Unlock(void)
Definition thread.c:199
~cRwLock()
Definition thread.c:172
cStateLock * stateLock
Definition thread.h:237
cStateKey(bool IgnoreFirst=false)
Sets up a new state key.
Definition thread.c:863
int state
Definition thread.h:239
void Remove(bool IncState=true)
Removes this key from the lock it was previously used with.
Definition thread.c:885
~cStateKey()
Definition thread.c:872
void Reset(void)
Resets the state of this key, so that the next call to a lock's Lock() function with this key will re...
Definition thread.c:880
bool timedOut
Definition thread.h:240
bool write
Definition thread.h:238
bool StateChanged(void)
Returns true if this key is used for obtaining a write lock, and the lock's state differs from that o...
Definition thread.c:895
cVector< int > flags
Definition thread.c:587
cVector< tThreadId > threadIds
Definition thread.c:586
cStateLockLog(void)
Definition thread.c:602
uint8_t logCounter[SLL_THREADS][SLL_MAX_LIST]
Definition thread.c:590
int logFlags[SLL_SIZE]
Definition thread.c:589
void Check(const char *Name, bool Lock, bool Write=false)
Definition thread.c:659
tThreadId logThreadIds[SLL_SIZE]
Definition thread.c:588
bool dumped
Definition thread.c:595
void Dump(const char *Name, tThreadId ThreadId)
Definition thread.c:614
cMutex mutex
Definition thread.c:585
tThreadId threadId
Definition thread.h:177
const char * name
Definition thread.h:176
cRwLock rwLock
Definition thread.h:178
int state
Definition thread.h:179
void SetExplicitModify(void)
If you have obtained a write lock on this lock, and you don't want its state to be automatically incr...
Definition thread.c:836
cStateLock(const char *Name=NULL)
Definition thread.c:731
friend class cStateKey
Definition thread.h:173
cStateKey * syncStateKey
Definition thread.h:181
int explicitModify
Definition thread.h:180
void Unlock(cStateKey &StateKey, bool IncState=true)
Releases a lock that has been obtained by a previous call to Lock() with the given StateKey.
Definition thread.c:785
void SetSyncStateKey(cStateKey &StateKey)
Sets the given StateKey to be synchronized to the state of this lock.
Definition thread.c:815
void SetModified(void)
Sets this lock to have its state incremented when the current write lock state key is removed.
Definition thread.c:851
bool Lock(cStateKey &StateKey, bool Write=false, int TimeoutMs=0)
Tries to get a lock and returns true if successful.
Definition thread.c:741
@ emDisabled
Definition thread.h:175
@ emEnabled
Definition thread.h:175
static cString static cString vsprintf(const char *fmt, va_list &ap)
Definition tools.c:1229
static cString sprintf(const char *fmt,...) __attribute__((format(printf
Definition tools.c:1216
cThreadLock(cThread *Thread=NULL)
Definition thread.c:430
bool Lock(cThread *Thread)
Definition thread.c:443
bool locked
Definition thread.h:161
~cThreadLock()
Definition thread.c:437
cThread * thread
Definition thread.h:160
virtual ~cThread()
Definition thread.c:265
void SetIOPriority(int Priority)
Definition thread.c:277
static void SetMainThreadId(void)
Definition thread.c:394
virtual void Action(void)=0
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
void bool Start(void)
Sets the description of this thread, which will be used when logging starting or stopping of the thre...
Definition thread.c:320
void SetDescription(const char *Description,...) __attribute__((format(printf
Definition thread.c:283
void SetPriority(int Priority)
Definition thread.c:271
bool active
Definition thread.h:83
void Lock(void)
Definition thread.h:95
tThreadId childThreadId
Definition thread.h:86
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
Definition thread.c:254
bool lowPriority
Definition thread.h:89
bool running
Definition thread.h:84
static void * StartThread(cThread *Thread)
Definition thread.c:295
void Cancel(int WaitSeconds=0)
Cancels the thread by first setting 'running' to false, so that the Action() loop can finish in an or...
Definition thread.c:370
static tThreadId mainThreadId
Definition thread.h:90
pthread_t childTid
Definition thread.h:85
bool Active(void)
Checks whether the thread is still alive.
Definition thread.c:345
static tThreadId ThreadId(void)
Definition thread.c:389
char * description
Definition thread.h:88
uint64_t Elapsed(void) const
Returns the number of milliseconds that have elapsed since the last call to Set().
Definition tools.c:829
bool TimedOut(void) const
Returns true if the number of milliseconds given in the last call to Set() have passed.
Definition tools.c:824
int Size(void) const
Definition tools.h:767
virtual void Append(T Data)
Definition tools.h:787
static cMutex Mutex
Definition epg.c:1439
#define BT_BUF_SIZE
Definition thread.c:456
static bool GetAbsTime(struct timespec *Abstime, int MillisecondsFromNow)
Definition thread.c:39
#define SLL_LENGTH
Definition thread.c:577
#define dbglockseq(n, l, w)
Definition thread.c:724
#define ABORT
Definition thread.c:27
#define SLL_MAX_LIST
Definition thread.c:579
#define THREAD_STOP_SLEEP
Definition thread.c:318
#define SLL_SIZE
Definition thread.c:576
int SystemExec(const char *Command, bool Detached)
Definition thread.c:1058
#define SLL_THREADS
Definition thread.c:578
#define SLL_LOCK_FLAG
Definition thread.c:581
#define SLL_WRITE_FLAG
Definition thread.c:580
#define THREAD_STOP_TIMEOUT
Definition thread.c:317
static cStateLockLog StateLockLog
Definition thread.c:722
#define dbglocking(a...)
Definition thread.c:36
pid_t tThreadId
Definition thread.h:17
cString DayDateTime(time_t t)
Converts the given time to a string of the form "www dd.mm. hh:mm".
Definition tools.c:1281
char * strn0cpy(char *dest, const char *src, size_t n)
Definition tools.c:135
#define LOG_ERROR_STR(s)
Definition tools.h:40
#define dsyslog(a...)
Definition tools.h:37
T max(T a, T b)
Definition tools.h:64
#define esyslog(a...)
Definition tools.h:35
#define LOG_ERROR
Definition tools.h:39