-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
tsrm.c
423 lines (341 loc) · 9.94 KB
/
tsrm.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* SNOOPY COMMAND LOGGER
*
* File: tsrm.c
*
* Copyright (c) 2015 Bostjan Skufca <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Includes order: from local to global
*/
#include "snoopy.h"
#include "tsrm.h"
#include "configuration.h"
#include "inputdatastorage.h"
#include "util/list-snoopy.h"
#include <pthread.h>
#include <stdlib.h>
/*
* Global variables
*/
pthread_once_t snoopy_tsrm_init_onceControl = PTHREAD_ONCE_INIT;
pthread_mutex_t snoopy_tsrm_threadRepo_mutex;
pthread_mutexattr_t snoopy_tsrm_threadRepo_mutexAttr;
list_t snoopy_tsrm_threadRepo_data = {
.first = NULL,
.last = NULL,
.count = 0,
};
list_t *snoopy_tsrm_threadRepo = &snoopy_tsrm_threadRepo_data;
/*
* Non-exported function prototypes
*/
void snoopy_tsrm_init ();
int snoopy_tsrm_doesThreadRepoEntryExist (snoopy_tsrm_threadId_t threadId, int mutex_already_locked);
snoopy_tsrm_threadId_t snoopy_tsrm_getCurrentThreadId ();
listNode_t* snoopy_tsrm_getCurrentThreadRepoEntry ();
snoopy_tsrm_threadData_t* snoopy_tsrm_getCurrentThreadData ();
snoopy_tsrm_threadData_t* snoopy_tsrm_createNewThreadData (snoopy_tsrm_threadId_t threadId);
/*
* snoopy_tsrm_ctor
*
* Description:
* Initialize threadRepo list.
*
* Params:
* (none)
*
* Return:
* void
*/
void snoopy_tsrm_ctor ()
{
snoopy_tsrm_threadId_t curTid;
snoopy_tsrm_threadData_t *tData;
// Initialize threading support
pthread_once(&snoopy_tsrm_init_onceControl, &snoopy_tsrm_init);
// Get my thread id - before mutex, no need for mutex here
curTid = snoopy_tsrm_getCurrentThreadId();
// Mutex START
pthread_mutex_lock(&snoopy_tsrm_threadRepo_mutex);
// Create my entry if it does not exist
if (SNOOPY_FALSE == snoopy_tsrm_doesThreadRepoEntryExist(curTid, SNOOPY_TRUE)) {
tData = snoopy_tsrm_createNewThreadData(curTid);
snoopy_util_list_push(snoopy_tsrm_threadRepo, tData);
}
// Mutex END
pthread_mutex_unlock(&snoopy_tsrm_threadRepo_mutex);
}
/*
* snoopy_tsrm_dtor
*
* Description:
* Removes own entry from threadRepo, or removes threadRepo altogether.
*
* Params:
* (none)
*
* Return:
* void
*/
void snoopy_tsrm_dtor ()
{
listNode_t *tRepoEntry;
snoopy_tsrm_threadData_t *tData;
// Get my thread data structure
tRepoEntry = snoopy_tsrm_getCurrentThreadRepoEntry();
if (NULL == tRepoEntry) {
// This is an error condition, but let's not free NULLs below
return;
}
// Mutex START
pthread_mutex_lock(&snoopy_tsrm_threadRepo_mutex);
// Remove from repo
tData = snoopy_util_list_remove(snoopy_tsrm_threadRepo, tRepoEntry);
// Mutex END
pthread_mutex_unlock(&snoopy_tsrm_threadRepo_mutex);
// Free the results AFTER my node has been removed from repo
free(tData->inputdatastorage);
free(tData->configuration);
free(tData);
return;
}
/*
* snoopy_tsrm_init
*
* Description:
* Initialize threading subsystem. This function should be run
* only once, by only one thread.
*
* Params:
* (none)
*
* Return:
* void
*/
void snoopy_tsrm_init ()
{
// Initialize threadRepo mutex
pthread_mutexattr_init (&snoopy_tsrm_threadRepo_mutexAttr);
pthread_mutexattr_settype(&snoopy_tsrm_threadRepo_mutexAttr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init (&snoopy_tsrm_threadRepo_mutex, &snoopy_tsrm_threadRepo_mutexAttr);
}
/*
* snoopy_tsrm_doesThreadRepoEntryExist
*
* Description:
* Check if thread with id threadId already has an entry in Snoopy's thread
* repository.
*
* Params:
* snoopy_tsrm_threadId_t: Thread ID to look for
*
* Return:
* int: SNOOPY_TRUE if yes, SNOOPY_FALSE if not
*/
int snoopy_tsrm_doesThreadRepoEntryExist (snoopy_tsrm_threadId_t threadId, int mutex_already_locked)
{
const snoopy_tsrm_threadData_t *tData;
int retVal = SNOOPY_FALSE;
// Mutex START
if (SNOOPY_TRUE != mutex_already_locked) {
pthread_mutex_lock(&snoopy_tsrm_threadRepo_mutex);
}
listNode_t * curNode = NULL;
while (NULL != (curNode = snoopy_util_list_fetchNextNode(snoopy_tsrm_threadRepo, curNode))) {
if (NULL == curNode->value) {
continue;
}
tData = curNode->value;
if (0 != pthread_equal(threadId, tData->threadId)) {
/* Thread ID matches */
retVal = SNOOPY_TRUE;
goto FOUND;
}
}
FOUND:
// Mutex END
if (SNOOPY_TRUE != mutex_already_locked) {
pthread_mutex_unlock(&snoopy_tsrm_threadRepo_mutex);
}
return retVal;
}
/*
* snoopy_tsrm_createNewThreadData
*
* Description:
* Mallocs space and creates new thread data structure, for given thread ID.
*
* Params:
* snoopy_tsrm_threadId_t: Thread ID to create entry for
*
* Return:
* snoopy_tsrm_threadData_t*: Pointer to newly-created threadData structure
*/
snoopy_tsrm_threadData_t* snoopy_tsrm_createNewThreadData (snoopy_tsrm_threadId_t threadId)
{
snoopy_tsrm_threadData_t *tData;
// Allocte storage memory for new threadData structure
tData = malloc(sizeof(snoopy_tsrm_threadData_t));
tData->configuration = malloc(sizeof(snoopy_configuration_t));
tData->inputdatastorage = malloc(sizeof(snoopy_inputdatastorage_t));
// Store thread ID
tData->threadId = threadId;
// Initialize empty values
snoopy_configuration_setUninitialized (tData->configuration);
snoopy_inputdatastorage_setUninitialized(tData->inputdatastorage);
return tData;
}
/*
* snoopy_tsrm_getCurrentThreadId
*
* Description:
* Retrieve id of current thread
*
* Params:
* (none)
*
* Return:
* snoopy_tsrm_threadId_t: Current thread id
*/
snoopy_tsrm_threadId_t snoopy_tsrm_getCurrentThreadId ()
{
return pthread_self();
}
/*
* snoopy_tsrm_getCurrentThreadRepoEntry
*
* Description:
* Retrieve pointer to threadRepo list node structure of current thread.
*
* Params:
* (none)
*
* Return:
* listNode_t*: Current threadRepo entry, or null if it does not exist
*/
listNode_t * snoopy_tsrm_getCurrentThreadRepoEntry ()
{
snoopy_tsrm_threadId_t myThreadId;
listNode_t *tRepoEntry = NULL;
const snoopy_tsrm_threadData_t *tData;
// This is the thread ID we are looking for
myThreadId = snoopy_tsrm_getCurrentThreadId();
// Mutex START
pthread_mutex_lock(&snoopy_tsrm_threadRepo_mutex);
listNode_t * curNode = NULL;
while (NULL != (curNode = snoopy_util_list_fetchNextNode(snoopy_tsrm_threadRepo, curNode))) {
// This should not happen, but maybe, just maybe, there is another thread
// that is just creating a new entry.
if (NULL == curNode->value) {
continue;
}
tData = curNode->value;
if (0 != pthread_equal(myThreadId, tData->threadId)) {
tRepoEntry = curNode;
goto FOUND;
}
}
FOUND:
// Mutex END
pthread_mutex_unlock(&snoopy_tsrm_threadRepo_mutex);
// Return
return tRepoEntry;
}
/*
* snoopy_tsrm_getCurrentThreadData
*
* Description:
* Retrieve pointer to thread data structure of current thread. Creates
* a new repo entry if it does not exist.
*
* Params:
* (none)
*
* Return:
* snoopy_tsrm_threadData_t: Current threadRepo list entry, or NULL
*/
snoopy_tsrm_threadData_t* snoopy_tsrm_getCurrentThreadData ()
{
listNode_t *tRepoEntry;
tRepoEntry = snoopy_tsrm_getCurrentThreadRepoEntry();
if (NULL == tRepoEntry) {
return NULL;
}
return tRepoEntry->value;
}
/*
* snoopy_tsrm_get_configuration()
*
* Description:
* Retrieve configuration array pointer for current thread.
* This function is to be consumed by the rest of Snoopy.
*
* Params:
* (none)
*
* Return:
* snoopy_configuration_t*: Pointer to thread-specific Snoopy configuration data structure
*/
snoopy_configuration_t* snoopy_tsrm_get_configuration ()
{
snoopy_tsrm_threadData_t *tData;
tData = snoopy_tsrm_getCurrentThreadData();
return tData->configuration;
}
/*
* snoopy_tsrm_get_inputdatastorage()
*
* Description:
* Retrieve input data storage array pointer for current thread.
* This function is to be consumed by the rest of Snoopy.
*
* Params:
* (none)
*
* Return:
* snoopy_inputdatastorage_t*: Pointer to thread-specific input data storage structure
*/
snoopy_inputdatastorage_t* snoopy_tsrm_get_inputdatastorage ()
{
snoopy_tsrm_threadData_t *tData;
tData = snoopy_tsrm_getCurrentThreadData();
return tData->inputdatastorage;
}
/*
* snoopy_tsrm_get_threadCount()
*
* Description:
* Retrieves number of currently configured threads.
*
* Params:
* (none)
*
* Return:
* snoopy_inputdatastorage_t*: Pointer to thread-specific input data storage structure
*/
int snoopy_tsrm_get_threadCount ()
{
int threadCount;
// Mutex START
pthread_mutex_lock(&snoopy_tsrm_threadRepo_mutex);
// Get count
threadCount = snoopy_tsrm_threadRepo->count;
// Mutex END
pthread_mutex_unlock(&snoopy_tsrm_threadRepo_mutex);
return threadCount;
}