Skip to content

Commit f4d96bb

Browse files
author
Robbert van Renesse
committed
Updated some things
1 parent 26856bd commit f4d96bb

13 files changed

+167
-49
lines changed

code/DinersCV2.hny

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const N = 5
44

55
mutex = synch.Lock()
66
forks = [False,] * N
7-
conds = [synch.Condition(?mutex),] * N
7+
conds = [synch.Condition(),] * N
88

99
def diner(which):
1010
let left, right = (which, (which + 1) % N):

code/atm.hny

+2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ def atm_check_balance(acct): # return the balance on acct
1616
release(?accounts[acct].lock)
1717

1818
def atm_withdraw(acct, amount): # withdraw amount from acct
19+
assert amount >= 0
1920
acquire(?accounts[acct].lock)
2021
accounts[acct].balance -= amount
2122
result = True # return success
2223
release(?accounts[acct].lock)
2324

2425
def customer(atm, acct, amount):
26+
assert amount >= 0
2527
let bal = atm_check_balance(acct):
2628
if amount <= bal:
2729
atm_withdraw(acct, amount)

code/linkedlist.hny

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from synch import Lock, acquire, release
22
from alloc import malloc, free
33

44
def _node(v, n): # allocate and initialize a new list node
5-
result = malloc({ .lock: Lock(), .value: (0, v), .next: n })
5+
result = malloc({ .lock: Lock(), .value: v, .next: n })
66

77
def _find(lst, v):
88
var before = lst
@@ -22,7 +22,7 @@ def SetObject():
2222
def insert(lst, v):
2323
let before, after = _find(lst, v):
2424
if after->value != (0, v):
25-
before->next = _node(v, after)
25+
before->next = _node((0, v), after)
2626
release(?after->lock)
2727
release(?before->lock)
2828

code/lockspec.hny

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ def acquire(lk):
66
!lk = True
77

88
def release(lk):
9-
assert !lk
10-
atomically !lk = False
9+
atomically:
10+
assert !lk
11+
!lk = False

code/locksusp.hny

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def Lock():
66
def acquire(lk):
77
atomically:
88
if lk->acquired:
9-
stop lk->suspended[len lk->suspended]
9+
stop ?lk->suspended[len lk->suspended]
1010
assert lk->acquired
1111
else:
1212
lk->acquired = True

code/queueMS.hny

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from synch import Lock, acquire, release
1+
from synch import Lock, acquire, release, atomic_load, atomic_store
22
from alloc import malloc, free
33

44
def Queue():
@@ -8,14 +8,14 @@ def Queue():
88
def put(q, v):
99
let node = malloc({ .value: v, .next: None }):
1010
acquire(?q->tllock)
11-
q->tail->next = node
11+
atomic_store(?q->tail->next, node)
1212
q->tail = node
1313
release(?q->tllock)
1414

1515
def get(q):
1616
acquire(?q->hdlock)
1717
let dummy = q->head
18-
let node = dummy->next:
18+
let node = atomic_load(?dummy->next):
1919
if node == None:
2020
result = None
2121
release(?q->hdlock)

code/queueconc.hny

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from synch import Lock, acquire, release
22
from alloc import malloc, free
33

44
def Queue():
5-
result = { .head: None, .tail: None, .lock: Lock(), .time: 0 }
5+
result = { .head: None, .tail: None, .lock: Lock() }
66

77
def put(q, v):
88
let node = malloc({ .value: v, .next: None }):

code/ticket.hny

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ def fetch_and_increment(p):
55
result = !p
66
!p = (!p + 1) % MAX_THREADS
77

8+
def atomic_load(p):
9+
atomically result = !p
10+
811
def Lock():
912
result = { .counter: 0, .dispenser: 0 }
1013

1114
def acquire(lk):
1215
let my_ticket = fetch_and_increment(?lk->dispenser):
13-
atomically await lk->counter == my_ticket
16+
while atomic_load(?lk->counter) != my_ticket:
17+
pass
1418

1519
def release(lk):
16-
let next = (lk->counter + 1) % MAX_THREADS:
17-
atomically lk->counter = next
20+
fetch_and_increment(?lk->counter)

code/trap6.hny

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from synch import Lock, acquire, release
22

3-
sequential done
3+
sequential t_done, i_done
44

55
count = 0
66
countlock = Lock()
7-
done = [ False, False ]
7+
t_done = i_done = [ False, False ]
88

99
def increment():
1010
let prior = setintlevel(True):
@@ -15,13 +15,15 @@ def increment():
1515

1616
def handler(self):
1717
increment()
18-
done[self] = True
18+
i_done[self] = True
1919

2020
def thread(self):
2121
trap handler(self)
2222
increment()
23-
await all(done)
24-
assert count == 4, count
23+
await i_done[self]
24+
t_done[self] = True
25+
await all(t_done)
26+
assert count == 4
2527

2628
spawn thread(0)
2729
spawn thread(1)

harmony_model_checker/charm/charm.c

+45-11
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,15 @@ static bool onestep(
475475
}
476476

477477
// Weight of this step
478-
int weight = (node->to_parent == NULL || ctx == node->to_parent->after) ? 0 : 1;
478+
unsigned int weight = (node->to_parent == NULL || ctx == node->to_parent->after) ? 0 : 1;
479479

480480
// Allocate edge now
481481
struct edge *edge = walloc(w, sizeof(struct edge) + step->nlog * sizeof(hvalue_t), false);
482482
edge->src = node;
483483
edge->ctx = ctx;
484484
edge->choice = choice_copy;
485485
edge->interrupt = interrupt;
486+
edge->weight = weight;
486487
edge->after = after;
487488
edge->ai = step->ai;
488489
memcpy(edge->log, step->log, step->nlog * sizeof(hvalue_t));
@@ -507,7 +508,9 @@ static bool onestep(
507508
else {
508509
unsigned int len = node->len + weight;
509510
unsigned int steps = node->steps + instrcnt;
510-
if (len < next->len || (len == next->len && steps < next->steps)) {
511+
// TODO: not sure how to minimize. For some cases, this works better than
512+
// if (len < next->len || (len == next->len && steps < next->steps)) {
513+
if (len < next->len || (len == next->len && steps <= next->steps)) {
511514
next->len = len;
512515
next->steps = steps;
513516
next->to_parent = edge;
@@ -2164,6 +2167,15 @@ int main(int argc, char **argv){
21642167

21652168
printf("Phase 3: analysis\n");
21662169
if (minheap_empty(global->failures)) {
2170+
double now = gettime();
2171+
global->phase2 = true;
2172+
global->scc_todo = scc_alloc(0, global->graph.size, NULL, NULL);
2173+
barrier_wait(&middle_barrier);
2174+
// Workers working on finding SCCs
2175+
barrier_wait(&end_barrier);
2176+
2177+
printf("%u components (%.3lf seconds)\n", global->ncomponents, gettime() - now);
2178+
21672179
#ifdef DUMP_GRAPH
21682180
printf("digraph Harmony {\n");
21692181
for (unsigned int i = 0; i < global->graph.size; i++) {
@@ -2179,15 +2191,6 @@ int main(int argc, char **argv){
21792191
printf("}\n");
21802192
#endif
21812193

2182-
double now = gettime();
2183-
global->phase2 = true;
2184-
global->scc_todo = scc_alloc(0, global->graph.size, NULL, NULL);
2185-
barrier_wait(&middle_barrier);
2186-
// Workers working on finding SCCs
2187-
barrier_wait(&end_barrier);
2188-
2189-
printf("%u components (%.3lf seconds)\n", global->ncomponents, gettime() - now);
2190-
21912194
// mark the components that are "good" because they have a way out
21922195
struct component *components = calloc(global->ncomponents, sizeof(*components));
21932196
for (unsigned int i = 0; i < global->graph.size; i++) {
@@ -2276,6 +2279,37 @@ int main(int argc, char **argv){
22762279
}
22772280
}
22782281

2282+
#ifdef DUMP_GRAPH
2283+
if (true) {
2284+
FILE *df = fopen("charm.gv", "w");
2285+
fprintf(df, "digraph Harmony {\n");
2286+
for (unsigned int i = 0; i < global->graph.size; i++) {
2287+
struct node *node = global->graph.nodes[i];
2288+
fprintf(df, " s%u [label=\"%u/%u\"]\n", i, i, node->len);
2289+
}
2290+
for (unsigned int i = 0; i < global->graph.size; i++) {
2291+
struct node *node = global->graph.nodes[i];
2292+
for (struct edge *edge = node->fwd; edge != NULL; edge = edge->fwdnext) {
2293+
struct state *state = node->state;
2294+
unsigned int j;
2295+
for (j = 0; j < state->bagsize; j++) {
2296+
if (state->contexts[j] == edge->ctx) {
2297+
break;
2298+
}
2299+
}
2300+
assert(j < state->bagsize);
2301+
fprintf(df, " s%u -> s%u [style=%s label=\"%u/%u\"]\n",
2302+
node->id, edge->dst->id,
2303+
edge->dst->to_parent == edge ? "solid" : "dashed",
2304+
multiplicities(state)[j],
2305+
edge->weight);
2306+
}
2307+
}
2308+
fprintf(df, "}\n");
2309+
fclose(df);
2310+
}
2311+
#endif
2312+
22792313
#ifdef OBSOLETE
22802314
if (true) {
22812315
FILE *df = fopen("charm.dump", "w");

harmony_model_checker/charm/graph.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ struct edge {
3434
struct access_info *ai; // to detect data races
3535
uint16_t nsteps; // # microsteps
3636
bool interrupt : 1; // set if state change is an interrupt
37-
uint16_t nlog : 15; // size of print history
37+
uint8_t weight : 1; // context switch or not
38+
uint16_t nlog : 14; // size of print history
3839
hvalue_t log[0]; // print history (immediately follows edge)
3940
};
4041

harmony_model_checker/harmony/verbose.py

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def print_macrostep(self, f, mas):
122122
print("shared variables:", file=f)
123123
for k, v in self.shared.items():
124124
print(" %s: %s"%(k, verbose_string(v)), file=f)
125+
print("state id: %s"%mas["id"], file=f)
125126
print("================================================", file=f)
126127
self.lastmis = mis[0]
127128
self.start = int(self.lastmis["pc"])

0 commit comments

Comments
 (0)