Skip to content

Commit edc7d17

Browse files
committed
Hi
1 parent 8d40810 commit edc7d17

File tree

7 files changed

+63
-23
lines changed

7 files changed

+63
-23
lines changed

synch3/GNUmakefile

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
SERVER_PROGRAMS = $(sort $(patsubst %.c,%,$(wildcard serviceserver-??.c)))
2-
SORT_PROGRAMS = $(sort $(patsubst %.c,%,$(wildcard sort-??.c)))
32
SERVICE_PROGRAMS = serviceblaster serviceclient servicelookup serviceserver \
43
$(SERVER_PROGRAMS)
5-
PROGRAMS = $(SERVICE_PROGRAMS) $(SORT_PROGRAMS)
4+
PROGRAMS = $(SERVICE_PROGRAMS)
65
all: $(PROGRAMS)
76

87
O ?= 2
@@ -15,12 +14,9 @@ include ../common/rules.mk
1514
$(SERVICE_PROGRAMS): %: %.o
1615
$(CC) $(CFLAGS) $(O) -o $@ $^
1716

18-
$(SORT_PROGRAMS): %: %.o linereader.o
19-
$(CC) $(CFLAGS) $(O) -o $@ $^
20-
2117

2218
clean:
23-
rm -f *.o *.core $(PROGRAMS)
19+
rm -f *.o *.core $(PROGRAMS) sort-[0-9][0-9]
2420
rm -rf $(DEPSDIR) *.dSYM
2521

2622
.PHONY: all clean

synch4/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
incr
2+
incrcmpxchg
3+
incrlock
24
sort-[0-9][0-9]

synch4/GNUmakefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
SORT_PROGRAMS = $(sort $(patsubst %.c,%,$(wildcard sort-??.c)))
2-
PROGRAMS = $(SORT_PROGRAMS) incr
2+
INCR_PROGRAMS = incr incrlock incrcmpxchg
3+
PROGRAMS = $(SORT_PROGRAMS) $(INCR_PROGRAMS)
34
all: $(PROGRAMS)
45

56
O ?= 2
@@ -12,7 +13,7 @@ include ../common/rules.mk
1213
$(SORT_PROGRAMS): %: %.o linereader.o
1314
$(CC) $(CFLAGS) $(O) -o $@ $^
1415

15-
incr: incr.c
16+
$(INCR_PROGRAMS): %: %.c
1617
$(CC) $(CPPFLAGS) $(CFLAGS) $(DEPCFLAGS) -O0 -o $@ $<
1718

1819

synch4/incr.c

+1-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@ void* threadfunc(void* arg) {
99
return 0;
1010
}
1111

12-
void* atomic_threadfunc(void* arg) {
13-
unsigned* x = (unsigned*) arg;
14-
for (int i = 0; i != 10000000; ++i)
15-
__sync_fetch_and_add(x, 1);
16-
return 0;
17-
}
18-
1912
int main() {
2013
pthread_t th[4];
2114
unsigned n = 0;
2215
for (int i = 0; i != 4; ++i)
23-
pthread_create(&th[i], NULL, atomic_threadfunc, (void*) &n);
16+
pthread_create(&th[i], NULL, threadfunc, (void*) &n);
2417
for (int i = 0; i != 4; ++i)
2518
pthread_join(th[i], NULL);
2619
printf("%u\n", n);

synch4/incrcmpxchg.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! -O0
2+
#include <pthread.h>
3+
#include <stdio.h>
4+
5+
void* atomic_threadfunc(void* arg) {
6+
unsigned* x = (unsigned*) arg;
7+
for (int i = 0; i != 10000000; ++i) {
8+
unsigned expected = *x;
9+
while (1) {
10+
unsigned actual =
11+
__sync_val_compare_and_swap(
12+
x, expected, expected + 1);
13+
if (actual == expected)
14+
break;
15+
expected = actual;
16+
}
17+
}
18+
return 0;
19+
}
20+
21+
int main() {
22+
pthread_t th[4];
23+
unsigned n = 0;
24+
for (int i = 0; i != 4; ++i)
25+
pthread_create(&th[i], NULL, atomic_threadfunc, (void*) &n);
26+
for (int i = 0; i != 4; ++i)
27+
pthread_join(th[i], NULL);
28+
printf("%u\n", n);
29+
}

synch4/incrlock.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! -O0
2+
#include <pthread.h>
3+
#include <stdio.h>
4+
5+
void* atomic_threadfunc(void* arg) {
6+
unsigned* x = (unsigned*) arg;
7+
for (int i = 0; i != 10000000; ++i)
8+
__sync_fetch_and_add(x, 1);
9+
return 0;
10+
}
11+
12+
int main() {
13+
pthread_t th[4];
14+
unsigned n = 0;
15+
for (int i = 0; i != 4; ++i)
16+
pthread_create(&th[i], NULL, atomic_threadfunc, (void*) &n);
17+
for (int i = 0; i != 4; ++i)
18+
pthread_join(th[i], NULL);
19+
printf("%u\n", n);
20+
}

synch4/sort-08.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static void* sort_lines(void* arg) {
2020
pthread_mutex_lock(&mutex);
2121
if (cur_nthreads < MAX_NTHREADS) {
2222
method = 1;
23-
cur_nthreads += 2;
23+
cur_nthreads += 1;
2424
if (cur_nthreads > max_nthreads)
2525
max_nthreads = cur_nthreads;
2626
} else
@@ -31,19 +31,18 @@ static void* sort_lines(void* arg) {
3131
lineset left, right;
3232
split_lines(&left, &right, ls);
3333

34-
pthread_t left_thread, right_thread;
34+
pthread_t left_thread;
3535
int r1 = pthread_create(&left_thread, NULL, &sort_lines, &left);
36-
int r2 = pthread_create(&right_thread, NULL, &sort_lines, &right);
37-
assert(r1 == 0 && r2 == 0);
36+
assert(r1 == 0);
3837

38+
sort_lines(&right);
3939
r1 = pthread_join(left_thread, NULL);
40-
r2 = pthread_join(right_thread, NULL);
41-
assert(r1 == 0 && r2 == 0);
40+
assert(r1 == 0);
4241

4342
merge_lines(ls, &left, &right);
4443

4544
pthread_mutex_lock(&mutex);
46-
cur_nthreads -= 2;
45+
cur_nthreads -= 1;
4746
pthread_mutex_unlock(&mutex);
4847
} else
4948
qsort(ls->lines, lineset_size(ls), sizeof(line), line_compare);

0 commit comments

Comments
 (0)