Threads

From AMC-OS Developers
Revision as of 17:57, 29 July 2020 by Kindman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Thread definition in AMC-OS

A thread is an execution unit running in the context of a specific process. It is consequently limited to the address space, code, data and execution rights of its parent process. If we refer to x86 architecture, a thread is the same as a task.

A thread is characterized by :

  • an internal thread ID (ITID) and global thread id (GTID)
  • a task state segment (TSS) containing CPU state (registers, stack, ...)
  • scheduling data (priority flag, counter of total and remaining active time...)
  • a list of events related to the thread

Threads are managed by the TSI module (Threads System Interface).

Threads System Interface (TSI)

Each process is defined by a TSI_ENTRY containing all thread information.

Thread ID (iTID and TID)

An internal thread ID (iTID) is a 16-bit unsigned integer, uniquely assigned to each thread in the whole system, with a maximum of TSI_MAX_THREADS. The global thread ID (TID) is a 32-bit unsigned integer composed of the PID in the 16 upper bits, and of the ITID in the 16 lower bits.

TSI maintains a linked list of unsorted used and free iTIDs, allowing allocation and release of iTIDs in O(1), by respectively removing or adding the iTID at the beginning of the list. Both list share the same global table called StatusTable with two initial pointers, one for the linked list of free entries (firstFreeEntry), one for the linked list of used entries (firstUsedEntry).

Internal functions for iTID management are :

  • iTID TSI_AllocateTID(TSI_ENTRY *)
  • void TSI_ReleaseTID(iTID)

To get the TSI_ENTRY for a specific iTID, an index table called TSI_QuickTable is maintained.

Task scheduling and management

Thread status

A thread can mainly be considered as :

  • inactive (stopped, waiting for external event, dying)
  • active (waiting for execution)
  • running (currently executed on a CPU)

The status of the thread can change in these situations :

  • suspension by timer (end of allowed execution time), from running to active. Handled by interrupt task KERNEL_PeriodicTimer.
  • suspension request (waiting for an external event, dying), or exception from running to inactive. Handled (finally) by task manager TASKMGR_SwitchTask
  • activation request (external event occured, new thread), from inactive to active. Handled by TSI.

Active threads list

Active threads are maintained in a circulary doubly linked list. A pointer refers to the next eligible thread for execution. The list and pointers are modified by the task manager when removing a thread from the list (suspension request), or by TSI when adding a thread to the list (activation request).

Running threads

The currently running thread is directly stored in a table indexed by CPU.

Allowed execution time and priority

The priority defines the amount of allowed contiguous execution time on a CPU. At each timer interrupt, the counter is decremented and when it reaches 0 a new thread is dispatched for execution (except if there is no other thread waiting for execution).

Periodic Timer interrupt

The periodic timer interrupt task :

  • decrements threads waiting for timeout
  • decrements running threads remaining execution time
  • for each CPU that reaches end of execution time, send an IPI to request task change and dispatching of a new thread.