Fix atomic counting in socketpair's bind_until_success

This commit is contained in:
He Sun 2019-12-02 20:14:10 +08:00 committed by Tate, Hongliang Tian
parent 3c1378b7eb
commit b91566d486

@ -140,19 +140,13 @@ impl UnixSocketFile {
} }
fn bind_until_success(&self) -> String { fn bind_until_success(&self) -> String {
let mut path = SOCK_PATH_PREFIX.to_string(); loop {
let mut index = SOCKETPAIR_NUM.load(Ordering::SeqCst); let sock_path_suffix = SOCKETPAIR_NUM.fetch_add(1, Ordering::SeqCst);
path.push_str(&index.to_string()); let sock_path = format!("{}{}", SOCK_PATH_PREFIX, sock_path_suffix);
while self.bind(&path).is_err() { if self.bind(&sock_path).is_ok() {
if index == std::usize::MAX { return sock_path;
SOCKETPAIR_NUM.store(0, Ordering::SeqCst); //flip SOCKETPAIR_NUM
} }
index += 1;
path = SOCK_PATH_PREFIX.to_string();
path.push_str(&index.to_string());
} }
SOCKETPAIR_NUM.fetch_max(index + 1, Ordering::SeqCst);
path
} }
} }