Skip to content

Commit

Permalink
feat(journal): add casInt for ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Jan 7, 2022
1 parent 3c11fd4 commit 9ab2b6e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ dist-newstyle/
cabal.project.local*
bench-*
bench.*
# C
a.out
core
21 changes: 21 additions & 0 deletions src/journal/cbits/atomic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdatomic.h>

_Bool
c_atomic_compare_exchange_strong(_Atomic(int) *object, int expected, int desired) {
return atomic_compare_exchange_strong(object, &expected, desired);
}

// #include <stdio.h> // XXX: remove
// int
// main(void) {
// atomic_int acnt = 3;
// int res;
// printf("acnt = %u\n", acnt);
// res = c_atomic_compare_exchange_strong(&acnt, 3, 4);
// printf("cas(3, 4) = %d\n", res);
// res = c_atomic_compare_exchange_strong(&acnt, 2, 5);
// printf("cas(2, 5) = %d\n", res);
// res = c_atomic_compare_exchange_strong(&acnt, 4, 5);
// printf("cas(4, 5) = %d\n", res);
// printf("acnt = %u\n", acnt);
// }
2 changes: 2 additions & 0 deletions src/journal/journal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extra-source-files:
CHANGELOG.md
LICENSE
README.md
cbits/atomic.c

library
hs-source-dirs: src/
Expand Down Expand Up @@ -60,6 +61,7 @@ library
ghc-options: -O2
if os(linux)
extra-libraries: atomic
c-sources: cbits/atomic.c
default-language: Haskell2010

test-suite test
Expand Down
15 changes: 15 additions & 0 deletions src/journal/src/Journal/Internal/Atomics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Journal.Internal.Atomics where

import Foreign
import Foreign.C.Types

------------------------------------------------------------------------

Expand Down Expand Up @@ -55,3 +56,17 @@ fetchAddInt32Ptr = c_atomic_fetch_add_int_4

fetchAddInt64Ptr :: Ptr Int64 -> Int64 -> IO Int64
fetchAddInt64Ptr = c_atomic_fetch_add_int_8

------------------------------------------------------------------------

foreign import ccall unsafe "c_atomic_compare_exchange_strong"
c_atomic_compare_exchange_strong :: Ptr Int -> Int -> Int -> IO CBool

casIntPtr :: Ptr Int -> Int -> Int -> IO Bool
casIntPtr ptr expected desired = do
result <- c_atomic_compare_exchange_strong ptr expected desired
case result of
0 -> return False
1 -> return True
_ ->
error "casIntAddr: impossible, c_atomic_compare_exchange_strong should return a _Bool"
12 changes: 3 additions & 9 deletions src/journal/src/Journal/Internal/ByteBufferPtr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -455,16 +455,10 @@ writeWord32OffAddr bb offset@(I# offset#) value = do
-- indicating whether the compare and swap succeded or not. Implies a full
-- memory barrier.
casIntAddr :: ByteBuffer -> Int -> Int -> Int -> IO Bool
casIntAddr bb offset@(I# offset#) (I# old#) (I# new#) = do
casIntAddr bb offset expected desired = do
boundCheck bb offset
withForeignPtr (bbData bb) $ \(Ptr addr#) ->
IO $ \s ->
case casIntAddr# addr# offset# old# new# s of
(# s', before# #) -> case before# ==# old# of
1# -> (# s', True #)
0# -> (# s', False #)

casIntAddr# = undefined
withForeignPtr (bbData bb) $ \ptr ->
casIntPtr (ptr `plusPtr` offset) expected desired

-- | Given a bytebuffer, and offset in machine words, and a value to add,
-- atomically add the value to the element. Returns the value of the element
Expand Down

0 comments on commit 9ab2b6e

Please sign in to comment.