/
myThread.c
231 lines (178 loc) · 5.72 KB
/
myThread.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
/* mi lebreria de hilos basica
*/
#include <stdio.h>
#include <setjmp.h>
#include <ucontext.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "Structures.h"
#include "myThread.h"
//typedef long thread_t;
extern struct Thread **tasks;
extern int mode; // 1 = expropiativo 2= no expropiativo
sigset_t sigThreadMask; // para el manejo de las sennales
struct sigaction schedulerHandle; //con este es que voy a inicializar el scheduler (ver el thread_init)
ucontext_t notifierContext; //funciona como notificador
struct itimerval quantum; //estrutura para el timer
long timeIntervalInMSec;
sigset_t sigsetMask;
extern Thread * current;
ucontext_t mainContext;
state *states;
void stop_timer(){
struct itimerval zero_timer = { 0 };
setitimer(ITIMER_PROF, &zero_timer, &quantum);
}
void start_timer(){
setitimer(ITIMER_PROF, &quantum, NULL);
}
void generalFunction(void (*rutine)(int, int), int arg, int id){
(*rutine)(arg, id);
states[id].finish = 1;
thread_yield();
<<<<<<< HEAD
=======
void stop_timer(){
struct itimerval zero_timer = { 0 };
setitimer(ITIMER_PROF, &zero_timer, &quantum);
}
void start_timer(){
setitimer(ITIMER_PROF, &quantum, NULL);
}
void generalFunction(void (*rutine)(int),int arg){
(*rutine)(arg);
current->finish = 1;
>>>>>>> 9f390b8f4e9511acfa13047bec0d3822c7fabb91
return;
}
/*
void threadCompletedNotifier(){
current->isCompleted=1;
raise(SIGPROF);
}*/
static long currentId = 0;
int getNextThreadId(){
return ++currentId;
}
struct Thread* getNewThreadnoStack(int tickets, char *id)
{
struct Thread *newThread = (struct Thread *)malloc(sizeof(struct Thread));
newThread->tickets = tickets;
newThread->tid = id; //getNextThreadId();
newThread->finish = 0;
return newThread;
}
struct sched_t *scheduler;
void timer_handler(int sigNum){
scheduler->manageTimer();
}
void thread_init(long nquantum, int totalProcessInit, struct sched_t *schedulerIn) {
scheduler = schedulerIn;
sigemptyset(&sigsetMask);
sigaddset(&sigsetMask,SIGPROF);
getcontext(&mainContext);
memset(&schedulerHandle,0,sizeof(schedulerHandle));
schedulerHandle.sa_handler = timer_handler;
sigaction(SIGPROF,&schedulerHandle,NULL);
timeIntervalInMSec = nquantum;
quantum.it_value.tv_sec = 0;// http://linux.die.net/man/2/setitimer
quantum.it_value.tv_usec = timeIntervalInMSec;
quantum.it_interval.tv_sec=0;
quantum.it_interval.tv_usec = timeIntervalInMSec;
<<<<<<< HEAD
states = (state*)malloc(sizeof(state) * totalProcessInit);
int k;
for( k = 0; k < totalProcessInit; k++){
state newState;;
newState.id = k;
newState.finish = 0;
newState.percent = 0.0;
newState.result = 0.0;
newState.active = 0;
states[k] = newState;
}
=======
>>>>>>> 9f390b8f4e9511acfa13047bec0d3822c7fabb91
/*if(mode == EXPROPIATIVO)
setitimer(ITIMER_PROF, &quantum, NULL);*/
/* sigemptyset(&sigsetMask);
sigaddset(&sigsetMask, SIGPROF); //para mandar sennal cuando expire el timer
timeIntervalInMSec = nquantum;
schedulerHandle.sa_sigaction = timer_handler;
schedulerHandle.sa_flags = SA_SIGINFO|SA_RESTART;
sigemptyset(&schedulerHandle.sa_mask);
sigaction(SIGRTMIN, &schedulerHandle, NULL);
//inicializo el quantum
quantum.it_value.tv_sec = 0;// http://linux.die.net/man/2/setitimer
quantum.it_value.tv_usec = timeIntervalInMSec;
quantum.it_interval.tv_sec=0;
quantum.it_interval.tv_usec = timeIntervalInMSec;
setitimer(ITIMER_PROF, &quantum, NULL);*/
}
int thread_create(char *id, int tickets, void (*rutina)(int, int), int arg, int idInt) {
struct Thread *newThread = getNewThreadnoStack(tickets, id);
ucontext_t context;
getcontext(&(context));
context.uc_stack.ss_sp = malloc(STACKSIZE);
context.uc_stack.ss_size = STACKSIZE;
context.uc_stack.ss_flags = 0;
context.uc_link = &mainContext;
makecontext (&(context), (void (*) (void))generalFunction, 3, rutina, arg, idInt);
scheduler->addTask(newThread, context);
return 1;
}
void thread_yield(){
<<<<<<< HEAD
raise(SIGPROF); //con esto deberia bastar mandando la sennal se captura y cambio segun el scheduler
=======
raise(SIGPROF);
//raise(SIGPROF); //con esto deberia bastar mandando la sennal se captura y cambio segun el scheduler
>>>>>>> 9f390b8f4e9511acfa13047bec0d3822c7fabb91
}
void thread_exit(){
//deberia terminar el contexto y podriamos y deberiamos tener una estructura para meter a los threads terminados, asi que tambien lo meteria a la lista de terminados
}
thread_t self(){
return 0; //devuelvo el id del thread actual // pensaria tener una lista y en la estructura de la lista tener el actual. o tener el thread actual por aparte. estoy analizando
}
int thread_join(){
scheduler->switchContext();
return 0;//espera a que todos hayan terminado
}
char* getCurrentid(){
return current->tid;
}
float getCurrentPercent(){
return current->percent;
}
double getCurrentResult(){
return current->result;
}
int isFinished(){
return current->finish;
}
int getActualNumberOfProcess(){
<<<<<<< HEAD
void updateThread(float percent, double result, int id){
states[id].percent = percent;
states[id].result = (2 * result);
=======
return actualNumberOfProcess;
}
void updateCurrentScheduler(){
scheduler->updateCurrent();
}
void updateThread(float percent, double result){
current->percent = percent;
current->result = 2 * result;
>>>>>>> 9f390b8f4e9511acfa13047bec0d3822c7fabb91
}
char* getCurrentid(){
return current->tid;
}
int isFinished(){
return current->finish;
}