Skip to content

Commit

Permalink
helper: reworked io_uring_put_sqe
Browse files Browse the repository at this point in the history
  • Loading branch information
YoSTEALTH committed Apr 12, 2024
1 parent 33eb14a commit db4ce19
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/liburing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dynamic_import import importer


__version__ = '2024.4.9'
__version__ = '2024.4.11'

importer(cache=True, exclude_dir=['lib', 'include'])
# - `importer()` helps this project manage all import needs. It auto scans for
Expand Down
2 changes: 1 addition & 1 deletion src/liburing/helper.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ from .lib.uring cimport *
from .queue cimport io_uring, io_uring_sqe


cpdef bool io_uring_put_sqe(io_uring ring, io_uring_sqe sqe) noexcept nogil
cpdef bool io_uring_put_sqe(io_uring ring, io_uring_sqe sqe_s) noexcept
54 changes: 37 additions & 17 deletions src/liburing/helper.pyx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
cpdef inline bool io_uring_put_sqe(io_uring ring, io_uring_sqe sqe) noexcept nogil:
cpdef inline bool io_uring_put_sqe(io_uring ring, io_uring_sqe sqe_s) noexcept:
''' Put `io_uring_sqe` into `io_uring` ring's memory.
Type
ring: io_uring
sqe: io_uring_sqe
sqe_s: io_uring_sqe
return: bool
Example
Expand All @@ -14,25 +14,45 @@ cpdef inline bool io_uring_put_sqe(io_uring ring, io_uring_sqe sqe) noexcept nog
# multiple
>>> sqe = io_uring_sqe(2)
>>> io_uring_prep_read(sqe[0], ...)
>>> io_uring_prep_read(sqe[1], ...)
>>> io_uring_prep_write(sqe[1], ...)
# back-end (single | multiple)
>>> if io_uring_put_sqe(ring, sqe):
... io_uring_submit(ring)
... ...
>>> else:
... # failed: `ring` was full, submit and try to again.
... io_uring_submit(ring)
... io_uring_put_sqe(ring, sqe)
... ...
# back-end single | multiple
>>> if io_uring_put_sqe(self.ring, sqe):
... # do stuff
# failed: `ring` was full, submit and try to again.
>>> if io_uring_put_sqe(self.ring, sqe):
... io_uring_submit(self.ring)
... if io_uring_put_sqe(self.ring, sqe):
... # do stuff
Note
- Returns `False` if queue is full. Will need to `io_uring_submit()` and try again.
- `io_uring_sqe()` will auto delete memory used for `sqe` if not referenced/reused.
'''
cdef unsigned int i, size = sizeof(__io_uring_sqe)
cdef:
__u32 i
size_t size
io_uring_sqe sqe
__io_uring_sqe* _sqe

if __io_uring_sq_space_left(ring.ptr) >= sqe.len:
for i in range(sqe.len):
memcpy(__io_uring_get_sqe(ring.ptr), &sqe.ptr[i], size)
if sqe_s.len == 0:
return True
# note:
# - its ok to submit `0` sqe thus `True`
# - this also accounts for `ptr` gotten from `io_uring_get_sqe`
# as its `len == 0` thus not try to copy memory
else:
size = sizeof(__io_uring_sqe)
if sqe_s.len == 1:
if (_sqe := __io_uring_get_sqe(ring.ptr)) is not NULL:
memcpy(_sqe, sqe_s.ptr, size)
return True
elif __io_uring_sq_space_left(ring.ptr) >= sqe_s.len:
for i in range(sqe_s.len):
if (_sqe := __io_uring_get_sqe(ring.ptr)) is NULL:
return False # is triggered when submitting `> entries` can handle!
# TODO: maybe need to set some kind of flag to continue where it was left off?
sqe = sqe_s[i]
memcpy(_sqe, sqe.ptr, size)
else:
return True

0 comments on commit db4ce19

Please sign in to comment.