/* * 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 #include #include #include 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 &); }; } }