added onix fix libs
This commit is contained in:
1 parent
ff5f02bbde
commit
8069cc893f
84 files changed
+78912
No files matched your search
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <OnixS/FIXEngine/Threading/Mutex.h>
|
||||
|
||||
namespace OnixS {
|
||||
namespace Threading {
|
||||
/// The condition variable - a synchronization objects that
|
||||
/// allows threads to wait for certain events (conditions)
|
||||
/// to occur.
|
||||
///
|
||||
/// The condition variable enables threads to atomically block
|
||||
/// and test a condition under the protection of the mutual exclusion
|
||||
/// lock (Mutex) until the condition is satisfied.
|
||||
class ONIXS_FIXENGINE_API Condition
|
||||
{
|
||||
public:
|
||||
/// Initializes the instance.
|
||||
Condition();
|
||||
|
||||
/// Destructs the instance.
|
||||
~Condition();
|
||||
|
||||
/// Signals the one waiting thread.
|
||||
void signal();
|
||||
|
||||
/// Signals all waiting threads.
|
||||
void signalAll();
|
||||
|
||||
/// Blocks on a condition.
|
||||
/// @param lock Represents a reference to the Mutex
|
||||
/// which is used to protect the condition testing.
|
||||
void wait(Mutex & lock);
|
||||
|
||||
private:
|
||||
// Implementation details.
|
||||
void * impl_;
|
||||
|
||||
// Copying & assignment is prohibited.
|
||||
|
||||
Condition(const Condition &);
|
||||
Condition & operator = (const Condition &);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined (_WIN32) // The Windows platform.
|
||||
|
||||
#if !defined (STRICT)
|
||||
#define STRICT
|
||||
#endif
|
||||
|
||||
#if !defined (WIN32_LEAN_AND_MEAN)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#else // The Linux platform.
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#endif // The Platform Selector.
|
||||
|
||||
#include <OnixS/FIXEngine/ABI.h>
|
||||
#include <OnixS/FIXEngine/Compiler.h>
|
||||
#include <OnixS/FIXEngine/Definitions.h>
|
||||
|
||||
namespace OnixS {
|
||||
namespace Threading {
|
||||
#if defined (_WIN32)
|
||||
/// Type alias for the thread identifier.
|
||||
typedef DWORD ThreadId;
|
||||
#else
|
||||
/// Type alias for thread identifier.
|
||||
typedef pthread_t ThreadId;
|
||||
#endif
|
||||
|
||||
/// The infinite timeout value.
|
||||
enum { InfiniteTimeout = -1 };
|
||||
|
||||
/// The base class for custom items which can be stored in thread-safe containers.
|
||||
class ONIXS_FIXENGINE_API ItemBase
|
||||
{
|
||||
public:
|
||||
|
||||
/// Initializes the instance.
|
||||
ItemBase() : next_() {}
|
||||
|
||||
/// Destructs the instance.
|
||||
virtual ~ItemBase() ONIXS_FIXENGINE_DEFAULT;
|
||||
|
||||
/// Returns the next item.
|
||||
ItemBase * next() const
|
||||
{
|
||||
return next_;
|
||||
}
|
||||
|
||||
/// Sets the next item.
|
||||
void setNext(ItemBase * nextNode)
|
||||
{
|
||||
next_ = nextNode;
|
||||
}
|
||||
|
||||
/// Performs a required processing of the item.
|
||||
virtual void process() = 0;
|
||||
|
||||
private:
|
||||
|
||||
// The pointer to the next item.
|
||||
ItemBase * volatile next_;
|
||||
};
|
||||
|
||||
typedef PtrTraits<ItemBase>::UniquePtr ItemBaseUniquePtr;
|
||||
|
||||
/// The base class for a custom pool allocator to provide
|
||||
/// a strategy to create/destroy items for the thread-safe pool.
|
||||
class ONIXS_FIXENGINE_API PoolAllocatorBase
|
||||
{
|
||||
public:
|
||||
|
||||
/// Destructs the instance.
|
||||
virtual ~PoolAllocatorBase() ONIXS_FIXENGINE_DEFAULT;
|
||||
|
||||
/// A strategy to create an item.
|
||||
virtual ItemBase * alloc() = 0;
|
||||
|
||||
/// A strategy to destroy an item.
|
||||
virtual void free(ItemBase * element) const
|
||||
{
|
||||
delete element;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,511 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <OnixS/FIXEngine/ABI.h>
|
||||
#include <OnixS/FIXEngine/Compiler.h>
|
||||
#include <OnixS/FIXEngine/Threading/Definitions.h>
|
||||
|
||||
#ifdef ONIXS_FIXENGINE_CXX11
|
||||
#include <exception>
|
||||
#endif
|
||||
|
||||
namespace OnixS {
|
||||
|
||||
namespace System
|
||||
{
|
||||
class FutureSharedState;
|
||||
}
|
||||
|
||||
namespace Threading {
|
||||
|
||||
/// The state of a \c SharedFuture object (similar to std::future_status, @see http://en.cppreference.com/w/cpp/thread/future_status ).
|
||||
struct FutureStatus
|
||||
{
|
||||
enum Enum
|
||||
{
|
||||
/// The shared state is ready.
|
||||
ready,
|
||||
/// The shared state did not become ready before the specified timeout duration has passed.
|
||||
timeout,
|
||||
/// The shared state contains a deferred function, so the result will be computed only when explicitly requested.
|
||||
deferred
|
||||
};
|
||||
};
|
||||
|
||||
namespace Implementation {
|
||||
|
||||
class FutureHelper;
|
||||
class PromiseBase;
|
||||
|
||||
// Implementation details.
|
||||
class ValueBase
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~ValueBase() ONIXS_FIXENGINE_DEFAULT;
|
||||
virtual ValueBase * clone() const = 0;
|
||||
};
|
||||
|
||||
/// Implementation details.
|
||||
class FutureValue
|
||||
{
|
||||
public:
|
||||
|
||||
FutureValue(ValueBase * value);
|
||||
FutureValue();
|
||||
FutureValue(const FutureValue & other);
|
||||
~FutureValue();
|
||||
|
||||
ValueBase * value() const { return value_; }
|
||||
|
||||
private:
|
||||
|
||||
ValueBase * value_;
|
||||
};
|
||||
|
||||
/// Implementation details.
|
||||
template<typename ValueType>
|
||||
class Value : public ValueBase
|
||||
{
|
||||
typedef Value<ValueType> Type;
|
||||
|
||||
public:
|
||||
|
||||
static const ValueType & value(const ValueBase * value)
|
||||
{
|
||||
return static_cast<const Type*>(value)->value();
|
||||
}
|
||||
|
||||
static ValueBase * create(const ValueType & value)
|
||||
{
|
||||
return new Type(value);
|
||||
}
|
||||
|
||||
ValueBase * clone() const ONIXS_FIXENGINE_OVERRIDE
|
||||
{
|
||||
return create(value_);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
explicit Value(const ValueType & value) : value_(value) {}
|
||||
|
||||
const ValueType & value() const { return value_; }
|
||||
|
||||
const ValueType value_;
|
||||
};
|
||||
|
||||
/// The base implementation of the SharedFuture<T>.
|
||||
class FutureBase
|
||||
{
|
||||
public:
|
||||
/// Check if a future instance is associated with an asynchronous result.
|
||||
///
|
||||
/// Returns \c true if the *this has an associated asynchronous result, false otherwise.
|
||||
bool valid() const ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
return state_ != ONIXS_FIXENGINE_NULLPTR;
|
||||
}
|
||||
|
||||
/// Returns \c true if the asynchronous result associated with this Future is ready
|
||||
/// (has a value or an exception stored in the shared state), \c false otherwise.
|
||||
///
|
||||
/// There are often situations where a \c get() call on a Future may not be a blocking call,
|
||||
/// or is only a blocking call under certain circumstances.
|
||||
/// This method gives the ability to test for early completion and allows us to avoid
|
||||
/// associating a continuation, which needs to be scheduled with some non-trivial overhead
|
||||
/// and near-certain loss of the cache efficiency.
|
||||
///
|
||||
/// @throw The std::logic_error if this instance does not refer to a shared state.
|
||||
ONIXS_FIXENGINE_API bool isReady() const;
|
||||
|
||||
/// Returns \c true if the asynchronous result associated with this Future has a stored value,
|
||||
/// \c false otherwise.
|
||||
///
|
||||
/// @throw The std::logic_error if this instance does not refer to a shared state.
|
||||
ONIXS_FIXENGINE_API bool hasValue() const;
|
||||
|
||||
/// Returns \c true if the asynchronous result associated with this Future has a stored exception,
|
||||
/// \c false otherwise.
|
||||
///
|
||||
/// @throw The std::logic_error if this instance does not refer to a shared state.
|
||||
ONIXS_FIXENGINE_API bool hasException() const;
|
||||
|
||||
#ifdef ONIXS_FIXENGINE_CXX11
|
||||
|
||||
/// Returns the stored exception.
|
||||
///
|
||||
/// @throw The std::logic_error if this instance does not refer to a shared state.
|
||||
/// @throw The std::logic_error if the operation has *not* finished with an error.
|
||||
ONIXS_FIXENGINE_API std::exception_ptr getExceptionPtr() const;
|
||||
|
||||
#endif
|
||||
|
||||
enum { InfiniteTimeout = -1 };
|
||||
|
||||
/// Waits for the result to become available during the timeout.
|
||||
///
|
||||
/// \note Calling \c wait on the same Future from multiple threads is \e not safe;
|
||||
/// the intended use is for each thread that waits on the same shared state to have a \e copy of a Future.
|
||||
///
|
||||
/// \throw The std::logic_error if this instance does not refer to a shared state.
|
||||
ONIXS_FIXENGINE_API FutureStatus::Enum wait(int timeoutInMs = InfiniteTimeout) const;
|
||||
|
||||
protected:
|
||||
FutureBase()
|
||||
: state_() {}
|
||||
|
||||
ONIXS_FIXENGINE_API FutureBase(const FutureBase & other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API FutureBase & operator=(const FutureBase & other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
#ifdef ONIXS_FIXENGINE_CXX11
|
||||
|
||||
ONIXS_FIXENGINE_API FutureBase(FutureBase && other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API FutureBase & operator=(FutureBase && other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
#endif
|
||||
|
||||
struct moving_init_t {};
|
||||
|
||||
/// Initializes the instance with shared state.
|
||||
ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState * state) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
/// Initializes the instance with shared state.
|
||||
ONIXS_FIXENGINE_API FutureBase(const System::FutureSharedState * state, moving_init_t) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
/// Destroys a future object.
|
||||
///
|
||||
/// If this is the last reference to the asynchronous result associated with *this (if any), then destroy that asynchronous result.
|
||||
ONIXS_FIXENGINE_API ~FutureBase() ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API void swap(FutureBase & other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API const FutureValue & getValue() const;
|
||||
|
||||
ONIXS_FIXENGINE_API void getVoid() const;
|
||||
|
||||
private:
|
||||
const System::FutureSharedState * state_;
|
||||
|
||||
friend class FutureHelper;
|
||||
friend class PromiseBase;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct FutureGetReturn
|
||||
{
|
||||
typedef const T & Type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct FutureGetReturn<void>
|
||||
{
|
||||
typedef void Type;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/// Represents a future result of an asynchronous operation - a result that will eventually appear in the Future after
|
||||
/// the processing is complete.
|
||||
///
|
||||
/// The class template Future provides a mechanism to access the result of \e asynchronous operations :
|
||||
/// \li An asynchronous operation (e.g. created via Promise) can provide a Future object to the creator of that asynchronous
|
||||
/// operation (e.g. via the Promise::getFuture).
|
||||
/// \li The creator of the asynchronous operation can then use a variety of methods to query, wait for, or extract a value from Future.
|
||||
/// These methods may block if the asynchronous operation has not yet provided a value.
|
||||
/// \li When the asynchronous operation is ready to send a result to the creator, it can do so by modifying the <em> shared state </em>
|
||||
/// (e.g. via the Promise::setValue) that is linked to the creator's Future.
|
||||
///
|
||||
/// The Future is the synchronization object constructed around the \e receiving end of the Promise channel. It allows for the separation
|
||||
/// of the initiation of an operation and the act of waiting for its result.
|
||||
///
|
||||
/// The Future is \e copiable and multiple SharedFuture objects may refer to the same shared state.
|
||||
///
|
||||
/// Unlike the std::future, this implementation has an extended API, allowing you to query the state of SharedFuture.
|
||||
/// The internal implementation is lock-free; therefore, setting and polling states do not lead to blocking of threads and/or switching
|
||||
/// of an execution context.
|
||||
///
|
||||
/// \note An access to the same shared state from multiple threads is safe if each thread does it through its own copy of
|
||||
/// a SharedFuture object.
|
||||
///
|
||||
/// \see http://en.cppreference.com/w/cpp/thread/shared_future .
|
||||
template <typename T>
|
||||
class SharedFuture : public Implementation::FutureBase
|
||||
{
|
||||
public:
|
||||
SharedFuture() ONIXS_FIXENGINE_NOTHROW
|
||||
: FutureBase() {}
|
||||
|
||||
/// The copy constructor.
|
||||
SharedFuture(const SharedFuture<T> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
: FutureBase(other) {}
|
||||
|
||||
/// The copy assignment.
|
||||
SharedFuture<T> & operator=(const SharedFuture<T> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
FutureBase::operator=(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
#ifdef ONIXS_FIXENGINE_CXX11
|
||||
|
||||
SharedFuture(SharedFuture<T> && other) ONIXS_FIXENGINE_NOTHROW
|
||||
: FutureBase(std::move(other)) {}
|
||||
|
||||
FutureBase & operator=(SharedFuture<T> && other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
FutureBase::operator=(std::move(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// Returns the result. If the result is not ready, the method will block. When completes, it either returns a value or throws an exception.
|
||||
///
|
||||
/// This method waits until the Future has a valid result and retrieves it. It effectively calls the wait() in order to wait for the result.
|
||||
///
|
||||
/// \exception If an exception was stored in the shared state referenced by this Future (e.g. via a call to the Promise::setException()) then that exception will be thrown.
|
||||
typename Implementation::FutureGetReturn<T>::Type get() const
|
||||
{
|
||||
return Implementation::Value<T>::value(getValue().value());
|
||||
}
|
||||
|
||||
/// swaps two SharedFuture objects
|
||||
void swap(SharedFuture<T> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
FutureBase::swap(other);
|
||||
}
|
||||
|
||||
private:
|
||||
/// Initializes the instance with shared state.
|
||||
SharedFuture(const System::FutureSharedState * state)
|
||||
: FutureBase(state) {}
|
||||
|
||||
/// Initializes the instance with shared state.
|
||||
SharedFuture(const System::FutureSharedState * state, moving_init_t t) ONIXS_FIXENGINE_NOTHROW
|
||||
: FutureBase(state, t) {}
|
||||
|
||||
friend class Implementation::FutureHelper;
|
||||
friend class Implementation::PromiseBase;
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void SharedFuture<void>::get() const
|
||||
{
|
||||
getVoid();
|
||||
}
|
||||
|
||||
namespace Implementation {
|
||||
|
||||
/// The base implementation of the PromiseFuture<T>.
|
||||
class PromiseBase
|
||||
{
|
||||
public:
|
||||
/// Check if a future instance is associated with an asynchronous result.
|
||||
///
|
||||
/// Returns \c true if the *this has an associated asynchronous result, false otherwise.
|
||||
bool valid();
|
||||
|
||||
ONIXS_FIXENGINE_API void setExceptionImpl();
|
||||
|
||||
protected:
|
||||
ONIXS_FIXENGINE_API PromiseBase() ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API PromiseBase(const PromiseBase & other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API PromiseBase & operator=(const PromiseBase & other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
#ifdef ONIXS_FIXENGINE_CXX11
|
||||
|
||||
ONIXS_FIXENGINE_API PromiseBase(PromiseBase && other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API PromiseBase & operator=(PromiseBase && other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
#endif
|
||||
|
||||
/// Destroys a future object.
|
||||
///
|
||||
/// If this is the last reference to the asynchronous result associated with *this (if any), then destroy that asynchronous result.
|
||||
ONIXS_FIXENGINE_API ~PromiseBase() ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API void swap(PromiseBase & other) ONIXS_FIXENGINE_NOTHROW;
|
||||
|
||||
ONIXS_FIXENGINE_API void setValue(ValueBase * value);
|
||||
|
||||
ONIXS_FIXENGINE_API void setVoid();
|
||||
|
||||
|
||||
ONIXS_FIXENGINE_API SharedFuture<FutureValue> getFutureImpl() const;
|
||||
|
||||
template<typename T>
|
||||
SharedFuture<T> getTypedFuture() const
|
||||
{
|
||||
return SharedFuture<T>(getFutureImpl().state_);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// Implementation details.
|
||||
struct Impl;
|
||||
Impl * impl_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/// Provides a facility to store a value or an exception that can be acquired asynchronously via a `SharedFuture` object created by the `Promise` object.
|
||||
/// Each `Promise` object is associated with a shared state, which contains a some state information and a result which may be not yet evaluated, evaluated to a value or evaluated to an exception.
|
||||
/// The promise stores the result or the exception in the shared state. Marks the state ready and unblocks any thread waiting on a future associated with the shared state.
|
||||
template <typename T>
|
||||
class Promise : public Implementation::PromiseBase
|
||||
{
|
||||
public:
|
||||
|
||||
Promise() ONIXS_FIXENGINE_NOTHROW
|
||||
: PromiseBase() {}
|
||||
|
||||
/// The copy constructor.
|
||||
Promise(const Promise<T> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
: PromiseBase(other) {}
|
||||
|
||||
/// The copy assignment.
|
||||
Promise<T> & operator=(const Promise<T> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
PromiseBase::operator=(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
#ifdef ONIXS_FIXENGINE_CXX11
|
||||
|
||||
Promise(Promise<T> && other) ONIXS_FIXENGINE_NOTHROW
|
||||
: PromiseBase(std::move(other)) {}
|
||||
|
||||
Promise & operator=(Promise<T> && other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
PromiseBase::operator=(std::move(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// Swaps two Promise objects
|
||||
void swap(Promise<T> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
PromiseBase::swap(other);
|
||||
}
|
||||
|
||||
SharedFuture<T> getFuture() const
|
||||
{
|
||||
return getTypedFuture<T>();
|
||||
}
|
||||
|
||||
/// Sets the successful result value to the given shared state.
|
||||
void set(const T & value)
|
||||
{
|
||||
setValue(Implementation::Value<T>::create(value));
|
||||
}
|
||||
|
||||
/// Sets the result to indicate an exception.
|
||||
///
|
||||
/// @note Does nothing if the shared state already stores a value or exception.
|
||||
template<typename ExceptionType>
|
||||
void setException(const ExceptionType & exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
setExceptionImpl();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Promise<void> : public Implementation::PromiseBase
|
||||
{
|
||||
public:
|
||||
|
||||
Promise() ONIXS_FIXENGINE_NOTHROW
|
||||
: PromiseBase() {}
|
||||
|
||||
/// The copy constructor.
|
||||
Promise(const Promise<void> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
: PromiseBase(other) {}
|
||||
|
||||
/// The copy assignment.
|
||||
Promise<void> & operator=(const Promise<void> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
PromiseBase::operator=(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
#ifdef ONIXS_FIXENGINE_CXX11
|
||||
|
||||
Promise(Promise<void> && other) ONIXS_FIXENGINE_NOTHROW
|
||||
: PromiseBase(std::move(other)) {}
|
||||
|
||||
Promise & operator=(Promise<void> && other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
PromiseBase::operator=(std::move(other));
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/// Swaps two Promise objects
|
||||
void swap(Promise<void> & other) ONIXS_FIXENGINE_NOTHROW
|
||||
{
|
||||
PromiseBase::swap(other);
|
||||
}
|
||||
|
||||
SharedFuture<void> getFuture() const
|
||||
{
|
||||
return getTypedFuture<void>();
|
||||
}
|
||||
|
||||
/// Sets the successful result to the given shared state.
|
||||
void set()
|
||||
{
|
||||
setVoid();
|
||||
}
|
||||
|
||||
/// Sets the result to indicate an exception.
|
||||
///
|
||||
/// @note Does nothing if the shared state already stores a value or exception.
|
||||
template<typename ExceptionType>
|
||||
void setException(const ExceptionType & exception)
|
||||
{
|
||||
try
|
||||
{
|
||||
throw exception;
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
setExceptionImpl();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <OnixS/FIXEngine/Threading/Mutex.h>
|
||||
#include <OnixS/FIXEngine/Threading/Semaphore.h>
|
||||
|
||||
namespace OnixS {
|
||||
namespace Threading {
|
||||
/// Performs the automatic acquisition and release of the synchronization object.
|
||||
template<class Acquirable> class Guard
|
||||
{
|
||||
public:
|
||||
/// Acquires the synchronization object upon the initialization.
|
||||
Guard(Acquirable & acquirable)
|
||||
: acquirable_(acquirable) {
|
||||
acquirable_.acquire();
|
||||
}
|
||||
|
||||
/// Releases the synchronization object upon the destruction.
|
||||
~Guard() {
|
||||
acquirable_.release();
|
||||
}
|
||||
|
||||
private:
|
||||
Acquirable & acquirable_;
|
||||
|
||||
Guard(const Guard &);
|
||||
Guard & operator = (const Guard &);
|
||||
};
|
||||
|
||||
// Aliases for guards for known synchronization objects.
|
||||
|
||||
typedef Guard<Mutex> MutexGuard;
|
||||
typedef Guard<Semaphore> SemaphoreGuard;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <OnixS/FIXEngine/ABI.h>
|
||||
|
||||
namespace OnixS {
|
||||
namespace Threading {
|
||||
/// Protects a resources from the simultaneous access by multiple threads.
|
||||
///
|
||||
/// Only one thread at a time can own the instance of this class.
|
||||
///
|
||||
/// This implementation is optimized for locking threads that are in the same process.
|
||||
///
|
||||
/// @note It is NOT suitable for use for the interprocess synchronization.
|
||||
/// @note The recursive locking is NOT supported.
|
||||
class ONIXS_FIXENGINE_API Mutex
|
||||
{
|
||||
public:
|
||||
friend ONIXS_FIXENGINE_API_DECL(class, Condition);
|
||||
|
||||
/// Initializes the instance.
|
||||
Mutex();
|
||||
|
||||
/// Destructs the instance.
|
||||
~Mutex();
|
||||
|
||||
/// Acquires the lock ownership. If the Mutex already
|
||||
/// occupied by the other thread, blocks the current
|
||||
/// thread and waits until it will be released
|
||||
/// by the owner.
|
||||
void acquire();
|
||||
|
||||
/// Acquires the lock ownership if the Mutex is not
|
||||
/// owned by any thread. If the Mutex is already
|
||||
/// owned by the other thread, returns @c false.
|
||||
/// In either cases it doesn't block thread and
|
||||
/// quites immediately.
|
||||
bool tryAcquire();
|
||||
|
||||
/// Releases the ownership (lock) and
|
||||
/// unblocks one of waiting threads.
|
||||
void release();
|
||||
|
||||
private:
|
||||
// Implementation details.
|
||||
void * impl_;
|
||||
|
||||
// Copying and assignment are prohibited.
|
||||
|
||||
Mutex(const Mutex &);
|
||||
Mutex & operator = (const Mutex &);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <OnixS/FIXEngine/ABI.h>
|
||||
|
||||
namespace OnixS {
|
||||
namespace Threading {
|
||||
/// The limits number of threads that can access the shared resource simultaneously.
|
||||
class ONIXS_FIXENGINE_API Semaphore
|
||||
{
|
||||
public:
|
||||
/// Initializes the instance with the given value of
|
||||
/// the "count". The @param count indicates the initial "count"
|
||||
/// for the semaphore. The state of the semaphore is signaled
|
||||
/// when its count is greater than zero and
|
||||
/// non-signaled when it is zero.
|
||||
Semaphore(unsigned int count = 1);
|
||||
|
||||
/// Destructs the object.
|
||||
~Semaphore();
|
||||
|
||||
/// Decrements the semaphore "counter" by one.
|
||||
/// If the semaphore "counter" is already zero,
|
||||
/// blocks the thread until the semaphore "count"
|
||||
/// becomes greater than zero.
|
||||
void acquire();
|
||||
|
||||
/// Tries to acquire the semaphore object. If
|
||||
/// its "counter" is already zero, returns
|
||||
/// @c false. In all cases, it doesn't block
|
||||
/// the thread and quites immediately.
|
||||
bool tryAcquire();
|
||||
|
||||
/// Increments the semaphore "counter" by one.
|
||||
/// If there are other threads waiting for
|
||||
/// the object, it blocks one the them.
|
||||
void release();
|
||||
|
||||
private:
|
||||
// Implementation details.
|
||||
void * impl_;
|
||||
|
||||
// Copying & assignment is prohibited.
|
||||
|
||||
Semaphore(const Semaphore &);
|
||||
Semaphore & operator = (const Semaphore &);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <OnixS/FIXEngine/ABI.h>
|
||||
#include <OnixS/FIXEngine/Threading/Definitions.h>
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
namespace OnixS {
|
||||
namespace Threading {
|
||||
/// Logical processors that a thread is allowed to run on (first logical CPU has index 0).
|
||||
typedef size_t CpuIndex;
|
||||
typedef std::set < CpuIndex > CpuIndexes;
|
||||
|
||||
/// Represents set of CPU indices.
|
||||
class ONIXS_FIXENGINE_API ThreadAffinity
|
||||
{
|
||||
public:
|
||||
|
||||
/// Constructs the thread affinity from the string presentation.
|
||||
/// The symbol ',' is used as the CPU index delimiter.
|
||||
explicit ThreadAffinity(const std::string & str);
|
||||
|
||||
/// Destructs the instance.
|
||||
~ThreadAffinity();
|
||||
|
||||
/// The read-only access to the index collection.
|
||||
const CpuIndexes & cpuIndexes() const;
|
||||
|
||||
/// The collection of CPU indices.
|
||||
CpuIndexes & cpuIndexes();
|
||||
|
||||
/// Serializes the thread affinity to the string presentation.
|
||||
std::string toString();
|
||||
|
||||
private:
|
||||
|
||||
// The copying and assignment are prohibited.
|
||||
ThreadAffinity(const ThreadAffinity &);
|
||||
ThreadAffinity & operator = (const ThreadAffinity &);
|
||||
|
||||
CpuIndexes * indexes_;
|
||||
};
|
||||
|
||||
/// Exposes manipulations over the current thread.
|
||||
class ONIXS_FIXENGINE_API ThisThread
|
||||
{
|
||||
public:
|
||||
/// Suspends the execution of the current thread for
|
||||
/// a given amount of time in milliseconds.
|
||||
static void sleep(unsigned int milliseconds);
|
||||
|
||||
/// Executes a singular instruction during the given number of microseconds.
|
||||
static void spinWait(int microseconds);
|
||||
|
||||
/// Sets the processor affinity mask for the current thread.
|
||||
static void affinity(const CpuIndexes & cpuIndexes);
|
||||
|
||||
/// Sets the processor affinity mask for the current thread.
|
||||
static void affinity(CpuIndex cpuIndex);
|
||||
|
||||
/// Sets the priority for the current thread.
|
||||
static void priority(int priority);
|
||||
|
||||
/// Sets the scheduling policy for the current thread.
|
||||
///
|
||||
/// @note This method also sets the priority to the minimal value for the new policy,
|
||||
/// therefore, the priority should be set to a necessary value afterwards.
|
||||
static void policy(int policy);
|
||||
|
||||
/// Gets the platform identifier for the current thread.
|
||||
static ThreadId id();
|
||||
|
||||
private:
|
||||
// No instances of the current thread are available.
|
||||
ThisThread(const ThisThread &);
|
||||
ThisThread & operator = (const ThisThread &);
|
||||
};
|
||||
|
||||
/// Manipulates a thread.
|
||||
/// Custom thread classes should derive from this class and implement the 'run' method.
|
||||
class ONIXS_FIXENGINE_API Thread
|
||||
{
|
||||
public:
|
||||
|
||||
/// Creates the `Thread` object without running a real thread.
|
||||
explicit Thread(const std::string & name);
|
||||
|
||||
/// Cleans up internal resources.
|
||||
///
|
||||
/// @note Calls the `std::terminate` if the thread is not stopped at the moment.
|
||||
virtual ~Thread();
|
||||
|
||||
/// Creates and runs an actual thread.
|
||||
/// Can be called only once for the current 'Thread' instance.
|
||||
///
|
||||
/// @throw The std::exception if the thread is started or joined already.
|
||||
void start();
|
||||
|
||||
/// Sends a stop signal to the thread.
|
||||
void stopAsync();
|
||||
|
||||
/// Returns 'true' if a stop signal is sent to the thread
|
||||
/// by the `stopAsync` method or in a different way, otherwise 'false'.
|
||||
bool stopRequested() const;
|
||||
|
||||
/// Joins the thread completion.
|
||||
void join() const;
|
||||
|
||||
/// Returns the platform identifier.
|
||||
ThreadId id() const;
|
||||
|
||||
private:
|
||||
|
||||
/// The thread procedure.
|
||||
virtual void run() = 0;
|
||||
|
||||
/// Implementation details.
|
||||
struct Impl;
|
||||
Impl * impl_;
|
||||
|
||||
/// The copying and assignment are prohibited.
|
||||
Thread(const Thread &);
|
||||
Thread & operator = (const Thread &);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <OnixS/FIXEngine/ABI.h>
|
||||
#include <OnixS/FIXEngine/Threading/Definitions.h>
|
||||
|
||||
namespace OnixS {
|
||||
namespace Threading {
|
||||
|
||||
/// The thread-safe pool of objects by pointers.
|
||||
/// The pool is safe for many writers and one reader.
|
||||
/// The common scenario : a producer of data creates the objects using the pool
|
||||
/// and sends them to a consumer threads to process, then these threads returns
|
||||
/// objects back to producer's pool and so on.
|
||||
class ONIXS_FIXENGINE_API ThreadSafePool
|
||||
{
|
||||
public:
|
||||
|
||||
/// Initializes the instance.
|
||||
///
|
||||
/// @param preferredSize The preferred size of the pool.
|
||||
/// @param allocator The allocator to provide a strategy to create/destroy pool items.
|
||||
ThreadSafePool(size_t preferredSize, PoolAllocatorBase & allocator);
|
||||
|
||||
/// Removes pooled elements and cleans up internal resources.
|
||||
~ThreadSafePool();
|
||||
|
||||
/// Gets an item from the pool.
|
||||
/// Gives the ownership of the item to the smart pointer.
|
||||
/// If the pool is empty then creates an item by the allocator strategy.
|
||||
/// If the number of allocated elements becomes greater than the preferred pool size,
|
||||
/// after a high load, then destroys a one element, when possible, by the allocator strategy.
|
||||
///
|
||||
/// @param poolItem The smart pointer to store the pool item.
|
||||
void get(ItemBaseUniquePtr & poolItem);
|
||||
|
||||
/// Returns an item to the pool.
|
||||
/// Takes the ownership of the item from the smart pointer.
|
||||
///
|
||||
/// @throw The DomainException if the smart pointer is null.
|
||||
///
|
||||
/// @param poolItem The smart pointer to the pool item for returning to the pool.
|
||||
void put(ItemBaseUniquePtr & poolItem);
|
||||
|
||||
/// Returns the preferred size of the pool.
|
||||
size_t preferredSize();
|
||||
|
||||
private:
|
||||
|
||||
/// Implementation details.
|
||||
struct Impl;
|
||||
Impl * impl_;
|
||||
|
||||
/// The copying and assignment are prohibited.
|
||||
ThreadSafePool(const ThreadSafePool &);
|
||||
ThreadSafePool & operator = (const ThreadSafePool &);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright Onix Solutions Limited [OnixS]. All rights reserved.
|
||||
*
|
||||
* This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
|
||||
* and international copyright treaties.
|
||||
*
|
||||
* Access to and use of the software is governed by the terms of the applicable OnixS Software
|
||||
* Services Agreement (the Agreement) and Customer end user license agreements granting
|
||||
* a non-assignable, non-transferable and non-exclusive license to use the software
|
||||
* for it's own data processing purposes under the terms defined in the Agreement.
|
||||
*
|
||||
* Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
|
||||
* of this source code or associated reference material to any other location for further reproduction
|
||||
* or redistribution, and any amendments to this copyright notice, are expressly prohibited.
|
||||
*
|
||||
* Any reproduction or redistribution for sale or hiring of the Software not in accordance with
|
||||
* the terms of the Agreement is a violation of copyright law.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <OnixS/FIXEngine/ABI.h>
|
||||
#include <OnixS/FIXEngine/Threading/Definitions.h>
|
||||
|
||||
namespace OnixS {
|
||||
namespace Threading {
|
||||
|
||||
struct ThreadSafeQueueResult
|
||||
{
|
||||
enum Enum
|
||||
{
|
||||
/// No data during the timeout.
|
||||
Timeout,
|
||||
|
||||
/// We get data immediately or after waiting during the timeout.
|
||||
Data,
|
||||
|
||||
// We don't get data but was signaled to stop wait.
|
||||
Signaled
|
||||
};
|
||||
};
|
||||
|
||||
/// The thread-safe queue to send data from some thread(s) to another single thread.
|
||||
/// The thread-safe queue owns enqueued elements and deallocates element's memory on the destruction.
|
||||
class ONIXS_FIXENGINE_API ThreadSafeQueue
|
||||
{
|
||||
public:
|
||||
|
||||
/// Initializes the instance.
|
||||
ThreadSafeQueue();
|
||||
|
||||
/// Cleans up enqueued elements and internal resources.
|
||||
~ThreadSafeQueue();
|
||||
|
||||
/// Adds an item to the queue.
|
||||
/// Takes the ownership of the item from the smart pointer.
|
||||
///
|
||||
/// @throw The DomainException if the smart pointer is null.
|
||||
///
|
||||
/// @param enqueueItem The smart pointer to the allocated item to enqueue.
|
||||
void enqueue(ItemBaseUniquePtr & enqueueItem);
|
||||
|
||||
/// Tries to get an item from the queue during the timeout.
|
||||
/// Gives the ownership of the item to the smart pointer.
|
||||
/// Returns the `ThreadSafeQueueResult` value which reflects the result status ('Timeout', 'Data', 'Signaled').
|
||||
///
|
||||
/// @param dequeueItem The smart pointer to store the dequeued item.
|
||||
/// @param waitInMs The waiting timeout.
|
||||
ThreadSafeQueueResult::Enum dequeue(ItemBaseUniquePtr & dequeueItem, const int waitInMs = InfiniteTimeout);
|
||||
|
||||
/// Removes all elements from the queue.
|
||||
void clear();
|
||||
|
||||
/// Returns 'true' if the queue is empty, otherwise 'false'.
|
||||
bool empty() const;
|
||||
|
||||
/// Wakes up the queue if it was in the waiting mode.
|
||||
/// The `dequeue` method will return the 'Signaled' status if it was in the waiting mode.
|
||||
void signal();
|
||||
|
||||
private:
|
||||
|
||||
/// Implementation details.
|
||||
struct Impl;
|
||||
Impl * impl_;
|
||||
|
||||
/// The copying and assignment are prohibited.
|
||||
ThreadSafeQueue(const ThreadSafeQueue &);
|
||||
ThreadSafeQueue & operator = (const ThreadSafeQueue &);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user