-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.c
89 lines (84 loc) · 2.72 KB
/
jobs.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
#include "jobs.h"
boolean hasPendingJobs(tID threadId) {
ThreadJobs threadJobs = jobsForThreads[threadId];
int i = 0;
for (i; i < threadJobs.jobsAmount; i++) {
tID testedJob = threadJobs.jobs[i] - 1;
if (jobs[testedJob] == NotStarted)
return true;
}
return false;
}
/* return next job for thread.*/
tID getJobForThread(tID threadId) {
tID yourNextJob = NULL_TID;
if (!hasPendingJobs(threadId)) {
return NULL_TID;
}
ThreadJobs threadJobs = jobsForThreads[threadId];
int i = 0;
for (i = 0; i < threadJobs.jobsAmount; i++) {
tID jobId = threadJobs.jobs[i] - 1;
int prevJob = 0;
boolean legalJobToExecute = true;
for (prevJob = 0; prevJob < jobsAmount && legalJobToExecute; prevJob++) {
if (jobs[jobId] != NotStarted || (deps[prevJob][jobId] == 1 && jobs[prevJob] != Done)) {
legalJobToExecute = false;
break;
}
//printf("jobs[%d] != NotStarted=>%d || (deps[%d][%d]==1=>%d && jobs[%d] != Done =>%d\n",jobId,(jobs[jobId] != NotStarted),(jobId-1),(prevJob-1),(deps[jobId-1][prevJob-1]==1),prevJob,(jobs[prevJob] != Done));
}
if (legalJobToExecute) {
yourNextJob = jobId;
break;
}
}
return yourNextJob;
}
op_status execJob(tID jobId) {
ASSERT(jobs);
jobs[jobId] = InProgress;
printf("Thread %d performed job %d\n", current_thread->id, jobId +1);
jobs[jobId] = Done;
}
void printData() {
printf("===============info about the input:===============\n");
printf("ThreadNumber=%d\n", threadsAmount);
printf("jobsAmount=%d\n", jobsAmount);
printf("Deps:\n");
ASSERT(deps);
int i = 0, j = 0;
for (i = 0; i < jobsAmount; i++) {
for (j = 0; j < jobsAmount; j++) {
printf("%X ", deps[i][j]);
}
printf("\n");
}
printf("Jobs:\n");
ASSERT(jobs);
i = 0;
for (i = 0; i < jobsAmount; i++) {
switch (jobs[i]) {
case NotStarted:
printf("job[%d]=NotStarted ", i);
break;
case InProgress:
printf("job[%d]=InProgress ", i);
break;
case Done:
printf("job[%d]=Done ", i);
break;
default:
printf("job[%d]=ERROR! ", i);
}
}
printf("\n");
i = 0;
for (i = 0; i != threadsAmount; i++) {
printf("jobsForThreads %d:\n\tJob Amount:%d\n\tThread ID: %d\n\tJobs: ", i, jobsForThreads[i].jobsAmount, jobsForThreads[i].threadID);
for (j = 0; jobsForThreads[i].jobs[j] != 0; j++) {
printf("%d ", jobsForThreads[i].jobs[j]);
}
printf("\n");
}
}