tokio locks

This commit is contained in:
jif-oai
2025-10-29 21:10:31 +00:00
parent e7de8118ce
commit 69a9979b8a
3 changed files with 32 additions and 7 deletions

1
codex-rs/Cargo.lock generated
View File

@@ -1247,6 +1247,7 @@ version = "0.0.0"
dependencies = [
"serde_json",
"thiserror 2.0.16",
"tokio",
]
[[package]]

View File

@@ -6,6 +6,7 @@ edition.workspace = true
[dependencies]
serde_json = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["rt", "sync"] }
[lints]
workspace = true

View File

@@ -4,9 +4,11 @@ use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Mutex;
use std::sync::OnceLock;
use thiserror::Error;
use tokio::runtime::Builder as RuntimeBuilder;
use tokio::runtime::Handle as RuntimeHandle;
use tokio::sync::Mutex;
const INTERNAL_STORAGE_FILENAME: &str = "internal_storage.json";
@@ -58,14 +60,14 @@ impl Storage {
Ok(())
}
fn read(&self, key: &str) -> Result<Option<String>, InternalStorageError> {
let _guard = self.lock.lock().expect("internal storage lock poisoned");
async fn read_async(&self, key: &str) -> Result<Option<String>, InternalStorageError> {
let _guard = self.lock.lock().await;
let map = self.read_map()?;
Ok(map.get(key).map(value_to_string))
}
fn write(&self, key: &str, value: &str) -> Result<(), InternalStorageError> {
let _guard = self.lock.lock().expect("internal storage lock poisoned");
async fn write_async(&self, key: &str, value: &str) -> Result<(), InternalStorageError> {
let _guard = self.lock.lock().await;
let mut map = self.read_map()?;
map.insert(key.to_string(), Value::String(value.to_string()));
self.write_map(&map)
@@ -87,11 +89,19 @@ pub fn initialize(codex_home: PathBuf) {
}
pub fn read(key: &str) -> Result<Option<String>, InternalStorageError> {
storage()?.read(key)
let fut = storage()?.read_async(key);
match block_on(fut) {
Ok(res) => res,
Err(err) => Err(err.into()),
}
}
pub fn write(key: &str, value: &str) -> Result<(), InternalStorageError> {
storage()?.write(key, value)
let fut = storage()?.write_async(key, value);
match block_on(fut) {
Ok(res) => res,
Err(err) => Err(err.into()),
}
}
fn storage() -> Result<&'static Storage, InternalStorageError> {
@@ -111,3 +121,16 @@ fn value_to_string(value: &Value) -> String {
_ => value.to_string(),
}
}
fn block_on<F>(fut: F) -> Result<F::Output, io::Error>
where
F: std::future::Future,
{
match RuntimeHandle::try_current() {
Ok(handle) => Ok(handle.block_on(fut)),
Err(_) => {
let rt = RuntimeBuilder::new_current_thread().enable_all().build()?;
Ok(rt.block_on(fut))
}
}
}