Skip to content

Commit 4ed8bb2

Browse files
refactor(downloads): substitute RefCells with RwLocks for thread safety
1 parent acfc43b commit 4ed8bb2

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/cli/common.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Just a dumping ground for cli stuff
22
3-
use std::cell::RefCell;
43
use std::fmt::Display;
54
use std::fs;
65
use std::io::{BufRead, Write};
76
use std::path::{Path, PathBuf};
8-
use std::sync::{Arc, LazyLock, Mutex};
7+
use std::sync::{Arc, LazyLock, Mutex, RwLock};
98
use std::{cmp, env};
109

1110
use anyhow::{Context, Result, anyhow};
@@ -123,14 +122,14 @@ pub(crate) fn read_line(process: &Process) -> Result<String> {
123122

124123
pub(super) struct Notifier {
125124
tracker: Mutex<DownloadTracker>,
126-
ram_notice_shown: RefCell<bool>,
125+
ram_notice_shown: RwLock<bool>,
127126
}
128127

129128
impl Notifier {
130129
pub(super) fn new(quiet: bool, process: &Process) -> Self {
131130
Self {
132131
tracker: Mutex::new(DownloadTracker::new_with_display_progress(!quiet, process)),
133-
ram_notice_shown: RefCell::new(false),
132+
ram_notice_shown: RwLock::new(false),
134133
}
135134
}
136135

@@ -140,10 +139,10 @@ impl Notifier {
140139
}
141140

142141
if let Notification::SetDefaultBufferSize(_) = &n {
143-
if *self.ram_notice_shown.borrow() {
142+
if *self.ram_notice_shown.read().unwrap() {
144143
return;
145144
} else {
146-
*self.ram_notice_shown.borrow_mut() = true;
145+
*self.ram_notice_shown.write().unwrap() = true;
147146
}
148147
};
149148
let level = n.level();

src/settings.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::cell::RefCell;
21
use std::collections::BTreeMap;
32
use std::fmt;
43
use std::path::{Path, PathBuf};
54
use std::str::FromStr;
5+
use std::sync::RwLock;
66

77
use anyhow::{Context, Result};
88
use serde::{Deserialize, Serialize};
@@ -13,22 +13,22 @@ use crate::errors::*;
1313
use crate::notifications::*;
1414
use crate::utils;
1515

16-
#[derive(Clone, Debug, Eq, PartialEq)]
16+
#[derive(Debug)]
1717
pub struct SettingsFile {
1818
path: PathBuf,
19-
cache: RefCell<Option<Settings>>,
19+
cache: RwLock<Option<Settings>>,
2020
}
2121

2222
impl SettingsFile {
2323
pub(crate) fn new(path: PathBuf) -> Self {
2424
Self {
2525
path,
26-
cache: RefCell::new(None),
26+
cache: RwLock::default(),
2727
}
2828
}
2929

3030
fn write_settings(&self) -> Result<()> {
31-
let settings = self.cache.borrow();
31+
let settings = self.cache.read().unwrap();
3232
utils::write_file(
3333
"settings",
3434
&self.path,
@@ -40,10 +40,10 @@ impl SettingsFile {
4040
fn read_settings(&self) -> Result<()> {
4141
let mut needs_save = false;
4242
{
43-
let b = self.cache.borrow();
43+
let b = self.cache.read().unwrap();
4444
if b.is_none() {
4545
drop(b);
46-
*self.cache.borrow_mut() = Some(if utils::is_file(&self.path) {
46+
*self.cache.write().unwrap() = Some(if utils::is_file(&self.path) {
4747
let content = utils::read_file("settings", &self.path)?;
4848
Settings::parse(&content).with_context(|| RustupError::ParsingFile {
4949
name: "settings",
@@ -65,14 +65,17 @@ impl SettingsFile {
6565
self.read_settings()?;
6666

6767
// Settings can no longer be None so it's OK to unwrap
68-
f(self.cache.borrow().as_ref().unwrap())
68+
f(self.cache.read().unwrap().as_ref().unwrap())
6969
}
7070

7171
pub(crate) fn with_mut<T, F: FnOnce(&mut Settings) -> Result<T>>(&self, f: F) -> Result<T> {
7272
self.read_settings()?;
7373

7474
// Settings can no longer be None so it's OK to unwrap
75-
let result = { f(self.cache.borrow_mut().as_mut().unwrap())? };
75+
let result = {
76+
let mut result = self.cache.write().unwrap();
77+
f(result.as_mut().unwrap())?
78+
};
7679
self.write_settings()?;
7780
Ok(result)
7881
}

0 commit comments

Comments
 (0)