Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:38:48

0001 //
0002 // MIT License
0003 // Copyright (c) 2020 Jonathan R. Madsen
0004 // Permission is hereby granted, free of charge, to any person obtaining a copy
0005 // of this software and associated documentation files (the "Software"), to deal
0006 // in the Software without restriction, including without limitation the rights
0007 // to use, copy, modify, merge, publish, distribute, sublicense, and
0008 // copies of the Software, and to permit persons to whom the Software is
0009 // furnished to do so, subject to the following conditions:
0010 // The above copyright notice and this permission notice shall be included in
0011 // all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
0012 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
0013 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
0014 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0015 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0016 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0017 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0018 //
0019 // ---------------------------------------------------------------
0020 // Tasking class header file
0021 //
0022 // Class Description:
0023 //
0024 // This file defines types and macros used to expose Tasking threading model.
0025 
0026 #pragma once
0027 
0028 #include <array>
0029 #include <cstddef>
0030 #include <future>
0031 #include <mutex>
0032 #include <thread>
0033 
0034 namespace PTL
0035 {
0036 // global thread types
0037 using Thread       = std::thread;
0038 using NativeThread = std::thread::native_handle_type;
0039 // std::thread::id does not cast to integer
0040 using Pid_t = std::thread::id;
0041 
0042 // Condition
0043 using Condition = std::condition_variable;
0044 
0045 // Thread identifier
0046 using ThreadId = Thread::id;
0047 
0048 // will be used in the future when migrating threading to task-based style
0049 template <typename Tp>
0050 using Future = std::future<Tp>;
0051 template <typename Tp>
0052 using SharedFuture = std::shared_future<Tp>;
0053 template <typename Tp>
0054 using Promise = std::promise<Tp>;
0055 
0056 // global mutex types
0057 using Mutex          = std::mutex;
0058 using RecursiveMutex = std::recursive_mutex;
0059 
0060 // static functions: get_id(), sleep_for(...), sleep_until(...), yield(),
0061 namespace ThisThread
0062 {
0063 using namespace std::this_thread;
0064 }
0065 
0066 // Helper function for getting a unique static mutex for a specific
0067 // class or type
0068 // Usage example:
0069 //      a template class "Cache<T>" that required a static
0070 //      mutex for specific to type T:
0071 //          AutoLock l(TypeMutex<Cache<T>>());
0072 template <typename Tp, typename MutexTp = Mutex, size_t N = 4>
0073 MutexTp&
0074 TypeMutex(const unsigned int& _n = 0)
0075 {
0076     static std::array<MutexTp, N> _mutex_array{};
0077     return _mutex_array[_n % N];
0078 }
0079 
0080 //======================================================================================//
0081 
0082 namespace Threading
0083 {
0084 enum
0085 {
0086     SEQUENTIAL_ID    = -2,
0087     MASTER_ID        = -1,
0088     WORKER_ID        = 0,
0089     GENERICTHREAD_ID = -1000
0090 };
0091 
0092 Pid_t
0093 GetPidId();
0094 
0095 unsigned
0096 GetNumberOfPhysicalCpus();
0097 
0098 unsigned
0099 GetNumberOfCores();
0100 
0101 int
0102 GetThreadId();
0103 
0104 void
0105 SetThreadId(int aNewValue);
0106 
0107 bool
0108 SetPinAffinity(int idx);
0109 
0110 bool
0111 SetThreadPriority(int _v);
0112 
0113 bool
0114 SetPinAffinity(int idx, NativeThread& _t);
0115 
0116 bool
0117 SetThreadPriority(int _v, NativeThread& _t);
0118 
0119 }  // namespace Threading
0120 }  // namespace PTL