-
Notifications
You must be signed in to change notification settings - Fork 0
/
macierz.c
107 lines (103 loc) · 2.76 KB
/
macierz.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <semaphore.h>
#include <unistd.h>
#include "threadpool.h"
typedef struct data {
int64_t *row_sum;
sem_t *row_semaphore;
int64_t value;
uint64_t sleep_time;
} data_t;
void add_to_sum(void *arg, size_t size) {
data_t *data = (data_t *)arg;
if(!data) {
exit(-1);
}
int32_t err;
usleep(1000 * data->sleep_time);
if((err = sem_wait(data->row_semaphore)) != 0) {
exit(err);
}
*(data->row_sum) += data->value;
if((err = sem_post(data->row_semaphore)) != 0) {
exit(err);
}
free(data);
}
void destroy_result_and_semaphores(size_t n, int64_t *result, sem_t *semaphores) {
int32_t err;
if(semaphores) {
for(size_t i=0; i<n; i++) {
if((err = sem_destroy(&semaphores[i])) != 0) {
exit(err);
}
}
free(semaphores);
}
if(result) {
free(result);
}
}
int main() {
uint32_t k, n;
int32_t err;
scanf("%d %d", &k, &n);
int64_t *result = malloc(k * sizeof(int64_t));
if(!result) {
return -1;
}
sem_t *sum_semaphores = malloc(k * sizeof(sem_t));
if(!sum_semaphores) {
destroy_result_and_semaphores(k, result, sum_semaphores);
}
for(size_t i = 0; i < k; i++) {
result[i] = 0;
if((err = sem_init(&sum_semaphores[i], 0, 1)) != 0) {
destroy_result_and_semaphores(k, result, sum_semaphores);
return err;
}
}
thread_pool_t pool;
if((err = thread_pool_init(&pool, 4)) != 0) {
destroy_result_and_semaphores(n, result, sum_semaphores);
return err;
}
for(uint32_t i = 0; i < k * n; i++) {
int64_t value;
uint64_t time;
scanf("%ld %d", &value, &time);
data_t *data = malloc(sizeof(data_t));
if(!data) {
thread_pool_destroy(&pool);
destroy_result_and_semaphores(n, result, sum_semaphores);
return -1;
}
data->value = value;
data->sleep_time = time;
data->row_sum = &result[i/n];
data->row_semaphore = &sum_semaphores[i/n];
runnable_t runnable;
runnable.arg = data;
runnable.function = add_to_sum;
runnable.argsz = sizeof(data_t);
if((err = defer(&pool, runnable)) != 0) {
thread_pool_destroy(&pool);
destroy_result_and_semaphores(n, result, sum_semaphores);
return err;
}
}
thread_pool_destroy(&pool);
for(size_t i=0; i < k; i++) {
printf("%ld\n", result[i]);
}
for(size_t i=0; i < k; i++) {
if((err = sem_destroy(&sum_semaphores[i])) != 0) {
return -1;
}
}
free(result);
free(sum_semaphores);
return 0;
}