Skip to content

Commit

Permalink
Merge pull request #91 from Asurar0/sync_primitives_improvements
Browse files Browse the repository at this point in the history
Improved synchronization primitives
  • Loading branch information
Snowiiii authored Sep 12, 2024
2 parents 0bebc6f + df07029 commit a609805
Show file tree
Hide file tree
Showing 24 changed files with 341 additions and 276 deletions.
84 changes: 84 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ tokio = { version = "1.40", features = [
"io-util",
"sync",
] }

# Concurrency/Parallelism and Synchronization
rayon = "1.10.0"
parking_lot = "0.12.3"
crossbeam = "0.8.4"

uuid = { version = "1.10.0", features = ["serde", "v3", "v4"] }
derive_more = { version = "1.0.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
1 change: 1 addition & 0 deletions pumpkin-core/src/math/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};

use super::vector3::Vector3;

#[derive(Clone, Copy)]
/// Aka Block Position
pub struct WorldPosition(pub Vector3<i32>);

Expand Down
2 changes: 2 additions & 0 deletions pumpkin-core/src/math/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ impl<T: Math + Copy> Neg for Vector3<T> {
}

impl<T> From<(T, T, T)> for Vector3<T> {
#[inline(always)]
fn from((x, y, z): (T, T, T)) -> Self {
Vector3 { x, y, z }
}
}

impl<T> From<Vector3<T>> for (T, T, T) {
#[inline(always)]
fn from(vector: Vector3<T>) -> Self {
(vector.x, vector.y, vector.z)
}
Expand Down
4 changes: 3 additions & 1 deletion pumpkin-inventory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pumpkin-world = { path = "../pumpkin-world"}
num-traits = "0.2"
num-derive = "0.4"
thiserror = "1.0.63"
itertools = "0.13.0"
itertools = "0.13.0"
parking_lot.workspace = true
crossbeam.workspace = true
21 changes: 7 additions & 14 deletions pumpkin-inventory/src/drag_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use crate::container_click::MouseDragType;
use crate::{Container, InventoryError};
use itertools::Itertools;
use num_traits::Euclid;
use parking_lot::{Mutex, RwLock};
use pumpkin_world::item::ItemStack;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use std::sync::Arc;
#[derive(Debug, Default)]
pub struct DragHandler(RwLock<HashMap<u64, Arc<Mutex<Drag>>>>);

Expand All @@ -23,10 +24,7 @@ impl DragHandler {
drag_type,
slots: vec![],
};
let mut drags = match self.0.write() {
Ok(drags) => drags,
Err(_) => Err(InventoryError::LockError)?,
};
let mut drags = self.0.write();
drags.insert(container_id, Arc::new(Mutex::new(drag)));
Ok(())
}
Expand All @@ -37,13 +35,10 @@ impl DragHandler {
player: i32,
slot: usize,
) -> Result<(), InventoryError> {
let drags = match self.0.read() {
Ok(drags) => drags,
Err(_) => Err(InventoryError::LockError)?,
};
let drags = self.0.read();
match drags.get(&container_id) {
Some(drag) => {
let mut drag = drag.lock().unwrap();
let mut drag = drag.lock();
if drag.player != player {
Err(InventoryError::MultiplePlayersDragging)?
}
Expand All @@ -68,13 +63,11 @@ impl DragHandler {
return Ok(());
}

let Ok(mut drags) = self.0.write() else {
Err(InventoryError::LockError)?
};
let mut drags = self.0.write();
let Some((_, drag)) = drags.remove_entry(container_id) else {
Err(InventoryError::OutOfOrderDragging)?
};
let drag = drag.lock().unwrap();
let drag = drag.lock();

if player != drag.player {
Err(InventoryError::MultiplePlayersDragging)?
Expand Down
3 changes: 2 additions & 1 deletion pumpkin-inventory/src/open_container.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Container, WindowType};
use parking_lot::Mutex;
use pumpkin_world::item::ItemStack;
use std::sync::{Arc, Mutex};
use std::sync::Arc;

pub struct OpenContainer {
players: Vec<i32>,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub enum PacketError {
MalformedLength,
}

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ConnectionState {
HandShake,
Status,
Expand Down
2 changes: 2 additions & 0 deletions pumpkin-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ serde_json = "1.0"
static_assertions = "1.1.0"
log.workspace = true

parking_lot.workspace = true

noise = "0.9.0"

rand = "0.8.5"
Expand Down
60 changes: 30 additions & 30 deletions pumpkin-world/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::{
fs::OpenOptions,
io::{Read, Seek},
path::PathBuf,
sync::{Arc, Mutex},
sync::Arc,
};

use flate2::{bufread::ZlibDecoder, read::GzDecoder};
use itertools::Itertools;
use parking_lot::Mutex;
use pumpkin_core::math::vector2::Vector2;
use rayon::prelude::*;
use thiserror::Error;
Expand Down Expand Up @@ -149,40 +150,39 @@ impl Level {
dbg!("a");
return;
}
if let Ok(mut loaded_chunks) = self.loaded_chunks.lock() {
let channel = channel.clone();
let mut loaded_chunks = self.loaded_chunks.lock();
let channel = channel.clone();

// Check if chunks is already loaded
if loaded_chunks.contains_key(at) {
channel
.blocking_send(Ok(loaded_chunks.get(at).unwrap().clone()))
.expect("Failed sending ChunkData.");
return;
}
let at = *at;
let data = match &self.save_file {
Some(save_file) => {
match Self::read_chunk(save_file, at) {
Err(WorldError::ChunkNotGenerated(_)) => {
// This chunk was not generated yet.
Ok(self.world_gen.generate_chunk(at))
}
// TODO this doesn't warn the user about the error. fix.
result => result,
// Check if chunks is already loaded
if loaded_chunks.contains_key(at) {
channel
.blocking_send(Ok(loaded_chunks.get(at).unwrap().clone()))
.expect("Failed sending ChunkData.");
return;
}
let at = *at;
let data = match &self.save_file {
Some(save_file) => {
match Self::read_chunk(save_file, at) {
Err(WorldError::ChunkNotGenerated(_)) => {
// This chunk was not generated yet.
Ok(self.world_gen.generate_chunk(at))
}
}
None => {
// There is no savefile yet -> generate the chunks
Ok(self.world_gen.generate_chunk(at))
// TODO this doesn't warn the user about the error. fix.
result => result,
}
}
.unwrap();
let data = Arc::new(data);
channel
.blocking_send(Ok(data.clone()))
.expect("Failed sending ChunkData.");
loaded_chunks.insert(at, data);
None => {
// There is no savefile yet -> generate the chunks
Ok(self.world_gen.generate_chunk(at))
}
}
.unwrap();
let data = Arc::new(data);
channel
.blocking_send(Ok(data.clone()))
.expect("Failed sending ChunkData.");
loaded_chunks.insert(at, data);
})
}

Expand Down
2 changes: 2 additions & 0 deletions pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ log.workspace = true
# networking
mio = { version = "1.0.2", features = ["os-poll", "net"]}

parking_lot.workspace = true
crossbeam.workspace = true
uuid.workspace = true
tokio.workspace = true
rayon.workspace = true
Loading

0 comments on commit a609805

Please sign in to comment.