forked from remzi-arpacidusseau/ostep-homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector-try-wait.c
More file actions
37 lines (30 loc) · 718 Bytes
/
vector-try-wait.c
File metadata and controls
37 lines (30 loc) · 718 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "common_threads.h"
#include "main-header.h"
#include "vector-header.h"
int retry = 0;
void vector_add(vector_t *v_dst, vector_t *v_src) {
top:
if (pthread_mutex_trylock(&v_dst->lock) != 0) {
goto top;
}
if (pthread_mutex_trylock(&v_src->lock) != 0) {
retry++;
Pthread_mutex_unlock(&v_dst->lock);
goto top;
}
int i;
for (i = 0; i < VECTOR_SIZE; i++) {
v_dst->values[i] = v_dst->values[i] + v_src->values[i];
}
Pthread_mutex_unlock(&v_dst->lock);
Pthread_mutex_unlock(&v_src->lock);
}
void fini() {
printf("Retries: %d\n", retry);
}
#include "main-common.c"