Skip to content

Commit

Permalink
Add cds
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Aug 13, 2024
1 parent 0b1ebc7 commit 248ea37
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 24 deletions.
48 changes: 24 additions & 24 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@

[workspace]
resolver = "2"

members = [
"array",
"backtracking",
"bit_manipulation",
"ciphers",
"conversions",
"deque",
"dynamic_programming",
"graph",
"hash_table",
"list",
"math",
"priority_queue",
"queue",
"search",
"sort",
"src/leetcode/0*.*",
"src/leetcode/1*.*",
"src/leetcode/2*.*",
"src/leetcode/3*.*",
"stack",
"string",
"tree",
"vector",
"array",
"backtracking",
"bit_manipulation",
"cds",
"ciphers",
"conversions",
"deque",
"dynamic_programming",
"graph",
"hash_table",
"list",
"math",
"priority_queue",
"queue",
"search",
"sort",
"src/leetcode/0*.*",
"src/leetcode/1*.*",
"src/leetcode/2*.*",
"src/leetcode/3*.*",
"stack",
"string",
"tree",
"vector",
]

[profile.release]
Expand Down
7 changes: 7 additions & 0 deletions cds/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "cds"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
13 changes: 13 additions & 0 deletions cds/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

#![deny(
warnings,
clippy::all,
clippy::cargo,
clippy::nursery,
clippy::pedantic
)]

//pub mod lock_coupling_linked_list;
30 changes: 30 additions & 0 deletions cds/src/lock_coupling_linked_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by GNU General Public License
// that can be found in the LICENSE file.

use std::ptr::NonNull;
use std::sync::Mutex;

pub struct LockCouplingLinkedList<T> {
len: usize,
head: NodePtr<T>,
}

type NodePtr<T> = Mutex<Option<NonNull<Node<T>>>>;

pub struct Node<T> {
next: NodePtr<T>,
value: T,
}

impl<T> LockCouplingLinkedList<T> {}

impl<T> Node<T> {
#[must_use]
fn new(value: T) -> Self {
Self {
next: Mutex::new(None),
value,
}
}
}
12 changes: 12 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@
- [广度优先搜索 BFS](graph/bfs.md)
- [最短路径](graph/shortest-path.md)
- [最小生成树](graph/minimum-spanning-tree.md)
- [并发数据结构 Concurrent Data Structures](concurrent-data-structures/index.md)
- [简介](concurrent-data-structures/intro.md)
- [Lock-coupling Linked List](concurrent-data-structures/lock-coupling-linked-list.md)
- [Concurrent Ring Buffer](concurrent-data-structures/concurrent-ring-buffer.md)
- [Concurrent Hash Map](concurrent-data-structures/concurrent-hash-map.md)
- [Concurrent Linked List](concurrent-data-structures/concurrent-linked-list.md)
- [Concurrent Deque](concurrent-data-structures/concurrent-deque.md)
- [Concurrent Queue](concurrent-data-structures/concurrent-queue.md)
- [Concurrent SkipList Map](concurrent-data-structures/concurrent-skip-list-map.md)
- [Concurrent SkipList Set](concurrent-data-structures/concurrent-skip-list-set.md)
- [Concurrent Radix Tree](concurrent-data-structures/concurrent-radix-tree.md)
- [Skip Graph](concurrent-data-structures/skip-graph.md)

# 第二部分: 算法 Algorithms

Expand Down
1 change: 1 addition & 0 deletions src/concurrent-data-structures/concurrent-deque.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Concurrent Deque
1 change: 1 addition & 0 deletions src/concurrent-data-structures/concurrent-hash-map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Concurrent Hash Map
1 change: 1 addition & 0 deletions src/concurrent-data-structures/concurrent-linked-list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Concurrent List
10 changes: 10 additions & 0 deletions src/concurrent-data-structures/concurrent-queue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Concurrent Queue

Multi-producer, multi-consumer lock-free FIFO

## 参考

- [ConcurrentQueue](https://github.com/cameron314/concurrentqueue)
- [A Fast General Purpose Lock-Free Queue for C++](https://moodycamel.com/blog/2014/a-fast-general-purpose-lock-free-queue-for-c++)
- [A Fast Lock-Free Queue for C++](https://moodycamel.com/blog/2013/a-fast-lock-free-queue-for-c++)
- [Detailed Design of a Lock-Free Queue](https://moodycamel.com/blog/2014/detailed-design-of-a-lock-free-queue)
1 change: 1 addition & 0 deletions src/concurrent-data-structures/concurrent-radix-tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Concurrent Radix Tree
3 changes: 3 additions & 0 deletions src/concurrent-data-structures/concurrent-ring-buffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Concurrent Ring Buffer

Single-producer, single-consumer lock-free FIFO
1 change: 1 addition & 0 deletions src/concurrent-data-structures/concurrent-skip-list-map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Concurrent SkipList Map
1 change: 1 addition & 0 deletions src/concurrent-data-structures/concurrent-skip-list-set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Concurrent SkipList Set
1 change: 1 addition & 0 deletions src/concurrent-data-structures/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# 并发数据结构 Concurrent Data Structures
29 changes: 29 additions & 0 deletions src/concurrent-data-structures/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 简介

并发数据结构 Concurrent data structures, CDS 有三个关键的主题:

- 安全 Safety, 满足多线程并发的规范
- 顺序规则 sequential specification, 像一个队列一样按顺序操作
- 同步 synchronization
- 可扩展性 Scalability, 随着处理器核心数的增多, 性能更好
- 理想情况下, 线性递增
- 实际情况, 超过 16 个线程之后, 会退化成次线性递增 (sublinear scaling)
- 有进度 Progress, 保证操作过程向前推进
- lock freedom: 至少有一个进度向前推进
- wait freedom: 所有的进度都向前推进

## 安全性 Safety

使用锁或者其它同步原语 (primitive synchronization) 来保护并发数据结构.

- 使用全局锁来保护顺序数据结构
- 使用自定义的同步协议来保护数据结构

## 可扩展性 Scalability

- 减少锁保护的作用域
- 读写锁 read-write locking
- hand-over-hand locking
- lock coupling
- 避免写数据以便减少无效的缓存
- 乐观锁
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Lock-coupling Linked List
5 changes: 5 additions & 0 deletions src/concurrent-data-structures/skip-graph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Skip Graph

## 参考

- [Skip graph](https://en.wikipedia.org/wiki/Skip_graph)

0 comments on commit 248ea37

Please sign in to comment.