Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
6a417d48
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6a417d48
编写于
6月 03, 2022
作者:
M
Mara Bos
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Lazily allocate+initialize locks.
上级
ac5aa1de
变更
17
隐藏空白更改
内联
并排
Showing
17 changed file
with
145 addition
and
36 deletion
+145
-36
library/std/src/sys/sgx/condvar.rs
library/std/src/sys/sgx/condvar.rs
+8
-4
library/std/src/sys/sgx/mutex.rs
library/std/src/sys/sgx/mutex.rs
+8
-1
library/std/src/sys/sgx/rwlock.rs
library/std/src/sys/sgx/rwlock.rs
+8
-1
library/std/src/sys/unix/locks/futex.rs
library/std/src/sys/unix/locks/futex.rs
+0
-3
library/std/src/sys/unix/locks/mod.rs
library/std/src/sys/unix/locks/mod.rs
+5
-5
library/std/src/sys/unix/locks/pthread_condvar.rs
library/std/src/sys/unix/locks/pthread_condvar.rs
+13
-4
library/std/src/sys/unix/locks/pthread_mutex.rs
library/std/src/sys/unix/locks/pthread_mutex.rs
+10
-1
library/std/src/sys/unix/locks/pthread_rwlock.rs
library/std/src/sys/unix/locks/pthread_rwlock.rs
+8
-1
library/std/src/sys/unsupported/locks/condvar.rs
library/std/src/sys/unsupported/locks/condvar.rs
+0
-3
library/std/src/sys/wasm/mod.rs
library/std/src/sys/wasm/mod.rs
+2
-2
library/std/src/sys/windows/locks/condvar.rs
library/std/src/sys/windows/locks/condvar.rs
+0
-3
library/std/src/sys_common/condvar.rs
library/std/src/sys_common/condvar.rs
+1
-3
library/std/src/sys_common/condvar/check.rs
library/std/src/sys_common/condvar/check.rs
+2
-1
library/std/src/sys_common/lazy_box.rs
library/std/src/sys_common/lazy_box.rs
+77
-0
library/std/src/sys_common/mod.rs
library/std/src/sys_common/mod.rs
+1
-0
library/std/src/sys_common/mutex.rs
library/std/src/sys_common/mutex.rs
+1
-3
library/std/src/sys_common/rwlock.rs
library/std/src/sys_common/rwlock.rs
+1
-1
未找到文件。
library/std/src/sys/sgx/condvar.rs
浏览文件 @
6a417d48
use
crate
::
sys
::
locks
::
Mutex
;
use
crate
::
sys_common
::
lazy_box
::{
LazyBox
,
LazyInit
};
use
crate
::
time
::
Duration
;
use
super
::
waitqueue
::{
SpinMutex
,
WaitQueue
,
WaitVariable
};
...
...
@@ -7,16 +8,19 @@ pub struct Condvar {
inner
:
SpinMutex
<
WaitVariable
<
()
>>
,
}
pub
type
MovableCondvar
=
Box
<
Condvar
>
;
pub
(
crate
)
type
MovableCondvar
=
LazyBox
<
Condvar
>
;
impl
LazyInit
for
Condvar
{
fn
init
()
->
Box
<
Self
>
{
Box
::
new
(
Self
::
new
())
}
}
impl
Condvar
{
pub
const
fn
new
()
->
Condvar
{
Condvar
{
inner
:
SpinMutex
::
new
(
WaitVariable
::
new
(()))
}
}
#[inline]
pub
unsafe
fn
init
(
&
mut
self
)
{}
#[inline]
pub
unsafe
fn
notify_one
(
&
self
)
{
let
_
=
WaitQueue
::
notify_one
(
self
.inner
.lock
());
...
...
library/std/src/sys/sgx/mutex.rs
浏览文件 @
6a417d48
use
super
::
waitqueue
::{
try_lock_or_false
,
SpinMutex
,
WaitQueue
,
WaitVariable
};
use
crate
::
sys_common
::
lazy_box
::{
LazyBox
,
LazyInit
};
pub
struct
Mutex
{
inner
:
SpinMutex
<
WaitVariable
<
bool
>>
,
}
// not movable: see UnsafeList implementation
pub
type
MovableMutex
=
Box
<
Mutex
>
;
pub
(
crate
)
type
MovableMutex
=
LazyBox
<
Mutex
>
;
impl
LazyInit
for
Mutex
{
fn
init
()
->
Box
<
Self
>
{
Box
::
new
(
Self
::
new
())
}
}
// Implementation according to “Operating Systems: Three Easy Pieces”, chapter 28
impl
Mutex
{
...
...
library/std/src/sys/sgx/rwlock.rs
浏览文件 @
6a417d48
...
...
@@ -2,6 +2,7 @@
mod
tests
;
use
crate
::
num
::
NonZeroUsize
;
use
crate
::
sys_common
::
lazy_box
::{
LazyBox
,
LazyInit
};
use
super
::
waitqueue
::{
try_lock_or_false
,
NotifiedTcs
,
SpinMutex
,
SpinMutexGuard
,
WaitQueue
,
WaitVariable
,
...
...
@@ -13,7 +14,13 @@ pub struct RwLock {
writer
:
SpinMutex
<
WaitVariable
<
bool
>>
,
}
pub
type
MovableRwLock
=
Box
<
RwLock
>
;
pub
(
crate
)
type
MovableRwLock
=
LazyBox
<
RwLock
>
;
impl
LazyInit
for
RwLock
{
fn
init
()
->
Box
<
Self
>
{
Box
::
new
(
Self
::
new
())
}
}
// Check at compile time that RwLock size matches C definition (see test_c_rwlock_initializer below)
//
...
...
library/std/src/sys/unix/locks/futex.rs
浏览文件 @
6a417d48
...
...
@@ -115,9 +115,6 @@ pub const fn new() -> Self {
Self
{
futex
:
AtomicU32
::
new
(
0
)
}
}
#[inline]
pub
unsafe
fn
init
(
&
mut
self
)
{}
// All the memory orderings here are `Relaxed`,
// because synchronization is done by unlocking and locking the mutex.
...
...
library/std/src/sys/unix/locks/mod.rs
浏览文件 @
6a417d48
...
...
@@ -9,14 +9,14 @@
))]
{
mod
futex
;
mod
futex_rwlock
;
pub
use
futex
::{
Mutex
,
MovableMutex
,
Condvar
,
MovableCondvar
};
pub
use
futex_rwlock
::{
RwLock
,
MovableRwLock
};
pub
(
crate
)
use
futex
::{
Mutex
,
MovableMutex
,
MovableCondvar
};
pub
(
crate
)
use
futex_rwlock
::{
RwLock
,
MovableRwLock
};
}
else
{
mod
pthread_mutex
;
mod
pthread_rwlock
;
mod
pthread_condvar
;
pub
use
pthread_mutex
::{
Mutex
,
MovableMutex
};
pub
use
pthread_rwlock
::{
RwLock
,
MovableRwLock
};
pub
use
pthread_condvar
::{
Condvar
,
MovableCondvar
}
;
pub
(
crate
)
use
pthread_mutex
::{
Mutex
,
MovableMutex
};
pub
(
crate
)
use
pthread_rwlock
::{
RwLock
,
MovableRwLock
};
pub
(
crate
)
use
pthread_condvar
::
MovableCondvar
;
}
}
library/std/src/sys/unix/locks/pthread_condvar.rs
浏览文件 @
6a417d48
use
crate
::
cell
::
UnsafeCell
;
use
crate
::
sys
::
locks
::{
pthread_mutex
,
Mutex
};
use
crate
::
sys_common
::
lazy_box
::{
LazyBox
,
LazyInit
};
use
crate
::
time
::
Duration
;
pub
struct
Condvar
{
inner
:
UnsafeCell
<
libc
::
pthread_cond_t
>
,
}
pub
type
MovableCondvar
=
Box
<
Condvar
>
;
pub
(
crate
)
type
MovableCondvar
=
Lazy
Box
<
Condvar
>
;
unsafe
impl
Send
for
Condvar
{}
unsafe
impl
Sync
for
Condvar
{}
...
...
@@ -18,6 +19,14 @@ fn saturating_cast_to_time_t(value: u64) -> libc::time_t {
if
value
>
<
libc
::
time_t
>
::
MAX
as
u64
{
<
libc
::
time_t
>
::
MAX
}
else
{
value
as
libc
::
time_t
}
}
impl
LazyInit
for
Condvar
{
fn
init
()
->
Box
<
Self
>
{
let
mut
condvar
=
Box
::
new
(
Self
::
new
());
unsafe
{
condvar
.init
()
};
condvar
}
}
impl
Condvar
{
pub
const
fn
new
()
->
Condvar
{
// Might be moved and address is changing it is better to avoid
...
...
@@ -32,14 +41,14 @@ pub const fn new() -> Condvar {
target_os
=
"android"
,
target_os
=
"redox"
))]
pub
unsafe
fn
init
(
&
mut
self
)
{}
unsafe
fn
init
(
&
mut
self
)
{}
// NOTE: ESP-IDF's PTHREAD_COND_INITIALIZER support is not released yet
// So on that platform, init() should always be called
// Moreover, that platform does not have pthread_condattr_setclock support,
// hence that initialization should be skipped as well
#[cfg(target_os
=
"espidf"
)]
pub
unsafe
fn
init
(
&
mut
self
)
{
unsafe
fn
init
(
&
mut
self
)
{
let
r
=
libc
::
pthread_cond_init
(
self
.inner
.get
(),
crate
::
ptr
::
null
());
assert_eq!
(
r
,
0
);
}
...
...
@@ -52,7 +61,7 @@ pub unsafe fn init(&mut self) {
target_os
=
"redox"
,
target_os
=
"espidf"
)))]
pub
unsafe
fn
init
(
&
mut
self
)
{
unsafe
fn
init
(
&
mut
self
)
{
use
crate
::
mem
::
MaybeUninit
;
let
mut
attr
=
MaybeUninit
::
<
libc
::
pthread_condattr_t
>
::
uninit
();
let
r
=
libc
::
pthread_condattr_init
(
attr
.as_mut_ptr
());
...
...
library/std/src/sys/unix/locks/pthread_mutex.rs
浏览文件 @
6a417d48
use
crate
::
cell
::
UnsafeCell
;
use
crate
::
mem
::
MaybeUninit
;
use
crate
::
sys
::
cvt_nz
;
use
crate
::
sys_common
::
lazy_box
::{
LazyBox
,
LazyInit
};
pub
struct
Mutex
{
inner
:
UnsafeCell
<
libc
::
pthread_mutex_t
>
,
}
pub
type
MovableMutex
=
Box
<
Mutex
>
;
pub
(
crate
)
type
MovableMutex
=
Lazy
Box
<
Mutex
>
;
#[inline]
pub
unsafe
fn
raw
(
m
:
&
Mutex
)
->
*
mut
libc
::
pthread_mutex_t
{
...
...
@@ -16,6 +17,14 @@ pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t {
unsafe
impl
Send
for
Mutex
{}
unsafe
impl
Sync
for
Mutex
{}
impl
LazyInit
for
Mutex
{
fn
init
()
->
Box
<
Self
>
{
let
mut
mutex
=
Box
::
new
(
Self
::
new
());
unsafe
{
mutex
.init
()
};
mutex
}
}
impl
Mutex
{
pub
const
fn
new
()
->
Mutex
{
// Might be moved to a different address, so it is better to avoid
...
...
library/std/src/sys/unix/locks/pthread_rwlock.rs
浏览文件 @
6a417d48
use
crate
::
cell
::
UnsafeCell
;
use
crate
::
sync
::
atomic
::{
AtomicUsize
,
Ordering
};
use
crate
::
sys_common
::
lazy_box
::{
LazyBox
,
LazyInit
};
pub
struct
RwLock
{
inner
:
UnsafeCell
<
libc
::
pthread_rwlock_t
>
,
...
...
@@ -7,11 +8,17 @@ pub struct RwLock {
num_readers
:
AtomicUsize
,
}
pub
type
MovableRwLock
=
Box
<
RwLock
>
;
pub
(
crate
)
type
MovableRwLock
=
Lazy
Box
<
RwLock
>
;
unsafe
impl
Send
for
RwLock
{}
unsafe
impl
Sync
for
RwLock
{}
impl
LazyInit
for
RwLock
{
fn
init
()
->
Box
<
Self
>
{
Box
::
new
(
Self
::
new
())
}
}
impl
RwLock
{
pub
const
fn
new
()
->
RwLock
{
RwLock
{
...
...
library/std/src/sys/unsupported/locks/condvar.rs
浏览文件 @
6a417d48
...
...
@@ -10,9 +10,6 @@ pub const fn new() -> Condvar {
Condvar
{}
}
#[inline]
pub
unsafe
fn
init
(
&
mut
self
)
{}
#[inline]
pub
unsafe
fn
notify_one
(
&
self
)
{}
...
...
library/std/src/sys/wasm/mod.rs
浏览文件 @
6a417d48
...
...
@@ -54,8 +54,8 @@ pub mod locks {
#![allow(unsafe_op_in_unsafe_fn)]
mod
futex
;
mod
futex_rwlock
;
pub
use
futex
::{
Mutex
,
MovableMutex
,
Condvar
,
MovableCondvar
};
pub
use
futex_rwlock
::{
RwLock
,
MovableRwLock
};
pub
(
crate
)
use
futex
::{
Mutex
,
MovableMutex
,
Condvar
,
MovableCondvar
};
pub
(
crate
)
use
futex_rwlock
::{
RwLock
,
MovableRwLock
};
}
#[path
=
"atomics/futex.rs"
]
pub
mod
futex
;
...
...
library/std/src/sys/windows/locks/condvar.rs
浏览文件 @
6a417d48
...
...
@@ -18,9 +18,6 @@ pub const fn new() -> Condvar {
Condvar
{
inner
:
UnsafeCell
::
new
(
c
::
CONDITION_VARIABLE_INIT
)
}
}
#[inline]
pub
unsafe
fn
init
(
&
mut
self
)
{}
#[inline]
pub
unsafe
fn
wait
(
&
self
,
mutex
:
&
Mutex
)
{
let
r
=
c
::
SleepConditionVariableSRW
(
self
.inner
.get
(),
mutex
::
raw
(
mutex
),
c
::
INFINITE
,
0
);
...
...
library/std/src/sys_common/condvar.rs
浏览文件 @
6a417d48
...
...
@@ -15,9 +15,7 @@ pub struct Condvar {
impl
Condvar
{
/// Creates a new condition variable for use.
pub
fn
new
()
->
Self
{
let
mut
c
=
imp
::
MovableCondvar
::
from
(
imp
::
Condvar
::
new
());
unsafe
{
c
.init
()
};
Self
{
inner
:
c
,
check
:
CondvarCheck
::
new
()
}
Self
{
inner
:
imp
::
MovableCondvar
::
new
(),
check
:
CondvarCheck
::
new
()
}
}
/// Signals one waiter on this condition variable to wake up.
...
...
library/std/src/sys_common/condvar/check.rs
浏览文件 @
6a417d48
use
crate
::
ptr
;
use
crate
::
sync
::
atomic
::{
AtomicPtr
,
Ordering
};
use
crate
::
sys
::
locks
as
imp
;
use
crate
::
sys_common
::
lazy_box
::{
LazyBox
,
LazyInit
};
use
crate
::
sys_common
::
mutex
::
MovableMutex
;
pub
trait
CondvarCheck
{
...
...
@@ -9,7 +10,7 @@ pub trait CondvarCheck {
/// For boxed mutexes, a `Condvar` will check it's only ever used with the same
/// mutex, based on its (stable) address.
impl
CondvarCheck
for
Box
<
imp
::
Mutex
>
{
impl
<
T
:
LazyInit
>
CondvarCheck
for
LazyBox
<
T
>
{
type
Check
=
SameMutexCheck
;
}
...
...
library/std/src/sys_common/lazy_box.rs
0 → 100644
浏览文件 @
6a417d48
#![allow(dead_code)]
// Only used on some platforms.
// This is used to wrap pthread {Mutex, Condvar, RwLock} in.
use
crate
::
marker
::
PhantomData
;
use
crate
::
ops
::{
Deref
,
DerefMut
};
use
crate
::
ptr
::
null_mut
;
use
crate
::
sync
::
atomic
::{
AtomicPtr
,
Ordering
::{
AcqRel
,
Acquire
},
};
pub
(
crate
)
struct
LazyBox
<
T
:
LazyInit
>
{
ptr
:
AtomicPtr
<
T
>
,
_
phantom
:
PhantomData
<
T
>
,
}
pub
(
crate
)
trait
LazyInit
{
/// This is called before the box is allocated, to provide the value to
/// move into the new box.
///
/// It might be called more than once per LazyBox, as multiple threads
/// might race to initialize it concurrently, each constructing and initializing
/// their own box. (All but one of them will be destroyed right after.)
fn
init
()
->
Box
<
Self
>
;
}
impl
<
T
:
LazyInit
>
LazyBox
<
T
>
{
#[inline]
pub
const
fn
new
()
->
Self
{
Self
{
ptr
:
AtomicPtr
::
new
(
null_mut
()),
_
phantom
:
PhantomData
}
}
#[inline]
fn
get_pointer
(
&
self
)
->
*
mut
T
{
let
ptr
=
self
.ptr
.load
(
Acquire
);
if
ptr
.is_null
()
{
self
.initialize
()
}
else
{
ptr
}
}
#[cold]
fn
initialize
(
&
self
)
->
*
mut
T
{
let
new_ptr
=
Box
::
into_raw
(
T
::
init
());
match
self
.ptr
.compare_exchange
(
null_mut
(),
new_ptr
,
AcqRel
,
Acquire
)
{
Ok
(
_
)
=>
new_ptr
,
Err
(
ptr
)
=>
{
// Lost the race to another thread.
// Drop the box we created, and use the one from the other thread instead.
drop
(
unsafe
{
Box
::
from_raw
(
new_ptr
)
});
ptr
}
}
}
}
impl
<
T
:
LazyInit
>
Deref
for
LazyBox
<
T
>
{
type
Target
=
T
;
#[inline]
fn
deref
(
&
self
)
->
&
T
{
unsafe
{
&*
self
.get_pointer
()
}
}
}
impl
<
T
:
LazyInit
>
DerefMut
for
LazyBox
<
T
>
{
#[inline]
fn
deref_mut
(
&
mut
self
)
->
&
mut
T
{
unsafe
{
&
mut
*
self
.get_pointer
()
}
}
}
impl
<
T
:
LazyInit
>
Drop
for
LazyBox
<
T
>
{
fn
drop
(
&
mut
self
)
{
let
ptr
=
*
self
.ptr
.get_mut
();
if
!
ptr
.is_null
()
{
drop
(
unsafe
{
Box
::
from_raw
(
ptr
)
});
}
}
}
library/std/src/sys_common/mod.rs
浏览文件 @
6a417d48
...
...
@@ -24,6 +24,7 @@
pub
mod
condvar
;
pub
mod
fs
;
pub
mod
io
;
pub
mod
lazy_box
;
pub
mod
memchr
;
pub
mod
mutex
;
pub
mod
process
;
...
...
library/std/src/sys_common/mutex.rs
浏览文件 @
6a417d48
...
...
@@ -61,9 +61,7 @@ unsafe impl Sync for MovableMutex {}
impl
MovableMutex
{
/// Creates a new mutex.
pub
fn
new
()
->
Self
{
let
mut
mutex
=
imp
::
MovableMutex
::
from
(
imp
::
Mutex
::
new
());
unsafe
{
mutex
.init
()
};
Self
(
mutex
)
Self
(
imp
::
MovableMutex
::
new
())
}
pub
(
super
)
fn
raw
(
&
self
)
->
&
imp
::
Mutex
{
...
...
library/std/src/sys_common/rwlock.rs
浏览文件 @
6a417d48
...
...
@@ -74,7 +74,7 @@ fn drop(&mut self) {
impl
MovableRwLock
{
/// Creates a new reader-writer lock for use.
pub
fn
new
()
->
Self
{
Self
(
imp
::
MovableRwLock
::
from
(
imp
::
RwLock
::
new
()
))
Self
(
imp
::
MovableRwLock
::
new
(
))
}
/// Acquires shared access to the underlying lock, blocking the current
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录