-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhilos.h
163 lines (131 loc) · 3.68 KB
/
hilos.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
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef HILOS_H
#define HILOS_H
#include <queue>
#include <condition_variable>
#include <thread>
#include <vector>
#include "interprete.h"
// struct InterpPriori
struct InterpPriori{
public:
size_t prioridad;
private :
InterpreteBF interprete;
public:
/*
PRE: Recibe una prioridad (size_t), y un
interpreteBF (InterpreteBF) ya creado.
POST: Inicializa un Interprete con prioridad.
*/
InterpPriori(size_t prioridad, InterpreteBF iBF);
/*
Destruye un interprete con prioridad.
*/
~InterpPriori();
/*
Ejecuta el interprete, que guarda.
*/
bool ejecutar();
};
// struct IPComparador
struct IPComparador{
/*
PRE: Recibe dos punteros a instancias de InterpPriori (InterpPriori const*).
POST: Devuelve true, si el primero tiene mayor prioridad que el segundo.
*/
bool operator() (InterpPriori const* iP1, InterpPriori const* iP2);
};
typedef
std::priority_queue<InterpPriori*, std::vector<InterpPriori*>, IPComparador>
colaPrioridadInterprete_t;
//class PrioriColaInterpProtegida
class PrioriColaInterpProtegida{
private:
colaPrioridadInterprete_t heapInterp;
bool colaAbierta;
std::mutex centinela;
std::condition_variable continuar_desencolar;
public:
/*
PRE: Recibe un valor booleano que indique cuando el hilo principal
ya no va encolar mas elementos.
POST: Inicializa una cola de interpretes con prioridad protegida.
*/
PrioriColaInterpProtegida();
/*Destruye una cola de interpretes con prioridad protegida.*/
~PrioriColaInterpProtegida();
/*
Se le notifica a la cola que no se encolaran mas elementos
en ella.
*/
void cerrarCola();
/*
PRE: Recibe un interprete con prioridad (InterpPriori *)
POST: Encola el interprete recibido en la cola.
*/
void encolar(InterpPriori *interpP);
/*
Desencola un interprete con prioridad (InterpPriori *) y
lo devuelve.
Si el valor devuelto es NULL, es porque la cola esta cerrada
y vacia.
*/
InterpPriori *desencolar();
/*Devuelve true si la cola esta vacia, false en caso contrario.*/
bool estaVacia();
};
//class Thread
class Thread {
private:
std::thread thread;
public:
/*Inicializa un Thread*/
Thread() {}
/*Inicia un nuevo hilo de ejecucion*/
void start() {
thread = std::thread(&Thread::run, this);
}
/*Espera a que el hilo en ejecucion termine*/
void join() {
thread.join();
}
/*Ejecuta la funcion del Thread*/
virtual void run() = 0;
/*Destruye el Thread*/
virtual ~Thread() {}
/*Impide que se copien hilos por parametro*/
Thread(const Thread&) = delete;
/*Evita que se copien hilos por asignacion*/
Thread& operator=(const Thread&) = delete;
/*
Mueve semanticamente los recursos de un Thread a otro,
por parametro.
*/
Thread(Thread&& other) {
this->thread = std::move(other.thread);
}
/*
Mueve semanticamente los recursos de un Thread a otro,
por asignacion.
*/
Thread& operator=(Thread&& other) {
this->thread = std::move(other.thread);
return *this;
}
};
//clase HiloBF
class HiloBF: public Thread {
private:
PrioriColaInterpProtegida &heapInterp;
public:
/*
PRE: Recibe una cola de interpretes con prioridad protegida.
POST: Inicializa un hilo de ejecucion de script intepretes brainfuck.
*/
explicit HiloBF(PrioriColaInterpProtegida &hCola);
/*Destruye el hilo de ejecucion.*/
~HiloBF();
/*Ejecuta el hilo de ejecuccion.*/
virtual void run() override;
};
#endif // HILOS_H