-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.c
66 lines (54 loc) · 1.71 KB
/
thread.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
#include <stdio.h>
#include <stdlib.h>
#include "thread.h"
void thread(void *p){
s_Thread *t;
s_File *f;
s_SHA256_Digest *d;
// transform void *p pointer to the thread structure
t = (s_Thread*)p;
f = t->f;
d = t->d;
file_Open(f); // open file
while (f->eof == 0){
file_Read(f); // read file to data array
if (f->data){ // if data available
if (f->eof) // only terminate data when eof found
f->data = sha256_PrepareData(f->data, &f->data_size, f->file_size); // prepare data buffer to be a multiple of 512 bits
d->digest = sha256_Transform(f->data, f->data_size, d->digest); // execute SHA256 hash on the data
}
}
file_Close(f); // close file
// exit and end thread
_endthread();
}
HANDLE* thread_FindUnusedThread(HANDLE *t){
int i;
for (i = 0; i < MAX_THREADS; i++){ // loop through all threads
switch (WaitForSingleObject(t[i], 0)){ // check if thread is busy
case WAIT_TIMEOUT: // if busy do nothing
break;
case WAIT_OBJECT_0: // if free or finished
default:
return &t[i];
break;
}
}
return NULL;
}
int thread_WaitUntilFinished(HANDLE *t){
int i, threads_done = 0;
for (i = 0; i < MAX_THREADS; i++){
switch (WaitForSingleObject(t[i], 0)){
case WAIT_TIMEOUT:
threads_done = 0;
break;
case WAIT_OBJECT_0:
default:
t[i] = (HANDLE)0;
threads_done++;
break;
}
}
return threads_done == MAX_THREADS;
}