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
Peter Limkilde Svendsen
relibc
Commits
2f69f0e7
Verified
Commit
2f69f0e7
authored
4 years ago
by
Jeremy Soller
Browse files
Options
Downloads
Patches
Plain Diff
Add simple semaphore implementation using futex
parent
79452dbd
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/sync/mod.rs
+2
-0
2 additions, 0 deletions
src/sync/mod.rs
src/sync/semaphore.rs
+46
-0
46 additions, 0 deletions
src/sync/semaphore.rs
with
48 additions
and
0 deletions
src/sync/mod.rs
+
2
−
0
View file @
2f69f0e7
pub
mod
mutex
;
pub
mod
mutex
;
pub
mod
once
;
pub
mod
once
;
pub
mod
semaphore
;
pub
use
self
::{
pub
use
self
::{
mutex
::{
Mutex
,
MutexGuard
},
mutex
::{
Mutex
,
MutexGuard
},
once
::
Once
,
once
::
Once
,
semaphore
::
Semaphore
,
};
};
use
crate
::
platform
::{
types
::
*
,
Pal
,
Sys
};
use
crate
::
platform
::{
types
::
*
,
Pal
,
Sys
};
...
...
This diff is collapsed.
Click to expand it.
src/sync/semaphore.rs
0 → 100644
+
46
−
0
View file @
2f69f0e7
// From https://www.remlab.net/op/futex-misc.shtml
//TODO: improve implementation
use
crate
::
platform
::{
types
::
*
,
Pal
,
Sys
};
use
super
::
AtomicLock
;
use
core
::
sync
::
atomic
::
Ordering
;
pub
struct
Semaphore
{
lock
:
AtomicLock
,
}
impl
Semaphore
{
pub
const
fn
new
(
value
:
c_int
)
->
Self
{
Self
{
lock
:
AtomicLock
::
new
(
value
),
}
}
pub
fn
post
(
&
self
)
{
self
.lock
.fetch_add
(
1
,
Ordering
::
Relaxed
);
self
.lock
.notify_one
();
}
pub
fn
wait
(
&
self
)
{
let
mut
value
=
1
;
loop
{
match
self
.lock
.compare_exchange_weak
(
value
,
value
-
1
,
Ordering
::
Acquire
,
Ordering
::
Relaxed
)
{
Ok
(
ok
)
=>
return
,
Err
(
err
)
=>
{
value
=
err
;
}
}
if
value
==
0
{
self
.lock
.wait_if
(
0
);
value
=
1
;
}
}
}
}
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