-
Notifications
You must be signed in to change notification settings - Fork 0
/
mypthread.h
executable file
·134 lines (106 loc) · 3.06 KB
/
mypthread.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
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <mutex.h>
#include <string.h>
#ifndef MYTHREAD_H
#define MYTHREAD_H
#define FALSE 0
#define TRUE 1
#define RUNNING 0
#define READY 1 //espera para ser agendado (scheduling)
#define BLOCKED 2 //esperando unirse
#define DEFUNCT 3 //muerto
#define NTHREADS 8 //numero maximo de hilos
typedef struct mythread_attr {
//valor por defecto: SIGSTKSZ
unsigned long stackSize;
}mythread_attr_t;
typedef struct mythread {
pid_t tid;
}mythread_t;
/*
* Thread Control Block (TCB)
*/
typedef struct mythread_private {
pid_t tid;
int state;
//solo se usa en el algortimo de tiempo real, ubicacion del arreglo del algortimo
int id;
//solo se usa en el algoritmo de tiempo real, si es igual a -1, es porque no tiene prioridad
int priority;
//funcion donde esta asignado el hilo
void * (*start_func) (void *);
//argumentos de la funcion
void *args;
//valor de retorno de la funcion
void *returnValue;
struct mythread_private *blockedForJoin;
//mutex utilizado por el despachador para programar este hilo
struct mutex sched_mutex;
struct mythread_private *prev, *next;
}mythread_private_t;
//puntero a la cabeza de la cola
extern mythread_private_t *mythread_q_head;
mythread_t mythread_self(void);
/*
* Prepara el contexto de un nuevo hilo, attr se ignora por el momento
*/
int mythread_create(mythread_t *new_thread_ID,
mythread_attr_t *attr,
void * (*start_func)(void *),
void *arg,
int priority_A);
/*
* cambia de un hilo ejecutandose a otro que este listo para ejecutarse (READY)
*/
int mythread_yield(void);
/*
* cambia de algoritmo de scheduling
*/
int my_thread_chsched(int sched);
/*
* inicializa variables globales
*/
void my_thread_init();
/*
* suspende la llamada del hilo si el hilo actual no ha terminado,
* en cola el hilo actual, luego dispatch y lo marca como ready para
* volver a ser llamado
*/
int mythread_join(mythread_t target_thread, void **status);
/*
* desencola un hilo y lo mata
*/
void mythread_end(void *retval);
pid_t __mythread_gettid();
mythread_private_t *__mythread_selfptr();
int my_thread_detach_FIFO(mythread_private_t *);
int my_thread_detach_Lottery(mythread_private_t *);
int my_thread_detach_RT(mythread_private_t *);
void __mythread_debug_futex_init();
/*
* Solo se usa con el debuguer
*/
extern char debug_msg[1000];
extern struct mutex debug_futex;
#ifdef DEBUG
#define DEBUG_PRINTF(...) __mythread_debug_futex_init(); \
mutex_down(&debug_futex); \
sprintf(debug_msg, __VA_ARGS__); \
write(1, debug_msg, strlen(debug_msg)); \
mutex_up(&debug_futex);
#else
#define DEBUG_PRINTF(...) do {} while(0);
#endif
#define ERROR_PRINTF(...) __mythread_debug_futex_init(); \
mutex_down(&debug_futex); \
sprintf(debug_msg, __VA_ARGS__); \
write(1, debug_msg, strlen(debug_msg)); \
mutex_up(&debug_futex);
#define LOG_PRINTF(...) __mythread_debug_futex_init(); \
mutex_down(&debug_futex); \
sprintf(debug_msg, __VA_ARGS__); \
write(1, debug_msg, strlen(debug_msg)); \
mutex_up(&debug_futex);
#endif /* MYTHREAD_H */