Skip to content

Commit 01fa184

Browse files
authored
Merge pull request #3275 from BernardXiong/delayUtil
Delay until
2 parents c05d9df + 2c1f7b7 commit 01fa184

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

components/dfs/src/dfs_file.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ int dfs_file_stat(const char *path, struct stat *buf)
369369

370370
if ((fs = dfs_filesystem_lookup(fullpath)) == NULL)
371371
{
372-
LOG_E(
373-
"can't find mounted filesystem on this path:%s", fullpath);
372+
LOG_E("can't find mounted filesystem on this path:%s", fullpath);
374373
rt_free(fullpath);
375374

376375
return -ENOENT;
@@ -399,8 +398,7 @@ int dfs_file_stat(const char *path, struct stat *buf)
399398
if (fs->ops->stat == NULL)
400399
{
401400
rt_free(fullpath);
402-
LOG_E(
403-
"the filesystem didn't implement this function");
401+
LOG_E("the filesystem didn't implement this function");
404402

405403
return -ENOSYS;
406404
}
@@ -565,7 +563,7 @@ void ls(const char *pathname)
565563
}
566564
else
567565
{
568-
rt_kprintf("%-25lu\n", stat.st_size);
566+
rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
569567
}
570568
}
571569
else

components/drivers/src/pipe.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
static int pipe_fops_open(struct dfs_fd *fd)
2121
{
22+
int rc = 0;
2223
rt_device_t device;
2324
rt_pipe_t *pipe;
2425

@@ -31,6 +32,11 @@ static int pipe_fops_open(struct dfs_fd *fd)
3132
if (device->ref_count == 0)
3233
{
3334
pipe->fifo = rt_ringbuffer_create(pipe->bufsz);
35+
if (pipe->fifo == RT_NULL)
36+
{
37+
rc = -RT_ENOMEM;
38+
goto __exit;
39+
}
3440
}
3541

3642
switch (fd->flags & O_ACCMODE)
@@ -48,9 +54,10 @@ static int pipe_fops_open(struct dfs_fd *fd)
4854
}
4955
device->ref_count ++;
5056

57+
__exit:
5158
rt_mutex_release(&(pipe->lock));
5259

53-
return 0;
60+
return rc;
5461
}
5562

5663
static int pipe_fops_close(struct dfs_fd *fd)
@@ -90,7 +97,8 @@ static int pipe_fops_close(struct dfs_fd *fd)
9097

9198
if (device->ref_count == 1)
9299
{
93-
rt_ringbuffer_destroy(pipe->fifo);
100+
if (pipe->fifo != RT_NULL)
101+
rt_ringbuffer_destroy(pipe->fifo);
94102
pipe->fifo = RT_NULL;
95103
}
96104
device->ref_count --;

include/rtthread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ rt_err_t rt_thread_delete(rt_thread_t thread);
138138

139139
rt_err_t rt_thread_yield(void);
140140
rt_err_t rt_thread_delay(rt_tick_t tick);
141+
rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick);
141142
rt_err_t rt_thread_mdelay(rt_int32_t ms);
142143
rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg);
143144
rt_err_t rt_thread_suspend(rt_thread_t thread);

src/thread.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,63 @@ rt_err_t rt_thread_delay(rt_tick_t tick)
530530
}
531531
RTM_EXPORT(rt_thread_delay);
532532

533+
/**
534+
* This function will let current thread delay until (*tick + inc_tick).
535+
*
536+
* @param tick the tick of last wakeup.
537+
* @param inc_tick the increment tick
538+
*
539+
* @return RT_EOK
540+
*/
541+
rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
542+
{
543+
register rt_base_t level;
544+
struct rt_thread *thread;
545+
546+
RT_ASSERT(tick != RT_NULL);
547+
548+
/* set to current thread */
549+
thread = rt_thread_self();
550+
RT_ASSERT(thread != RT_NULL);
551+
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
552+
553+
/* disable interrupt */
554+
level = rt_hw_interrupt_disable();
555+
556+
if (rt_tick_get() - *tick < inc_tick)
557+
{
558+
*tick = rt_tick_get() - *tick + inc_tick;
559+
560+
/* suspend thread */
561+
rt_thread_suspend(thread);
562+
563+
/* reset the timeout of thread timer and start it */
564+
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, tick);
565+
rt_timer_start(&(thread->thread_timer));
566+
567+
/* enable interrupt */
568+
rt_hw_interrupt_enable(level);
569+
570+
rt_schedule();
571+
572+
/* clear error number of this thread to RT_EOK */
573+
if (thread->error == -RT_ETIMEOUT)
574+
{
575+
thread->error = RT_EOK;
576+
}
577+
}
578+
else
579+
{
580+
rt_hw_interrupt_enable(level);
581+
}
582+
583+
/* get the wakeup tick */
584+
*tick = rt_tick_get();
585+
586+
return RT_EOK;
587+
}
588+
RTM_EXPORT(rt_thread_delay_util);
589+
533590
/**
534591
* This function will let current thread delay for some milliseconds.
535592
*

0 commit comments

Comments
 (0)