-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
282 lines (191 loc) · 7.79 KB
/
main.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @file main.c
* @author Gustavo Campos (www.github.com/solariun)
* @brief
* @version 0.1
* @date 2020-12-22
*
* @copyright Copyright (c) 2020
*
*/
#define __DEBUG__ 1
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#ifndef bool
#include <stdbool.h>
#endif
#include "CorePartition.h"
#include "DMLayer.h"
DMLayer* pDMLayer = NULL;
const char pszProducer[] = "THREAD/PRODUCE/VALUE";
const char pszBinConsumer[] = "THREAD/CONSUME/BIN/VALUE";
void Thread_Producer (void* pValue)
{
int nRand = 0;
while (Cpx_Yield ())
{
bool nResponse = false;
nRand ++;
nResponse = DMLayer_SetNumber (pDMLayer, pszProducer, strlen (pszProducer), Cpx_GetID (), (dmlnumber) nRand);
NOTRACE ("[%s (%zu)]: func: (%u), nRand: [%u]\n", __FUNCTION__, Cpx_GetID(), nResponse, nRand);
}
}
int nValues[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void Consumer_Callback_Notify (DMLayer* pDMLayer, const char* pszVariable, size_t nVarSize, size_t nUserType, uint8_t nNotifyType)
{
bool nSuccess = false;
nValues[nUserType] = (int)DMLayer_GetNumber (pDMLayer, pszVariable, nVarSize, &nSuccess);
NOTRACE ("->[%s]: Variable: [%*s]\n", __FUNCTION__, (int) nVarSize, pszVariable);
VERIFY (nSuccess == true, "Error, variable is invalid", );
NOTRACE ("->[%s]: from: [%zu], Type: [%u] -> Value: [%u]\n", __FUNCTION__, nUserType, nNotifyType, nValues[nUserType]);
DMLayer_SetBinary (pDMLayer, pszBinConsumer, strlen (pszBinConsumer), (size_t)Cpx_GetID (), (void*)nValues, sizeof (nValues));
}
void Thread_Consumer (void* pValue)
{
int nRemoteValues[10];
int nCount = 0;
size_t nUserType = 0;
assert (DMLayer_AddObserverCallback (pDMLayer, pszProducer, strlen (pszProducer), Consumer_Callback_Notify));
while (DMLayer_ObserveVariable (pDMLayer, pszBinConsumer, strlen (pszBinConsumer), &nUserType) || Cpx_Yield ())
{
NOTRACE ("[%s] From: [%zu] -> type: [%u - bin: %u], size: [%zu]\n",
__FUNCTION__,
nUserType,
DMLayer_GetVariableType (pDMLayer, pszBinConsumer, strlen (pszBinConsumer)),
(uint8_t)VAR_TYPE_BINARY,
DMLayer_GetVariableBinarySize (pDMLayer, pszBinConsumer, strlen (pszBinConsumer)));
if (DMLayer_GetVariableType (pDMLayer, pszBinConsumer, strlen (pszBinConsumer)) == VAR_TYPE_BINARY)
{
DMLayer_GetBinary (pDMLayer, pszBinConsumer, strlen (pszBinConsumer), nRemoteValues, sizeof (nRemoteValues));
printf ("[%s] Values: ", __FUNCTION__);
for (nCount = 0; nCount < sizeof (nRemoteValues) / sizeof (nRemoteValues[0]); nCount++)
{
printf ("[%u] ", nRemoteValues[nCount]);
}
printf ("\n");
}
}
}
void DMLayer_YieldContext ()
{
Cpx_Yield ();
}
bool DMLayer_LockInit(DMLayer* pDMLayer)
{
VERIFY (NULL != pDMLayer, "Error, DMLayer is invalid.", false);
NOTRACE ("Starting up Lock.. \n");
/*
* Initiate Variable lock
*/
CpxSmartLock* pLock = malloc (sizeof (pLock));
VERIFY (Cpx_LockInit(pLock), "Error initiating lock.", false);
DMLayer_SetUserData(pDMLayer, (void*) pLock, false);
/*
* Initiate Notify lock
*/
pLock = malloc (sizeof (pLock));
VERIFY (Cpx_LockInit(pLock), "Error initiating lock.", false);
DMLayer_SetUserData(pDMLayer, (void*) pLock, true);
return true;
}
bool DMLayer_Lock(DMLayer* pDMLayer, bool bIsNotify)
{
CpxSmartLock* pLock = NULL;
VERIFY (NULL != pDMLayer, "Error, DMLayer is invalid.", false);
VERIFY ((pLock = (CpxSmartLock*) DMLayer_GetUserData(pDMLayer, bIsNotify)) != NULL, "No Lock defined.", false);
NOTRACE ("%s: (Notify: %pX-%u): Pre-Lock: [%u:%zu]\n", __FUNCTION__, pLock, bIsNotify, pLock->bExclusiveLock, pLock->nSharedLockCount);
VERIFY (Cpx_Lock(pLock), "Failed acquire exclusive lock", false);
NOTRACE ("%s: Lock: [%u:%zu]\n", __FUNCTION__, pLock->bExclusiveLock, pLock->nSharedLockCount);
return true;
}
bool DMLayer_SharedLock(DMLayer* pDMLayer, bool bIsNotify)
{
CpxSmartLock* pLock = NULL;
VERIFY (NULL != pDMLayer, "Error, DMLayer is invalid.", false);
VERIFY ((pLock = (CpxSmartLock*) DMLayer_GetUserData(pDMLayer, bIsNotify)) != NULL, "No Lock defined.", false);
NOTRACE ("%s: (Notify: %pX-%u): Pre-Lock: [%u:%zu]\n", __FUNCTION__, pLock, bIsNotify, pLock->bExclusiveLock, pLock->nSharedLockCount);
VERIFY (Cpx_SharedLock(pLock), "Failed acquire shared lock", false);
NOTRACE (" Lock: [%zu]\n", pLock->nSharedLockCount);
return true;
}
bool DMLayer_Unlock(DMLayer* pDMLayer, bool bIsNotify)
{
CpxSmartLock* pLock = NULL;
VERIFY (NULL != pDMLayer, "Error, DMLayer is invalid.", false);
VERIFY ((pLock = (CpxSmartLock*) DMLayer_GetUserData(pDMLayer, bIsNotify)) != NULL, "No Lock defined.", false);
NOTRACE ("%s: (Notify: %u)\n", __FUNCTION__, bIsNotify);
VERIFY (Cpx_Unlock(pLock), "Failed unlock", false);
NOTRACE (" Lock: [%u]\n", pLock->bExclusiveLock);
return true;
}
bool DMLayer_SharedUnlock(DMLayer* pDMLayer, bool bIsNotify)
{
CpxSmartLock* pLock = NULL;
VERIFY (NULL != pDMLayer, "Error, DMLayer is invalid.", false);
VERIFY ((pLock = (CpxSmartLock*) DMLayer_GetUserData(pDMLayer, bIsNotify)) != NULL, "No Lock defined.", false);
NOTRACE ("%s: (Notify: %u)\n", __FUNCTION__, bIsNotify);
VERIFY (Cpx_SharedUnlock(pLock), "Failed to unlock", false);
NOTRACE (" Lock: [%zu]\n", pLock->nSharedLockCount);
return true;
}
bool DMLayer_LockEnd(DMLayer* pDMLayer)
{
CpxSmartLock* pLock = NULL;
VERIFY (NULL != pDMLayer, "Error, DMLayer is invalid.", false);
/*
* Releaseing Varaible lock
*/
VERIFY ((pLock = (CpxSmartLock*) DMLayer_GetUserData(pDMLayer, false)) != NULL, "No variable Lock defined.", false);
Cpx_Unlock(pLock);
free (pLock);
DMLayer_SetUserData(pDMLayer, NULL, false);
/*
* Releaseing Notify lock
*/
VERIFY ((pLock = (CpxSmartLock*) DMLayer_GetUserData(pDMLayer, true)) != NULL, "No notification Lock defined.", false);
Cpx_Unlock(pLock);
free (pLock);
DMLayer_SetUserData(pDMLayer, NULL, true);
return true;
}
void Cpx_SleepTicks (uint32_t nSleepTime)
{
usleep ((useconds_t)nSleepTime * 1000);
}
uint32_t Cpx_GetCurrentTick (void)
{
struct timeval tp;
gettimeofday (&tp, NULL);
return (uint32_t)tp.tv_sec * 1000 + tp.tv_usec / 1000; // get current timestamp in milliseconds
}
void Cpx_StackOverflowHandler ()
{
printf ("Error, Thread#%zu Stack %zu / %zu max\n", Cpx_GetID (), Cpx_GetStackSize (), Cpx_GetMaxStackSize ());
exit (1);
}
int main ()
{
// start random
srand ((unsigned int)(time_t)time (NULL));
VERIFY ((pDMLayer = DMLayer_CreateInstance ()) != NULL, "Error creating DMLayer instance", 1);
assert (Cpx_Start (20));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 1));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 300));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 300));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 500));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 500));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 50));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 800));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 800));
assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 1000));
//assert (Cpx_CreateThread (Thread_Producer, NULL, 1024, 60000));
assert (Cpx_CreateThread (Thread_Consumer, NULL, 1024, 1000));
Cpx_Join ();
DMLayer_ReleaseInstance(pDMLayer);
return 0;
}