Initial release
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
//------------------------------------------------------------------------
|
||||
// Project : SDK Base
|
||||
// Version : 1.0
|
||||
//
|
||||
// Category : Helpers
|
||||
// Filename : base/thread/include/fcondition.h
|
||||
// Created by : Steinberg, 1995
|
||||
// Description : the threads and locks and signals...
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// This file is part of a Steinberg SDK. It is subject to the license terms
|
||||
// in the LICENSE file found in the top-level directory of this distribution
|
||||
// and at www.steinberg.net/sdklicenses.
|
||||
// No part of the SDK, including this file, may be copied, modified, propagated,
|
||||
// or distributed except according to the terms contained in the LICENSE file.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
/** @file base/thread/include/fcondition.h
|
||||
signal. */
|
||||
/** @defgroup baseThread Thread Handling */
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "pluginterfaces/base/ftypes.h"
|
||||
|
||||
#if SMTG_PTHREADS
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
namespace Steinberg {
|
||||
namespace Base {
|
||||
namespace Thread {
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
/** FCondition - wraps the signal and wait calls in win32
|
||||
@ingroup baseThread */
|
||||
//------------------------------------------------------------------------
|
||||
class FCondition
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
/** Condition constructor.
|
||||
* @param name name of condition
|
||||
*/
|
||||
FCondition (const char8* name = nullptr /* "FCondition" */ );
|
||||
|
||||
/** Condition destructor.
|
||||
*/
|
||||
~FCondition ();
|
||||
|
||||
/** Signals one thread.
|
||||
*/
|
||||
void signal ();
|
||||
|
||||
/** Signals all threads.
|
||||
*/
|
||||
void signalAll ();
|
||||
|
||||
/** Waits for condition.
|
||||
*/
|
||||
void wait ();
|
||||
|
||||
/** Waits for condition with timeout
|
||||
* @param timeout time out in milliseconds
|
||||
* @return false if timed out
|
||||
*/
|
||||
bool waitTimeout (int32 timeout = -1);
|
||||
|
||||
/** Resets condition.
|
||||
*/
|
||||
void reset ();
|
||||
|
||||
#if SMTG_OS_WINDOWS
|
||||
/** Gets condition handle.
|
||||
* @return handle
|
||||
*/
|
||||
void* getHandle () { return event; }
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
private:
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_t mutex; ///< Mutex object
|
||||
pthread_cond_t cond; ///< Condition object
|
||||
int32 state; ///< Use to hold the state of the signal
|
||||
int32 waiters; ///< Number of waiters
|
||||
|
||||
#if DEVELOPMENT
|
||||
int32 waits; ///< Waits count
|
||||
int32 signalCount; ///< Signals count
|
||||
#endif
|
||||
#elif SMTG_OS_WINDOWS
|
||||
void* event; ///< Event handle
|
||||
#endif
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
} // namespace Thread
|
||||
} // namespace Base
|
||||
} // namespace Steinberg
|
||||
+165
@@ -0,0 +1,165 @@
|
||||
//------------------------------------------------------------------------
|
||||
// Project : SDK Base
|
||||
// Version : 1.0
|
||||
//
|
||||
// Category : Helpers
|
||||
// Filename : base/thread/include/flock.h
|
||||
// Created by : Steinberg, 1995
|
||||
// Description : locks
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// This file is part of a Steinberg SDK. It is subject to the license terms
|
||||
// in the LICENSE file found in the top-level directory of this distribution
|
||||
// and at www.steinberg.net/sdklicenses.
|
||||
// No part of the SDK, including this file, may be copied, modified, propagated,
|
||||
// or distributed except according to the terms contained in the LICENSE file.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
/** @file base/thread/include/flock.h
|
||||
Locks. */
|
||||
/** @defgroup baseLocks Locks */
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/source/fobject.h"
|
||||
#include "pluginterfaces/base/ftypes.h"
|
||||
|
||||
#if SMTG_PTHREADS
|
||||
#include <pthread.h>
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
struct CRITSECT // CRITICAL_SECTION
|
||||
{
|
||||
void* DebugInfo; // PRTL_CRITICAL_SECTION_DEBUG DebugInfo;
|
||||
Steinberg::int32 LockCount; // LONG LockCount;
|
||||
Steinberg::int32 RecursionCount; // LONG RecursionCount;
|
||||
void* OwningThread; // HANDLE OwningThread
|
||||
void* LockSemaphore; // HANDLE LockSemaphore
|
||||
Steinberg::int32 SpinCount; // ULONG_PTR SpinCount
|
||||
};
|
||||
#endif
|
||||
|
||||
namespace Steinberg {
|
||||
namespace Base {
|
||||
namespace Thread {
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
/** Lock interface declaration.
|
||||
@ingroup baseLocks */
|
||||
//------------------------------------------------------------------------
|
||||
struct ILock
|
||||
{
|
||||
//------------------------------------------------------------------------
|
||||
virtual ~ILock () {}
|
||||
|
||||
/** Enables lock. */
|
||||
virtual void lock () = 0;
|
||||
|
||||
/** Disables lock. */
|
||||
virtual void unlock () = 0;
|
||||
|
||||
/** Tries to disable lock. */
|
||||
virtual bool trylock () = 0;
|
||||
//------------------------------------------------------------------------
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
/** FLock declaration.
|
||||
@ingroup baseLocks */
|
||||
//------------------------------------------------------------------------
|
||||
class FLock : public ILock
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
/** Lock constructor.
|
||||
* @param name lock name
|
||||
*/
|
||||
FLock (const char8* name = "FLock");
|
||||
|
||||
/** Lock destructor. */
|
||||
~FLock () SMTG_OVERRIDE;
|
||||
|
||||
//-- ILock -----------------------------------------------------------
|
||||
void lock () SMTG_OVERRIDE;
|
||||
void unlock () SMTG_OVERRIDE;
|
||||
bool trylock () SMTG_OVERRIDE;
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
protected:
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_t mutex; ///< Mutex object
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
CRITSECT section; ///< Critical section object
|
||||
#endif
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
/** FLockObj declaration. Reference counted lock
|
||||
@ingroup baseLocks */
|
||||
//------------------------------------------------------------------------
|
||||
class FLockObject : public FObject, public FLock
|
||||
{
|
||||
public:
|
||||
OBJ_METHODS (FLockObject, FObject)
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
/** FGuard - automatic object for locks.
|
||||
@ingroup baseLocks */
|
||||
//------------------------------------------------------------------------
|
||||
class FGuard
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
/** FGuard constructor.
|
||||
* @param _lock guard this lock
|
||||
*/
|
||||
FGuard (ILock& _lock) : lock (_lock) { lock.lock (); }
|
||||
|
||||
/** FGuard destructor. */
|
||||
~FGuard () { lock.unlock (); }
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
private:
|
||||
ILock& lock; ///< guarded lock
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
/** Conditional Guard - Locks only if valid lock is passed.
|
||||
@ingroup baseLocks */
|
||||
//------------------------------------------------------------------------
|
||||
class FConditionalGuard
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
/** FConditionGuard constructor.
|
||||
* @param _lock guard this lock
|
||||
*/
|
||||
FConditionalGuard (FLock* _lock) : lock (_lock)
|
||||
{
|
||||
if (lock)
|
||||
lock->lock ();
|
||||
}
|
||||
|
||||
/** FConditionGuard destructor. */
|
||||
~FConditionalGuard ()
|
||||
{
|
||||
if (lock)
|
||||
lock->unlock ();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
private:
|
||||
FLock* lock; ///< guarded lock
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
} // namespace Thread
|
||||
} // namespace Base
|
||||
} // namespace Steinberg
|
||||
@@ -0,0 +1,259 @@
|
||||
//------------------------------------------------------------------------
|
||||
// Project : SDK Base
|
||||
// Version : 1.0
|
||||
//
|
||||
// Category : Helpers
|
||||
// Filename : base/thread/source/fcondition.cpp
|
||||
// Created by : Steinberg, 1995
|
||||
// Description : signals...
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// This file is part of a Steinberg SDK. It is subject to the license terms
|
||||
// in the LICENSE file found in the top-level directory of this distribution
|
||||
// and at www.steinberg.net/sdklicenses.
|
||||
// No part of the SDK, including this file, may be copied, modified, propagated,
|
||||
// or distributed except according to the terms contained in the LICENSE file.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define DEBUG_LOCK 0
|
||||
|
||||
#include "base/thread/include/fcondition.h"
|
||||
#include "base/source/fdebug.h"
|
||||
|
||||
#include <climits>
|
||||
#include <cstdlib>
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
#if SMTG_PTHREADS
|
||||
//------------------------------------------------------------------------
|
||||
#if __MACH__
|
||||
extern "C" {
|
||||
#include <mach/clock.h>
|
||||
#include <sched.h>
|
||||
#include <mach/mach_init.h>
|
||||
#include <mach/task.h>
|
||||
#include <mach/task_policy.h>
|
||||
#include <mach/thread_act.h>
|
||||
#include <mach/semaphore.h>
|
||||
#include <mach/thread_policy.h>
|
||||
#include <mach/mach.h>
|
||||
#include <mach/mach_time.h>
|
||||
#include <unistd.h>
|
||||
}
|
||||
#include <dlfcn.h>
|
||||
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#if SMTG_OS_MACOS
|
||||
#include <TargetConditionals.h>
|
||||
#if !SMTG_OS_IOS
|
||||
#include <CoreServices/CoreServices.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include <sys/time.h>
|
||||
//------------------------------------------------------------------------
|
||||
#elif SMTG_OS_WINDOWS
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace Steinberg {
|
||||
namespace Base {
|
||||
namespace Thread {
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
/** FCondition CTOR.
|
||||
* @param name - can be used to set the name of the event.
|
||||
*/
|
||||
FCondition::FCondition (const char8* name)
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
(void)name; // unused
|
||||
pthread_mutex_init (&mutex, 0);
|
||||
pthread_cond_init (&cond, 0);
|
||||
state = 0;
|
||||
waiters = 0;
|
||||
#if DEVELOPMENT
|
||||
waits = 0;
|
||||
signalCount = 0;
|
||||
#endif
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
// use name if existing
|
||||
event = CreateEventA (nullptr, FALSE, FALSE, name);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
FCondition::~FCondition ()
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_destroy (&mutex);
|
||||
pthread_cond_destroy (&cond);
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
CloseHandle (event);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
void FCondition::signal ()
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_lock (&mutex);
|
||||
state = 1;
|
||||
#if DEVELOPMENT
|
||||
signalCount++;
|
||||
#endif
|
||||
pthread_cond_signal (&cond);
|
||||
pthread_mutex_unlock (&mutex);
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
BOOL result = PulseEvent (event);
|
||||
if (!result)
|
||||
{
|
||||
SMTG_PRINTSYSERROR;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
void FCondition::signalAll ()
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_lock (&mutex);
|
||||
state = waiters + 1;
|
||||
|
||||
#if DEVELOPMENT
|
||||
signalCount++;
|
||||
#endif
|
||||
pthread_cond_broadcast (&cond);
|
||||
pthread_mutex_unlock (&mutex);
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
BOOL result = SetEvent (event);
|
||||
if (!result)
|
||||
{
|
||||
SMTG_PRINTSYSERROR;
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
void FCondition::wait ()
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_lock (&mutex);
|
||||
#if DEVELOPMENT
|
||||
waits++;
|
||||
#endif
|
||||
waiters++;
|
||||
while (!state)
|
||||
pthread_cond_wait (&cond, &mutex);
|
||||
if (--waiters == 0)
|
||||
state = 0;
|
||||
else
|
||||
--state;
|
||||
pthread_mutex_unlock (&mutex);
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
WaitForSingleObject (event, INFINITE);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
bool FCondition::waitTimeout (int32 milliseconds)
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
// this is the implementation running on mac (2018-07-18)
|
||||
if (milliseconds == -1)
|
||||
{ // infinite timeout
|
||||
wait ();
|
||||
return true;
|
||||
}
|
||||
|
||||
struct timespec time;
|
||||
|
||||
#if __MACH__
|
||||
// this is the implementation running on mac (2018-07-18)
|
||||
time.tv_sec = milliseconds / 1000;
|
||||
time.tv_nsec = 1000000 * (milliseconds - (time.tv_sec * 1000));
|
||||
|
||||
pthread_mutex_lock (&mutex);
|
||||
#if DEVELOPMENT
|
||||
waits++;
|
||||
#endif
|
||||
// this is the implementation running on mac (2018-07-18)
|
||||
waiters++;
|
||||
|
||||
bool result = true;
|
||||
while (!state)
|
||||
{
|
||||
int32 err = pthread_cond_timedwait_relative_np (&cond, &mutex, &time);
|
||||
if (err == ETIMEDOUT)
|
||||
{
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
else
|
||||
result = true;
|
||||
}
|
||||
if (--waiters == 0)
|
||||
state = 0;
|
||||
else
|
||||
--state;
|
||||
pthread_mutex_unlock (&mutex);
|
||||
return result;
|
||||
|
||||
#else
|
||||
// dead code? not compiled in unit test and sequencer (2018-07-18)
|
||||
clock_gettime (CLOCK_REALTIME, &time);
|
||||
time.tv_nsec += milliseconds * 1000; // ?????????
|
||||
|
||||
pthread_mutex_lock (&mutex);
|
||||
bool result = false;
|
||||
if (pthread_cond_timedwait (&cond, &mutex, &time) == 0)
|
||||
result = true;
|
||||
pthread_mutex_unlock (&mutex);
|
||||
return result;
|
||||
|
||||
#endif
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
if (milliseconds == -1)
|
||||
milliseconds = INFINITE;
|
||||
|
||||
DWORD result = WaitForSingleObject (event, milliseconds);
|
||||
return result == WAIT_TIMEOUT ? false : true;
|
||||
|
||||
#endif
|
||||
|
||||
#if !SMTG_OS_WINDOWS
|
||||
// WARNING ("Return false on time out not implemented!")
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
void FCondition::reset ()
|
||||
{
|
||||
#if SMTG_OS_WINDOWS
|
||||
ResetEvent (event);
|
||||
#elif SMTG_PTHREADS
|
||||
state = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
} // namespace Thread
|
||||
} // namespace Base
|
||||
} // namespace Steinberg
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
//------------------------------------------------------------------------
|
||||
// Project : SDK Base
|
||||
// Version : 1.0
|
||||
//
|
||||
// Category : Helpers
|
||||
// Filename : base/thread/source/flock.cpp
|
||||
// Created by : Steinberg, 1995
|
||||
// Description : Locks
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// This file is part of a Steinberg SDK. It is subject to the license terms
|
||||
// in the LICENSE file found in the top-level directory of this distribution
|
||||
// and at www.steinberg.net/sdklicenses.
|
||||
// No part of the SDK, including this file, may be copied, modified, propagated,
|
||||
// or distributed except according to the terms contained in the LICENSE file.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#define DEBUG_LOCK 0
|
||||
|
||||
#include "base/thread/include/flock.h"
|
||||
#include "base/source/fdebug.h"
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
#if SMTG_OS_WINDOWS
|
||||
//------------------------------------------------------------------------
|
||||
#ifndef WINVER
|
||||
#define WINVER 0x0500
|
||||
#endif
|
||||
#ifndef _WIN32_WINNT
|
||||
#define _WIN32_WINNT WINVER
|
||||
#endif
|
||||
|
||||
#include <windows.h>
|
||||
#include <objbase.h>
|
||||
#define INIT_CS(cs) \
|
||||
InitializeCriticalSection ((LPCRITICAL_SECTION)&cs);
|
||||
|
||||
#endif // SMTG_OS_WINDOWS
|
||||
|
||||
namespace Steinberg {
|
||||
namespace Base {
|
||||
namespace Thread {
|
||||
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// FLock implementation
|
||||
//------------------------------------------------------------------------
|
||||
FLock::FLock (const char8* /*name*/)
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutexattr_t mutexAttr;
|
||||
pthread_mutexattr_init (&mutexAttr);
|
||||
pthread_mutexattr_settype (&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
|
||||
if (pthread_mutex_init (&mutex, &mutexAttr) != 0)
|
||||
{SMTG_WARNING("mutex_init failed")}
|
||||
pthread_mutexattr_destroy (&mutexAttr);
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
INIT_CS (section)
|
||||
#else
|
||||
#warning implement FLock!
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
FLock::~FLock ()
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_destroy (&mutex);
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
DeleteCriticalSection ((LPCRITICAL_SECTION)§ion);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
void FLock::lock ()
|
||||
{
|
||||
#if DEBUG_LOCK
|
||||
FDebugPrint ("FLock::lock () %x\n", this);
|
||||
#endif
|
||||
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_lock (&mutex);
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
EnterCriticalSection ((LPCRITICAL_SECTION)§ion);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
void FLock::unlock ()
|
||||
{
|
||||
#if DEBUG_LOCK
|
||||
FDebugPrint ("FLock::unlock () %x\n", this);
|
||||
#endif
|
||||
|
||||
#if SMTG_PTHREADS
|
||||
pthread_mutex_unlock (&mutex);
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
LeaveCriticalSection ((LPCRITICAL_SECTION)§ion);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
bool FLock::trylock ()
|
||||
{
|
||||
#if SMTG_PTHREADS
|
||||
return pthread_mutex_trylock (&mutex) == 0;
|
||||
|
||||
#elif SMTG_OS_WINDOWS
|
||||
return TryEnterCriticalSection ((LPCRITICAL_SECTION)§ion) != 0 ? true : false;
|
||||
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
} // namespace Thread
|
||||
} // namespace Base
|
||||
} // namespace Steinberg
|
||||
Reference in New Issue
Block a user