ApraLinuxUtils 1.0.0
C++ utility library for embedded Linux systems
 
Loading...
Searching...
No Matches
ProcessThread.cpp
Go to the documentation of this file.
1/*
2 * ProcessThread.cpp
3 *
4 * Copyright (c) 2024 Apra Labs
5 *
6 * This file is part of ApraUtils.
7 *
8 * Licensed under the MIT License.
9 * See LICENSE file in the project root for full license information.
10 */
11#include "utils/ProcessThread.h"
12
13#include <stdio.h>
14#include <iostream>
15#include <exception>
16#include "utils/Macro.h"
17#include "utils/ScopeLock.h"
18
19namespace apra
20{
21
22ProcessThread::ProcessThread(string name, int64_t freq) :
23 m_threadname(name), m_frequSec(0), m_requestQueue(), m_responseQueue(), m_typeofThread(
24 FREERUNNING), m_requestLock(), m_responseLock(), m_shouldIquit(
25 false), m_queueSizeLimit(1000)
26{
27 setFPS(freq);
28}
29
31{
32 printf("END******************************************%32s \n",
33 getName().c_str());
34
35}
36
37void ProcessThread::setFPS(int64_t fps)
38{
39 if (fps > 0)
40 {
41 m_frequSec = 1000000.0 / fps;
42 }
43}
44
46{
47 return m_shouldIquit;
48}
50{
51 return m_threadname;
52}
53
58
63
65{
66 uint32_t status = 0;
67 ProcessThread *pHThread = (ProcessThread*) arg;
68 if (pHThread)
69 {
70 uint32_t tid = syscall(SYS_gettid);
71 printf("*********************************************%32s::%lu::%u\n",
72 pHThread->getName().c_str(), pHThread->m_threadID, tid);
73 status = pHThread->mainLoop();
74 }
75 return (void*) status;
76}
77
79{
80 m_shouldIquit = true;
81 int32_t m_Error = pthread_create(&m_threadID, NULL,
82 ProcessThread::beginProxy, (void*) (this));
83 return m_Error;
84}
85
87{
88 int32_t ret = -1;
89 std::string name = getName() + "::";
90 try
91 {
92 m_shouldIquit = false;
93 void *retVal = NULL;
94 usleep(200000);
95 ret = pthread_join(m_threadID, &retVal);
96 } catch (std::exception &ex)
97 {
98 cout << name << ex.what() << endl;
99 cout << name << endl;
100 } catch (const char *msg)
101 {
102 cout << name << msg << endl;
103 cout << name << "ending thread" << endl;
104 } catch (...)
105 {
106 cout << name << "Unknown exception! Ending thread" << endl;
107 }
108 return ret;
109}
110
118{
119 std::string name = getName() + "::";
120 try
121 {
122 while (shouldIquit())
123 {
124 bool executedonce = false;
125 PROCESSTIME(pt,
126 {
127 someFunction(executedonce)
128 ;
129 })
130 ;
131 if (m_frequSec > 0)
132 {
133 int64_t td = m_frequSec - pt;
134 if (td > 0)
135 {
136 usleep(m_frequSec - pt);
137 }
138 }
139 }
140 } catch (std::exception &ex)
141 {
142 cout << name << ex.what() << endl;
143 cout << name << "exiting thread" << endl;
144 } catch (const char *msg)
145 {
146 cout << name << msg << endl;
147 cout << name << "exiting thread" << endl;
148 } catch (...)
149 {
150 cout << name << "Unknown exception! exiting thread" << endl;
151 }
152 printf("ENDING *********************************************%32s::%lu \n",
153 getName().c_str(), m_threadID);
154 return 0;
155}
156
158{
161 m_responseQueue.push(message);
162}
163
164void ProcessThread::someFunction(bool &executedOnce)
165{
166 switch (m_typeofThread)
167 {
168 case FREERUNNING:
169 process(NULL);
170 break;
171 case ONLY_MESSAGE:
172 executedOnce = true;
174 {
175 Message *item = NULL;
176 {
178 if (m_requestQueue.size() > 0)
179 {
180 item = m_requestQueue.front();
181 m_requestQueue.pop();
182 }
183 }
184 if (item)
185 {
186 process(item);
187 if (item->getType() != REQUEST_RESPONSE)
188 {
189 delete item;
190 }
191 executedOnce = true;
192 }
193 if (!executedOnce)
194 {
195 process(NULL);
196 }
197 }
198 break;
199 }
200}
201
202void ProcessThread::trimQueue(std::queue<Message*> &queue)
203{
204 if (queue.size() > m_queueSizeLimit)
205 {
206 Message *item = queue.front();
207 queue.pop();
208 if (item != NULL)
209 {
210 delete item;
211 item = NULL;
212 }
213 }
214}
215
217{
219 Message *item = NULL;
220 if (!m_responseQueue.empty())
221 {
222 item = m_responseQueue.front();
223 m_responseQueue.pop();
224 }
225 return item;
226}
227}
#define PROCESSTIME(var, x)
Definition Macro.h:57
@ REQUEST_RESPONSE
Definition MessageType.h:17
MESSAGE_TYPE getType()
Definition Message.cpp:32
void setType(THREAD_TYPE t)
void enqueResponse(Message *message)
std::queue< Message * > m_responseQueue
static void * beginProxy(void *arg)
ProcessThread(string name, int64_t freq=0)
void enque(Message *p)
THREAD_TYPE m_typeofThread
void someFunction(bool &executedOnce)
THREAD_TYPE getType()
virtual int32_t end()
std::queue< Message * > m_requestQueue
void setFPS(int64_t fps)
void trimQueue(std::queue< Message * > &queue)
virtual void process(Message *obj)=0
THREAD_TYPE
Definition ThreadType.h:18
@ FREERUNNING
Definition ThreadType.h:19
@ MESSAGE_AND_FREERUNNING
Definition ThreadType.h:19
@ ONLY_MESSAGE
Definition ThreadType.h:19