Skip to content

Commit

Permalink
fixes to enable ball and nimuring
Browse files Browse the repository at this point in the history
  • Loading branch information
blackmius committed Feb 22, 2024
1 parent 3c43908 commit 590c5c5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
43 changes: 25 additions & 18 deletions src/nimuring/barrier.nim
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
from atomics import MemoryOrder
from std/atomics import MemoryOrder

{.push stackTrace:off.}
when defined(cpp):
{.emit: "#include <atomic>".}
proc atomic_load_explicit*[T](location: ptr T; order: MemoryOrder): T {.inline.} =
{.emit: ["return std::atomic_load_explicit(reinterpret_cast<const std::atomic<", T, "> *>(", location, "), ", order, ");"].}
proc atomic_store_explicit*[T](location: ptr T; desired: T; order: MemoryOrder) {.inline.} =
{.emit: ["std::atomic_store_explicit(reinterpret_cast<std::atomic<", T, "> *>(", location, "), ", desired, ", ", order, ");"].}
proc atomic_thread_fence*(order: MemoryOrder) {.inline.} =
{.emit: ["std::atomic_thread_fence(", order, ");"].}
when defined(isNimSkull):
{.push, header: "<stdatomic.h>".}
proc atomic_load_explicit*[T](location: ptr T; order: MemoryOrder): T {.importc.}
proc atomic_store_explicit*[T](location: ptr T; desired: T; order: MemoryOrder = moSequentiallyConsistent) {.importc.}
proc atomic_thread_fence*(order: MemoryOrder) {.importc: "atomic_thread_fence".}
{.pop.}
else:
{.emit: "#include <stdatomic.h>".}
proc atomic_load_explicit*[T](location: ptr T; order: MemoryOrder): T {.inline.} =
{.emit: "return atomic_load_explicit((_Atomic `T` *)(`location`), `order`);".}
proc atomic_store_explicit*[T](location: ptr T; desired: T; order: MemoryOrder) {.inline.} =
{.emit: ["atomic_store_explicit((_Atomic ", T, " *)(", location, "), ", desired, ", ", order, ");"].}
proc atomic_thread_fence*(order: MemoryOrder) {.inline.} =
{.emit: ["atomic_thread_fence(", order, ");"].}
{.pop.}
{.push stackTrace:off.}
when defined(cpp):
{.emit: "#include <atomic>".}
proc atomic_load_explicit*[T](location: ptr T; order: MemoryOrder): T {.inline.} =
{.emit: ["return std::atomic_load_explicit(reinterpret_cast<const std::atomic<", T, "> *>(", location, "), ", order, ");"].}
proc atomic_store_explicit*[T](location: ptr T; desired: T; order: MemoryOrder) {.inline.} =
{.emit: ["std::atomic_store_explicit(reinterpret_cast<std::atomic<", T, "> *>(", location, "), ", desired, ", ", order, ");"].}
proc atomic_thread_fence*(order: MemoryOrder) {.inline.} =
{.emit: ["std::atomic_thread_fence(", order, ");"].}
else:
{.emit: "#include <stdatomic.h>".}
proc atomic_load_explicit*[T](location: ptr T; order: MemoryOrder): T {.inline.} =
{.emit: "return atomic_load_explicit((_Atomic `T` *)(`location`), `order`);".}
proc atomic_store_explicit*[T](location: ptr T; desired: T; order: MemoryOrder) {.inline.} =
{.emit: ["atomic_store_explicit((_Atomic ", T, " *)(", location, "), ", desired, ", ", order, ");"].}
proc atomic_thread_fence*(order: MemoryOrder) {.inline.} =
{.emit: ["atomic_thread_fence(", order, ");"].}
{.pop.}

export MemoryOrder
9 changes: 6 additions & 3 deletions src/nimuring/queue.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ type
overflow*: ptr int
cqes: pointer

NimuringError* = object of CatchableError

const
defaultFlags: SetupFlags = {}

Expand All @@ -69,7 +71,8 @@ proc init(ring: var Ring; offset: ptr Offsets) =
ring.tail = cast[ptr uint32](ring.ring + offset.tail)
ring.mask = cast[ptr uint32](ring.ring + offset.ring_mask)
ring.entries = cast[ptr uint32](ring.ring + offset.ring_entries)
assert offset.ring_entries > 0
if offset.ring_entries <= 0:
raise newException(NimuringError, "io_uring is not initiated")

proc newRing(fd: FileHandle; offset: ptr CqringOffsets; size: uint32): CqRing =
## mmap a Cq ring from the given file-descriptor, using the size spec'd
Expand Down Expand Up @@ -110,7 +113,6 @@ template destroyImpl() {.dirty.} =
deallocShared(queue.params)

when defined(isNimSkull):
echo "heh"
proc `=destroy`(queue: var Queue) =
destroyImpl
else:
Expand All @@ -127,7 +129,8 @@ proc `=copy`(dest: var Queue; source: Queue) {.error: "Queue can has only one ow
proc isPowerOfTwo(x: int): bool = (x != 0) and ((x and (x - 1)) == 0)

proc newQueue*(sqEntries: int; flags = defaultFlags; sqThreadCpu = 0; sqThreadIdle = 0; wqFd = 0; cqEntries = 0): Queue =
assert sqEntries.isPowerOfTwo, "Entries must be in the power of two"
if not sqEntries.isPowerOfTwo:
raise newException(NimuringError, "Entries must be in the power of two")
var params = createShared(Params)
params.flags = flags
params.sqThreadCpu = sqThreadCpu.uint32
Expand Down
7 changes: 3 additions & 4 deletions tests/queue/test_entries_is_not_power_of_two.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
exitcode: 1
"""
import balls
import nimuring

discard newQueue(3)
expect NimuringError:
discard newQueue(3)
7 changes: 3 additions & 4 deletions tests/queue/test_entries_not_zero.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
discard """
exitcode: 1
"""
import balls
import nimuring

discard newQueue(0)
expect NimuringError:
discard newQueue(0)

0 comments on commit 590c5c5

Please sign in to comment.