Skip to content

Commit

Permalink
kernel: Treat aborting by main() as a fatal system error
Browse files Browse the repository at this point in the history
An application-supplied main() routine is now considered to be
essential to system operation. Thus, if main() experiences an
error that aborts the main thread a fatal system error is raised.

Note: If main() completes its work and does a standard return-
to-caller the main thread terminates normally.

Change-Id: Icc9499f13578299244a856a246ad2a7d34a72f54
Signed-off-by: Allan Stephens <[email protected]>
  • Loading branch information
ajstephens authored and benwrs committed Nov 10, 2016
1 parent 40325d2 commit 073442e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
12 changes: 5 additions & 7 deletions doc/kernel_v2/threads/system_threads.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ The kernel spawns the following system threads.

**Main thread**
This thread performs kernel initialization, then calls the application's
:cpp:func:`main()` function. If the application does not supply a
:cpp:func:`main()` function, the main thread terminates once initialization
is complete.
:cpp:func:`main()` function (if one is defined).

By default, the main thread uses the highest configured preemptible thread
priority (i.e. 0). If the kernel is not configured to support preemptible
threads, the main thread uses the lowest configured cooperative thread
priority (i.e. -1).

The main thread is an essential thread while it is performing kernel
initialization, which means a fatal system error is raised if the thread
aborts. Once initialization is complete the thread is considered
non-essential, so the termination or aborting of a :cpp:func:`main()`
function supplied by the application does not cause a fatal system error.
initialization or executing the application's :cpp:func:`main()` function;
this means a fatal system error is raised if the thread aborts. If
:cpp:func:`main()` is not defined, or if it executes and then does a normal
return, the main thread terminates normally and no error is raised.

**Idle thread**
This thread executes when there is no other work for the system to do.
Expand Down
5 changes: 3 additions & 2 deletions kernel/unified/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ static void _main(void *unused1, void *unused2, void *unused3)

_init_static_threads();

_main_thread->flags &= ~K_ESSENTIAL;

#ifdef CONFIG_BOOT_TIME_MEASUREMENT
/* record timestamp for kernel's _main() function */
extern uint64_t __main_tsc;
Expand All @@ -219,6 +217,9 @@ static void _main(void *unused1, void *unused2, void *unused3)
k_thread_priority_set(_main_thread, MDEF_MAIN_THREAD_PRIORITY);
#endif
main();

/* Terminate thread normally since it has no more work to do */
_main_thread->flags &= ~K_ESSENTIAL;
}

void __weak main(void)
Expand Down

0 comments on commit 073442e

Please sign in to comment.