summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorGrissiom <[email protected]>2014-07-13 18:26:48 +0800
committerGrissiom <[email protected]>2014-07-14 18:45:46 +0800
commitff1eb2cbc9b7bb146cae957d15cccfb67285d57e (patch)
tree820c4d4dc047cbd6a913de93efe6b28747ed1fe2 /components
parent64a2902fa9d5a2cbf15873c1857c6909db13dd75 (diff)
ymodem: refactor _rym_read_code
Make fast path fast and slow path slow.
Diffstat (limited to 'components')
-rw-r--r--components/utilities/ymodem/ymodem.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/components/utilities/ymodem/ymodem.c b/components/utilities/ymodem/ymodem.c
index fdd1950cc0..f5ebeab832 100644
--- a/components/utilities/ymodem/ymodem.c
+++ b/components/utilities/ymodem/ymodem.c
@@ -70,18 +70,23 @@ static enum rym_code _rym_read_code(
struct rym_ctx *ctx,
rt_tick_t timeout)
{
- /* consume the available sem and read the data in buffer if possible */
- while (rt_sem_trytake(&ctx->sem) == RT_EOK)
- ;
+ /* Fast path */
if (rt_device_read(ctx->dev, 0, ctx->buf, 1) == 1)
return *ctx->buf;
- /* no data yet, wait for one */
- if (rt_sem_take(&ctx->sem, timeout) != RT_EOK)
- return RYM_CODE_NONE;
- /* read one */
- if (rt_device_read(ctx->dev, 0, ctx->buf, 1) == 1)
- return *ctx->buf;
- return RYM_CODE_NONE;
+
+ /* Slow path */
+ do {
+ rt_size_t rsz;
+
+ /* No data yet, wait for one */
+ 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 */