mirror of
https://github.com/vdr-projects/vdr.git
synced 2025-03-01 10:50:46 +00:00
- Updated the Estonian OSD texts (thanks to Arthur Konovalov). - Some cable providers don't mark short channel names according to the standard, but rather go their own way and use "name>short name". VDR now splits at this character for cable channels (thanks to Gerhard Steiner for reporting this one). - Added a check for Setup.DiSEqC in cDvbDevice::ProvidesTransponder(), otherwise the EPG scan didn't work on systems that don't use DiSEqC (thanks to Michael Reinelt for reporting this one). - Made the Makefile patch friendlier (thanks to Ludwig Nussel). - Made cOsd::isOpen an integer counter to avoid problems with messages when a cOsdObject uses the raw OSD (thanks to Andreas Regel for reporting this one). - Updated the Danish OSD texts (thanks to Mogens Elneff). - The file 'summary.vdr' has been replaced with 'info.vdr' and now contains the information about a recording, in the same format as the events are stored in 'epg.data' (see man vdr(5) for details). Existing summary files can be converted to the new format by running the Perl script 'summary2info.pl', as in summary2info.pl /video (the parameter given has to be the video directory). If there is no 'info.vdr' file for a recording, an attempt is made to read a 'summary.vdr'. - The "Summary" button in the "Recordings" menu has been renamed to "Info", and the page it brings up now shows the recording's information, much like the EPG event page. Therefore it now no longer uses the skin's SetText() function, but rather the SetRecording() function. Skin plugins may need to adjust that function accordingly (see skinsttng.c, for instance). - The SVDRP command LSTR now lists the recording information in the same tagged format as the LSTE command lists the EPG data. - The audio track menu now contains track descriptions when replaying (provided such descriptions were available in the EPG data when the recording was made, and are stored in the info.vdr file). - Avoiding extra blanks at the end of names of instant recordings. - Removed converting byte order on big endian systems from cDvbOsd::Flush(), which, according to Johannes Stezenbach and Paavo Hartikainen, is wrong. - Added cPlayer::DeviceSetVideoDisplayFormat() (thanks to Marco Schlüßler). - No longer saving the setup in case of a fatal error, to keep the volume level from being set to a wrong value (thanks to Marco Schlüßler). - Fixed a possible hangup when ending a replay session while cIndexFile::CatchUp() is waiting (thanks to Marco Schlüßler). - The SVDRP command DELR no longer deletes recordings that are currently being written to by a timer (thanks to Sascha Volkenandt for pointing out this one). - Pressing the "Play" key in live viewing mode now resumes a previous replay session (thanks to Mirko Dölle). - Now dropping EPG events that have a zero start time or duration (thanks to Oliver Endriss). - No longer stopping Transfer Mode or replay immediately when the Power button is pressed (thanks to Rolf Ahrenberg). - Moved the NPTL and UTF-8 checks after the version and help output (thanks to Andreas Kool for pointing out that 'vdr --version' failed on an UTF-8 system). - Made tChannelID::operator==() inline for better performance (thanks to Georg Acher). - Introduced cListBase::count for better performance (thanks to Georg Acher). - cEvent no longer stores the channelID directly, but rather has a pointer to the schedule it is in. - Now using hash tables to speed up cSchedule::GetEvent() (partially based on a patch from Georg Acher). - Avoiding unnecessary calls to getLength() in libsi/si.c, and avoiding the '& 0xff' in CRC32::crc32() of libsi/util.c (thanks to Georg Acher). - Speeded up deleting duplicate channels. - Fixed listing recordings with empty episode names in the LSTR command (thanks to Stefan Huelswitt for pointing this out). - Added cThread::SetPriority() and using it in cSectionHandler::Action() to reduce the priority of the section handler threads (as suggested by Georg Acher).
501 lines
11 KiB
C
501 lines
11 KiB
C
/*
|
|
* thread.c: A simple thread base class
|
|
*
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
* how to reach the author.
|
|
*
|
|
* $Id: thread.c 1.43 2005/05/29 11:40:30 kls Exp $
|
|
*/
|
|
|
|
#include "thread.h"
|
|
#include <errno.h>
|
|
#include <malloc.h>
|
|
#include <stdarg.h>
|
|
#include <sys/resource.h>
|
|
#include <sys/time.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
#include "tools.h"
|
|
|
|
static bool GetAbsTime(struct timespec *Abstime, int MillisecondsFromNow)
|
|
{
|
|
struct timeval now;
|
|
if (gettimeofday(&now, NULL) == 0) { // get current time
|
|
now.tv_usec += MillisecondsFromNow * 1000; // add the timeout
|
|
while (now.tv_usec >= 1000000) { // take care of an overflow
|
|
now.tv_sec++;
|
|
now.tv_usec -= 1000000;
|
|
}
|
|
Abstime->tv_sec = now.tv_sec; // seconds
|
|
Abstime->tv_nsec = now.tv_usec * 1000; // nano seconds
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// --- cCondWait -------------------------------------------------------------
|
|
|
|
cCondWait::cCondWait(void)
|
|
{
|
|
signaled = false;
|
|
pthread_mutex_init(&mutex, NULL);
|
|
pthread_cond_init(&cond, NULL);
|
|
}
|
|
|
|
cCondWait::~cCondWait()
|
|
{
|
|
pthread_cond_broadcast(&cond); // wake up any sleepers
|
|
pthread_cond_destroy(&cond);
|
|
pthread_mutex_destroy(&mutex);
|
|
}
|
|
|
|
void cCondWait::SleepMs(int TimeoutMs)
|
|
{
|
|
cCondWait w;
|
|
w.Wait(max(TimeoutMs, 3)); // making sure the time is >2ms to avoid a possible busy wait
|
|
}
|
|
|
|
bool cCondWait::Wait(int TimeoutMs)
|
|
{
|
|
pthread_mutex_lock(&mutex);
|
|
if (!signaled) {
|
|
if (TimeoutMs) {
|
|
struct timespec abstime;
|
|
if (GetAbsTime(&abstime, TimeoutMs)) {
|
|
while (!signaled) {
|
|
if (pthread_cond_timedwait(&cond, &mutex, &abstime) == ETIMEDOUT)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
pthread_cond_wait(&cond, &mutex);
|
|
}
|
|
bool r = signaled;
|
|
signaled = false;
|
|
pthread_mutex_unlock(&mutex);
|
|
return r;
|
|
}
|
|
|
|
void cCondWait::Signal(void)
|
|
{
|
|
pthread_mutex_lock(&mutex);
|
|
signaled = true;
|
|
pthread_cond_broadcast(&cond);
|
|
pthread_mutex_unlock(&mutex);
|
|
}
|
|
|
|
// --- cCondVar --------------------------------------------------------------
|
|
|
|
cCondVar::cCondVar(void)
|
|
{
|
|
pthread_cond_init(&cond, 0);
|
|
}
|
|
|
|
cCondVar::~cCondVar()
|
|
{
|
|
pthread_cond_broadcast(&cond); // wake up any sleepers
|
|
pthread_cond_destroy(&cond);
|
|
}
|
|
|
|
void cCondVar::Wait(cMutex &Mutex)
|
|
{
|
|
if (Mutex.locked) {
|
|
int locked = Mutex.locked;
|
|
Mutex.locked = 0; // have to clear the locked count here, as pthread_cond_wait
|
|
// does an implizit unlock of the mutex
|
|
pthread_cond_wait(&cond, &Mutex.mutex);
|
|
Mutex.locked = locked;
|
|
}
|
|
}
|
|
|
|
bool cCondVar::TimedWait(cMutex &Mutex, int TimeoutMs)
|
|
{
|
|
bool r = true; // true = condition signaled false = timeout
|
|
|
|
if (Mutex.locked) {
|
|
struct timespec abstime;
|
|
if (GetAbsTime(&abstime, TimeoutMs)) {
|
|
int locked = Mutex.locked;
|
|
Mutex.locked = 0; // have to clear the locked count here, as pthread_cond_timedwait
|
|
// does an implizit unlock of the mutex.
|
|
if (pthread_cond_timedwait(&cond, &Mutex.mutex, &abstime) == ETIMEDOUT)
|
|
r = false;
|
|
Mutex.locked = locked;
|
|
}
|
|
}
|
|
return r;
|
|
}
|
|
|
|
void cCondVar::Broadcast(void)
|
|
{
|
|
pthread_cond_broadcast(&cond);
|
|
}
|
|
|
|
// --- cRwLock ---------------------------------------------------------------
|
|
|
|
cRwLock::cRwLock(bool PreferWriter)
|
|
{
|
|
pthread_rwlockattr_t attr = { PreferWriter ? PTHREAD_RWLOCK_PREFER_WRITER_NP : PTHREAD_RWLOCK_PREFER_READER_NP };
|
|
pthread_rwlock_init(&rwlock, &attr);
|
|
}
|
|
|
|
cRwLock::~cRwLock()
|
|
{
|
|
pthread_rwlock_destroy(&rwlock);
|
|
}
|
|
|
|
bool cRwLock::Lock(bool Write, int TimeoutMs)
|
|
{
|
|
int Result = 0;
|
|
struct timespec abstime;
|
|
if (TimeoutMs) {
|
|
if (!GetAbsTime(&abstime, TimeoutMs))
|
|
TimeoutMs = 0;
|
|
}
|
|
if (Write)
|
|
Result = TimeoutMs ? pthread_rwlock_timedwrlock(&rwlock, &abstime) : pthread_rwlock_wrlock(&rwlock);
|
|
else
|
|
Result = TimeoutMs ? pthread_rwlock_timedrdlock(&rwlock, &abstime) : pthread_rwlock_rdlock(&rwlock);
|
|
return Result == 0;
|
|
}
|
|
|
|
void cRwLock::Unlock(void)
|
|
{
|
|
pthread_rwlock_unlock(&rwlock);
|
|
}
|
|
|
|
// --- cMutex ----------------------------------------------------------------
|
|
|
|
cMutex::cMutex(void)
|
|
{
|
|
locked = 0;
|
|
pthread_mutexattr_t attr = { PTHREAD_MUTEX_ERRORCHECK_NP };
|
|
pthread_mutex_init(&mutex, &attr);
|
|
}
|
|
|
|
cMutex::~cMutex()
|
|
{
|
|
pthread_mutex_destroy(&mutex);
|
|
}
|
|
|
|
void cMutex::Lock(void)
|
|
{
|
|
pthread_mutex_lock(&mutex);
|
|
locked++;
|
|
}
|
|
|
|
void cMutex::Unlock(void)
|
|
{
|
|
if (!--locked)
|
|
pthread_mutex_unlock(&mutex);
|
|
}
|
|
|
|
// --- cThread ---------------------------------------------------------------
|
|
|
|
bool cThread::emergencyExitRequested = false;
|
|
|
|
cThread::cThread(const char *Description)
|
|
{
|
|
running = false;
|
|
childTid = 0;
|
|
description = NULL;
|
|
SetDescription(Description);
|
|
}
|
|
|
|
cThread::~cThread()
|
|
{
|
|
free(description);
|
|
}
|
|
|
|
void cThread::SetPriority(int Priority)
|
|
{
|
|
if (setpriority(PRIO_PROCESS, 0, Priority) < 0)
|
|
LOG_ERROR;
|
|
}
|
|
|
|
void cThread::SetDescription(const char *Description, ...)
|
|
{
|
|
free(description);
|
|
description = NULL;
|
|
if (Description) {
|
|
va_list ap;
|
|
va_start(ap, Description);
|
|
vasprintf(&description, Description, ap);
|
|
va_end(ap);
|
|
}
|
|
}
|
|
|
|
void *cThread::StartThread(cThread *Thread)
|
|
{
|
|
if (Thread->description)
|
|
dsyslog("%s thread started (pid=%d, tid=%ld)", Thread->description, getpid(), pthread_self());
|
|
Thread->Action();
|
|
if (Thread->description)
|
|
dsyslog("%s thread ended (pid=%d, tid=%ld)", Thread->description, getpid(), pthread_self());
|
|
Thread->running = false;
|
|
return NULL;
|
|
}
|
|
|
|
bool cThread::Start(void)
|
|
{
|
|
if (!running) {
|
|
running = true;
|
|
if (pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this) == 0) {
|
|
pthread_detach(childTid); // auto-reap
|
|
pthread_setschedparam(childTid, SCHED_RR, 0);
|
|
}
|
|
else {
|
|
LOG_ERROR;
|
|
running = false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool cThread::Active(void)
|
|
{
|
|
if (running) {
|
|
//
|
|
// Single UNIX Spec v2 says:
|
|
//
|
|
// The pthread_kill() function is used to request
|
|
// that a signal be delivered to the specified thread.
|
|
//
|
|
// As in kill(), if sig is zero, error checking is
|
|
// performed but no signal is actually sent.
|
|
//
|
|
int err;
|
|
if ((err = pthread_kill(childTid, 0)) != 0) {
|
|
if (err != ESRCH)
|
|
LOG_ERROR;
|
|
childTid = 0;
|
|
running = false;
|
|
}
|
|
else
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void cThread::Cancel(int WaitSeconds)
|
|
{
|
|
if (running) {
|
|
if (WaitSeconds > 0) {
|
|
for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) {
|
|
if (!Active())
|
|
return;
|
|
cCondWait::SleepMs(10);
|
|
}
|
|
esyslog("ERROR: thread %ld won't end (waited %d seconds) - canceling it...", childTid, WaitSeconds);
|
|
}
|
|
pthread_cancel(childTid);
|
|
childTid = 0;
|
|
running = false;
|
|
}
|
|
}
|
|
|
|
bool cThread::EmergencyExit(bool Request)
|
|
{
|
|
if (!Request)
|
|
return emergencyExitRequested;
|
|
esyslog("initiating emergency exit");
|
|
return emergencyExitRequested = true; // yes, it's an assignment, not a comparison!
|
|
}
|
|
|
|
// --- cMutexLock ------------------------------------------------------------
|
|
|
|
cMutexLock::cMutexLock(cMutex *Mutex)
|
|
{
|
|
mutex = NULL;
|
|
locked = false;
|
|
Lock(Mutex);
|
|
}
|
|
|
|
cMutexLock::~cMutexLock()
|
|
{
|
|
if (mutex && locked)
|
|
mutex->Unlock();
|
|
}
|
|
|
|
bool cMutexLock::Lock(cMutex *Mutex)
|
|
{
|
|
if (Mutex && !mutex) {
|
|
mutex = Mutex;
|
|
Mutex->Lock();
|
|
locked = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// --- cThreadLock -----------------------------------------------------------
|
|
|
|
cThreadLock::cThreadLock(cThread *Thread)
|
|
{
|
|
thread = NULL;
|
|
locked = false;
|
|
Lock(Thread);
|
|
}
|
|
|
|
cThreadLock::~cThreadLock()
|
|
{
|
|
if (thread && locked)
|
|
thread->Unlock();
|
|
}
|
|
|
|
bool cThreadLock::Lock(cThread *Thread)
|
|
{
|
|
if (Thread && !thread) {
|
|
thread = Thread;
|
|
Thread->Lock();
|
|
locked = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// --- cPipe -----------------------------------------------------------------
|
|
|
|
// cPipe::Open() and cPipe::Close() are based on code originally received from
|
|
// Andreas Vitting <Andreas@huji.de>
|
|
|
|
cPipe::cPipe(void)
|
|
{
|
|
pid = -1;
|
|
f = NULL;
|
|
}
|
|
|
|
cPipe::~cPipe()
|
|
{
|
|
Close();
|
|
}
|
|
|
|
bool cPipe::Open(const char *Command, const char *Mode)
|
|
{
|
|
int fd[2];
|
|
|
|
if (pipe(fd) < 0) {
|
|
LOG_ERROR;
|
|
return false;
|
|
}
|
|
if ((pid = fork()) < 0) { // fork failed
|
|
LOG_ERROR;
|
|
close(fd[0]);
|
|
close(fd[1]);
|
|
return false;
|
|
}
|
|
|
|
char *mode = "w";
|
|
int iopipe = 0;
|
|
|
|
if (pid > 0) { // parent process
|
|
if (strcmp(Mode, "r") == 0) {
|
|
mode = "r";
|
|
iopipe = 1;
|
|
}
|
|
close(fd[iopipe]);
|
|
f = fdopen(fd[1 - iopipe], mode);
|
|
if ((f = fdopen(fd[1 - iopipe], mode)) == NULL) {
|
|
LOG_ERROR;
|
|
close(fd[1 - iopipe]);
|
|
}
|
|
return f != NULL;
|
|
}
|
|
else { // child process
|
|
int iofd = STDOUT_FILENO;
|
|
if (strcmp(Mode, "w") == 0) {
|
|
mode = "r";
|
|
iopipe = 1;
|
|
iofd = STDIN_FILENO;
|
|
}
|
|
close(fd[iopipe]);
|
|
if (dup2(fd[1 - iopipe], iofd) == -1) { // now redirect
|
|
LOG_ERROR;
|
|
close(fd[1 - iopipe]);
|
|
_exit(-1);
|
|
}
|
|
else {
|
|
int MaxPossibleFileDescriptors = getdtablesize();
|
|
for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
|
|
close(i); //close all dup'ed filedescriptors
|
|
if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1) {
|
|
LOG_ERROR_STR(Command);
|
|
close(fd[1 - iopipe]);
|
|
_exit(-1);
|
|
}
|
|
}
|
|
_exit(0);
|
|
}
|
|
}
|
|
|
|
int cPipe::Close(void)
|
|
{
|
|
int ret = -1;
|
|
|
|
if (f) {
|
|
fclose(f);
|
|
f = NULL;
|
|
}
|
|
|
|
if (pid > 0) {
|
|
int status = 0;
|
|
int i = 5;
|
|
while (i > 0) {
|
|
ret = waitpid(pid, &status, WNOHANG);
|
|
if (ret < 0) {
|
|
if (errno != EINTR && errno != ECHILD) {
|
|
LOG_ERROR;
|
|
break;
|
|
}
|
|
}
|
|
else if (ret == pid)
|
|
break;
|
|
i--;
|
|
cCondWait::SleepMs(100);
|
|
}
|
|
if (!i) {
|
|
kill(pid, SIGKILL);
|
|
ret = -1;
|
|
}
|
|
else if (ret == -1 || !WIFEXITED(status))
|
|
ret = -1;
|
|
pid = -1;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
// --- SystemExec ------------------------------------------------------------
|
|
|
|
int SystemExec(const char *Command)
|
|
{
|
|
pid_t pid;
|
|
|
|
if ((pid = fork()) < 0) { // fork failed
|
|
LOG_ERROR;
|
|
return -1;
|
|
}
|
|
|
|
if (pid > 0) { // parent process
|
|
int status;
|
|
if (waitpid(pid, &status, 0) < 0) {
|
|
LOG_ERROR;
|
|
return -1;
|
|
}
|
|
return status;
|
|
}
|
|
else { // child process
|
|
int MaxPossibleFileDescriptors = getdtablesize();
|
|
for (int i = STDERR_FILENO + 1; i < MaxPossibleFileDescriptors; i++)
|
|
close(i); //close all dup'ed filedescriptors
|
|
if (execl("/bin/sh", "sh", "-c", Command, NULL) == -1) {
|
|
LOG_ERROR_STR(Command);
|
|
_exit(-1);
|
|
}
|
|
_exit(0);
|
|
}
|
|
}
|
|
|