Skip to content

Commit a2b3da6

Browse files
committed
Move claiming of transferred memos into a separate function
1 parent 1806467 commit a2b3da6

File tree

1 file changed

+69
-53
lines changed

1 file changed

+69
-53
lines changed

src/function/sync.rs

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -54,64 +54,23 @@ impl SyncTable {
5454
) -> ClaimResult<'me> {
5555
let mut write = self.syncs.lock();
5656
match write.entry(key_index) {
57-
std::collections::hash_map::Entry::Occupied(mut occupied_entry) => {
57+
std::collections::hash_map::Entry::Occupied(occupied_entry) => {
5858
let id = occupied_entry.get().id;
5959

6060
let id = match id {
6161
SyncOwnerId::Thread(id) => id,
6262
SyncOwnerId::Transferred => {
63-
let current_id = thread::current().id();
64-
let database_key_index = DatabaseKeyIndex::new(self.ingredient, key_index);
65-
66-
return match zalsa
67-
.runtime()
68-
.claim_transferred(database_key_index, allow_reentry)
69-
{
70-
ClaimTransferredResult::ClaimedBy(other_thread) => {
71-
occupied_entry.get_mut().anyone_waiting = true;
72-
73-
match other_thread.block(write) {
74-
BlockResult::Cycle => ClaimResult::Cycle { inner: false },
75-
BlockResult::Running(running) => ClaimResult::Running(running),
76-
}
77-
}
78-
ClaimTransferredResult::Reentrant => {
79-
let SyncState {
80-
id, claimed_twice, ..
81-
} = occupied_entry.into_mut();
82-
83-
if *claimed_twice {
84-
return ClaimResult::Cycle { inner: false };
85-
}
86-
87-
*id = SyncOwnerId::Thread(current_id);
88-
*claimed_twice = true;
89-
90-
ClaimResult::Claimed(ClaimGuard {
91-
key_index,
92-
zalsa,
93-
sync_table: self,
94-
mode: ReleaseMode::SelfOnly,
95-
})
96-
}
97-
ClaimTransferredResult::Cycle { inner: nested } => {
98-
ClaimResult::Cycle { inner: nested }
99-
}
100-
ClaimTransferredResult::Released => {
101-
occupied_entry.insert(SyncState {
102-
id: SyncOwnerId::Thread(thread::current().id()),
103-
anyone_waiting: false,
104-
is_transfer_target: false,
105-
claimed_twice: false,
106-
});
107-
ClaimResult::Claimed(ClaimGuard {
108-
key_index,
109-
zalsa,
110-
sync_table: self,
111-
mode: ReleaseMode::Default,
112-
})
113-
}
114-
};
63+
return match self.try_claim_transferred(
64+
zalsa,
65+
occupied_entry,
66+
allow_reentry,
67+
) {
68+
Ok(claimed) => claimed,
69+
Err(other_thread) => match other_thread.block(write) {
70+
BlockResult::Cycle => ClaimResult::Cycle { inner: false },
71+
BlockResult::Running(running) => ClaimResult::Running(running),
72+
},
73+
}
11574
}
11675
};
11776

@@ -153,6 +112,63 @@ impl SyncTable {
153112
}
154113
}
155114

115+
#[cold]
116+
fn try_claim_transferred<'me>(
117+
&'me self,
118+
zalsa: &'me Zalsa,
119+
mut entry: OccupiedEntry<Id, SyncState>,
120+
allow_reentry: bool,
121+
) -> Result<ClaimResult<'me>, OtherThread<'me>> {
122+
let key_index = *entry.key();
123+
let database_key_index = DatabaseKeyIndex::new(self.ingredient, key_index);
124+
125+
match zalsa
126+
.runtime()
127+
.claim_transferred(database_key_index, allow_reentry)
128+
{
129+
ClaimTransferredResult::ClaimedBy(other_thread) => {
130+
entry.get_mut().anyone_waiting = true;
131+
Err(other_thread)
132+
}
133+
ClaimTransferredResult::Reentrant => {
134+
let SyncState {
135+
id, claimed_twice, ..
136+
} = entry.into_mut();
137+
138+
if *claimed_twice {
139+
return Ok(ClaimResult::Cycle { inner: false });
140+
}
141+
142+
*id = SyncOwnerId::Thread(thread::current().id());
143+
*claimed_twice = true;
144+
145+
Ok(ClaimResult::Claimed(ClaimGuard {
146+
key_index,
147+
zalsa,
148+
sync_table: self,
149+
mode: ReleaseMode::SelfOnly,
150+
}))
151+
}
152+
ClaimTransferredResult::Cycle { inner: nested } => {
153+
Ok(ClaimResult::Cycle { inner: nested })
154+
}
155+
ClaimTransferredResult::Released => {
156+
entry.insert(SyncState {
157+
id: SyncOwnerId::Thread(thread::current().id()),
158+
anyone_waiting: false,
159+
is_transfer_target: false,
160+
claimed_twice: false,
161+
});
162+
Ok(ClaimResult::Claimed(ClaimGuard {
163+
key_index,
164+
zalsa,
165+
sync_table: self,
166+
mode: ReleaseMode::Default,
167+
}))
168+
}
169+
}
170+
}
171+
156172
fn make_transfer_target(&self, key_index: Id) -> Option<SyncOwnerId> {
157173
let mut syncs = self.syncs.lock();
158174
syncs.get_mut(&key_index).map(|state| {

0 commit comments

Comments
 (0)