Skip to content

Commit 0691436

Browse files
committed
Initial synchronization section code, continued.
1 parent 4e014a5 commit 0691436

File tree

5 files changed

+117
-21
lines changed

5 files changed

+117
-21
lines changed

common/rules.mk

+34-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# compiler flags
2-
CFLAGS := -std=gnu11 -Wall -Wextra -Wshadow -g $(DEFS) $(CFLAGS)
2+
CFLAGS := -std=gnu2x -Wall -Wextra -Wshadow -g $(DEFS) $(CFLAGS)
33
CXXFLAGS := -std=gnu++2a -Wall -Wextra -Wshadow -g $(DEFS) $(CXXFLAGS)
44

55
O ?= -O3
@@ -19,6 +19,14 @@ ifeq ($(PIE),0)
1919
LDFLAGS += -no-pie
2020
endif
2121

22+
# skip x86 versions in ARM Docker
23+
X86 ?= 1
24+
ifneq ($(X86),1)
25+
ifneq ($(findstring /usr/x86_64-linux-gnu/bin:,$(PATH)),)
26+
PATH := $(subst /usr/x86_64-linux-gnu/bin:,,$(PATH))
27+
endif
28+
endif
29+
2230
# compiler variant
2331
ifeq ($(COMPILER),clang)
2432
ifeq ($(origin CC),default)
@@ -69,12 +77,10 @@ endif
6977

7078
# sanitizer arguments
7179
ifndef SAN
72-
SAN := $(SANITIZE)
80+
SAN := $(or $(SANITIZE),$(ASAN),$(UBSAN))
7381
endif
74-
ifeq ($(SAN),1)
75-
ifndef ASAN
76-
ASAN := $(if $(strip $(shell $(CC) -v 2>&1 | grep 'build=aarch.*target=x86')),,1)
77-
endif
82+
ifndef ASAN
83+
ASAN := $(if $(strip $(shell $(CC) -v 2>&1 | grep 'build=aarch.*target=x86')),0,1)
7884
endif
7985
ifndef TSAN
8086
ifeq ($(WANT_TSAN),1)
@@ -83,38 +89,46 @@ TSAN := $(SAN)
8389
endif
8490

8591
check_for_sanitizer = $(if $(strip $(shell $(CC) -fsanitize=$(1) -x c -E /dev/null 2>&1 | grep sanitize=)),$(info ** WARNING: The `$(CC)` compiler does not support `-fsanitize=$(1)`.),1)
92+
SANFLAGS :=
8693
ifeq ($(TSAN),1)
8794
ifeq ($(call check_for_sanitizer,thread),1)
88-
CFLAGS += -fsanitize=thread
89-
CXXFLAGS += -fsanitize=thread
95+
SANFLAGS += -fsanitize=thread
9096
endif
9197
else
92-
ifeq ($(or $(ASAN),$(LSAN),$(LEAKSAN)),1)
98+
ifneq ($(ASAN),0)
9399
ifeq ($(call check_for_sanitizer,address),1)
94-
CFLAGS += -fsanitize=address
95-
CXXFLAGS += -fsanitize=address
100+
SANFLAGS += -fsanitize=address
96101
endif
97102
endif
98103
ifeq ($(or $(LSAN),$(LEAKSAN)),1)
99104
ifeq ($(call check_for_sanitizer,leak),1)
100-
CFLAGS += -fsanitize=leak
101-
CXXFLAGS += -fsanitize=leak
105+
SANFLAGS += -fsanitize=leak
102106
endif
103107
endif
104108
endif
105-
ifeq ($(or $(UBSAN),$(SAN)),1)
109+
ifneq ($(UBSAN),0)
106110
ifeq ($(call check_for_sanitizer,undefined),1)
107-
CFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
108-
CXXFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
111+
SANFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
109112
endif
110113
endif
114+
ifeq ($(or $(TSAN),$(LSAN),$(LEAKSAN),$(SAN)),1)
115+
CFLAGS += $(SANFLAGS)
116+
CXXFLAGS += $(SANFLAGS)
117+
endif
111118

112119
# profiling
113120
ifeq ($(or $(PROFILE),$(PG)),1)
114121
CFLAGS += -pg
115122
CXXFLAGS += -pg
116123
endif
117124

125+
# NDEBUG
126+
ifeq ($(NDEBUG),1)
127+
CPPFLAGS += -DNDEBUG=1
128+
CFLAGS += -Wno-unused
129+
CXXFLAGS += -Wno-unused
130+
endif
131+
118132
# these rules ensure dependencies are created
119133
DEPCFLAGS = -MD -MF $(DEPSDIR)/$(patsubst %.o,%,$(@F)).d -MP
120134
DEPSDIR := .deps
@@ -125,11 +139,11 @@ include $(DEPFILES)
125139
endif
126140

127141
# when the C compiler or optimization flags change, rebuild all objects
128-
ifneq ($(strip $(DEP_CC)),$(strip $(CC) $(CPPFLAGS) $(CFLAGS) $(O)))
129-
DEP_CC := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CC:=$(CC) $(CPPFLAGS) $(CFLAGS) $(O)" >$(DEPSDIR)/_cc.d)
142+
ifneq ($(strip $(DEP_CC)),$(strip $(CC) $(CPPFLAGS) $(CFLAGS) $(O) X86=$(X86)))
143+
DEP_CC := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CC:=$(CC) $(CPPFLAGS) $(CFLAGS) $(O) X86=$(X86)" >$(DEPSDIR)/_cc.d)
130144
endif
131-
ifneq ($(strip $(DEP_CXX)),$(strip $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) $(LDFLAGS)))
132-
DEP_CXX := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CXX:=$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) $(LDFLAGS)" >$(DEPSDIR)/_cxx.d)
145+
ifneq ($(strip $(DEP_CXX)),$(strip $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) X86=$(X86) $(LDFLAGS)))
146+
DEP_CXX := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CXX:=$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) X86=$(X86) $(LDFLAGS)" >$(DEPSDIR)/_cxx.d)
133147
endif
134148

135149

synchs1/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
sample[0-9][0-9]
22
counting
3+
philosophy[0-9][0-9]

synchs1/GNUmakefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
PROGRAMS = sample01 sample02 sample03 counting
1+
PROGRAMS = counting \
2+
$(patsubst %.cc,%,$(wildcard sample[0-9][0-9].cc philosophy[0-9][0-9].cc))
23

34
all: $(PROGRAMS)
45

@@ -7,6 +8,8 @@ CONFIGMK ?= config.mk
78

89
O ?= 3
910
PTHREAD ?= 1
11+
WANT_TSAN = 1
12+
X86 = 0
1013
include ../common/rules.mk
1114
-include $(CONFIGMK)
1215

@@ -20,6 +23,7 @@ $(PROGRAMS): \
2023

2124
clean:
2225
rm -f $(ALLPROGRAMS) *.o
26+
rm -f $(wildcard sample[0-9][0-9] philosophy[0-9][0-9])
2327
rm -rf $(DEPSDIR) *.dSYM
2428

2529
.PHONY: all always clean

synchs1/philosophy01.cc

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <cstdio>
2+
#include <cassert>
3+
#include <unistd.h>
4+
#include <random>
5+
#include <thread>
6+
#include <mutex>
7+
#include <condition_variable>
8+
9+
static constexpr int K = 8; // number of stalls
10+
11+
unsigned long long stalls[K];
12+
13+
void wait_until_uncomfortable() {
14+
// stoic philosophers are always comfortable
15+
}
16+
17+
void poop_into(int stall) {
18+
stalls[stall] += 1;
19+
}
20+
21+
void user(int preferred_stall) {
22+
while (true) {
23+
wait_until_uncomfortable();
24+
poop_into(preferred_stall);
25+
}
26+
}
27+
28+
int main() {
29+
std::default_random_engine randomness((std::random_device())());
30+
std::uniform_int_distribution<int> pick_stall(0, K - 1);
31+
32+
for (size_t i = 0; i != 32; ++i) {
33+
std::thread t(user, pick_stall(randomness));
34+
t.detach();
35+
}
36+
37+
sleep(5);
38+
}

synchs1/philosophy02.cc

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <cstdio>
2+
#include <cassert>
3+
#include <unistd.h>
4+
#include <random>
5+
#include <thread>
6+
#include <mutex>
7+
#include <condition_variable>
8+
9+
static constexpr int K = 8; // number of stalls
10+
11+
unsigned long long stalls[K];
12+
13+
void wait_until_uncomfortable() {
14+
// stoic philosophers are always comfortable
15+
}
16+
17+
void poop_into(int stall) {
18+
stalls[stall] += 1;
19+
}
20+
21+
void user() {
22+
std::default_random_engine randomness(std::random_device{}());
23+
std::uniform_int_distribution<int> pick_stall(0, K - 1);
24+
25+
while (true) {
26+
wait_until_uncomfortable();
27+
int preferred_stall = pick_stall(randomness);
28+
poop_into(preferred_stall);
29+
}
30+
}
31+
32+
int main() {
33+
for (size_t i = 0; i != 32; ++i) {
34+
std::thread t(user);
35+
t.detach();
36+
}
37+
38+
sleep(5);
39+
}

0 commit comments

Comments
 (0)