Skip to content

Commit f48247c

Browse files
committed
Add memory barriers to data structs that need them for ARM64
This will hopefully finally fix the automated Mac OS test failures.
1 parent 13250d7 commit f48247c

4 files changed

Lines changed: 12 additions & 0 deletions

File tree

debian/changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ libtrace4 (4.0.30-1) unstable; urgency=medium
1313
of one.
1414
* Improved robustness of ndag read methods in the (now hopefully)
1515
unlikely event that the data received on the socket is corrupt.
16+
* Resolve memory synchronization issues with certain internal
17+
data structures (sliding window, ring buffers) when running on ARM64.
1618

1719
-- Shane Alcock <shane@alcock.co.nz> Wed, 11 Feb 2026 15:58:39 +1300
1820

lib/data-struct/ring_buffer.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ DLLEXPORT void libtrace_ringbuffer_write(libtrace_ringbuffer_t *rb, void *value)
263263
/* Need an empty to start with */
264264
wait_for_empty(rb);
265265
rb->elements[rb->end] = value;
266+
__sync_synchronize();
266267
rb->end = (rb->end + 1) % rb->size;
267268
notify_full(rb);
268269
}
@@ -311,6 +312,7 @@ DLLEXPORT size_t libtrace_ringbuffer_write_bulk(libtrace_ringbuffer_t *rb,
311312
rb->elements[end] = values[i];
312313
end = (end + 1) % rb->size;
313314
}
315+
__sync_synchronize();
314316
rb->end = end;
315317
notify_full(rb);
316318
} while (i < min_nb_buffers);
@@ -350,8 +352,10 @@ DLLEXPORT void *libtrace_ringbuffer_read(libtrace_ringbuffer_t *rb)
350352

351353
/* We need a full slot */
352354
wait_for_full(rb);
355+
__sync_synchronize();
353356
value = rb->elements[rb->start];
354357
rb->start = (rb->start + 1) % rb->size;
358+
__sync_synchronize();
355359
/* Now that's an empty slot */
356360
notify_empty(rb);
357361
return value;
@@ -406,6 +410,7 @@ DLLEXPORT size_t libtrace_ringbuffer_read_bulk(libtrace_ringbuffer_t *rb,
406410
start = (start + 1) % rb->size;
407411
}
408412
rb->start = start;
413+
__sync_synchronize();
409414
/* Now that's an empty slot */
410415
notify_empty(rb);
411416
} while (i < min_nb_buffers);

lib/data-struct/simple_circular_buffer.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ DLLEXPORT int libtrace_scb_recv_sock(libtrace_scb_t *buf, int sock,
116116
if (ret < 0) {
117117
return ret;
118118
}
119+
__sync_synchronize();
119120
buf->write_offset += ret;
120121
return (buf->write_offset - buf->read_offset);
121122
}
@@ -139,6 +140,7 @@ DLLEXPORT void libtrace_scb_advance_read(libtrace_scb_t *buf, uint32_t forward)
139140
buf->read_offset -= buf->count_bytes;
140141
buf->write_offset -= buf->count_bytes;
141142
}
143+
__sync_synchronize();
142144
}
143145

144146
DLLEXPORT int libtrace_scb_set_recv_thold(libtrace_scb_t *buf, uint32_t thold)

lib/data-struct/sliding_window.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ int libtrace_slidingwindow_try_write(libtrace_slidingwindow_t *sw,
8585
if (adjusted_number < sw->size) {
8686
// Add it
8787
sw->elements[(adjusted_number + sw->start) % sw->size] = value;
88+
__sync_synchronize();
8889
return 1;
8990
} else {
9091
// Out of range don't add it
@@ -118,12 +119,14 @@ int libtrace_slidingwindow_try_read(libtrace_slidingwindow_t *sw, void **value,
118119
uint64_t *number)
119120
{
120121
if (sw->elements[sw->start]) {
122+
__sync_synchronize();
121123
*value = sw->elements[sw->start];
122124
sw->elements[sw->start] = NULL;
123125
if (number)
124126
*number = sw->start_number;
125127
++sw->start_number;
126128
sw->start = (sw->start + 1) % sw->size;
129+
__sync_synchronize();
127130
return 1;
128131
} else {
129132
return 0;

0 commit comments

Comments
 (0)