Skip to content

Commit

Permalink
Add support for delays of different durations in work queue processing
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-nutt committed Oct 10, 2014
1 parent 75e7a4d commit 16a3e83
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 67 deletions.
49 changes: 32 additions & 17 deletions include/nuttx/wqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

#include <sys/types.h>
#include <stdint.h>
#include <signal.h>
#include <semaphore.h>
#include <queue.h>

/****************************************************************************
Expand All @@ -66,11 +66,14 @@
* build (CONFIG_SCHED_KERNEL=n) but must be defined in kernel mode
* in order to build the high priority work queue.
* CONFIG_SCHED_WORKPRIORITY - The execution priority of the worker
* thread. Default: 192
* thread. Default: 224
* CONFIG_SCHED_WORKPERIOD - How often the worker thread checks for
* work in units of microseconds. Default: 50*1000 (50 MS).
* work in units of microseconds. If the high priority worker thread is
* performing garbage collection, then the default is 50*1000 (50 MS).
* Otherwise, if the lower priority worker thread is performing garbage
* collection, the default is 100*1000.
* CONFIG_SCHED_WORKSTACKSIZE - The stack size allocated for the worker
* thread. Default: CONFIG_IDLETHREAD_STACKSIZE.
* thread. Default: 2048.
* CONFIG_SIG_SIGWORK - The signal number that will be used to wake-up
* the worker thread. Default: 17
*
Expand All @@ -86,7 +89,20 @@
* CONFIG_SCHED_LPWORKPERIOD - How often the lower priority worker thread
* checks for work in units of microseconds. Default: 50*1000 (50 MS).
* CONFIG_SCHED_LPWORKSTACKSIZE - The stack size allocated for the lower
* priority worker thread. Default: CONFIG_IDLETHREAD_STACKSIZE.
* priority worker thread. Default: 2048.
*
* The user-mode work queue is only available in the protected or kernel
* builds. This those configurations, the user-mode work queue provides the
* same (non-standard) facility for use by applications.
*
* CONFIG_SCHED_USRWORK. If CONFIG_SCHED_USRWORK is also defined then the
* user-mode work queue will be created.
* CONFIG_SCHED_USRWORKPRIORITY - The minimum execution priority of the lower
* priority worker thread. Default: 100
* CONFIG_SCHED_USRWORKPERIOD - How often the lower priority worker thread
* checks for work in units of microseconds. Default: 100*1000 (100 MS).
* CONFIG_SCHED_USRWORKSTACKSIZE - The stack size allocated for the lower
* priority worker thread. Default: 2048.
*/

/* Is this a protected build (CONFIG_BUILD_PROTECTED=y) */
Expand Down Expand Up @@ -147,22 +163,20 @@

#ifdef CONFIG_SCHED_WORKQUEUE

/* We are building work queues... Work queues need signal support */

#ifdef CONFIG_DISABLE_SIGNALS
# warning "Worker thread support requires signals"
#endif

/* High priority, kernel work queue configuration ***************************/

#ifdef CONFIG_SCHED_HPWORK

# ifndef CONFIG_SCHED_WORKPRIORITY
# define CONFIG_SCHED_WORKPRIORITY 192
# define CONFIG_SCHED_WORKPRIORITY 224
# endif

# ifndef CONFIG_SCHED_WORKPERIOD
# define CONFIG_SCHED_WORKPERIOD (50*1000) /* 50 milliseconds */
# ifdef CONFIG_SCHED_LOWORK
# define CONFIG_SCHED_WORKPERIOD (100*1000) /* 100 milliseconds */
# else
# define CONFIG_SCHED_WORKPERIOD (50*1000) /* 50 milliseconds */
# endif
# endif

# ifndef CONFIG_SCHED_WORKSTACKSIZE
Expand Down Expand Up @@ -223,11 +237,11 @@
#ifdef CONFIG_SCHED_USRWORK

# ifndef CONFIG_SCHED_USRWORKPRIORITY
# define CONFIG_SCHED_USRWORKPRIORITY 50
# define CONFIG_SCHED_USRWORKPRIORITY 100
# endif

# ifndef CONFIG_SCHED_USRWORKPERIOD
# define CONFIG_SCHED_USRWORKPERIOD (50*1000) /* 50 milliseconds */
# define CONFIG_SCHED_USRWORKPERIOD (100*1000) /* 100 milliseconds */
# endif

# ifndef CONFIG_SCHED_USRWORKSTACKSIZE
Expand Down Expand Up @@ -286,8 +300,9 @@

struct wqueue_s
{
pid_t pid; /* The task ID of the worker thread */
struct dq_queue_s q; /* The queue of pending work */
uint32_t delay; /* Delay between polling cycles (ticks) */
struct dq_queue_s q; /* The queue of pending work */
pid_t pid[1]; /* The task ID of the worker thread(s) */
};

/* Defines the work callback */
Expand Down
25 changes: 15 additions & 10 deletions libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ if SCHED_HPWORK

config SCHED_WORKPRIORITY
int "High priority worker thread priority"
default 192
default 224
---help---
The execution priority of the higher priority worker thread.

Expand All @@ -440,10 +440,14 @@ config SCHED_WORKPRIORITY

config SCHED_WORKPERIOD
int "High priority worker thread period"
default 50000
default 100000 if SCHED_LPWORK
default 50000 if !SCHED_LPWORK
---help---
How often the worker thread checks for work in units of microseconds.
Default: 50*1000 (50 MS).
Default: If the high priority worker thread is performing garbage
collection, then the default is 50*1000 (50 MS). Otherwise, if the
lower priority worker thread is performing garbage collection, the
default is 100*1000.

config SCHED_WORKSTACKSIZE
int "High priority worker thread stack size"
Expand All @@ -452,6 +456,8 @@ config SCHED_WORKSTACKSIZE
---help---
The stack size allocated for the worker thread. Default: 2K.

endif # SCHED_HPWORK

config SCHED_LPWORK
bool "Low priority (kernel) worker thread"
default n
Expand Down Expand Up @@ -533,7 +539,6 @@ config SCHED_LPWORKSTACKSIZE
The stack size allocated for the lower priority worker thread. Default: 2K.

endif # SCHED_LPWORK
endif # SCHED_HPWORK

if BUILD_PROTECTED

Expand All @@ -545,20 +550,20 @@ config SCHED_USRWORK

if SCHED_USRWORK

config SCHED_LPWORKPRIORITY
config SCHED_USRWORKPRIORITY
int "User mode priority worker thread priority"
default 50
default 100
---help---
The execution priority of the lopwer priority worker thread. Default: 192

config SCHED_LPWORKPERIOD
config SCHED_USRWORKPERIOD
int "User mode worker thread period"
default 50000
default 100000
---help---
How often the lower priority worker thread checks for work in units
of microseconds. Default: 50*1000 (50 MS).
of microseconds. Default: 100*1000 (100 MS).

config SCHED_LPWORKSTACKSIZE
config SCHED_USRWORKSTACKSIZE
int "User mode worker thread stack size"
default 2048
---help---
Expand Down
34 changes: 22 additions & 12 deletions libc/wqueue/work_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@
# define WORK_CLOCK CLOCK_REALTIME
#endif

/* The work poll period is in system ticks. */

#define WORKPERIOD_TICKS (CONFIG_SCHED_WORKPERIOD / USEC_PER_TICK)
#ifndef MIN
# define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif

/****************************************************************************
* Private Type Declarations
Expand Down Expand Up @@ -121,7 +121,7 @@ void work_process(FAR struct wqueue_s *wqueue)
* we process items in the work list.
*/

next = WORKPERIOD_TICKS;
next = wqueue->delay;
flags = irqsave();

/* Get the time that we started this polling cycle in clock ticks. */
Expand Down Expand Up @@ -221,18 +221,28 @@ void work_process(FAR struct wqueue_s *wqueue)
}
}

/* There is no work to be performed now. Check if we need to delay or if we have
* already exceed the duration of the polling period.
*/
/* Get the delay (in clock ticks) since we started the sampling */

if (next < WORKPERIOD_TICKS)
elapsed = clock_systimer() - work->qtime;
if (elapsed <= wqueue->delay)
{
/* Wait awhile to check the work list. We will wait here until either
* the time elapses or until we are awakened by a signal. Interrupts
* will be re-enabled while we wait.
/* How must time would we need to delay to get to the end of the
* sampling period? The amount of time we delay should be the smaller
* of the time to the end of the sampling period and the time to the
* next work expiry.
*/

usleep(next * USEC_PER_TICK);
remaining = wqueue->delay - elapsed;
next = MIN(next, remaining);
if (next > 0)
{
/* Wait awhile to check the work list. We will wait here until
* either the time elapses or until we are awakened by a signal.
* Interrupts will be re-enabled while we wait.
*/

usleep(next * USEC_PER_TICK);
}
}

irqrestore(flags);
Expand Down
2 changes: 1 addition & 1 deletion libc/wqueue/work_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int work_qqueue(FAR struct wqueue_s *wqueue, FAR struct work_s *work,
work->qtime = clock_systimer(); /* Time work queued */

dq_addlast((FAR dq_entry_t *)work, &wqueue->q);
kill(wqueue->pid, SIGWORK); /* Wake up the worker thread */
kill(wqueue->pid[0], SIGWORK); /* Wake up the worker thread */

irqrestore(flags);
return OK;
Expand Down
2 changes: 1 addition & 1 deletion libc/wqueue/work_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int work_signal(int qid)
{
/* Signal the worker thread */

ret = kill(g_usrwork.pid, SIGWORK);
ret = kill(g_usrwork.pid[0], SIGWORK);
if (ret < 0)
{
int errcode = errno;
Expand Down
18 changes: 10 additions & 8 deletions libc/wqueue/work_usrthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <debug.h>

#include <nuttx/wqueue.h>
#include <nuttx/clock.h>

#if defined(CONFIG_SCHED_WORKQUEUE) && defined(CONFIG_SCHED_USRWORK) && \
!defined(__KERNEL__)
Expand Down Expand Up @@ -130,20 +131,21 @@ int work_usrstart(void)
{
/* Initialize work queue data structures */

g_usrwork.delay = CONFIG_SCHED_USRWORKPERIOD / USEC_PER_TICK;
dq_init(&g_usrwork.q);

/* Start a user-mode worker thread for use by applications. */

svdbg("Starting user-mode worker thread\n");

g_usrwork.pid = task_create("uwork",
CONFIG_SCHED_USRWORKPRIORITY,
CONFIG_SCHED_USRWORKSTACKSIZE,
(main_t)work_usrthread,
(FAR char * const *)NULL);
g_usrwork.pid[0] = task_create("uwork",
CONFIG_SCHED_USRWORKPRIORITY,
CONFIG_SCHED_USRWORKSTACKSIZE,
(main_t)work_usrthread,
(FAR char * const *)NULL);

DEBUGASSERT(g_usrwork.pid > 0);
if (g_usrwork.pid < 0)
DEBUGASSERT(g_usrwork.pid[0] > 0);
if (g_usrwork.pid[0] < 0)
{
int errcode = errno;
DEBUGASSERT(errcode > 0);
Expand All @@ -152,7 +154,7 @@ int work_usrstart(void)
return -errcode;
}

return g_usrwork.pid;
return g_usrwork.pid[0];
}

#endif /* CONFIG_SCHED_WORKQUEUE && CONFIG_SCHED_USRWORK && !__KERNEL__*/
16 changes: 9 additions & 7 deletions sched/wqueue/kwork_hpthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <nuttx/wqueue.h>
#include <nuttx/kthread.h>
#include <nuttx/kmalloc.h>
#include <nuttx/clock.h>

#include "wqueue/wqueue.h"

Expand Down Expand Up @@ -151,19 +152,20 @@ int work_hpstart(void)
{
/* Initialize work queue data structures */

g_hpwork.delay = CONFIG_SCHED_WORKPERIOD / USEC_PER_TICK;
dq_init(&g_hpwork.q);

/* Start the high-priority, kernel mode worker thread */

svdbg("Starting high-priority kernel worker thread\n");

g_hpwork.pid = kernel_thread(HPWORKNAME, CONFIG_SCHED_WORKPRIORITY,
CONFIG_SCHED_WORKSTACKSIZE,
(main_t)work_hpthread,
(FAR char * const *)NULL);
g_hpwork.pid[0] = kernel_thread(HPWORKNAME, CONFIG_SCHED_WORKPRIORITY,
CONFIG_SCHED_WORKSTACKSIZE,
(main_t)work_hpthread,
(FAR char * const *)NULL);

DEBUGASSERT(g_hpwork.pid > 0);
if (g_hpwork.pid < 0)
DEBUGASSERT(g_hpwork.pid[0] > 0);
if (g_hpwork.pid[0] < 0)
{
int errcode = errno;
DEBUGASSERT(errcode > 0);
Expand All @@ -172,7 +174,7 @@ int work_hpstart(void)
return -errcode;
}

return g_hpwork.pid;
return g_hpwork.pid[0];
}

#endif /* CONFIG_SCHED_WORKQUEUE && CONFIG_SCHED_HPWORK*/
4 changes: 2 additions & 2 deletions sched/wqueue/kwork_inherit.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void lpwork_boostpriority(uint8_t reqprio)
* thread from the process ID.
*/

wpid = g_lpwork.pid;
wpid = g_lpwork.pid[0];
wtcb = sched_gettcb(wpid);

/* Prevent context switches until we get the priorities right */
Expand Down Expand Up @@ -214,7 +214,7 @@ void lpwork_restorepriority(uint8_t reqprio)
* thread from the process ID.
*/

wpid = g_lpwork.pid;
wpid = g_lpwork.pid[0];
wtcb = sched_gettcb(wpid);

/* Prevent context switches until we get the priorities right */
Expand Down
Loading

0 comments on commit 16a3e83

Please sign in to comment.