Files
librubberband/src/system/Thread.h

147 lines
2.7 KiB
C
Raw Normal View History

2007-11-06 21:41:16 +00:00
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Rubber Band
An audio time-stretching and pitch-shifting library.
Copyright 2007-2009 Chris Cannam.
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.
*/
#ifndef _RUBBERBAND_THREAD_H_
#define _RUBBERBAND_THREAD_H_
2007-11-28 11:50:47 +00:00
#ifdef _WIN32
#include <windows.h>
#else /* !_WIN32 */
2007-11-06 21:41:16 +00:00
#include <pthread.h>
2007-11-28 11:50:47 +00:00
#endif /* !_WIN32 */
2007-11-06 21:41:16 +00:00
#include <string>
//#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
2007-11-06 21:41:16 +00:00
typedef pthread_t Id;
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
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
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;
#ifndef NO_THREAD_CHECKS
DWORD m_lockedBy;
#endif
2007-11-28 11:50:47 +00:00
#else
2007-11-06 21:41:16 +00:00
pthread_mutex_t m_mutex;
#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
#endif
2007-11-06 21:41:16 +00:00
};
class MutexLocker
{
public:
MutexLocker(Mutex *);
~MutexLocker();
private:
Mutex *m_mutex;
};
class Condition
{
public:
Condition(std::string name);
2007-11-06 21:41:16 +00:00
~Condition();
// 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
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:
2007-11-28 11:50:47 +00:00
#ifdef _WIN32
HANDLE m_mutex;
HANDLE m_condition;
bool m_locked;
2007-11-28 11:50:47 +00:00
#else
2007-11-06 21:41:16 +00:00
pthread_mutex_t m_mutex;
pthread_cond_t m_condition;
bool m_locked;
#endif
#ifdef DEBUG_CONDITION
std::string m_name;
2007-11-28 11:50:47 +00:00
#endif
2007-11-06 21:41:16 +00:00
};
}
#endif