ymodem: refactor _rym_read_code
Make fast path fast and slow path slow.
This commit is contained in:
parent
64a2902fa9
commit
ff1eb2cbc9
|
@ -70,18 +70,23 @@ static enum rym_code _rym_read_code(
|
||||||
struct rym_ctx *ctx,
|
struct rym_ctx *ctx,
|
||||||
rt_tick_t timeout)
|
rt_tick_t timeout)
|
||||||
{
|
{
|
||||||
/* consume the available sem and read the data in buffer if possible */
|
/* Fast path */
|
||||||
while (rt_sem_trytake(&ctx->sem) == RT_EOK)
|
|
||||||
;
|
|
||||||
if (rt_device_read(ctx->dev, 0, ctx->buf, 1) == 1)
|
if (rt_device_read(ctx->dev, 0, ctx->buf, 1) == 1)
|
||||||
return *ctx->buf;
|
return *ctx->buf;
|
||||||
/* no data yet, wait for one */
|
|
||||||
if (rt_sem_take(&ctx->sem, timeout) != RT_EOK)
|
/* Slow path */
|
||||||
return RYM_CODE_NONE;
|
do {
|
||||||
/* read one */
|
rt_size_t rsz;
|
||||||
if (rt_device_read(ctx->dev, 0, ctx->buf, 1) == 1)
|
|
||||||
return *ctx->buf;
|
/* No data yet, wait for one */
|
||||||
return RYM_CODE_NONE;
|
if (rt_sem_take(&ctx->sem, timeout) != RT_EOK)
|
||||||
|
return RYM_CODE_NONE;
|
||||||
|
|
||||||
|
/* Try to read one */
|
||||||
|
rsz = rt_device_read(ctx->dev, 0, ctx->buf, 1);
|
||||||
|
if (rsz == 1)
|
||||||
|
return *ctx->buf;
|
||||||
|
} while (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* the caller should at least alloc _RYM_STX_PKG_SZ buffer */
|
/* the caller should at least alloc _RYM_STX_PKG_SZ buffer */
|
||||||
|
|
Loading…
Reference in New Issue