+static cpu_stack_t worker_stack[TASKS][WORKER_STACK_SIZE / sizeof(cpu_stack_t)];
+
+static int prime_numbers[] =
+{
+ 1, 3, 5, 7, 11, 13, 17, 19,
+ 23, 29, 31, 37, 41, 43, 47, 53,
+};
+
+STATIC_ASSERT(TASKS <= countof(prime_numbers));
+
+static void worker(void)
+{
+ long pid = (long)proc_currentUserData();
+ long tot = prime_numbers[pid - 1];
+ unsigned int my_count = 0;
+ int i;
+
+ for (i = 0; i < tot; i++)
+ {
+ my_count++;
+ PROC_ATOMIC(kprintf("> %s[%ld] running\n", __func__, pid));
+ timer_delay(tot * DELAY);
+ }
+ done[pid - 1] = 1;
+ PROC_ATOMIC(kprintf("> %s[%ld] completed\n", __func__, pid));
+}
+
+static int worker_test(void)
+{
+ long i;
+
+ // Init the test processes
+ kputs("Run Proc test..\n");
+ for (i = 0; i < TASKS; i++)
+ {
+ sprintf(&name[i][0], "worker_%ld", i + 1);
+ proc_new_with_name(name[i], worker, (iptr_t)(i + 1),
+ WORKER_STACK_SIZE, &worker_stack[i][0]);
+ }
+ kputs("> Main: Processes started\n");
+ while (1)
+ {
+ for (i = 0; i < TASKS; i++)
+ {
+ if (!done[i])
+ break;
+ }
+ if (i == TASKS)
+ break;
+ monitor_report();
+ timer_delay(93);
+ }
+ kputs("> Main: process test finished..ok!\n");
+ return 0;
+}
+
+#if CONFIG_KERN_PREEMPT
+/* Time to run each preemptible thread (in seconds) */
+#define TIME 10
+
+static char preempt_name[TASKS][32];
+
+static cpu_atomic_t barrier[TASKS];
+static cpu_atomic_t main_barrier;
+
+static unsigned int preempt_counter[TASKS];
+static unsigned int preempt_done[TASKS];
+
+static cpu_stack_t preempt_worker_stack[TASKS][WORKER_STACK_SIZE / sizeof(cpu_stack_t)];
+
+static void preempt_worker(void)
+{
+ long pid = (long)proc_currentUserData();
+ unsigned int *my_count = &preempt_counter[pid - 1];
+ ticks_t start, stop;
+ int i;
+
+ barrier[pid - 1] = 1;
+ /* Synchronize on the main barrier */
+ while (!main_barrier)
+ proc_yield();
+ PROC_ATOMIC(kprintf("> %s[%ld] running\n", __func__, pid));
+ start = timer_clock();
+ stop = ms_to_ticks(TIME * 1000);
+ while (timer_clock() - start < stop)
+ {
+ IRQ_ASSERT_ENABLED();
+ (*my_count)++;
+ /* be sure to wrap to a value different than 0 */
+ if (UNLIKELY(*my_count == (unsigned int)~0))
+ *my_count = 1;
+ }
+ PROC_ATOMIC(kprintf("> %s[%ld] completed: (counter = %d)\n",
+ __func__, pid, *my_count));
+ for (i = 0; i < TASKS; i++)
+ if (!preempt_counter[i])
+ {
+ preempt_done[pid - 1] = TEST_FAIL;
+ return;
+ }
+ preempt_done[pid - 1] = TEST_OK;
+}
+
+static int preempt_worker_test(void)
+{
+ unsigned long score = 0;
+ long i;
+
+ // Init the test processes
+ kputs("Run Preemption test..\n");
+ for (i = 0; i < TASKS; i++)
+ {
+ sprintf(&preempt_name[i][0], "preempt_worker_%ld", i + 1);
+ proc_new_with_name(preempt_name[i], preempt_worker, (iptr_t)(i + 1),
+ WORKER_STACK_SIZE, &preempt_worker_stack[i][0]);
+ }
+ kputs("> Main: Processes created\n");
+ /* Synchronize on start */
+ while (1)
+ {
+ for (i = 0; i < TASKS; i++)
+ if (!barrier[i])
+ break;
+ if (i == TASKS)
+ break;
+ proc_yield();
+ }
+ /* Now all threads have been created, start them all */
+ main_barrier = 1;
+ MEMORY_BARRIER;
+ kputs("> Main: Processes started\n");
+ while (1)
+ {
+ for (i = 0; i < TASKS; i++)
+ {
+ if (!preempt_done[i])
+ break;
+ else if (preempt_done[i] == TEST_FAIL)
+ {
+ kputs("> Main: process test finished..fail!\n");
+ return -1;
+ }
+ }
+ if (i == TASKS)
+ break;
+ monitor_report();
+ timer_delay(1000);
+ }
+ for (i = 0; i < TASKS; i++)
+ score += preempt_counter[i];
+ kputs("> Main: process test finished..ok!\n");
+ kprintf("> Score: %lu\n", score);
+ return 0;
+}
+#endif /* CONFIG_KERN_PREEMPT */
+
+#if CONFIG_KERN_SIGNALS & CONFIG_KERN_PRI
+
+#define PROC_PRI_TEST_STACK(num) PROC_DEFINE_STACK(proc_test##num##_stack, KERN_MINSTACKSIZE);