J-Release Documentation
[o-du/l2.git] / src / o1 / Thread.cpp
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2020-2021] [HCL Technologies Ltd.]                          #
4 #                                                                              #
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                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
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 *******************************************************************************/
18
19 /* This file contains class for launching and managing POSIX threads */
20
21 #include "Thread.hpp"
22 #include "GlobalDefs.hpp"
23 #include <stdio.h>
24 #include <unistd.h>
25
26 /*******************************************************************
27  *
28  * @brief Constructor
29  *
30  * @details
31  *
32  *    Function : Thread
33  *
34  *    Functionality:
35  *      - Constructor intialization
36  *
37  * @params[in] None
38  * @return None
39  ******************************************************************/
40
41 Thread::Thread(): mThreadId(0)
42 {
43
44 }
45
46 /*******************************************************************
47  *
48  * @brief Destructor
49  *
50  * @details
51  *
52  *    Function : ~Thread
53  *
54  *    Functionality:
55  *      - Destructor
56  *
57  * @params[in] None
58  * @return None
59  ******************************************************************/
60
61 Thread::~Thread() 
62 {
63    
64 }
65
66 /*******************************************************************
67  *
68  * @brief Static function registered to run as pthread
69
70  *
71  * @details
72  *
73  *    Function : task
74  *
75  *    Functionality:
76  *      - Static function registered to run as pthread. It calls
77  *        the run function that is implemented by the derived
78  *        class instance.
79  *
80  * @params[in] void pointer to the base class instance that 
81                implements this Thread class
82  * @return void
83  ******************************************************************/
84
85 void* Thread::task(void* args)
86 {
87    Thread* thisPtr = static_cast<Thread*>(args);
88    thisPtr->run();
89 }
90
91
92 /*******************************************************************
93  *
94  * @brief Creates a thread
95  *
96  * @details
97  *
98  *    Function : start
99  *
100  *    Functionality:
101  *      - Starts a pthread registering the "task" function for
102  *        performing the thread task.
103  *
104  * @params[in] void
105  * @return true  : success
106  *         false : failure
107  ******************************************************************/
108
109 bool Thread::start()
110 {
111    return (pthread_create(&mThreadId, NULL, task, this) == 0);
112 }
113
114
115 /*******************************************************************
116  *
117  * @brief Stops the thread
118  *
119  * @details
120  *
121  *    Function : stop
122  *
123  *    Functionality:
124  *      - Stops the thread. Performs an clean ups prior to
125  *        stopping the thread
126  *
127  *
128  * @params[in] void
129  * @return true  : success
130  *         false : failure
131  ******************************************************************/
132 bool Thread::stop()
133 {
134    cleanUp();
135    return (pthread_cancel(mThreadId) == 0);
136 }
137
138 /*******************************************************************
139  *
140  * @brief  Wait for the thread to complete 
141  *
142  * @details
143  *
144  *    Function : join
145  *
146  *    Functionality:
147  *      - Waits for the thread to complete in the calling process/
148  *        thread
149  *
150  * @params[in] void
151  * @return true  : success
152  *         false : failure
153  ******************************************************************/
154 bool Thread::join()
155 {
156    return (pthread_join(mThreadId,NULL) == 0);
157 }
158
159
160 /*******************************************************************
161  *
162  * @brief  Set the affinity of the thread
163  *
164  * @details
165  *
166  *    Function : setAffinity
167  *
168  *    Functionality:
169  *      - Pins the thread to cpu core(s)
170  *
171  * @params[in] CPU cores
172  * @return true  : success
173  *         false : failure
174  ******************************************************************/
175 bool Thread::setAffinity(int coreNum)
176 {
177    int ret;
178    cpu_set_t cpuset;
179    long nCores = sysconf(_SC_NPROCESSORS_ONLN);
180
181    CPU_ZERO(&cpuset);
182    CPU_SET(coreNum%nCores, &cpuset);
183    ret = pthread_setaffinity_np(mThreadId, sizeof(cpu_set_t), &cpuset);
184    if (ret != 0)
185    {
186        return false;
187    }
188    return true;  
189 }
190
191 /*******************************************************************
192  *
193  * @brief  Print the affinity of the thread
194  *
195  * @details
196  *
197  *    Function : printAffinity
198  *
199  *    Functionality:
200  *      - Prints the cpu core(s) the thread is pinned to
201  *
202  * @params[in] void
203  * @return true  : success
204  *         false : failure
205  ******************************************************************/
206 bool Thread::printAffinity()
207 {
208    int ret;
209    long nCores = sysconf(_SC_NPROCESSORS_ONLN);
210
211    cpu_set_t cpuset;
212    CPU_ZERO(&cpuset);
213
214    ret = pthread_getaffinity_np(mThreadId, sizeof(cpu_set_t), &cpuset);
215    if (ret != 0)
216        return false;
217
218    for (int i = 0; i < nCores; i++)
219        if (CPU_ISSET(i, &cpuset))
220            O1_LOG("CPU %d ", i);
221    return true;
222 }
223
224 /**********************************************************************
225 End of file
226 **********************************************************************/