1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2020-2021] [HCL Technologies Ltd.] #
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
6 # you may not use this file except in compliance with the License. #
7 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # Unless required by applicable law or agreed to in writing, software #
12 # distributed under the License is distributed on an "AS IS" BASIS, #
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 # See the License for the specific language governing permissions and #
15 # limitations under the License. #
16 ################################################################################
17 *******************************************************************************/
19 /* This file contains class for launching and managing POSIX threads */
22 #include "GlobalDefs.hpp"
26 /*******************************************************************
35 * - Constructor intialization
39 ******************************************************************/
41 Thread::Thread(): mThreadId(0)
46 /*******************************************************************
59 ******************************************************************/
66 /*******************************************************************
68 * @brief Static function registered to run as pthread
76 * - Static function registered to run as pthread. It calls
77 * the run function that is implemented by the derived
80 * @params[in] void pointer to the base class instance that
81 implements this Thread class
83 ******************************************************************/
85 void* Thread::task(void* args)
87 Thread* thisPtr = static_cast<Thread*>(args);
92 /*******************************************************************
94 * @brief Creates a thread
101 * - Starts a pthread registering the "task" function for
102 * performing the thread task.
105 * @return true : success
107 ******************************************************************/
111 return (pthread_create(&mThreadId, NULL, task, this) == 0);
115 /*******************************************************************
117 * @brief Stops the thread
124 * - Stops the thread. Performs an clean ups prior to
125 * stopping the thread
129 * @return true : success
131 ******************************************************************/
135 return (pthread_cancel(mThreadId) == 0);
138 /*******************************************************************
140 * @brief Wait for the thread to complete
147 * - Waits for the thread to complete in the calling process/
151 * @return true : success
153 ******************************************************************/
156 return (pthread_join(mThreadId,NULL) == 0);
160 /*******************************************************************
162 * @brief Set the affinity of the thread
166 * Function : setAffinity
169 * - Pins the thread to cpu core(s)
171 * @params[in] CPU cores
172 * @return true : success
174 ******************************************************************/
175 bool Thread::setAffinity(int coreNum)
179 long nCores = sysconf(_SC_NPROCESSORS_ONLN);
182 CPU_SET(coreNum%nCores, &cpuset);
183 ret = pthread_setaffinity_np(mThreadId, sizeof(cpu_set_t), &cpuset);
191 /*******************************************************************
193 * @brief Print the affinity of the thread
197 * Function : printAffinity
200 * - Prints the cpu core(s) the thread is pinned to
203 * @return true : success
205 ******************************************************************/
206 bool Thread::printAffinity()
209 long nCores = sysconf(_SC_NPROCESSORS_ONLN);
214 ret = pthread_getaffinity_np(mThreadId, sizeof(cpu_set_t), &cpuset);
218 for (int i = 0; i < nCores; i++)
219 if (CPU_ISSET(i, &cpuset))
220 O1_LOG("CPU %d ", i);