-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
192 lines (146 loc) · 4.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// main.cpp
// atomic
//
// Created by GUSTAVO CAMPOS on 28/08/2021.
//
#include <unistd.h>
#include <sys/time.h>
#include <unistd.h>
#include <cstring>
#include <cstdint>
#include <iostream>
#include <setjmp.h>
#include <string>
#include "atomicx.hpp"
using namespace thread;
#ifdef FAKE_TIMER
uint nCounter=0;
#endif
void ListAllThreads();
atomicx_time Atomicx_GetTick (void)
{
struct timeval tp;
gettimeofday (&tp, NULL);
return (atomicx_time)tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
size_t nCounter = 0;
void Atomicx_SleepTick(atomicx_time nSleep)
{
#if 0
atomicx_time nCurrent= Atomicx_GetTick ();
std::cout << "Current: " << nCurrent << ", Sleep: " << nSleep << ", Thread time:" << atomicx::GetCurrent()->GetTargetTime () << ", Calculation:" << (atomicx::GetCurrent()->GetTargetTime () - nCurrent)<< std::endl << std::flush;
ListAllThreads();
#endif
usleep ((useconds_t)nSleep * 1000);
}
size_t nGlobalCount = 0;
struct transf
{
size_t Counter;
char pszData[80];
};
class ThreadConsummer : public atomicx
{
public:
ThreadConsummer() = delete;
ThreadConsummer(atomicx_time nNice, const char* pszName) : atomicx (stack), m_pszName(pszName)
{
std::cout << "Creating Eventual: " << pszName << ", ID: " << (size_t) this << std::endl;
SetNice(nNice);
}
~ThreadConsummer()
{
std::cout << "Deleting Eventual: " << GetName() << ": " << (size_t) this << std::endl;
}
void run(void) noexcept override
{
size_t nReceived;
for (;;)
{
transf tr = {.Counter=19, .pszData=""};
if ((nReceived = Receive(nGlobalCount, (uint8_t*) &tr, (uint16_t) sizeof (tr), 1000)))
{
printf ("Receiver: nReceived: %zu, Counter: %zu (%s)\n", nReceived, tr.Counter, tr.pszData);
}
else
{
printf ("Receiver: failed receive...\n");
ListAllThreads();
}
}
}
void StackOverflowHandler (void) noexcept override
{
std::cout << __FUNCTION__ << ":" << GetName() << "_" << (size_t) this << ": needed: " << GetUsedStackSize() << ", allocated: " << GetStackSize() << std::endl;
}
const char* GetName (void) override
{
return m_pszName;
}
private:
uint8_t stack[1024]="";
const char* m_pszName;
};
class Thread : public atomicx
{
public:
Thread(atomicx_time nNice, const char* pszName) : atomicx(1024, 250), m_pszName(pszName)
{
SetNice(nNice);
}
~Thread()
{
std::cout << "Deleting " << GetName() << ": " << (size_t) this << std::endl;
}
void run() noexcept override
{
transf tr { .Counter=0, .pszData=""};
for (;;)
{
tr.Counter ++;
snprintf(tr.pszData, sizeof tr.pszData, "Counter: %zu", tr.Counter);
if (Send (nGlobalCount, (uint8_t*) &tr, (uint16_t) sizeof (tr), 1000) == false)
{
printf ("Publish %zu: (%s) Failed to send data.\n", GetID(), GetName ());
}
else
{
printf ("%zu: (%s) Data Sent..\n", GetID(), GetName ());
}
}
}
void StackOverflowHandler (void) noexcept override
{
std::cout << __FUNCTION__ << ":" << GetName() << "_" << (size_t) this << ": needed: " << GetUsedStackSize() << ", allocated: " << GetStackSize() << std::endl;
}
const char* GetName (void) override
{
return m_pszName;
}
private:
const char* m_pszName;
};
void ListAllThreads()
{
std::cout << "[Thread]-----------------------------------------------" << std::endl;
for (auto& th : *(atomicx::GetCurrent()))
{
std::cout << (atomicx::GetCurrent() == &th ? "* " : " ") << th.GetID() << "\t" << th.GetName() << "\t, Nc: " << th.GetNice() << "\t, Stk: " << (th.IsStackSelfManaged() ? 'A' : ' ') << th.GetStackSize() << "/i:" << th.GetStackIncreasePace() << "\t, UsedStk: " << th.GetUsedStackSize() << "\t, St: " << th.GetStatus() << "/" << th.GetSubStatus() << " TTime: " << th.GetTargetTime () << ", t:" << th.GetLastUserExecTime() << "ms" << std::endl;
}
std::cout << "-------------------------------------------------------" << std::endl;
}
int main()
{
Thread t1(1000, "Producer 1");
//Thread t2(1000, "Producer 2");
//Thread t3(1000, "Producer 3");
//Thread t4(1000, "Producer 4");
ThreadConsummer e1(500, "Consumer 1");
std::cout << "LISTING....." << std::endl;
ListAllThreads ();
std::cout << "END LISTING....." << std::endl;
atomicx::Start();
std::cout << "[FULL LOCK]-------------------------" << std::endl;
ListAllThreads();
}