Skip to content

Commit cee5745

Browse files
committed
wip checkstyle
1 parent 3ed3d24 commit cee5745

File tree

4 files changed

+117
-119
lines changed

4 files changed

+117
-119
lines changed

libscheduler.so

96 Bytes
Binary file not shown.

scheduler.c

+102-108
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
#include <pthread.h>
66

77
static my_scheduler sch;
8-
static int init_protect = 0;
8+
static int init_protect = 0;
99

10-
void *add_rdy(my_thread *new_th, int front) {
11-
node* new = malloc(sizeof(*new));
10+
void *add_rdy (my_thread *new_th, int front) {
11+
node *new = malloc (sizeof (*new));
1212
new_th->state = RDY;
1313
new_th->clk = 0;
1414
new->data = new_th;
1515
new->next = NULL;
1616

1717
int prio = new_th->priority;
18-
if(sch.ready[prio]->size == 0) {
18+
if (sch.ready[prio]->size == 0) {
1919
sch.ready[prio]->head = sch.ready[prio]->tail = new;
2020
sch.ready[prio]->size ++;
2121
return NULL;
2222
}
23-
24-
if(front) {
23+
// front = 1 --> add node to front
24+
if (front) {
2525
sch.ready[prio]->head->next = new;
2626
sch.ready[prio]->head = new;
2727
} else {
@@ -32,25 +32,24 @@ void *add_rdy(my_thread *new_th, int front) {
3232
return NULL;
3333
}
3434

35-
my_thread *get_rdy() {
35+
my_thread *get_rdy () {
3636
// search for highest prio
3737
int prio = 5;
3838
while (sch.ready[prio]->size == 0 && prio > 0)
3939
prio --;
4040

4141
node *rdy = sch.ready[prio]->head;
42-
if (rdy == NULL) {
42+
if (rdy == NULL)
4343
return NULL;
44-
}
4544
my_thread *data = rdy->data;
46-
free(rdy);
47-
if(sch.ready[prio]->size == 1) {
45+
free (rdy);
46+
if (sch.ready[prio]->size == 1) {
4847
sch.ready[prio]->head = sch.ready[prio]->tail = NULL;
4948
sch.ready[prio]->size --;
5049
return data;
5150
}
5251
node *aux = sch.ready[prio]->tail;
53-
while(aux->next != rdy) {
52+
while (aux->next != rdy) {
5453
aux = aux->next;
5554
}
5655
sch.ready[prio]->head = aux;
@@ -60,227 +59,222 @@ my_thread *get_rdy() {
6059
return data;
6160
}
6261

63-
void *schedule() {
64-
pthread_mutex_lock(&sch.mutex);
62+
void *schedule () {
63+
pthread_mutex_lock (&sch.mutex);
6564
my_thread *prev = sch.running;
66-
my_thread *rdy = get_rdy();
65+
my_thread *rdy = get_rdy ();
6766

68-
if(rdy == NULL) {
69-
pthread_mutex_unlock(&sch.mutex);
67+
if (rdy == NULL) {
68+
pthread_mutex_unlock (&sch.mutex);
7069
return NULL;
7170
}
7271

73-
if(prev == NULL) {
72+
if (prev == NULL) {
7473
rdy->state = RUN;
7574
sch.running = rdy;
76-
pthread_cond_signal(&(rdy->cond));
77-
pthread_mutex_unlock(&sch.mutex);
75+
pthread_cond_signal (& (rdy->cond));
76+
pthread_mutex_unlock (&sch.mutex);
7877
return NULL;
7978
}
8079

81-
if(sch.running->clk == sch.sched_quantum) {
80+
if (sch.running->clk == sch.sched_quantum) {
8281
prev->clk = 0;
83-
if(prev->priority > rdy->priority) {
84-
add_rdy(rdy, 1);
85-
pthread_mutex_unlock(&sch.mutex);
82+
if (prev->priority > rdy->priority) {
83+
add_rdy (rdy, 1);
84+
pthread_mutex_unlock (&sch.mutex);
8685
return NULL;
8786
}
8887
prev->state = RDY;
89-
add_rdy(prev, 0);
88+
add_rdy (prev, 0);
9089
sch.running = rdy;
9190
rdy->state = RUN;
92-
pthread_cond_signal(&(rdy->cond));
91+
pthread_cond_signal (& (rdy->cond));
9392
while (prev->state != RUN)
94-
pthread_cond_wait(&(prev->cond), &sch.mutex);
95-
pthread_mutex_unlock(&sch.mutex);
93+
pthread_cond_wait (& (prev->cond), &sch.mutex);
94+
pthread_mutex_unlock (&sch.mutex);
9695
return NULL;
9796
}
9897

99-
if(sch.running->priority < rdy->priority) {
98+
if (sch.running->priority < rdy->priority) {
10099
sch.running->clk = 0;
101100
prev->state = RDY;
102101
sch.running = rdy;
103-
add_rdy(prev, 0);
102+
add_rdy (prev, 0);
104103
rdy->state = RUN;
105-
pthread_cond_signal(&(rdy->cond));
104+
pthread_cond_signal (& (rdy->cond));
106105
while (prev->state != RUN)
107-
pthread_cond_wait(&(prev->cond), &sch.mutex);
108-
pthread_mutex_unlock(&sch.mutex);
106+
pthread_cond_wait (& (prev->cond), &sch.mutex);
107+
pthread_mutex_unlock (&sch.mutex);
109108
return NULL;
110109
}
111110

112-
add_rdy(rdy, 1);
113-
pthread_mutex_unlock(&sch.mutex);
111+
add_rdy (rdy, 1);
112+
pthread_mutex_unlock (&sch.mutex);
114113
return NULL;
115114
}
116115

117-
int so_init(unsigned int quantum, unsigned int io) {
116+
int so_init (unsigned int quantum, unsigned int io) {
118117
// check params
119-
if(io > SO_MAX_NUM_EVENTS || quantum <= 0 || init_protect)
118+
if (io > SO_MAX_NUM_EVENTS || quantum <= 0 || init_protect)
120119
return -1;
121120
init_protect = 1; // protect double init
122121

123122
sch.sched_quantum = quantum;
124123
sch.sched_io = io;
125124
sch.running = NULL;
126125

127-
sch.ready = malloc(6 * sizeof(queue*));
128-
for(int i = 0; i < 6; i++) {
129-
sch.ready[i] = malloc(sizeof(queue));
126+
sch.ready = malloc (6 * sizeof (queue*));
127+
for (int i = 0; i < 6; i++) {
128+
sch.ready[i] = malloc (sizeof (queue));
130129
sch.ready[i]->size = 0;
131130
sch.ready[i]->head = sch.ready[i]->tail = NULL;
132131
}
133132

134-
return pthread_mutex_init(&sch.mutex, NULL);
133+
return pthread_mutex_init (&sch.mutex, NULL);
135134
}
136135

137-
void so_end() {
138-
if(init_protect) {
139-
for(int i = 0; i < sch.all.no_of_th; i++) {
140-
if(sch.all.threads[i]->state != TERM) {
141-
pthread_join(sch.all.threads[i]->tid, NULL);
142-
}
143-
pthread_cond_destroy(&(sch.all.threads[i]->cond));
144-
free(sch.all.threads[i]);
136+
void so_end () {
137+
if (init_protect) {
138+
for (int i = 0; i < sch.all.no_of_th; i++) {
139+
if (sch.all.threads[i]->state != TERM)
140+
pthread_join (sch.all.threads[i]->tid, NULL);
141+
pthread_cond_destroy (& (sch.all.threads[i]->cond));
142+
free (sch.all.threads[i]);
145143
}
146-
free(sch.all.threads);
144+
free (sch.all.threads);
147145

148-
for(int i = 0; i < 6; i++) {
146+
for (int i = 0; i < 6; i++) {
149147
node *aux = sch.ready[i]->tail;
150-
while(aux) {
148+
while (aux) {
151149
node *temp = aux;
152150
aux = aux->next;
153-
free(temp);
151+
free (temp);
154152
}
155-
free(sch.ready[i]);
153+
free (sch.ready[i]);
156154
}
157-
free(sch.ready);
155+
free (sch.ready);
158156

159-
for(int i = 0; i < sch.waiting.no_of_th; i++)
160-
free(sch.waiting.threads[i]);
161-
free(sch.waiting.threads);
157+
for (int i = 0; i < sch.waiting.no_of_th; i++)
158+
free (sch.waiting.threads[i]);
159+
free (sch.waiting.threads);
162160
}
163161

164162
init_protect = 0;
165-
pthread_mutex_destroy(&(sch.mutex));
166-
return;
163+
pthread_mutex_destroy (& (sch.mutex));
167164
}
168165

169-
int so_wait(unsigned int io) {
170-
if(io >= sch.sched_io || io < 0)
166+
int so_wait (unsigned int io) {
167+
if (io >= sch.sched_io || io < 0)
171168
return -1;
172169

173-
pthread_mutex_lock(&sch.mutex);
170+
pthread_mutex_lock (&sch.mutex);
174171
// add to waiting list
175172
sch.running->waiting_io = io;
176-
if(sch.waiting.no_of_th == 0) {
177-
sch.waiting.threads = malloc(sizeof(my_thread *));
173+
if (sch.waiting.no_of_th == 0) {
174+
sch.waiting.threads = malloc (sizeof (my_thread *));
178175
} else {
179-
my_thread **temp = realloc(sch.waiting.threads, sizeof(my_thread *) * (sch.waiting.no_of_th + 1));
176+
my_thread **temp = realloc (sch.waiting.threads, sizeof (my_thread *) * (sch.waiting.no_of_th + 1));
180177
sch.waiting.threads = temp;
181178
}
182179
sch.waiting.threads[sch.waiting.no_of_th] = sch.running;
183180
sch.waiting.no_of_th++;
184181

185182
// switch running thread
186183
my_thread *prev = sch.running;
187-
my_thread *rdy = get_rdy();
184+
my_thread *rdy = get_rdy ();
188185

189186
sch.running->clk = 0;
190187
prev->state = WAIT;
191188
sch.running = rdy;
192189
rdy->state = RUN;
193-
pthread_cond_signal(&(rdy->cond));
190+
pthread_cond_signal (& (rdy->cond));
194191
while (prev->state != RUN)
195-
pthread_cond_wait(&(prev->cond), &sch.mutex);
196-
pthread_mutex_unlock(&sch.mutex);
192+
pthread_cond_wait (& (prev->cond), &sch.mutex);
193+
pthread_mutex_unlock (&sch.mutex);
197194

198195
return 0;
199196
}
200197

201-
int so_signal(unsigned int io) {
202-
if(io >= sch.sched_io || io < 0)
198+
int so_signal (unsigned int io) {
199+
if (io >= sch.sched_io || io < 0)
203200
return -1;
204-
201+
205202
sch.running->clk++;
206-
203+
207204
int cnt = 0;
208-
for(int i = 0; i < sch.waiting.no_of_th; i++) {
209-
if(sch.waiting.threads[i] && sch.waiting.threads[i]->waiting_io == io) {
205+
for (int i = 0; i < sch.waiting.no_of_th; i++) {
206+
if (sch.waiting.threads[i] && sch.waiting.threads[i]->waiting_io == io) {
210207
cnt ++;
211-
add_rdy(sch.waiting.threads[i], 0);
208+
add_rdy (sch.waiting.threads[i], 0);
212209
sch.waiting.threads[i] = NULL;
213210
}
214211
}
215-
schedule();
212+
schedule ();
216213
return cnt;
217214
}
218215

219216
// using pthread_create documentation model
220-
static void *thread_start(void *arg) {
217+
static void *thread_start (void *arg) {
221218
my_thread *th_arg = arg;
222219
so_handler *func = th_arg->func;
223220

224221
// context switch
225-
pthread_mutex_lock(&sch.mutex);
226-
while(th_arg->state != RUN)
227-
pthread_cond_wait(&(th_arg->cond), &sch.mutex);
222+
pthread_mutex_lock (&sch.mutex);
223+
while (th_arg->state != RUN)
224+
pthread_cond_wait (& (th_arg->cond), &sch.mutex);
228225
sch.running = th_arg;
229-
pthread_mutex_unlock(&sch.mutex);
230-
226+
pthread_mutex_unlock (&sch.mutex);
231227

232-
func(th_arg->priority);
228+
func (th_arg->priority);
233229

234-
pthread_mutex_lock(&sch.mutex);
230+
pthread_mutex_lock (&sch.mutex);
235231
th_arg->state = TERM;
236232
sch.running = NULL;
237-
pthread_mutex_unlock(&sch.mutex);
238-
239-
schedule();
233+
pthread_mutex_unlock (&sch.mutex);
234+
235+
schedule ();
240236
return NULL;
241237
}
242238

243239

244-
tid_t so_fork(so_handler *func, unsigned int priority) {
245-
if(func == NULL || priority > 5)
240+
tid_t so_fork (so_handler *func, unsigned int priority) {
241+
if (func == NULL || priority > 5)
246242
return INVALID_TID;
247243

248244
// new thread
249-
my_thread *new_thread = malloc(sizeof(*new_thread));
245+
my_thread *new_thread = malloc (sizeof (*new_thread));
250246
new_thread->func = func;
251247
new_thread->priority = priority;
252248
new_thread->clk = 0;
253-
pthread_cond_init(&(new_thread->cond), NULL);
249+
pthread_cond_init (& (new_thread->cond), NULL);
254250

255251
// create -> tid = id; NULL = def attr; thread_start func; func params
256-
if(pthread_create(&(new_thread->tid), NULL, thread_start, (void*)new_thread) < 0)
252+
if (pthread_create (& (new_thread->tid), NULL, thread_start, (void*)new_thread) < 0)
257253
return INVALID_TID;
258254

259255
// add to thread list
260-
pthread_mutex_lock(&sch.mutex);
261-
if(sch.all.no_of_th == 0) {
262-
sch.all.threads = malloc(sizeof(my_thread *));
256+
pthread_mutex_lock (&sch.mutex);
257+
if (sch.all.no_of_th == 0) {
258+
sch.all.threads = malloc (sizeof (my_thread *));
263259
} else {
264-
my_thread **temp = realloc(sch.all.threads, sizeof(my_thread *) * (sch.all.no_of_th + 1));
260+
my_thread **temp = realloc (sch.all.threads, sizeof (my_thread *) * (sch.all.no_of_th + 1));
265261
sch.all.threads = temp;
266262
}
267263
sch.all.threads[sch.all.no_of_th] = new_thread;
268264
sch.all.no_of_th++;
269-
270-
add_rdy(new_thread, 0);
271-
if(sch.running) {
265+
266+
add_rdy (new_thread, 0);
267+
if (sch.running)
272268
sch.running->clk ++;
273-
}
274-
pthread_mutex_unlock(&sch.mutex);
275-
schedule();
269+
pthread_mutex_unlock (&sch.mutex);
270+
schedule ();
276271

277272
return new_thread->tid;
278273
}
279274

280-
void so_exec() {
275+
void so_exec () {
281276
sch.running->clk ++;
282-
if(sch.running->clk == sch.sched_quantum) {
283-
schedule();
284-
}
277+
if (sch.running->clk == sch.sched_quantum)
278+
schedule ();
285279
return;
286280
}

scheduler.o

192 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)