Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
relibc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
redox-os
relibc
Commits
beac2482
Verified
Commit
beac2482
authored
1 year ago
by
Jacob Lorentzon
Browse files
Options
Downloads
Patches
Plain Diff
Use futex directly in waitval.
parent
4faa3155
No related branches found
No related tags found
1 merge request
!380
Replace pthreads-emb with a native implementation
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/sync/waitval.rs
+9
-8
9 additions, 8 deletions
src/sync/waitval.rs
with
9 additions
and
8 deletions
src/sync/waitval.rs
+
9
−
8
View file @
beac2482
// Semaphores need one post per wait, Once doesn't have separate post and wait, and while mutexes
// wait for releasing the lock, it calls notify_one and needs locking again to access the value.
use
core
::
cell
::
UnsafeCell
;
use
core
::
mem
::
MaybeUninit
;
use
core
::
sync
::
atomic
::
Ordering
;
use
core
::
sync
::
atomic
::
{
AtomicU32
as
AtomicUint
,
Ordering
}
;
use
super
::
*
;
/// An unsafe "one thread to one thread" synchronization primitive. Used for and modeled after
/// pthread_join only, at the moment.
pub
struct
Waitval
<
T
>
{
state
:
Atomic
Lock
,
state
:
Atomic
Uint
,
value
:
UnsafeCell
<
MaybeUninit
<
T
>>
,
}
...
...
@@ -18,20 +17,22 @@ unsafe impl<T: Send + Sync> Sync for Waitval<T> {}
impl
<
T
>
Waitval
<
T
>
{
pub
const
fn
new
()
->
Self
{
Self
{
state
:
Atomic
Lock
::
new
(
0
),
state
:
Atomic
Uint
::
new
(
0
),
value
:
UnsafeCell
::
new
(
MaybeUninit
::
uninit
()),
}
}
// SAFETY: Caller must ensure both (1) that the value has not yet been initialized, and (2)
// that this is never run by more than one thread simultaneously.
pub
unsafe
fn
post
(
&
self
,
value
:
T
)
{
self
.value
.get
()
.write
(
MaybeUninit
::
new
(
value
));
self
.state
.store
(
1
,
Ordering
::
Release
);
self
.state
.notify_all
(
);
crate
::
sync
::
futex_wake
(
&
self
.state
,
i32
::
MAX
);
}
pub
fn
wait
(
&
self
)
->
&
T
{
while
self
.state
.load
(
Ordering
::
Acquire
)
==
0
{
self
.state
.wait_if
(
0
,
None
);
crate
::
sync
::
futex_wait
(
&
self
.state
,
0
,
None
);
}
unsafe
{
(
*
self
.value
.get
())
.assume_init_ref
()
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment