-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdrad_thread.h
150 lines (129 loc) · 3.21 KB
/
sdrad_thread.h
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
/**
* @file sdrad_thread.h
* @author Merve Gulmez
* @brief
* @version 0.1
* @date 2023-05-02
*
* @copyright © Ericsson AB 2022-2023
*
* SPDX-License-Identifier: BSD 3-Clause
*/
#include <sys/syscall.h>
#include <sys/mman.h>
#include <setjmp.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#ifdef SDRAD_MULTITHREAD
#define SDRAD_MUTEX_LOCK(...)\
sdrad_mutex_lock()
#define SDRAD_MALLOC_LOCK(...)\
sdrad_malloc_lock()
#else
#define SDRAD_MUTEX_LOCK(...)
#define SDRAD_MALLOC_LOCK(...)
#endif
#ifdef SDRAD_MULTITHREAD
#define SDRAD_MUTEX_UNLOCK(...) \
sdrad_mutex_unlock()
#define SDRAD_MALLOC_UNLOCK(...) \
sdrad_malloc_unlock()
#else
#define SDRAD_MUTEX_UNLOCK(...)
#define SDRAD_MALLOC_UNLOCK(...)
#endif
#ifdef SDRAD_MULTITHREAD
__attribute__((always_inline))
static inline int32_t sdrad_get_sti(sdrad_global_manager_t *sgm_ptr)
{
sti_t tid;
int32_t total_thread;
int32_t k;
tid = pthread_self();
total_thread = sgm_ptr -> sgm_total_thread;
for(k = 0; k < total_thread+1; k++)
{
if(tid == sgm_ptr -> sgm_thread_id[k]){
return k;
}
}
return -1;
}
__attribute__((always_inline))
static inline int32_t sdrad_get_sti_from_tid(sdrad_global_manager_t *sgm_ptr, sti_t tid)
{
int32_t total_thread;
int32_t k;
total_thread = sgm_ptr -> sgm_total_thread;
for(k = 0; k < total_thread+1; k++)
{
if(tid == sgm_ptr -> sgm_thread_id[k]){
return k;
}
}
return -1;
}
#else
#define sdrad_get_sti(...) ROOT_DOMAIN;
#endif
// mutex related functions
__attribute__((always_inline))
static inline void sdrad_mutex_lock()
{
int32_t ret;
ret = pthread_mutex_lock(&sdrad_global_manager.sgm_lock);
assert(ret == 0);
if(ret){
switch(ret){
case EDEADLK:
printf( "A deadlock condition was detected \n");
break;
default:
SDRADEXIT("pthread_mutex_lock");
}
}
}
// mutex related functions
__attribute__((always_inline))
static inline void sdrad_malloc_lock()
{
int32_t ret;
ret = pthread_mutex_lock(&malloc_mutex);
assert(ret == 0);
if(ret){
switch(ret){
case EDEADLK:
printf( "A deadlock condition was detected \n");
break;
default:
SDRADEXIT("pthread_mutex_lock");
}
}
}
__attribute__((always_inline))
static inline void sdrad_mutex_unlock()
{
pthread_mutex_unlock(&sdrad_global_manager.sgm_lock);
}
__attribute__((always_inline))
static inline void sdrad_malloc_unlock()
{
pthread_mutex_unlock(&malloc_mutex);
}
extern void sdrad_wait_cond();
extern void *sdrad_pthread_construstor();
extern void sdrad_mutex_constructor(sdrad_global_manager_t *sgm_ptr);