Skip to content

Commit 5a1776f

Browse files
committed
hal: a stream of depth N holds N samples
The ring keeps one slot free to tell full from empty, so a stream created with depth N took only N-1 samples. Allocate N+1 slots and keep reporting N as maxdepth. Drop the +1 that histobinstream and latencybinstream added for it, and fill the halmodule stream test to 10. Found by Bertho Stultiens in #4674.
1 parent 2655e62 commit 5a1776f

5 files changed

Lines changed: 12 additions & 13 deletions

File tree

‎src/hal/components/histobinstream.comp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,8 @@ EXTRA_SETUP() {
122122

123123
int err;
124124
// One complete set is MAXBINNUMBER+2 records: the negative tail, every
125-
// bin and the positive tail. The ring buffer's usable capacity is depth-1
126-
// (one slot reserved), so add 1 to fit it.
127-
if (0 != (err = hal_stream_create(&fifo, comp_id, keys[extra_arg], MAXBINNUMBER + 2 + 1, "u")))
125+
// bin and the positive tail.
126+
if (0 != (err = hal_stream_create(&fifo, comp_id, keys[extra_arg], MAXBINNUMBER + 2, "u")))
128127
return err;
129128
// The other side should run hal_stream_attach(&fifo, comp_id, key, "u")
130129
return 0;

‎src/hal/components/latencybinstream.comp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ EXTRA_SETUP() {
101101
if (extra_arg < 0 || extra_arg >= MAX_INST || !keys[extra_arg])
102102
return -EINVAL;
103103
int err;
104-
// One complete set is 2*MAXBINNUMBER+1+2 records. The ring buffer's
105-
// usable capacity is depth-1 (one slot reserved), so add 1 to fit it.
106-
if (0 != (err = hal_stream_create(&fifo, comp_id, keys[extra_arg], 2*MAXBINNUMBER + 1 + 2 + 1, "u")))
104+
// One complete set is 2*MAXBINNUMBER+1+2 records.
105+
if (0 != (err = hal_stream_create(&fifo, comp_id, keys[extra_arg], 2*MAXBINNUMBER + 1 + 2, "u")))
107106
return err;
108107
// The other side should run hal_stream_attach(&fifo, comp_id, LBSKEY, "u")
109108
return 0;

‎src/hal/hal_lib.c‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4358,7 +4358,7 @@ typedef struct __hal_stream_shm_t {
43584358
atomic_uint in;
43594359
atomic_uint out;
43604360
unsigned this_sample;
4361-
unsigned depth;
4361+
unsigned depth; // capacity in samples; the ring has depth+1 slots
43624362
int num_pins;
43634363
atomic_uint num_overruns;
43644364
atomic_uint num_underruns;
@@ -4427,7 +4427,7 @@ int hal_stream_create(hal_stream_t *stream, int comp, int key, unsigned depth, c
44274427
if(!result) return -EINVAL;
44284428
int pin_count = result;
44294429

4430-
size_t size = sizeof(hal_stream_shm_t) + sizeof(hal_stream_data_u) * depth * (1+pin_count);
4430+
size_t size = sizeof(hal_stream_shm_t) + sizeof(hal_stream_data_u) * (depth + 1) * (1+pin_count);
44314431
result = rtapi_shmem_new(key, comp, size);
44324432
if(result < 0) return result;
44334433
stream->shmem_id = result;
@@ -4455,8 +4455,9 @@ void hal_stream_destroy(hal_stream_t *stream)
44554455

44564456
static inline unsigned hal_stream_advance(hal_stream_t *stream, unsigned n)
44574457
{
4458+
// one slot more than depth, so a full ring is not mistaken for empty
44584459
n += 1;
4459-
if(n >= stream->fifo->depth) return 0;
4460+
if(n > stream->fifo->depth) return 0;
44604461
return n;
44614462
}
44624463

@@ -4546,7 +4547,7 @@ int hal_stream_depth(hal_stream_t *stream)
45464547
unsigned out = hal_stream_atomic_load_out(stream);
45474548
unsigned in = hal_stream_atomic_load_in(stream);
45484549
if(out > in)
4549-
return stream->fifo->depth + in - out;
4550+
return stream->fifo->depth + 1 + in - out;
45504551
return in - out;
45514552
}
45524553

@@ -4633,7 +4634,7 @@ int hal_stream_attach(hal_stream_t *stream, int comp_id, int key, const char *ty
46334634
/* now use data in fifo structure to calculate proper shmem size */
46344635
unsigned depth = fifo->depth;
46354636
int pin_count = fifo->num_pins;
4636-
size_t size = sizeof(hal_stream_shm_t) + sizeof(hal_stream_data_u) * depth * (1+pin_count);
4637+
size_t size = sizeof(hal_stream_shm_t) + sizeof(hal_stream_data_u) * (depth + 1) * (1+pin_count);
46374638
/* close shmem, re-open with proper size */
46384639
rtapi_shmem_delete(shmem_id, comp_id);
46394640
shmem_id = result = rtapi_shmem_new(key, comp_id, size);

‎tests/halmodule/stream/stream_reader.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44
c = hal.component("stream_reader")
55
reader = hal.stream(c, hal.streamer_base, "bfsu")
6-
for i in range(9):
6+
for i in range(10):
77
assert reader.readable
88
assert reader.read() == ((i % 2, i, i, i))
99
assert reader.num_underruns == 0

‎tests/halmodule/stream/stream_writer.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
c = hal.component("stream_writer")
55
writer = hal.stream(c, hal.streamer_base, 10, "bfsu")
66

7-
for i in range(9):
7+
for i in range(10):
88
assert writer.writable
99
writer.write((i % 2, i, i, i))
1010
assert not writer.writable

0 commit comments

Comments
 (0)