Merge pull request #4905 from mysterywolf/rb

[ringbuffer] rt_ringbuffer_data_len采用局部变量承接,防止出现资源竞争
This commit is contained in:
Bernard Xiong 2021-08-18 19:24:13 +08:00 committed by GitHub
commit 69cd36b5d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 4 deletions

View File

@ -335,11 +335,15 @@ rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
return rb->buffer_size;
case RT_RINGBUFFER_HALFFULL:
default:
if (rb->write_index > rb->read_index)
return rb->write_index - rb->read_index;
{
rt_size_t wi = rb->write_index, ri = rb->read_index;
if (wi > ri)
return wi - ri;
else
return rb->buffer_size - (rb->read_index - rb->write_index);
};
return rb->buffer_size - (ri - wi);
}
}
}
RTM_EXPORT(rt_ringbuffer_data_len);