Skip to content

Commit 8ecfa5e

Browse files
authored
Remove no default features from build (mattwparas#326)
* remove no default features from build * rework mutex implementation
1 parent 9d6f1da commit 8ecfa5e

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ jobs:
5050
echo $STEEL_HOME
5151
mkdir -p .steel/cogs
5252
cd cogs/
53-
cargo run --no-default-features -- install.scm
53+
cargo run -- install.scm
5454
5555
- uses: actions-rs/cargo@v1
5656
env:
5757
STEEL_HOME: ${{ env.STEEL_HOME }}
5858
with:
5959
command: test
60-
args: --all --no-default-features
60+
args: --all
6161

6262
- name: install cargo-tarpaulin
6363
run: cargo install cargo-tarpaulin --force

crates/steel-core/src/scheme/modules/sync.scm

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
;; Lock the given lock during the duration
1212
;; of the thunk.
1313
(define (lock! lock thunk)
14-
(dynamic-wind (lambda () (lock-acquire! lock))
15-
(lambda () (thunk))
16-
(lambda () (lock-release! lock))))
14+
(let ([lock-guard (lock-acquire! lock)])
15+
(dynamic-wind (lambda () void) (lambda () (thunk)) (lambda () (lock-release! lock-guard)))))
1716

1817
(struct ThreadPool (task-sender capacity thread-handles))
1918

2019
(struct Task (lock done func-or-result err) #:mutable)
2120

2221
(define *running* 'running)
2322
(define *waiting* 'waiting)
23+
(define *done* 'done)
2424

2525
(define (task func)
2626
(Task (mutex) *waiting* func #f))
@@ -52,7 +52,7 @@
5252

5353
;; Does this work?
5454
(with-handler (lambda (err)
55-
(set-Task-done! next-task #t)
55+
(set-Task-done! next-task *done*)
5656
(set-Task-err! next-task err))
5757
;; Capture exception, if it exists. Store it in the task
5858
(lock! (Task-lock next-task)
@@ -61,7 +61,7 @@
6161
;; This should be fine, we're updating the task to be finished,
6262
;; so we can check the progress of it
6363
(set-Task-func-or-result! next-task (func))
64-
(set-Task-done! next-task 'done))))
64+
(set-Task-done! next-task *done*))))
6565

6666
(listen-for-tasks))
6767

@@ -94,19 +94,14 @@
9494
(define (loop task)
9595
(cond
9696
[(equal? (Task-done task) *waiting*) (loop task)]
97-
9897
[(equal? (Task-done task) *running*)
9998
(try-block task)
10099
(loop task)]
101-
102-
[(equal? (Task-done task) 'done)
100+
[(equal? (Task-done task) *done*)
103101
(if (Task-err task)
104102
(Task-err task)
105103
(Task-func-or-result task))]
106-
107-
[else
108-
(try-block task)
109-
(loop task)]))
104+
[else (loop task)]))
110105

111106
(loop task))
112107

crates/steel-core/src/steel_vm/vm/threads.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,37 @@ impl crate::rvals::Custom for ThreadHandle {}
5050

5151
pub struct SteelMutex {
5252
mutex: Arc<parking_lot::Mutex<()>>,
53-
guard: AtomicCell<Option<parking_lot::ArcMutexGuard<parking_lot::RawMutex, ()>>>,
5453
}
5554

5655
impl crate::rvals::Custom for SteelMutex {}
5756

57+
pub struct MutexGuard {
58+
guard: AtomicCell<Option<parking_lot::ArcMutexGuard<parking_lot::RawMutex, ()>>>,
59+
}
60+
61+
impl crate::rvals::Custom for MutexGuard {}
62+
5863
impl SteelMutex {
5964
pub fn new() -> Self {
6065
Self {
6166
mutex: Arc::new(parking_lot::Mutex::new(())),
62-
guard: AtomicCell::new(None),
6367
}
6468
}
6569

6670
// Attempt to lock it before killing the other one
67-
pub fn lock(&self) {
71+
pub fn lock(&self) -> SteelVal {
6872
// Acquire the lock first
69-
let guard = self.mutex.lock_arc();
70-
self.guard.store(Some(guard));
73+
MutexGuard {
74+
guard: AtomicCell::new(Some(self.mutex.lock_arc())),
75+
}
76+
.into_steelval()
77+
.unwrap()
7178
}
79+
}
7280

81+
impl MutexGuard {
7382
pub fn unlock(&self) {
74-
self.guard.store(None);
83+
drop(self.guard.take());
7584
}
7685
}
7786

@@ -85,14 +94,13 @@ pub fn new_mutex() -> Result<SteelVal> {
8594
/// with the `lock!` function.
8695
#[steel_derive::function(name = "lock-acquire!")]
8796
pub fn mutex_lock(mutex: &SteelVal) -> Result<SteelVal> {
88-
SteelMutex::as_ref(mutex)?.lock();
89-
Ok(SteelVal::Void)
97+
Ok(SteelMutex::as_ref(mutex)?.lock())
9098
}
9199

92100
/// Unlock the given mutex.
93101
#[steel_derive::function(name = "lock-release!")]
94102
pub fn mutex_unlock(mutex: &SteelVal) -> Result<SteelVal> {
95-
SteelMutex::as_ref(mutex)?.unlock();
103+
MutexGuard::as_ref(mutex)?.unlock();
96104
Ok(SteelVal::Void)
97105
}
98106

0 commit comments

Comments
 (0)