slick-queue-py 1.1.0__cp311-cp311-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
slick_queue_py.py ADDED
@@ -0,0 +1,723 @@
1
+ """
2
+ Python implementation of SlickQueue-compatible shared memory queue.
3
+
4
+ This implements the same memory layout as the C++ `slick::SlickQueue<T>`
5
+ header (64 bytes), an array of `slot` structures starting at offset 64, and
6
+ the data array immediately after the slot array.
7
+
8
+ Multi-Producer Multi-Consumer Support:
9
+ - This implementation now uses atomic operations via the atomic_ops module
10
+ - On platforms with hardware 128-bit CAS support (x86-64 with CMPXCHG16B),
11
+ provides true lock-free multi-producer and multi-consumer semantics
12
+ - On other platforms, falls back to lock-based synchronization
13
+
14
+ C++/Python Interoperability:
15
+ - Python processes can produce/consume to queues created by C++
16
+ - C++ processes can produce/consume to queues created by Python
17
+ - Memory layout and atomic operations match exactly
18
+
19
+ Supported on Python 3.8+ (uses multiprocessing.shared_memory).
20
+ """
21
+ from __future__ import annotations
22
+
23
+ __version__ = '1.1.0'
24
+
25
+ import struct
26
+ import sys
27
+ from typing import Optional, Tuple, Union
28
+ from atomic_ops import AtomicReservedInfo, AtomicUInt64, AtomicCursor, check_platform_support, make_reserved_info, get_index, get_size
29
+
30
+ # Use Python's built-in shared memory (available in Python 3.8+)
31
+ from multiprocessing.shared_memory import SharedMemory
32
+
33
+ # Layout constants
34
+ # Shared memory header layout (64 bytes total):
35
+ # Offset 0-7: std::atomic<reserved_info> (8 bytes)
36
+ # Offset 8-11: size_ (uint32_t)
37
+ # Offset 12-15: element_size (uint32_t)
38
+ # Offset 16-23: std::atomic<uint64_t> last_published_ (8 bytes)
39
+ # Offset 24-27: header_magic (uint32_t) - value 0x534C5131 ('SLQ1')
40
+ # Offset 28-47: PADDING (20 bytes)
41
+ # Offset 48-51: init_state (atomic uint32_t)
42
+ # Offset 52-63: PADDING (12 bytes)
43
+ HEADER_SIZE = 64
44
+ RESERVED_INFO_SIZE = struct.calcsize(AtomicReservedInfo.RESERVED_INFO_FMT) # 8 bytes
45
+ SIZE_OFFSET = 8
46
+ ELEMENT_SIZE_OFFSET = 12
47
+ LAST_PUBLISHED_OFFSET = 16
48
+ HEADER_MAGIC_OFFSET = 24
49
+ HEADER_MAGIC = 0x534C5131 # 'SLQ1' in little-endian
50
+ INIT_STATE_OFFSET = 48
51
+
52
+ # Init state constants (matches C++ queue.h)
53
+ INIT_STATE_UNINITIALIZED = 0
54
+ INIT_STATE_LEGACY = 1
55
+ INIT_STATE_INITIALIZING = 2
56
+ INIT_STATE_READY = 3
57
+
58
+ # Invalid index constant
59
+ K_INVALID_INDEX = 2**64 - 1
60
+
61
+ # slot: atomic_uint64 data_index; uint32 size; 4 bytes padding => 16 bytes
62
+ SLOT_FMT = "<Q I 4x"
63
+ SLOT_SIZE = struct.calcsize(SLOT_FMT)
64
+
65
+
66
+ class SlickQueue:
67
+ """A fixed-size ring queue compatible with C++ SlickQueue.
68
+
69
+ Supports two modes:
70
+ - **Shared memory mode** (when name is provided): Uses shared memory for inter-process communication
71
+ - **Local memory mode** (when name is None): Uses local memory (single process)
72
+
73
+ Elements are fixed-length byte blobs of `element_size`.
74
+
75
+ Args:
76
+ name: Shared memory segment name. If None, uses local memory mode.
77
+ size: Queue capacity (must be power of 2). Required when creating or using local mode.
78
+ element_size: Size of each element in bytes. Required.
79
+ create: If True, create new shared memory segment (only for shared memory mode).
80
+ """
81
+
82
+ def __init__(self, *, name: Optional[str] = None, size: Optional[int] = None, element_size: Optional[int] = None):
83
+ # Store the original user-provided name (without / prefix)
84
+ # Python's SharedMemory will add the / prefix on POSIX systems automatically.
85
+ # We strip any leading / to avoid double-prefixing (//name) on POSIX systems.
86
+ self.name = name
87
+ if self.name is not None and self.name.startswith('/'):
88
+ # Strip leading / if user provided it - Python's SharedMemory will add it back on POSIX
89
+ self.name = self.name[1:]
90
+
91
+ # macOS has a 31-character limit for POSIX shared memory names (including leading /)
92
+ # Check the length that will be used (with / prefix on POSIX systems)
93
+ if self.name is not None and sys.platform == 'darwin':
94
+ # On macOS, Python's SharedMemory will prepend /, so check total length
95
+ final_name = '/' + self.name
96
+ if len(final_name) > 31:
97
+ raise ValueError(f"Shared memory name '{final_name}' is {len(final_name)} characters, "
98
+ f"but macOS has a 31-character limit. Please use a shorter name.")
99
+
100
+ self.use_shm = name is not None
101
+ self._shm: Optional[SharedMemory] = None
102
+ self._local_buf: Optional[bytearray] = None
103
+ self.size = None
104
+ self._own = False
105
+ self._last_published_valid = False
106
+ self._atomic_last_published = None
107
+
108
+ # Validate parameters
109
+ if size is not None:
110
+ self.size = int(size)
111
+ if self.size & (self.size - 1):
112
+ raise ValueError("size must be a power of two")
113
+ self.mask = self.size - 1
114
+
115
+ if element_size is not None:
116
+ self.element_size = int(element_size)
117
+
118
+ if self.use_shm:
119
+ # Shared memory mode (C++ with shm_name != nullptr)
120
+ if self.size:
121
+ # create shared memory
122
+ if element_size is None:
123
+ raise ValueError("size and element_size required when creating")
124
+ total = HEADER_SIZE + SLOT_SIZE * self.size + self.element_size * self.size
125
+ try:
126
+ self._shm = SharedMemory(name=self.name, create=True, size=total)
127
+ # print(f"**** create new shm {self.name}")
128
+ except FileExistsError:
129
+ # print(f"**** shm already exists, opening {self.name}")
130
+ self._shm = SharedMemory(name=self.name, create=False)
131
+
132
+ # Use CAS on init_state to determine ownership (matches C++ queue.h:618-648)
133
+ buf = self._shm.buf
134
+ init_state_atomic = AtomicUInt64(buf, INIT_STATE_OFFSET)
135
+
136
+ # Try to atomically claim ownership by CAS from UNINITIALIZED to INITIALIZING
137
+ success, actual_state = init_state_atomic.compare_exchange_weak(
138
+ INIT_STATE_UNINITIALIZED, INIT_STATE_INITIALIZING
139
+ )
140
+
141
+ if success:
142
+ # We are the creator - initialize the queue (matches C++ queue.h:622-647)
143
+ self._own = True
144
+
145
+ # Write header_magic at offset 24
146
+ struct.pack_into("<I", buf, HEADER_MAGIC_OFFSET, HEADER_MAGIC)
147
+
148
+ # Initialize reserved_info atomic at offset 0
149
+ atomic_reserved = AtomicReservedInfo(buf, 0)
150
+ # This stores packed (index=0, size=0)
151
+ struct.pack_into("<Q", buf, 0, 0)
152
+
153
+ # Initialize last_published at offset 16 with kInvalidIndex
154
+ struct.pack_into("<Q", buf, LAST_PUBLISHED_OFFSET, K_INVALID_INDEX)
155
+ self._last_published_valid = True
156
+
157
+ # Write size and element_size at offsets 8 and 12
158
+ struct.pack_into("<I I", buf, SIZE_OFFSET, self.size, element_size)
159
+
160
+ # Initialize slots data_index to max (uint64 max)
161
+ for i in range(self.size):
162
+ off = HEADER_SIZE + i * SLOT_SIZE
163
+ struct.pack_into(SLOT_FMT, buf, off, K_INVALID_INDEX, 1)
164
+
165
+ # Mark initialization complete
166
+ init_state_atomic.store_release(INIT_STATE_READY)
167
+
168
+ else:
169
+ # Opened existing - wait for initialization and validate (matches C++ queue.h:649-684)
170
+ self._own = False
171
+
172
+ # Wait for initialization to complete
173
+ if not self._wait_for_shared_memory_ready(buf):
174
+ self._shm.close()
175
+ raise RuntimeError("Timed out waiting for shared memory initialization")
176
+
177
+ # Detect format version
178
+ self._last_published_valid = self._detect_format_version(buf)
179
+
180
+ # Read and validate metadata
181
+ ss = struct.unpack_from("<I I", buf, SIZE_OFFSET)
182
+ if ss[0] != self.size:
183
+ self._shm.close()
184
+ raise ValueError(f"size mismatch. Expected {self.size} but got {ss[0]}")
185
+ if ss[1] != element_size:
186
+ self._shm.close()
187
+ raise ValueError(f"element size mismatch. Expected {element_size} but got {ss[1]}")
188
+ else:
189
+ # print(f"**** open existing shm {self.name}")
190
+ # open existing and read size from header
191
+ if element_size is None:
192
+ raise ValueError("element_size must be provided when opening existing shared memory")
193
+
194
+ # Open existing shared memory (size parameter not needed/ignored)
195
+ self._shm = SharedMemory(name=self.name, create=False)
196
+ buf = self._shm.buf
197
+
198
+ # Wait for initialization to complete (matches C++ queue.h:558-562)
199
+ if not self._wait_for_shared_memory_ready(buf):
200
+ self._shm.close()
201
+ raise RuntimeError("Timed out waiting for shared memory initialization")
202
+
203
+ # Detect format version (matches C++ queue.h:564-570)
204
+ self._last_published_valid = self._detect_format_version(buf)
205
+
206
+ # Read actual queue size from header
207
+ ss = struct.unpack_from("<I I", buf, SIZE_OFFSET)
208
+ self.size = ss[0]
209
+ elem_sz = ss[1]
210
+
211
+ if element_size != elem_sz:
212
+ self._shm.close()
213
+ raise ValueError(f"SharedMemory element_size mismatch. Expecting {element_size} but got {elem_sz}")
214
+
215
+ self.mask = self.size - 1
216
+ self.element_size = int(element_size)
217
+
218
+ self._buf = self._shm.buf
219
+ self._control_offset = HEADER_SIZE
220
+ self._data_offset = HEADER_SIZE + SLOT_SIZE * self.size
221
+
222
+ # Initialize atomic wrappers for lock-free operations
223
+ self._atomic_reserved = AtomicReservedInfo(self._buf, 0)
224
+ self._atomic_slots = []
225
+ for i in range(self.size):
226
+ slot_offset = HEADER_SIZE + i * SLOT_SIZE
227
+ self._atomic_slots.append(AtomicUInt64(self._buf, slot_offset))
228
+
229
+ # Initialize last_published atomic if modern format
230
+ if self._last_published_valid:
231
+ self._atomic_last_published = AtomicUInt64(self._buf, LAST_PUBLISHED_OFFSET)
232
+ else:
233
+ # Local memory mode (C++ with shm_name == nullptr)
234
+ if size is None or element_size is None:
235
+ raise ValueError("size and element_size required for local memory mode")
236
+
237
+ # Create local buffers (equivalent to C++ new T[size_] and new slot[size_])
238
+ # We use a bytearray to simulate the memory layout
239
+ total = HEADER_SIZE + SLOT_SIZE * self.size + self.element_size * self.size
240
+ self._local_buf = bytearray(total)
241
+
242
+ # Initialize header with modern format (local mode always uses modern format)
243
+ self._local_buf[:HEADER_SIZE] = bytes(HEADER_SIZE)
244
+ # Write size at offset 8
245
+ struct.pack_into("<I I", self._local_buf, SIZE_OFFSET, self.size, element_size)
246
+ # Initialize last_published at offset 16 with kInvalidIndex
247
+ struct.pack_into("<Q", self._local_buf, LAST_PUBLISHED_OFFSET, K_INVALID_INDEX)
248
+ # Write header_magic at offset 24
249
+ struct.pack_into("<I", self._local_buf, HEADER_MAGIC_OFFSET, HEADER_MAGIC)
250
+ # Write init_state = READY at offset 48
251
+ struct.pack_into("<I", self._local_buf, INIT_STATE_OFFSET, INIT_STATE_READY)
252
+ self._last_published_valid = True
253
+
254
+ # Initialize slots data_index to max
255
+ for i in range(self.size):
256
+ off = HEADER_SIZE + i * SLOT_SIZE
257
+ struct.pack_into(SLOT_FMT, self._local_buf, off, K_INVALID_INDEX, 1)
258
+
259
+ # Create a memoryview for consistency with shared memory path
260
+ self._buf = memoryview(self._local_buf)
261
+ self._control_offset = HEADER_SIZE
262
+ self._data_offset = HEADER_SIZE + SLOT_SIZE * self.size
263
+
264
+ # Initialize atomic wrappers (these work on local memory too)
265
+ self._atomic_reserved = AtomicReservedInfo(self._buf, 0)
266
+ self._atomic_slots = []
267
+ for i in range(self.size):
268
+ slot_offset = HEADER_SIZE + i * SLOT_SIZE
269
+ self._atomic_slots.append(AtomicUInt64(self._buf, slot_offset))
270
+
271
+ # Initialize last_published atomic (local mode always uses modern format)
272
+ self._atomic_last_published = AtomicUInt64(self._buf, LAST_PUBLISHED_OFFSET)
273
+
274
+ @staticmethod
275
+ def _wait_for_shared_memory_ready(buf: memoryview) -> bool:
276
+ """
277
+ Wait for shared memory initialization to complete.
278
+ Matches C++ queue.h:510-534.
279
+
280
+ Args:
281
+ buf: Memory buffer to check
282
+
283
+ Returns:
284
+ True if initialization completed successfully, False if timed out
285
+ """
286
+ import time
287
+ init_state_atomic = AtomicUInt64(buf, INIT_STATE_OFFSET)
288
+ max_wait_ms = 2000
289
+ legacy_grace_ms = 5
290
+
291
+ for i in range(max_wait_ms):
292
+ state = init_state_atomic.load_acquire()
293
+ if state == INIT_STATE_READY:
294
+ return True
295
+
296
+ if state == INIT_STATE_LEGACY and i >= legacy_grace_ms:
297
+ # Legacy format: check if size and element_size are non-zero
298
+ ss = struct.unpack_from("<I I", buf, SIZE_OFFSET)
299
+ if ss[0] != 0 and ss[1] != 0:
300
+ return True
301
+
302
+ time.sleep(0.001)
303
+
304
+ return False
305
+
306
+ @staticmethod
307
+ def _detect_format_version(buf: memoryview) -> bool:
308
+ """
309
+ Detect if the queue uses modern format with last_published.
310
+ Matches C++ queue.h:564-570.
311
+
312
+ Args:
313
+ buf: Memory buffer to check
314
+
315
+ Returns:
316
+ True if modern format (last_published_valid), False for legacy
317
+ """
318
+ init_state_atomic = AtomicUInt64(buf, INIT_STATE_OFFSET)
319
+ state = init_state_atomic.load_acquire()
320
+ if state == INIT_STATE_READY:
321
+ magic = struct.unpack_from("<I", buf, HEADER_MAGIC_OFFSET)[0]
322
+ return magic == HEADER_MAGIC
323
+ return False
324
+
325
+ # low-level helpers
326
+ def _read_reserved(self) -> Tuple[int, int]:
327
+ buf = self._buf
328
+ packed = struct.unpack_from(AtomicReservedInfo.RESERVED_INFO_FMT, buf, 0)[0]
329
+ return get_index(packed), get_size(packed)
330
+
331
+ def _write_reserved(self, index: int, sz: int) -> None:
332
+ packed = make_reserved_info(int(index), int(sz))
333
+ struct.pack_into(AtomicReservedInfo.RESERVED_INFO_FMT, self._buf, 0, packed)
334
+
335
+ def _read_slot(self, idx: int) -> Tuple[int, int]:
336
+ off = self._control_offset + idx * SLOT_SIZE
337
+ data_index, size = struct.unpack_from(SLOT_FMT, self._buf, off)
338
+ return int(data_index), int(size)
339
+
340
+ def _write_slot(self, idx: int, data_index: int, size: int) -> None:
341
+ off = self._control_offset + idx * SLOT_SIZE
342
+ struct.pack_into(SLOT_FMT, self._buf, off, int(data_index), int(size))
343
+
344
+ def get_shm_name(self) -> Optional[str]:
345
+ """
346
+ Get the actual shared memory name for C++ interop.
347
+
348
+ Returns the name with POSIX / prefix (required by C++ shm_open).
349
+ On POSIX systems (Linux/macOS), this returns the name with the / prefix.
350
+ On Windows, it returns the name without modification.
351
+
352
+ Returns:
353
+ The shared memory name that C++ code should use to open the queue.
354
+ On POSIX systems, this will have the / prefix that shm_open() requires.
355
+ """
356
+ if self._shm is not None:
357
+ # Use the actual name from SharedMemory (which has / prefix on POSIX)
358
+ return self._shm._name
359
+ elif self.name is not None:
360
+ # If SharedMemory not created yet, construct the expected name
361
+ # On POSIX, need to add / prefix; on Windows, use as-is
362
+ if sys.platform != 'win32':
363
+ return '/' + self.name
364
+ else:
365
+ return self.name
366
+ return None
367
+
368
+ # Public API mirroring C++ methods
369
+ def reserve(self, n: int = 1) -> int:
370
+ """
371
+ Reserve space in the queue for writing (multi-producer safe).
372
+
373
+ Uses atomic CAS to safely reserve slots from multiple producers.
374
+ Matches C++ queue.h:181-213.
375
+
376
+ Args:
377
+ n: Number of slots to reserve (default 1)
378
+
379
+ Returns:
380
+ Starting index of reserved space
381
+
382
+ Raises:
383
+ RuntimeError: If n > queue size
384
+ """
385
+ if n > self.size:
386
+ raise RuntimeError(f"required size {n} > queue size {self.size}")
387
+
388
+ # CAS loop for multi-producer safety (matching C++ line 189-205)
389
+ while True:
390
+ # Load current reserved_info with memory_order_relaxed (C++ line 185)
391
+ reserved_index, reserved_size = self._atomic_reserved.load()
392
+
393
+ index = reserved_index
394
+ idx = index & self.mask
395
+ buffer_wrapped = False
396
+
397
+ # Check if we need to wrap (C++ lines 194-204)
398
+ if (idx + n) > self.size:
399
+ # Wrap to beginning
400
+ index += self.size - idx
401
+ next_index = index + n
402
+ next_size = n
403
+ buffer_wrapped = True
404
+ else:
405
+ # Normal increment
406
+ next_index = reserved_index + n
407
+ next_size = n
408
+
409
+ # Atomic CAS with memory_order_release on success (C++ line 205)
410
+ success, actual = self._atomic_reserved.compare_exchange_weak(
411
+ expected=(reserved_index, reserved_size),
412
+ desired=(next_index, next_size)
413
+ )
414
+
415
+ if success:
416
+ # CAS succeeded, we own this reservation
417
+ if buffer_wrapped:
418
+ # Publish wrap marker (C++ lines 206-211)
419
+ slot_idx = reserved_index & self.mask
420
+ self._write_slot(slot_idx, index, n)
421
+ return index
422
+
423
+ # CAS failed, retry with updated value
424
+
425
+ def publish(self, index: int, n: int = 1) -> None:
426
+ """
427
+ Publish data written to reserved space (atomic with release semantics).
428
+
429
+ Makes the data visible to consumers. Matches C++ queue.h:325-338.
430
+
431
+ Args:
432
+ index: Index returned by reserve()
433
+ n: Number of slots to publish (default 1)
434
+ """
435
+ slot_idx = index & self.mask
436
+
437
+ # Write slot size (non-atomic part)
438
+ size_offset = self._control_offset + slot_idx * SLOT_SIZE + 8
439
+ struct.pack_into("<I 4x", self._buf, size_offset, n)
440
+
441
+ # Atomic store of data_index with memory_order_release (C++ line 329)
442
+ # This ensures all data writes are visible before the index is published
443
+ self._atomic_slots[slot_idx].store_release(index)
444
+
445
+ # Update last_published if modern format (C++ lines 331-337)
446
+ if self._last_published_valid:
447
+ while True:
448
+ current = self._atomic_last_published.load_acquire()
449
+ # Only update if current is invalid or less than our index
450
+ if current != K_INVALID_INDEX and current >= index:
451
+ break
452
+ success, _ = self._atomic_last_published.compare_exchange_weak(
453
+ current, index
454
+ )
455
+ if success:
456
+ break
457
+
458
+ def __getitem__(self, index: int) -> memoryview:
459
+ off = self._data_offset + (index & self.mask) * self.element_size
460
+ return self._buf[off: off + self.element_size]
461
+
462
+ def read(self, read_index: Union[int, AtomicCursor]) -> Union[Tuple[Optional[bytes], int, int], Tuple[Optional[bytes], int, int]]:
463
+ """
464
+ Read data from the queue.
465
+
466
+ This method has two modes:
467
+ 1. Single-consumer mode: read(int) -> (data, size, new_index)
468
+ 2. Multi-consumer mode: read(AtomicCursor) -> (data, size)
469
+
470
+ Single-consumer mode (matches C++ queue.h:246-273):
471
+ Uses a plain int cursor for single-consumer scenarios.
472
+ Returns the new read_index.
473
+
474
+ Multi-consumer mode (matches C++ queue.h:283-314):
475
+ Uses an AtomicCursor for work-stealing/load-balancing across multiple consumers.
476
+ Each consumer atomically claims items, ensuring each item is consumed exactly once.
477
+
478
+ Note: Unlike C++, the single-consumer version returns the new read_index rather
479
+ than updating by reference, as Python doesn't have true pass-by-reference.
480
+
481
+ Args:
482
+ read_index: Either an int (single-consumer) or AtomicCursor (multi-consumer)
483
+
484
+ Returns:
485
+ Single-consumer: Tuple of (data_bytes or None, item_size, new_read_index)
486
+ Multi-consumer: Tuple of (data_bytes or None, item_size)
487
+ If no data available returns (None, 0) or (None, 0, read_index)
488
+
489
+ Examples:
490
+ # Single consumer
491
+ read_index = 0
492
+ data, size, read_index = q.read(read_index)
493
+
494
+ # Multi-consumer work-stealing
495
+ cursor = AtomicCursor(cursor_shm.buf, 0)
496
+ data, size, index = q.read(cursor) # Atomically claim next item
497
+ """
498
+ if isinstance(read_index, AtomicCursor):
499
+ return self._read_atomic_cursor(read_index)
500
+ else:
501
+ return self._read_single_consumer(read_index)
502
+
503
+ def _read_single_consumer(self, read_index: int) -> Tuple[Optional[bytes], int, int]:
504
+ """
505
+ Single-consumer read with atomic acquire semantics.
506
+
507
+ Matches C++ queue.h:246-273. For single-consumer use only.
508
+
509
+ Args:
510
+ read_index: Current read position
511
+
512
+ Returns:
513
+ Tuple of (data_bytes or None, item_size, new_read_index).
514
+ If no data available returns (None, 0, read_index).
515
+ """
516
+ while True:
517
+ idx = read_index & self.mask
518
+
519
+ # Atomic load with memory_order_acquire (C++ line 252)
520
+ data_index = self._atomic_slots[idx].load_acquire()
521
+
522
+ # Read slot size (non-atomic part)
523
+ size_offset = self._control_offset + idx * SLOT_SIZE + 8
524
+ slot_size = struct.unpack_from("<I", self._buf, size_offset)[0]
525
+
526
+ # Check for queue reset (C++ lines 253-256)
527
+ reserved_index, _ = self._atomic_reserved.load()
528
+ if data_index != (2**64 - 1) and reserved_index < data_index:
529
+ read_index = 0
530
+ continue
531
+
532
+ # Check if data is ready (C++ lines 258-261)
533
+ if data_index == (2**64 - 1) or data_index < read_index:
534
+ return None, 0, read_index
535
+
536
+ # Check for wrap (C++ lines 262-266)
537
+ if data_index > read_index and ((data_index & self.mask) != idx):
538
+ read_index = data_index
539
+ continue
540
+
541
+ # Read data (C++ lines 270-272)
542
+ data_off = self._data_offset + (read_index & self.mask) * self.element_size
543
+ data = bytes(self._buf[data_off: data_off + slot_size * self.element_size])
544
+ new_read_index = data_index + slot_size
545
+ return data, slot_size, new_read_index
546
+
547
+ def _read_atomic_cursor(self, read_index: AtomicCursor) -> Tuple[Optional[bytes], int, int]:
548
+ """
549
+ Multi-consumer read using a shared atomic cursor (work-stealing pattern).
550
+
551
+ Matches C++ queue.h:283-314. Multiple consumers share a single atomic cursor,
552
+ atomically claiming items to process. Each item is consumed by exactly one consumer.
553
+
554
+ Args:
555
+ read_index: Shared AtomicCursor for coordinating multiple consumers
556
+
557
+ Returns:
558
+ Tuple of (data_bytes or None, item_size, data_index).
559
+ If no data available returns (None, 0, -1).
560
+ """
561
+ if self._buf is None:
562
+ raise RuntimeError("Queue buffer is not initialized")
563
+
564
+ while True:
565
+ # Load current cursor position (C++ line 285)
566
+ current_index = read_index.load()
567
+ idx = current_index & self.mask
568
+
569
+ # Load slot data_index (C++ line 288)
570
+ data_index = self._atomic_slots[idx].load_acquire()
571
+
572
+ # Read slot size (non-atomic part)
573
+ size_offset = self._control_offset + idx * SLOT_SIZE + 8
574
+ slot_size = struct.unpack_from("<I", self._buf, size_offset)[0]
575
+
576
+ # Check for queue reset (C++ lines 290-294)
577
+ reserved_index, _ = self._atomic_reserved.load()
578
+ if data_index != (2**64 - 1) and reserved_index < data_index:
579
+ read_index.store(0)
580
+ continue
581
+
582
+ # Check if data is ready (C++ lines 296-299)
583
+ if data_index == (2**64 - 1) or data_index < current_index:
584
+ return None, 0, -1
585
+
586
+ # Check for wrap (C++ lines 300-304)
587
+ if data_index > current_index and ((data_index & self.mask) != idx):
588
+ # Try to atomically update cursor to skip wrapped slots
589
+ read_index.compare_exchange_weak(current_index, data_index)
590
+ continue
591
+
592
+ # Try to atomically claim this item (C++ lines 306-313)
593
+ next_index = data_index + slot_size
594
+ success, _ = read_index.compare_exchange_weak(current_index, next_index)
595
+
596
+ if success:
597
+ # Successfully claimed the item, read and return it
598
+ data_off = self._data_offset + (current_index & self.mask) * self.element_size
599
+ data = bytes(self._buf[data_off: data_off + slot_size * self.element_size])
600
+ return data, slot_size, current_index
601
+
602
+ # CAS failed, another consumer claimed it, retry
603
+
604
+ def read_last(self) -> Tuple[Optional[bytes], int]:
605
+ """
606
+ Read the last published data in the queue.
607
+
608
+ Matches C++ queue.h:439-458.
609
+
610
+ Returns:
611
+ Tuple of (data_bytes or None, item_size).
612
+ If no data available returns (None, 0).
613
+ """
614
+ if self._last_published_valid:
615
+ # Modern format: use last_published atomic (C++ lines 440-446)
616
+ last_index = self._atomic_last_published.load_acquire()
617
+ if last_index == K_INVALID_INDEX:
618
+ return None, 0
619
+
620
+ # Read slot size from control array
621
+ slot_idx = last_index & self.mask
622
+ size_offset = self._control_offset + slot_idx * SLOT_SIZE + 8
623
+ slot_size = struct.unpack_from("<I", self._buf, size_offset)[0]
624
+
625
+ # Read data
626
+ data_off = self._data_offset + slot_idx * self.element_size
627
+ data = bytes(self._buf[data_off: data_off + slot_size * self.element_size])
628
+ return data, slot_size
629
+ else:
630
+ # Legacy format: use reserved_info (C++ lines 449-457)
631
+ reserved_index, reserved_size = self._read_reserved()
632
+ if reserved_index == 0:
633
+ return None, 0
634
+ last_index = reserved_index - reserved_size
635
+ off = self._data_offset + (last_index & self.mask) * self.element_size
636
+ data = bytes(self._buf[off: off + reserved_size * self.element_size])
637
+ return data, reserved_size
638
+
639
+ def reset(self) -> None:
640
+ """Reset the queue to its initial state.
641
+
642
+ This is a low-level operation that should be used with caution.
643
+ It is typically used in testing or when the queue needs to be reinitialized.
644
+ Matches C++ queue.h:465-477.
645
+ """
646
+ # Reset all slots to their initial state
647
+ for i in range(self.size):
648
+ self._write_slot(i, K_INVALID_INDEX, 1)
649
+
650
+ if self.use_shm:
651
+ # Reset reserved_info to initial state
652
+ self._write_reserved(0, 0)
653
+
654
+ # Reset last_published if modern format (C++ line 473)
655
+ if self._last_published_valid:
656
+ self._atomic_last_published.store_release(K_INVALID_INDEX)
657
+
658
+ def close(self) -> None:
659
+ """Close the queue connection.
660
+
661
+ For shared memory mode: releases all references to avoid 'exported pointers exist' errors.
662
+ For local memory mode: releases local buffer.
663
+ """
664
+ try:
665
+ # Release atomic wrapper references to the buffer
666
+ if hasattr(self, '_atomic_reserved') and self._atomic_reserved:
667
+ self._atomic_reserved.release()
668
+ self._atomic_reserved = None
669
+
670
+ if hasattr(self, '_atomic_slots') and self._atomic_slots:
671
+ for slot in self._atomic_slots:
672
+ slot.release()
673
+ self._atomic_slots = None
674
+
675
+ # Release last_published atomic if it exists
676
+ if hasattr(self, '_atomic_last_published') and self._atomic_last_published:
677
+ self._atomic_last_published.release()
678
+ self._atomic_last_published = None
679
+
680
+ self._buf = None
681
+
682
+ # Close shared memory if using it
683
+ if self.use_shm and self._shm:
684
+ try:
685
+ # prevent Exception ignored in: <function SharedMemory.__del__ at 0x00000176D1BFA8E0>
686
+ self._shm._mmap = None
687
+ self._shm.close()
688
+ self._shm = None
689
+ except Exception:
690
+ pass
691
+
692
+ # Clear local buffer if using it
693
+ if not self.use_shm and self._local_buf:
694
+ self._local_buf = None
695
+ except Exception as e:
696
+ print(e)
697
+ pass
698
+
699
+ def unlink(self) -> None:
700
+ """Unlink (delete) the shared memory segment.
701
+
702
+ Only applicable for shared memory mode. Does nothing for local memory mode.
703
+ """
704
+ if not self.use_shm:
705
+ return # Nothing to unlink for local memory
706
+
707
+ try:
708
+ if self._shm:
709
+ self._shm.unlink()
710
+ except Exception:
711
+ pass
712
+
713
+ def __enter__(self):
714
+ """Context manager entry."""
715
+ return self
716
+
717
+ def __exit__(self, exc_type, exc_val, exc_tb): # noqa: U100
718
+ """Context manager exit - ensures proper cleanup."""
719
+ self.close()
720
+ return False
721
+
722
+
723
+ __all__ = ["SlickQueue", "AtomicCursor", "__version__"]