2000-10-08 09:25:20 +02:00
|
|
|
/*
|
|
|
|
* thread.h: A simple thread base class
|
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2020-03-29 15:53:48 +02:00
|
|
|
* $Id: thread.h 4.5 2020/03/29 15:53:48 kls Exp $
|
2000-10-08 09:25:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __THREAD_H
|
|
|
|
#define __THREAD_H
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2001-09-15 13:00:58 +02:00
|
|
|
#include <stdio.h>
|
2000-10-08 09:25:20 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2016-12-08 10:18:32 +01:00
|
|
|
typedef pid_t tThreadId;
|
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
class cCondWait {
|
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
pthread_cond_t cond;
|
|
|
|
bool signaled;
|
|
|
|
public:
|
|
|
|
cCondWait(void);
|
|
|
|
~cCondWait();
|
2004-10-24 11:12:05 +02:00
|
|
|
static void SleepMs(int TimeoutMs);
|
|
|
|
///< Creates a cCondWait object and uses it to sleep for TimeoutMs
|
|
|
|
///< milliseconds, immediately giving up the calling thread's time
|
|
|
|
///< slice and thus avoiding a "busy wait".
|
2005-01-14 14:08:47 +01:00
|
|
|
///< In order to avoid a possible busy wait, TimeoutMs will be automatically
|
|
|
|
///< limited to values >2.
|
2004-10-16 09:36:28 +02:00
|
|
|
bool Wait(int TimeoutMs = 0);
|
|
|
|
///< Waits at most TimeoutMs milliseconds for a call to Signal(), or
|
|
|
|
///< forever if TimeoutMs is 0.
|
2020-03-29 15:53:48 +02:00
|
|
|
///< Returns true if Signal() has been called, false if the given
|
2004-10-16 09:36:28 +02:00
|
|
|
///< timeout has expired.
|
|
|
|
void Signal(void);
|
|
|
|
///< Signals a caller of Wait() that the condition it is waiting for is met.
|
|
|
|
};
|
|
|
|
|
2001-08-03 14:18:08 +02:00
|
|
|
class cMutex;
|
|
|
|
|
|
|
|
class cCondVar {
|
|
|
|
private:
|
|
|
|
pthread_cond_t cond;
|
|
|
|
public:
|
|
|
|
cCondVar(void);
|
|
|
|
~cCondVar();
|
2002-08-15 11:46:22 +02:00
|
|
|
void Wait(cMutex &Mutex);
|
|
|
|
bool TimedWait(cMutex &Mutex, int TimeoutMs);
|
2001-08-03 14:18:08 +02:00
|
|
|
void Broadcast(void);
|
|
|
|
};
|
|
|
|
|
2004-01-04 12:30:00 +01:00
|
|
|
class cRwLock {
|
2003-12-22 13:29:24 +01:00
|
|
|
private:
|
|
|
|
pthread_rwlock_t rwlock;
|
2016-12-08 10:18:32 +01:00
|
|
|
int locked;
|
|
|
|
tThreadId writeLockThreadId;
|
2003-12-22 13:29:24 +01:00
|
|
|
public:
|
2004-01-04 12:30:00 +01:00
|
|
|
cRwLock(bool PreferWriter = false);
|
|
|
|
~cRwLock();
|
2003-12-22 13:29:24 +01:00
|
|
|
bool Lock(bool Write, int TimeoutMs = 0);
|
|
|
|
void Unlock(void);
|
|
|
|
};
|
|
|
|
|
2000-11-18 13:57:32 +01:00
|
|
|
class cMutex {
|
2001-08-03 14:18:08 +02:00
|
|
|
friend class cCondVar;
|
2000-11-18 13:57:32 +01:00
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex;
|
2001-06-02 10:47:40 +02:00
|
|
|
int locked;
|
2000-11-18 13:57:32 +01:00
|
|
|
public:
|
2001-06-02 10:47:40 +02:00
|
|
|
cMutex(void);
|
|
|
|
~cMutex();
|
|
|
|
void Lock(void);
|
|
|
|
void Unlock(void);
|
2000-11-18 13:57:32 +01:00
|
|
|
};
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
class cThread {
|
|
|
|
friend class cThreadLock;
|
|
|
|
private:
|
2005-08-13 13:17:24 +02:00
|
|
|
bool active;
|
2005-08-14 11:24:57 +02:00
|
|
|
bool running;
|
2004-12-19 10:58:20 +01:00
|
|
|
pthread_t childTid;
|
2006-01-04 15:01:22 +01:00
|
|
|
tThreadId childThreadId;
|
2001-10-27 13:23:06 +02:00
|
|
|
cMutex mutex;
|
2003-10-18 12:29:08 +02:00
|
|
|
char *description;
|
2012-10-04 12:32:31 +02:00
|
|
|
bool lowPriority;
|
2005-11-27 15:57:03 +01:00
|
|
|
static tThreadId mainThreadId;
|
2000-10-08 09:25:20 +02:00
|
|
|
static void *StartThread(cThread *Thread);
|
2002-06-16 12:57:31 +02:00
|
|
|
protected:
|
2005-05-29 11:44:52 +02:00
|
|
|
void SetPriority(int Priority);
|
2009-04-13 13:55:23 +02:00
|
|
|
void SetIOPriority(int Priority);
|
2001-10-27 13:23:06 +02:00
|
|
|
void Lock(void) { mutex.Lock(); }
|
|
|
|
void Unlock(void) { mutex.Unlock(); }
|
2000-10-08 09:25:20 +02:00
|
|
|
virtual void Action(void) = 0;
|
2005-08-13 13:17:24 +02:00
|
|
|
///< A derived cThread class must implement the code it wants to
|
|
|
|
///< execute as a separate thread in this function. If this is
|
2005-08-14 11:24:57 +02:00
|
|
|
///< a loop, it must check Running() repeatedly to see whether
|
2005-08-13 13:17:24 +02:00
|
|
|
///< it's time to stop.
|
2005-08-14 11:24:57 +02:00
|
|
|
bool Running(void) { return running; }
|
2005-08-13 13:17:24 +02:00
|
|
|
///< Returns false if a derived cThread object shall leave its Action()
|
|
|
|
///< function.
|
2000-12-08 16:23:32 +01:00
|
|
|
void Cancel(int WaitSeconds = 0);
|
2005-08-14 11:24:57 +02:00
|
|
|
///< Cancels the thread by first setting 'running' to false, so that
|
2005-08-13 13:17:24 +02:00
|
|
|
///< the Action() loop can finish in an orderly fashion and then waiting
|
|
|
|
///< up to WaitSeconds seconds for the thread to actually end. If the
|
|
|
|
///< thread doesn't end by itself, it is killed.
|
2006-09-24 12:54:47 +02:00
|
|
|
///< If WaitSeconds is -1, only 'running' is set to false and Cancel()
|
|
|
|
///< returns immediately, without killing the thread.
|
2000-10-08 09:25:20 +02:00
|
|
|
public:
|
2012-10-04 12:32:31 +02:00
|
|
|
cThread(const char *Description = NULL, bool LowPriority = false);
|
2005-08-13 13:17:24 +02:00
|
|
|
///< Creates a new thread.
|
|
|
|
///< If Description is present, a log file entry will be made when
|
2015-01-14 11:39:55 +01:00
|
|
|
///< the thread starts and stops (see SetDescription()).
|
|
|
|
///< The Start() function must be called to actually start the thread.
|
2012-10-04 12:32:31 +02:00
|
|
|
///< LowPriority can be set to true to make this thread run at a lower
|
|
|
|
///< priority.
|
2000-10-08 09:25:20 +02:00
|
|
|
virtual ~cThread();
|
2005-10-09 11:14:14 +02:00
|
|
|
void SetDescription(const char *Description, ...) __attribute__ ((format (printf, 2, 3)));
|
2015-01-14 11:39:55 +01:00
|
|
|
///< Sets the description of this thread, which will be used when logging
|
|
|
|
///< starting or stopping of the thread. Make sure any important information
|
|
|
|
///< is within the first 15 characters of Description, because only these
|
|
|
|
///< may be displayed in thread listings (like 'htop', for instance).
|
2000-10-08 09:25:20 +02:00
|
|
|
bool Start(void);
|
2005-08-13 13:17:24 +02:00
|
|
|
///< Actually starts the thread.
|
2007-01-07 14:46:14 +01:00
|
|
|
///< If the thread is already running, nothing happens.
|
2005-08-14 11:24:57 +02:00
|
|
|
bool Active(void);
|
|
|
|
///< Checks whether the thread is still alive.
|
2005-12-11 12:10:28 +01:00
|
|
|
static tThreadId ThreadId(void);
|
2005-11-27 15:57:03 +01:00
|
|
|
static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; }
|
2006-01-03 10:20:41 +01:00
|
|
|
static void SetMainThreadId(void);
|
2000-10-08 09:25:20 +02:00
|
|
|
};
|
2002-02-23 13:55:57 +01:00
|
|
|
|
|
|
|
// cMutexLock can be used to easily set a lock on mutex and make absolutely
|
|
|
|
// sure that it will be unlocked when the block will be left. Several locks can
|
|
|
|
// be stacked, so a function that makes many calls to another function which uses
|
|
|
|
// cMutexLock may itself use a cMutexLock to make one longer lock instead of many
|
|
|
|
// short ones.
|
|
|
|
|
|
|
|
class cMutexLock {
|
|
|
|
private:
|
|
|
|
cMutex *mutex;
|
|
|
|
bool locked;
|
|
|
|
public:
|
|
|
|
cMutexLock(cMutex *Mutex = NULL);
|
|
|
|
~cMutexLock();
|
|
|
|
bool Lock(cMutex *Mutex);
|
|
|
|
};
|
2000-10-08 09:25:20 +02:00
|
|
|
|
|
|
|
// cThreadLock can be used to easily set a lock in a thread and make absolutely
|
|
|
|
// sure that it will be unlocked when the block will be left. Several locks can
|
|
|
|
// be stacked, so a function that makes many calls to another function which uses
|
|
|
|
// cThreadLock may itself use a cThreadLock to make one longer lock instead of many
|
|
|
|
// short ones.
|
|
|
|
|
|
|
|
class cThreadLock {
|
|
|
|
private:
|
|
|
|
cThread *thread;
|
|
|
|
bool locked;
|
|
|
|
public:
|
2000-10-29 13:17:22 +01:00
|
|
|
cThreadLock(cThread *Thread = NULL);
|
2000-10-08 09:25:20 +02:00
|
|
|
~cThreadLock();
|
2000-10-29 13:17:22 +01:00
|
|
|
bool Lock(cThread *Thread);
|
2000-10-08 09:25:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define LOCK_THREAD cThreadLock ThreadLock(this)
|
|
|
|
|
2015-09-01 11:14:27 +02:00
|
|
|
class cStateKey;
|
|
|
|
|
|
|
|
class cStateLock {
|
|
|
|
friend class cStateKey;
|
|
|
|
private:
|
2018-03-04 11:31:21 +01:00
|
|
|
enum { emDisabled = 0, emArmed, emEnabled };
|
2015-09-01 11:14:27 +02:00
|
|
|
const char *name;
|
|
|
|
tThreadId threadId;
|
|
|
|
cRwLock rwLock;
|
|
|
|
int state;
|
2018-03-04 11:31:21 +01:00
|
|
|
int explicitModify;
|
|
|
|
cStateKey *syncStateKey;
|
2015-09-01 11:14:27 +02:00
|
|
|
void Unlock(cStateKey &StateKey, bool IncState = true);
|
|
|
|
///< Releases a lock that has been obtained by a previous call to Lock()
|
|
|
|
///< with the given StateKey. If this was a write-lock, and IncState is true,
|
|
|
|
///< the state of the lock will be incremented. In any case, the (new) state
|
|
|
|
///< of the lock will be copied to the StateKey's state.
|
|
|
|
public:
|
|
|
|
cStateLock(const char *Name = NULL);
|
|
|
|
bool Lock(cStateKey &StateKey, bool Write = false, int TimeoutMs = 0);
|
|
|
|
///< Tries to get a lock and returns true if successful.
|
|
|
|
///< If TimoutMs is not 0, it waits for the given number of milliseconds
|
|
|
|
///< and returns false if no lock has been obtained within that time.
|
|
|
|
///< Otherwise it waits indefinitely for the lock. The given StateKey
|
|
|
|
///< will store which lock it has been used with, and will use that
|
|
|
|
///< information when its Remove() function is called.
|
|
|
|
///< There are two possible locks, one only for read access, and one
|
|
|
|
///< for reading and writing:
|
|
|
|
///<
|
|
|
|
///< If Write is false (i.e. a read-lock is requested), the lock's state
|
|
|
|
///< is compared to the given StateKey's state, and true is returned if
|
|
|
|
///< they differ.
|
|
|
|
///< If true is returned, the read-lock is still in place and the
|
|
|
|
///< protected data structures can be safely accessed (in read-only mode!).
|
|
|
|
///< Once the necessary operations have been performed, the lock must
|
|
|
|
///< be released by a call to the StateKey's Remove() function.
|
|
|
|
///< If false is returned, the state has not changed since the last check,
|
|
|
|
///< and the read-lock has been released. In that case the protected
|
|
|
|
///< data structures have not changed since the last call, so no action
|
|
|
|
///< is required. Note that if TimeoutMs is used with read-locks, Lock()
|
|
|
|
///< might return false even if the states of lock and key differ, just
|
|
|
|
///< because it was unable to obtain the lock within the given time.
|
|
|
|
///< You can call cStateKey::TimedOut() to detect this.
|
|
|
|
///<
|
|
|
|
///< If Write is true (i.e. a write-lock is requested), the states of the
|
|
|
|
///< lock and the given StateKey don't matter, it will always try to obtain
|
|
|
|
///< a write lock.
|
2018-03-04 11:31:21 +01:00
|
|
|
void SetSyncStateKey(cStateKey &StateKey);
|
|
|
|
///< Sets the given StateKey to be synchronized to the state of this lock.
|
|
|
|
///< The caller must currenty hold a write lock on this lock, with a cStateKey
|
|
|
|
///< that is different from the given StateKey. If, when removing the key that
|
|
|
|
///< is holding the write lock, the StateKey's current state is the same as that
|
|
|
|
///< of the lock, it will be increased together with the lock's state.
|
|
|
|
void SetExplicitModify(void);
|
2015-09-01 11:14:27 +02:00
|
|
|
///< If you have obtained a write lock on this lock, and you don't want its
|
|
|
|
///< state to be automatically incremented when the lock is released, a call to
|
2018-03-04 11:31:21 +01:00
|
|
|
///< this function will disable this, and you can explicitly call SetModified()
|
2015-09-01 11:14:27 +02:00
|
|
|
///< to increment the state.
|
2018-03-04 11:31:21 +01:00
|
|
|
void SetModified(void);
|
|
|
|
///< Sets this lock to have its state incremented when the current write lock
|
|
|
|
///< state key is removed. Must have called SetExplicitModify() before calling
|
|
|
|
///< this function.
|
2015-09-01 11:14:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class cStateKey {
|
|
|
|
friend class cStateLock;
|
|
|
|
private:
|
|
|
|
cStateLock *stateLock;
|
|
|
|
bool write;
|
|
|
|
int state;
|
|
|
|
bool timedOut;
|
|
|
|
public:
|
|
|
|
cStateKey(bool IgnoreFirst = false);
|
|
|
|
///< Sets up a new state key. If IgnoreFirst is true, the first use
|
|
|
|
///< of this key with a lock will not return true if the lock's state
|
|
|
|
///< hasn't explicitly changed.
|
|
|
|
~cStateKey();
|
|
|
|
void Reset(void);
|
|
|
|
///< Resets the state of this key, so that the next call to a lock's
|
|
|
|
///< Lock() function with this key will return true, even if the
|
|
|
|
///< lock's state hasn't changed.
|
|
|
|
void Remove(bool IncState = true);
|
|
|
|
///< Removes this key from the lock it was previously used with.
|
|
|
|
///< If this key was used to obtain a write lock, the state of the lock will
|
|
|
|
///< be incremented and copied to this key. You can set IncState to false
|
|
|
|
///< to prevent this.
|
|
|
|
bool StateChanged(void);
|
|
|
|
///< Returns true if this key is used for obtaining a write lock, and the
|
|
|
|
///< lock's state differs from that of the key. When used with a read lock,
|
|
|
|
///< it always returns true, because otherwise the lock wouldn't have been
|
|
|
|
///< obtained in the first place.
|
|
|
|
bool InLock(void) { return stateLock; }
|
|
|
|
///< Returns true if this key is currently in a lock.
|
|
|
|
bool TimedOut(void) const { return timedOut; }
|
|
|
|
///< Returns true if the last lock attempt this key was used with failed due
|
|
|
|
///< to a timeout.
|
|
|
|
};
|
|
|
|
|
2012-09-22 11:52:33 +02:00
|
|
|
class cIoThrottle {
|
|
|
|
private:
|
|
|
|
static cMutex mutex;
|
|
|
|
static int count;
|
|
|
|
bool active;
|
|
|
|
public:
|
|
|
|
cIoThrottle(void);
|
|
|
|
~cIoThrottle();
|
|
|
|
void Activate(void);
|
|
|
|
///< Activates the global I/O throttling mechanism.
|
|
|
|
///< This function may be called any number of times, but only
|
|
|
|
///< the first call after an inactive state will have an effect.
|
|
|
|
void Release(void);
|
|
|
|
///< Releases the global I/O throttling mechanism.
|
|
|
|
///< This function may be called any number of times, but only
|
|
|
|
///< the first call after an active state will have an effect.
|
|
|
|
bool Active(void) { return active; }
|
|
|
|
///< Returns true if this I/O throttling object is currently active.
|
|
|
|
static bool Engaged(void);
|
|
|
|
///< Returns true if any I/O throttling object is currently active.
|
|
|
|
};
|
|
|
|
|
2001-09-15 13:00:58 +02:00
|
|
|
// cPipe implements a pipe that closes all unnecessary file descriptors in
|
|
|
|
// the child process.
|
|
|
|
|
|
|
|
class cPipe {
|
|
|
|
private:
|
|
|
|
pid_t pid;
|
|
|
|
FILE *f;
|
|
|
|
public:
|
|
|
|
cPipe(void);
|
|
|
|
~cPipe();
|
|
|
|
operator FILE* () { return f; }
|
|
|
|
bool Open(const char *Command, const char *Mode);
|
|
|
|
int Close(void);
|
|
|
|
};
|
|
|
|
|
2017-06-03 12:43:22 +02:00
|
|
|
// cBackTrace can be used for debugging.
|
|
|
|
|
|
|
|
class cStringList;
|
|
|
|
class cString;
|
|
|
|
|
|
|
|
class cBackTrace {
|
|
|
|
public:
|
|
|
|
static cString Demangle(char *s);
|
|
|
|
///< Demangles the function name in the given string and returns the converted
|
|
|
|
///< version of s. s must be one of the strings returned by a call to
|
|
|
|
///< BackTrace() or GetCaller().
|
|
|
|
///< Note that this function works on the given string by inserting '\0'
|
|
|
|
///< characters to separate the individual parts. Therefore the string
|
|
|
|
///< will be modified upon return.
|
|
|
|
static void BackTrace(cStringList &StringList, int Level = 0, bool Mangled = false);
|
|
|
|
///< Produces a backtrace and stores it in the given StringList.
|
|
|
|
///< If Level is given, only calls up to the given value are listed.
|
|
|
|
///< If Mangled is true, the raw backtrace will be returned and you can use
|
|
|
|
///< Demangle() to make the function names readable.
|
|
|
|
static void BackTrace(FILE *f = NULL, int Level = 0, bool Mangled = false);
|
|
|
|
///< Produces a backtrace beginning at the given Level, and
|
|
|
|
///< writes it to the given file. If no file is given, the backtrace is
|
|
|
|
///< written to the logfile. If Mangled is true, the raw backtrace will
|
|
|
|
///< be printed/logged.
|
|
|
|
static cString GetCaller(int Level = 0, bool Mangled = false);
|
|
|
|
///< Returns the caller at the given Level (or the immediate caller,
|
|
|
|
///< if Level is 0).
|
|
|
|
///< If Mangled is true, the raw backtrace will be returned and you can use
|
|
|
|
///< Demangle() to make the function name readable.
|
|
|
|
};
|
|
|
|
|
2001-10-20 10:39:27 +02:00
|
|
|
// SystemExec() implements a 'system()' call that closes all unnecessary file
|
|
|
|
// descriptors in the child process.
|
2007-02-25 10:56:29 +01:00
|
|
|
// With Detached=true, calls command in background and in a separate session,
|
|
|
|
// with stdin connected to /dev/null.
|
2001-10-20 10:39:27 +02:00
|
|
|
|
2007-02-25 10:56:29 +01:00
|
|
|
int SystemExec(const char *Command, bool Detached = false);
|
2001-10-20 10:39:27 +02:00
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
#endif //__THREAD_H
|