-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtappet.c
More file actions
216 lines (194 loc) · 4.78 KB
/
tappet.c
File metadata and controls
216 lines (194 loc) · 4.78 KB
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
#include "libobjdata.h"
void init_buffer (buffer_t *b)
{
b->in_marker = b->out_marker = b->count = 0;
for (int i = 0; i < b->buff_size; i++)
{
b->slot[i].data = (char *) malloc (24);
b->slot[i].size = 0;
}
pthread_mutex_init (&b->mutex, NULL);
pthread_cond_init (&b->occupied_slot, NULL);
pthread_cond_init (&b->empty_slot, NULL);
}
void init_slot(buffer_t *b)
{
b->latest = 0;
b->reading = 0;
b->slots[0] = 0;
b->slots[1] = 0;
b->prev = 5;
}
void free_buffer (buffer_t *b)
{
// We don't need our mutex or condition variables any more
pthread_mutex_destroy (&b->mutex);
pthread_cond_destroy (&b->occupied_slot);
pthread_cond_destroy (&b->empty_slot);
free(b);
}
int main(int argc, char *argv[])
{
int options;
static pthread_attr_t tattr; // thread attributes. USed for controling scheduling
static struct sched_param tp;
void *lib_handle;
char *funcList [1000];
int funcIndex = 0;
int size = 1;
int argn = 1;
char *typeBuffer;
lib_handle = dlopen("./libobjdata.so", RTLD_LAZY);
if (!lib_handle)
{
dlerror();
return 1;
}
for(int index = 1; index < argc; index++)
{
if(strncmp(argv[index], "-t", 2) == 0)
{
funcList[funcIndex] = argv[++index];
funcIndex++;
}
else
{
if (strspn(argv[index], "0123456789"))
{
argn = atoi(argv[index]);
//printf("argn: %d\n", argn);
}
else
{
optind = index;
break;
}
}
}
//Command line parsing for the sync type and the size of the buffer
while((options=getopt(argc, argv, "b:s:"))!=-1)
{
switch(options)
{
case 'b':
typeBuffer = optarg;
break;
case 's':
size = atoi(optarg);
break;
case '?':
printf("Unknown option");
break;
}
}
//prepare thread attributes
if (pthread_attr_setschedpolicy(&tattr, SCHED_RR)) //Set the scheduling policy to red robin
{
fprintf (stderr, "Cannot set thread scheduling policy!\n");
exit (1);
}
tp.sched_priority = 50;
if (pthread_attr_setschedparam(&tattr, &tp)) //set priority between 0 and 99
{
fprintf (stderr, "Cannot set thread scheduling priority!\n");
exit (1);
}
if (pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM)) //Now set the thread scope to be system-wide
{
fprintf (stderr, "Cannot set thread execution scope!\n");
exit (1);
}
pthread_t *thread_table;
thread_table = (pthread_t*)malloc((funcIndex) * sizeof(pthread_t));
if (thread_table == NULL)
{
fprintf (stderr, "Cannot allocate memory for thread_table");
exit (1);
}
thread_arg *arg = (thread_arg*)malloc(sizeof(thread_arg) + (sizeof(buffer_t*) * funcIndex-1));
if (strcmp(typeBuffer,"async") == 0)
{
arg->argn = argn;
arg->is_sync = 1;
//printf("arg->argn: %d\n", arg->argn);
for (int i = 0; i < funcIndex-1; i++)
{
arg->buff[i] = (buffer_t*)malloc(sizeof(buffer_t));
//initial the buffer
init_slot(arg->buff[i]);
}
for (int i = 0; i < funcIndex; i++)
{
struct local_file* (*func)(void);
const char* error_msg;
func = dlsym(lib_handle, funcList[i]);
error_msg = dlerror();
//printf("%s\n", funcList[i]);
if (error_msg)
{
dlerror();
dlclose(lib_handle);
return 1;
}
if (pthread_create (&thread_table[i], NULL, (void *(*)(void *))func, arg) != 0)
{
perror ("Unable to create thread");
exit (1);
}
}
for (int i = 0; i < funcIndex; i++)
{
pthread_join (thread_table[i], NULL);
}
free(thread_table);
free(arg);
}
else
{
arg->argn = argn;
arg->is_sync = 0;
//printf("arg->argn: %d\n", arg->argn);
for (int i = 0; i < funcIndex-1; i++)
{
//initial the buffer
arg->buff[i] = (buffer_t*)malloc(sizeof(buffer_t) + sizeof(slot_t) * size);
if (arg->buff[i] == NULL)
{
fprintf (stderr, "Cannot allocate memory for buff");
exit(1);
}
arg->buff[i]->buff_size = size;
init_buffer(arg->buff[i]);
}
for (int i = 0; i < funcIndex; i++)
{
struct local_file* (*func)(void);
const char* error_msg;
func = dlsym(lib_handle, funcList[i]);
error_msg = dlerror();
if (error_msg)
{
dlerror();
dlclose(lib_handle);
return 1;
}
if (pthread_create (&thread_table[i], &tattr, (void *(*)(void *))func, arg) != 0)
{
perror ("Unable to create thread");
exit (1);
}
}
for (int i = 0; i < funcIndex; i++)
{
pthread_join (thread_table[i], NULL);
}
free(thread_table);
for (int i = 0; i <funcIndex-1; i++)
{
free_buffer(arg->buff[i]);
}
free(arg);
}
dlclose(lib_handle);
return 0;
}