2007-11-06 21:41:16 +00:00
|
|
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|
|
|
|
|
|
|
|
|
/*
|
2012-09-09 16:57:42 +01:00
|
|
|
Rubber Band Library
|
2007-11-06 21:41:16 +00:00
|
|
|
An audio time-stretching and pitch-shifting library.
|
2018-03-07 13:10:19 +00:00
|
|
|
Copyright 2007-2018 Particular Programs Ltd.
|
2012-09-09 16:57:42 +01:00
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version. See the file
|
|
|
|
|
COPYING included with this distribution for more information.
|
2012-09-09 16:57:42 +01:00
|
|
|
|
|
|
|
|
Alternatively, if you have a valid commercial licence for the
|
|
|
|
|
Rubber Band Library obtained by agreement with the copyright
|
|
|
|
|
holders, you may redistribute and/or modify it under the terms
|
|
|
|
|
described in that licence.
|
|
|
|
|
|
|
|
|
|
If you wish to distribute code using the Rubber Band Library
|
|
|
|
|
under terms other than those of the GNU General Public License,
|
|
|
|
|
you must obtain a valid commercial licence before doing so.
|
2007-11-06 21:41:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef _RUBBERBAND_THREAD_H_
|
|
|
|
|
#define _RUBBERBAND_THREAD_H_
|
|
|
|
|
|
2011-12-09 18:18:32 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
2012-09-09 16:57:42 +01:00
|
|
|
#ifndef NO_THREADING
|
2011-12-09 18:18:32 +00:00
|
|
|
|
2007-11-28 11:50:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#else /* !_WIN32 */
|
2012-09-09 16:57:42 +01:00
|
|
|
#ifdef USE_PTHREADS
|
2007-11-06 21:41:16 +00:00
|
|
|
#include <pthread.h>
|
2012-09-09 16:57:42 +01:00
|
|
|
#else /* !USE_PTHREADS */
|
|
|
|
|
#error No thread implementation selected
|
|
|
|
|
#endif /* !USE_PTHREADS */
|
2007-11-28 11:50:47 +00:00
|
|
|
#endif /* !_WIN32 */
|
2007-11-06 21:41:16 +00:00
|
|
|
|
2008-05-22 16:54:27 +00:00
|
|
|
//#define DEBUG_THREAD 1
|
|
|
|
|
//#define DEBUG_MUTEX 1
|
|
|
|
|
//#define DEBUG_CONDITION 1
|
|
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
namespace RubberBand
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
|
{
|
|
|
|
|
public:
|
2007-11-28 11:50:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
typedef HANDLE Id;
|
|
|
|
|
#else
|
2012-09-09 16:57:42 +01:00
|
|
|
#ifdef USE_PTHREADS
|
2007-11-06 21:41:16 +00:00
|
|
|
typedef pthread_t Id;
|
2012-09-09 16:57:42 +01:00
|
|
|
#endif
|
2007-11-28 11:50:47 +00:00
|
|
|
#endif
|
2007-11-06 21:41:16 +00:00
|
|
|
|
|
|
|
|
Thread();
|
|
|
|
|
virtual ~Thread();
|
|
|
|
|
|
|
|
|
|
Id id();
|
|
|
|
|
|
|
|
|
|
void start();
|
|
|
|
|
void wait();
|
|
|
|
|
|
|
|
|
|
static bool threadingAvailable();
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual void run() = 0;
|
|
|
|
|
|
|
|
|
|
private:
|
2007-11-28 11:50:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
HANDLE m_id;
|
|
|
|
|
bool m_extant;
|
|
|
|
|
static DWORD WINAPI staticRun(LPVOID lpParam);
|
|
|
|
|
#else
|
2012-09-09 16:57:42 +01:00
|
|
|
#ifdef USE_PTHREADS
|
2007-11-06 21:41:16 +00:00
|
|
|
pthread_t m_id;
|
|
|
|
|
bool m_extant;
|
|
|
|
|
static void *staticRun(void *);
|
2007-11-28 11:50:47 +00:00
|
|
|
#endif
|
2012-09-09 16:57:42 +01:00
|
|
|
#endif
|
2007-11-06 21:41:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Mutex
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Mutex();
|
|
|
|
|
~Mutex();
|
|
|
|
|
|
|
|
|
|
void lock();
|
|
|
|
|
void unlock();
|
|
|
|
|
bool trylock();
|
|
|
|
|
|
|
|
|
|
private:
|
2007-11-28 11:50:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
HANDLE m_mutex;
|
2008-05-22 16:54:27 +00:00
|
|
|
#ifndef NO_THREAD_CHECKS
|
|
|
|
|
DWORD m_lockedBy;
|
|
|
|
|
#endif
|
2007-11-28 11:50:47 +00:00
|
|
|
#else
|
2012-09-09 16:57:42 +01:00
|
|
|
#ifdef USE_PTHREADS
|
2007-11-06 21:41:16 +00:00
|
|
|
pthread_mutex_t m_mutex;
|
2008-05-22 16:54:27 +00:00
|
|
|
#ifndef NO_THREAD_CHECKS
|
|
|
|
|
pthread_t m_lockedBy;
|
2007-11-26 11:00:47 +00:00
|
|
|
bool m_locked;
|
2007-11-28 11:50:47 +00:00
|
|
|
#endif
|
2008-05-22 16:54:27 +00:00
|
|
|
#endif
|
2012-09-09 16:57:42 +01:00
|
|
|
#endif
|
2007-11-06 21:41:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MutexLocker
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MutexLocker(Mutex *);
|
|
|
|
|
~MutexLocker();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Mutex *m_mutex;
|
|
|
|
|
};
|
|
|
|
|
|
2011-03-19 12:41:38 +00:00
|
|
|
/**
|
|
|
|
|
The Condition class bundles a condition variable and mutex.
|
|
|
|
|
|
|
|
|
|
To wait on a condition, call lock(), test the termination condition
|
|
|
|
|
if desired, then wait(). The condition will be unlocked during the
|
|
|
|
|
wait and re-locked when wait() returns (which will happen when the
|
|
|
|
|
condition is signalled or the timer times out).
|
|
|
|
|
|
|
|
|
|
To signal a condition, call signal(). If the condition is signalled
|
|
|
|
|
between lock() and wait(), the signal may be missed by the waiting
|
|
|
|
|
thread. To avoid this, the signalling thread should also lock the
|
|
|
|
|
condition before calling signal() and unlock it afterwards.
|
|
|
|
|
*/
|
|
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
class Condition
|
|
|
|
|
{
|
|
|
|
|
public:
|
2007-11-26 21:51:05 +00:00
|
|
|
Condition(std::string name);
|
2007-11-06 21:41:16 +00:00
|
|
|
~Condition();
|
|
|
|
|
|
|
|
|
|
void lock();
|
2007-11-26 11:00:47 +00:00
|
|
|
void unlock();
|
2007-11-06 21:41:16 +00:00
|
|
|
void wait(int us = 0);
|
2007-11-28 11:50:47 +00:00
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
void signal();
|
|
|
|
|
|
|
|
|
|
private:
|
2008-05-22 16:54:27 +00:00
|
|
|
|
2007-11-28 11:50:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
HANDLE m_mutex;
|
|
|
|
|
HANDLE m_condition;
|
2008-05-22 16:54:27 +00:00
|
|
|
bool m_locked;
|
2007-11-28 11:50:47 +00:00
|
|
|
#else
|
2012-09-09 16:57:42 +01:00
|
|
|
#ifdef USE_PTHREADS
|
2007-11-06 21:41:16 +00:00
|
|
|
pthread_mutex_t m_mutex;
|
|
|
|
|
pthread_cond_t m_condition;
|
2008-05-22 16:54:27 +00:00
|
|
|
bool m_locked;
|
|
|
|
|
#endif
|
2012-09-09 16:57:42 +01:00
|
|
|
#endif
|
2008-05-22 16:54:27 +00:00
|
|
|
#ifdef DEBUG_CONDITION
|
2007-11-26 21:51:05 +00:00
|
|
|
std::string m_name;
|
2007-11-28 11:50:47 +00:00
|
|
|
#endif
|
2007-11-06 21:41:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-09 16:57:42 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
/* Stub threading interface. We do not have threading support in this code. */
|
|
|
|
|
|
|
|
|
|
namespace RubberBand
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class Thread
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef unsigned int Id;
|
|
|
|
|
|
|
|
|
|
Thread() { }
|
|
|
|
|
virtual ~Thread() { }
|
|
|
|
|
|
|
|
|
|
Id id() { return 0; }
|
|
|
|
|
|
|
|
|
|
void start() { }
|
|
|
|
|
void wait() { }
|
|
|
|
|
|
|
|
|
|
static bool threadingAvailable() { return false; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual void run() = 0;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Mutex
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Mutex() { }
|
|
|
|
|
~Mutex() { }
|
|
|
|
|
|
|
|
|
|
void lock() { }
|
|
|
|
|
void unlock() { }
|
|
|
|
|
bool trylock() { return false; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class MutexLocker
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MutexLocker(Mutex *) { }
|
|
|
|
|
~MutexLocker() { }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Condition
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Condition(std::string name) { }
|
|
|
|
|
~Condition() { }
|
|
|
|
|
|
|
|
|
|
void lock() { }
|
|
|
|
|
void unlock() { }
|
|
|
|
|
void wait(int us = 0) { }
|
|
|
|
|
|
|
|
|
|
void signal() { }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* NO_THREADING */
|
2011-12-09 18:18:32 +00:00
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
#endif
|