[WIP] fix deadlock in AtomTable::build_with

This commit is contained in:
Bennet Bleßmann
2023-08-28 23:04:30 +02:00
parent 86166dbf25
commit 13cbff7eab
24 changed files with 295 additions and 413 deletions

View File

@@ -1529,7 +1529,7 @@ impl Machine {
}
}
(HeapCellValueTag::Char, c) => {
let name = self.machine_st.atom_tbl.blocking_write().build_with(&c.to_string());
let name = AtomTable::build_with(&self.machine_st.atom_tbl,&c.to_string());
let h = self.machine_st.heap.len();
self.machine_st.heap.push(atom_as_cell!(name));
@@ -1804,7 +1804,7 @@ impl Machine {
match hostname::get().ok() {
Some(host) => match host.to_str() {
Some(host) => {
let hostname = self.machine_st.atom_tbl.blocking_write().build_with(host);
let hostname = AtomTable::build_with(&self.machine_st.atom_tbl, host);
let a1 = self.deref_register(1);
self.machine_st.unify_atom(hostname, a1);
@@ -1903,7 +1903,7 @@ impl Machine {
for entry in entries {
if let Ok(entry) = entry {
if let Some(name) = entry.file_name().to_str() {
let name = self.machine_st.atom_tbl.blocking_write().build_with(name);
let name = AtomTable::build_with(&self.machine_st.atom_tbl, name);
files.push(atom_as_cstr_cell!(name));
continue;
@@ -2144,11 +2144,7 @@ impl Machine {
}
};
let current_atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&current);
let current_atom = AtomTable::build_with(&self.machine_st.atom_tbl, &current);
let a1 = self.deref_register(1);
self.machine_st.unify_complete_string(current_atom, a1);
@@ -2189,7 +2185,7 @@ impl Machine {
}
};
let canonical_atom = self.machine_st.atom_tbl.blocking_write().build_with(cs);
let canonical_atom = AtomTable::build_with(&self.machine_st.atom_tbl, cs);
let a2 = self.deref_register(2);
self.machine_st.unify_complete_string(canonical_atom, a2);
@@ -2248,13 +2244,13 @@ impl Machine {
let atom_cell = match str_like {
AtomOrString::Atom(atom) => {
atom_as_cell!(if atom == atom!("[]") {
self.machine_st.atom_tbl.blocking_write().build_with("")
AtomTable::build_with(&self.machine_st.atom_tbl, "")
} else {
atom
})
}
AtomOrString::String(string) => {
atom_as_cell!(self.machine_st.atom_tbl.blocking_write().build_with(&string))
atom_as_cell!(AtomTable::build_with(&self.machine_st.atom_tbl, &string))
}
};
@@ -2316,7 +2312,7 @@ impl Machine {
match self.machine_st.try_from_list(self.machine_st.registers[2], stub_gen) {
Ok(addrs) => {
let string = self.machine_st.codes_to_string(addrs.into_iter(), stub_gen)?;
let atom = self.machine_st.atom_tbl.blocking_write().build_with(&string);
let atom = AtomTable::build_with(&self.machine_st.atom_tbl, &string);
self.machine_st.bind(a1.as_var().unwrap(), atom_as_cell!(atom));
}
@@ -2810,11 +2806,7 @@ impl Machine {
}
};
let chars_atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&string.trim());
let chars_atom = AtomTable::build_with(&self.machine_st.atom_tbl, &string.trim());
self.machine_st.unify_complete_string(chars_atom, chs);
}
@@ -3040,14 +3032,14 @@ impl Machine {
match (name, arity) {
(atom!("to_upper"), 1) => {
let reg = self.machine_st.deref(self.machine_st.heap[s+1]);
let atom = self.machine_st.atom_tbl.blocking_write().build_with(&c.to_uppercase().to_string());
let atom = AtomTable::build_with(&self.machine_st.atom_tbl, &c.to_uppercase().to_string());
let upper_str = string_as_cstr_cell!(atom);
unify!(self.machine_st, reg, upper_str);
self.machine_st.fail = false;
}
(atom!("to_lower"), 1) => {
let reg = self.machine_st.deref(self.machine_st.heap[s+1]);
let atom = self.machine_st.atom_tbl.blocking_write().build_with(&c.to_lowercase().to_string());
let atom = AtomTable::build_with(&self.machine_st.atom_tbl, &c.to_lowercase().to_string());
let lower_str = string_as_cstr_cell!(atom);
unify!(self.machine_st, reg, lower_str);
self.machine_st.fail = false;
@@ -3570,11 +3562,7 @@ impl Machine {
};
let output = self.deref_register(3);
let atom = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&string);
let atom = AtomTable::build_with(&self.machine_st.atom_tbl, &string);
self.machine_st.unify_complete_string(atom, output);
Ok(())
@@ -4069,7 +4057,7 @@ impl Machine {
cell_as_atom!(self.machine_st.heap[s])
}
(HeapCellValueTag::Char, c) => {
self.machine_st.atom_tbl.blocking_write().build_with(&c.to_string())
AtomTable::build_with(&self.machine_st.atom_tbl, &c.to_string())
}
_ => {
unreachable!()
@@ -4431,15 +4419,14 @@ impl Machine {
let h = self.machine_st.heap.len();
let header_term = functor!(
self.machine_st
.atom_tbl
.blocking_write()
.build_with(header_name.as_str()),
[cell(string_as_cstr_cell!(self
.machine_st
.atom_tbl
.blocking_write()
.build_with(header_value.to_str().unwrap())))]
AtomTable::build_with(
&self.machine_st.atom_tbl,
header_name.as_str()
),
[cell(string_as_cstr_cell!(AtomTable::build_with(
&self.machine_st.atom_tbl,
header_value.to_str().unwrap()
)))]
);
self.machine_st.heap.extend(header_term.into_iter());
@@ -4458,10 +4445,7 @@ impl Machine {
let reader = resp.bytes().unwrap().reader();
let mut stream = Stream::from_http_stream(
self.machine_st
.atom_tbl
.blocking_write()
.build_with(&address_string),
AtomTable::build_with(&self.machine_st.atom_tbl, &address_string),
Box::new(reader),
&mut self.machine_st.arena,
);
@@ -4578,14 +4562,14 @@ impl Machine {
Method::HEAD => atom!("head"),
_ => unreachable!(),
};
let path_atom = self.machine_st.atom_tbl.blocking_write().build_with(request.request.uri().path());
let path_atom = AtomTable::build_with(&self.machine_st.atom_tbl, request.request.uri().path());
let path_cell = atom_as_cstr_cell!(path_atom);
let headers: Vec<HeapCellValue> = request.request.headers().iter().map(|(header_name, header_value)| {
let h = self.machine_st.heap.len();
let header_term = functor!(
self.machine_st.atom_tbl.blocking_write().build_with(header_name.as_str()),
[cell(string_as_cstr_cell!(self.machine_st.atom_tbl.blocking_write().build_with(header_value.to_str().unwrap())))]
AtomTable::build_with(&self.machine_st.atom_tbl, header_name.as_str()),
[cell(string_as_cstr_cell!(AtomTable::build_with(&self.machine_st.atom_tbl, header_value.to_str().unwrap())))]
);
self.machine_st.heap.extend(header_term.into_iter());
@@ -4595,7 +4579,7 @@ impl Machine {
let headers_list = iter_to_heap_list(&mut self.machine_st.heap, headers.into_iter());
let query_str = request.request.uri().query().unwrap_or("");
let query_atom = self.machine_st.atom_tbl.blocking_write().build_with(query_str);
let query_atom = AtomTable::build_with(&self.machine_st.atom_tbl, query_str);
let query_cell = string_as_cstr_cell!(query_atom);
let hyper_req = request.request;
@@ -4825,11 +4809,10 @@ impl Machine {
unify!(self.machine_st, return_value, struct_value);
}
Value::CString(cstr) => {
let cstr = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(cstr.to_str().unwrap());
let cstr = AtomTable::build_with(
&self.machine_st.atom_tbl,
cstr.to_str().unwrap(),
);
self.machine_st.unify_complete_string(cstr, return_value);
}
}
@@ -4858,11 +4841,10 @@ impl Machine {
.map(|val| match val {
Value::Int(n) => fixnum_as_cell!(Fixnum::build_with(n)),
Value::Float(n) => HeapCellValue::from(float_alloc!(n, self.machine_st.arena)),
Value::CString(cstr) => atom_as_cell!(self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&cstr.into_string().unwrap())),
Value::CString(cstr) => atom_as_cell!(AtomTable::build_with(
&self.machine_st.atom_tbl,
&cstr.into_string().unwrap()
)),
Value::Struct(name, struct_args) => self.build_struct(&name, struct_args),
})
.collect();
@@ -4918,7 +4900,7 @@ impl Machine {
let src_sink = self.deref_register(1);
if let Some(file_spec) = self.machine_st.value_to_str_like(src_sink) {
let file_spec = file_spec.as_atom(&mut self.machine_st.atom_tbl.blocking_write());
let file_spec = file_spec.as_atom(&*self.machine_st.atom_tbl);
let mut stream =
self.machine_st
@@ -4961,7 +4943,7 @@ impl Machine {
let op = read_heap_cell!(self.deref_register(3),
(HeapCellValueTag::Char, c) => {
self.machine_st.atom_tbl.blocking_write().build_with(&c.to_string())
AtomTable::build_with(&self.machine_st.atom_tbl, &c.to_string())
}
(HeapCellValueTag::Atom, (name, _arity)) => {
name
@@ -6128,11 +6110,7 @@ impl Machine {
.read_term(&op_dir, Tokens::Default)
.map_err(|err| error_after_read_term(err, 0, &parser))
.and_then(|term| {
write_term_to_heap(
&term,
&mut self.machine_st.heap,
&mut self.machine_st.atom_tbl.blocking_write(),
)
write_term_to_heap(&term, &mut self.machine_st.heap, &self.machine_st.atom_tbl)
});
match term_write_result {
@@ -6296,7 +6274,7 @@ impl Machine {
name
}
_ => {
self.machine_st.atom_tbl.blocking_write().build_with(&match Number::try_from(port) {
AtomTable::build_with(&self.machine_st.atom_tbl, &match Number::try_from(port) {
Ok(Number::Fixnum(n)) => n.get_num().to_string(),
Ok(Number::Integer(n)) => n.to_string(),
_ => {
@@ -6310,10 +6288,7 @@ impl Machine {
atom!("127.0.0.1:80")
} else {
let buffer = format!("{}:{}", socket_atom.as_str(), port.as_str());
self.machine_st
.atom_tbl
.blocking_write()
.build_with(&buffer)
AtomTable::build_with(&self.machine_st.atom_tbl, &buffer)
};
let alias = self.machine_st.registers[4];
@@ -6495,7 +6470,7 @@ impl Machine {
(ArenaHeaderTag::TcpListener, tcp_listener) => {
match tcp_listener.accept().ok() {
Some((tcp_stream, socket_addr)) => {
let client = self.machine_st.atom_tbl.blocking_write().build_with(&socket_addr.to_string());
let client = AtomTable::build_with(&self.machine_st.atom_tbl, &socket_addr.to_string());
let mut tcp_stream = Stream::from_tcp_stream(
client,
@@ -7119,7 +7094,7 @@ impl Machine {
let chars = put_complete_string(
&mut self.machine_st.heap,
&result,
&mut self.machine_st.atom_tbl.blocking_write(),
&self.machine_st.atom_tbl,
);
let result_addr = self.deref_register(1);
@@ -7138,7 +7113,7 @@ impl Machine {
use git_version::git_version;
let buffer = git_version!(cargo_prefix = "cargo:", fallback = "unknown");
let buffer_atom = self.machine_st.atom_tbl.blocking_write().build_with(buffer);
let buffer_atom = AtomTable::build_with(&self.machine_st.atom_tbl, buffer);
let a1 = self.deref_register(1);
self.machine_st.unify_complete_string(buffer_atom, a1);
@@ -7503,11 +7478,7 @@ impl Machine {
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&buffer))
atom_as_cstr_cell!(AtomTable::build_with(&self.machine_st.atom_tbl, &buffer))
}
};
@@ -7644,11 +7615,8 @@ impl Machine {
if let Some(string) = self.machine_st.value_to_str_like(addr) {
for c in string.as_str().chars() {
if c as u32 > 255 {
let non_octet = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&c.to_string());
let non_octet =
AtomTable::build_with(&self.machine_st.atom_tbl, &c.to_string());
self.machine_st
.unify_atom(non_octet, self.machine_st.registers[2]);
return;
@@ -7705,7 +7673,7 @@ impl Machine {
let cstr = put_complete_string(
&mut self.machine_st.heap,
&value,
&mut self.machine_st.atom_tbl.blocking_write(),
&self.machine_st.atom_tbl,
);
unify!(self.machine_st, self.machine_st.registers[2], cstr);
@@ -7893,11 +7861,8 @@ impl Machine {
path_buf.push(&*library_name.as_str());
let library_path_str = path_buf.to_str().unwrap();
let library_path = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(library_path_str);
let library_path =
AtomTable::build_with(&self.machine_st.atom_tbl, library_path_str);
self.machine_st
.unify_atom(library_path, self.machine_st.registers[3]);
@@ -7994,11 +7959,8 @@ impl Machine {
if path.is_dir() {
if let Some(path) = path.to_str() {
let path_string = put_complete_string(
&mut self.machine_st.heap,
path,
&mut self.machine_st.atom_tbl.blocking_write(),
);
let path_string =
put_complete_string(&mut self.machine_st.heap, path, &self.machine_st.atom_tbl);
unify!(self.machine_st, self.machine_st.registers[1], path_string);
return;
@@ -8044,7 +8006,7 @@ impl Machine {
fstr.push_str("finis].");
let s = datetime.format(&fstr).to_string();
self.machine_st.atom_tbl.blocking_write().build_with(&s)
AtomTable::build_with(&self.machine_st.atom_tbl, &s)
}
pub(super) fn string_encoding_bytes(
@@ -8068,21 +8030,17 @@ impl Machine {
put_complete_string(
&mut self.machine_st.heap,
node.text().unwrap(),
&mut self.machine_st.atom_tbl.blocking_write(),
&self.machine_st.atom_tbl,
)
} else {
let mut avec = Vec::new();
for attr in node.attributes() {
let name = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(attr.name());
let name = AtomTable::build_with(&self.machine_st.atom_tbl, attr.name());
let value = put_complete_string(
&mut self.machine_st.heap,
&attr.value(),
&mut self.machine_st.atom_tbl.blocking_write(),
&self.machine_st.atom_tbl,
);
avec.push(str_loc_as_cell!(self.machine_st.heap.len()));
@@ -8108,11 +8066,7 @@ impl Machine {
cvec.into_iter()
));
let tag = self
.machine_st
.atom_tbl
.blocking_write()
.build_with(node.tag_name().name());
let tag = AtomTable::build_with(&self.machine_st.atom_tbl, node.tag_name().name());
let result = str_loc_as_cell!(self.machine_st.heap.len());
@@ -8132,17 +8086,17 @@ impl Machine {
None => put_complete_string(
&mut self.machine_st.heap,
&node.text(),
&mut self.machine_st.atom_tbl.blocking_write(),
&self.machine_st.atom_tbl,
),
Some(name) => {
let mut avec = Vec::new();
for attr in node.attrs() {
let name = self.machine_st.atom_tbl.blocking_write().build_with(attr.0);
let name = AtomTable::build_with(&self.machine_st.atom_tbl, attr.0);
let value = put_complete_string(
&mut self.machine_st.heap,
&attr.1,
&mut self.machine_st.atom_tbl.blocking_write(),
&self.machine_st.atom_tbl,
);
avec.push(str_loc_as_cell!(self.machine_st.heap.len()));
@@ -8168,7 +8122,7 @@ impl Machine {
cvec.into_iter()
));
let tag = self.machine_st.atom_tbl.blocking_write().build_with(name);
let tag = AtomTable::build_with(&self.machine_st.atom_tbl, name);
let result = str_loc_as_cell!(self.machine_st.heap.len());
self.machine_st
@@ -8189,11 +8143,7 @@ impl Machine {
if buffer.len() == 0 {
empty_list_as_cell!()
} else {
atom_as_cstr_cell!(self
.machine_st
.atom_tbl
.blocking_write()
.build_with(&buffer))
atom_as_cstr_cell!(AtomTable::build_with(&self.machine_st.atom_tbl, &buffer))
}
}
}