Skip to content

Commit 5ef95ed

Browse files
committed
Adding (util concurrent atomic)
1 parent f5139ca commit 5ef95ed

File tree

4 files changed

+82
-7
lines changed

4 files changed

+82
-7
lines changed

sitelib/util/concurrent/atomic.scm

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
;;; -*- mode: scheme; coding: utf-8 -*-
2+
;;;
3+
;;; util/concurrent/atomic.scm - Atomic operations
4+
;;;
5+
;;; Copyright (c) 2024 Takashi Kato <ktakashi@ymail.com>
6+
;;;
7+
;;; Redistribution and use in source and binary forms, with or without
8+
;;; modification, are permitted provided that the following conditions
9+
;;; are met:
10+
;;;
11+
;;; 1. Redistributions of source code must retain the above copyright
12+
;;; notice, this list of conditions and the following disclaimer.
13+
;;;
14+
;;; 2. Redistributions in binary form must reproduce the above copyright
15+
;;; notice, this list of conditions and the following disclaimer in the
16+
;;; documentation and/or other materials provided with the distribution.
17+
;;;
18+
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24+
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25+
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26+
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
;;;
30+
31+
#!nounbound
32+
(library (util concurrent atomic)
33+
(export atomic? make-atomic
34+
atomic-fixnum? make-atomic-fixnum
35+
36+
atomic-load atomic-store!
37+
atomic-fixnum-load atomic-fixnum-store!
38+
39+
atomic-fixnum-add! atomic-fixnum-sub!
40+
atomic-fixnum-inc! atomic-fixnum-dec!
41+
atomic-fixnum-or! atomic-fixnum-xor! atomic-fixnum-and!
42+
43+
atomic-exchange! atomic-fixnum-exchange!
44+
45+
atomic-compare-and-swap!
46+
47+
*memory-order:relaxed*
48+
*memory-order:consume*
49+
*memory-order:acquire*
50+
*memory-order:release*
51+
*memory-order:acq-rel*
52+
*memory-order:seq-cst*)
53+
(import (sagittarius atomic)))

src/atomic.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,17 @@ extern void Sg__Init_sagittarius_atomic();
332332
void Sg__InitAtomic()
333333
{
334334
SgObject lib = Sg_FindLibrary(SG_INTERN("(sagittarius atomic)"), TRUE);
335+
336+
Sg__Init_sagittarius_atomic();
337+
335338
#define insert_binding(name, value) \
336339
Sg_MakeBinding(SG_LIBRARY(lib), SG_INTERN(#name), SG_MAKE_INT(value), TRUE)
337340

338-
insert_binding(memory-order:relaxed, memory_order_relaxed);
339-
insert_binding(memory-order:consume, memory_order_consume);
340-
insert_binding(memory-order:acquire, memory_order_acquire);
341-
insert_binding(memory-order:release, memory_order_release);
342-
insert_binding(memory-order:acq-rel, memory_order_acq_rel);
343-
insert_binding(memory-order:seq-cst, memory_order_seq_cst);
341+
insert_binding(*memory-order:relaxed*, memory_order_relaxed);
342+
insert_binding(*memory-order:consume*, memory_order_consume);
343+
insert_binding(*memory-order:acquire*, memory_order_acquire);
344+
insert_binding(*memory-order:release*, memory_order_release);
345+
insert_binding(*memory-order:acq-rel*, memory_order_acq_rel);
346+
insert_binding(*memory-order:seq-cst*, memory_order_seq_cst);
344347

345-
Sg__Init_sagittarius_atomic();
346348
}

src/lib_atomic.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@
6666

6767
(define-c-proc atomic-compare-and-swap! (a::<atomic> e v :optional (success::<memory-order> (default-memory-order)) (failure::<memory-order> (default-memory-order))) ::<boolean> Sg_AtomicCompareAndSwap)
6868

69+
(define (atomic-fixnum-inc! atomic . opts)
70+
(apply atomic-fixnum-add! atomic 1 opts))
71+
(define (atomic-fixnum-dec! atomic . opts)
72+
(apply atomic-fixnum-sub! atomic 1 opts))
73+
6974
)

test/tests/sagittarius/atomic.scm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,22 @@
1313
(test-assert (atomic-fixnum? (make-atomic-fixnum 100)))
1414
(test-error (make-atomic-fixnum #t))
1515

16+
(test-assert (fixnum? *memory-order:relaxed*))
17+
(test-assert (fixnum? *memory-order:consume*))
18+
(test-assert (fixnum? *memory-order:acquire*))
19+
(test-assert (fixnum? *memory-order:release*))
20+
(test-assert (fixnum? *memory-order:acq-rel*))
21+
(test-assert (fixnum? *memory-order:seq-cst*))
22+
1623
(test-group "Basic check"
1724
(let ()
1825
(define atomic-boolean (make-atomic #t))
1926
(atomic-exchange! atomic-boolean #f)
2027
(test-equal #f (atomic-load atomic-boolean))
28+
(test-error (atomic-load atomic-boolean #f))
29+
;; it's a bit depending on the platform specific value,
30+
;; I hope memory order can't be this number
31+
(test-error (atomic-load atomic-boolean #xffffffff))
2132

2233
(test-equal #t (atomic-compare-and-swap! atomic-boolean #f #t))
2334
(test-equal #t (atomic-load atomic-boolean)))
@@ -39,6 +50,10 @@
3950
(test-equal 150 (atomic-fixnum-load atomic-fixnum))
4051
(test-assert (atomic-fixnum-sub! atomic-fixnum 50))
4152
(test-equal 100 (atomic-fixnum-load atomic-fixnum))
53+
(test-assert (atomic-fixnum-inc! atomic-fixnum))
54+
(test-equal 101 (atomic-fixnum-load atomic-fixnum))
55+
(test-assert (atomic-fixnum-dec! atomic-fixnum))
56+
(test-equal 100 (atomic-fixnum-load atomic-fixnum))
4257

4358
(test-error (atomic-store! atomic-fixnum 'symbol))
4459

0 commit comments

Comments
 (0)