.. index::
pair: thread manager; design
.. _design-thread-manager:
Thread Manager
==============
.. mps:prefix:: design.mps.thread-manager
Purpose
-------
The Thread Manager handles various thread-related functions required
by the MPS. These are:
- stack scanning;
- suspension and resumption of the mutator threads.
Context
-------
The barrier requires suspension and resumption of threads in order to
ensure that the collector has exclusive access to part of memory.
[design.mps.barrier.@@@@]
Stack scanning is provided as a service to the client. [Link?@@@@]
Overview
--------
.. c:type:: struct mps_thr_s *Thread
Each thread is represented by an object of type :c:type:`Thread`. The
:c:type:`Thread` type is implemented as an ADT. A list of :c:type:`Thread` objects
is maintained in the arena (as the :c:type:`Ring` structure
``arena->threadRing``). The :c:type:`Thread` object contains
operating-system-dependent information about the thread -- information
necessary for manipulating the thread and for scanning the thread
context. Thread "registration" adds or removes the current thread to
the :c:type:`Thread` list in the arena.
Detailed Design
---------------
Stack scan
..........
This is a module providing a stack scanning function. The scanning is
architecture and operating system dependent. Typically the function
will push the subste of the save registers (those preserved across
function calls) which may contain pointers (that is, neither
floating-point or debugging registers) and call :c:func:`TraceScanStack()`
on the appropriate range.
Thread interface
................
.. c:function:: Res ThreadRegister(Thread *threadReturn, Arena arena)
Register the current thread with the arena, allocating a new
``*threadReturn`` to point to it.
.. c:function:: void ThreadDeregister(Thread thread, Arena arena)
Remove ``thread`` from the list of threads managed by the arena and
free it.
.. c:function:: void ThreadRingSuspend(Ring threadRing)
Suspend all the threads on the list ``threadRing``, except for the
current thread.
.. c:function:: void ThreadRingResume(Ring threadRing)
Resume all the threads on the list ``threadRing``.
.. c:function:: Res ThreadScan(ScanState ss, Thread thread, void *stackBot)
Scan the stacks and root registers of ``thread``, treating each value
found as an ambiguous reference. The exact definition is operating
system and architecture dependent
Single-threaded generic implementation
......................................
In ``than.c``.
- Single threaded (calling :c:func:`ThreadRegister()` on a second thread
causes an assertion failure).
- :c:func:`ThreadRingSuspend()` and :c:func:`ThreadRingResume()` do nothing
because there are no other threads.
- :c:func:`ThreadScan()` calls :c:func:`StackScan()`.
POSIX threads implementation
............................
In ``thix.c``. See design.mps.pthreadext.
Win32 implementation
....................
In ``thw3.c``.
- Supports multiple threads.
- Structured exception style faults are expected.
- :c:func:`ThreadRingSuspend()` and :c:func:`ThreadRingResume()` loop over threads
and call Win32 API functions :c:func:`SuspendThread()` and
:c:func:`ResumeThread()`.
- :c:func:`ThreadRegister()` records information for the current thread:
- A Win32 "handle" with access flags :c:macro:`THREAD_SUSPEND_RESUME` and
:c:macro:`THREAD_GET_CONTEXT`. This handle is needed as parameter to
:c:func:`SuspendThread()` and :c:func:`ResumeThread()`.
- The Win32 :c:func:`GetCurrentThreadId()` so that the current thread may
be identified.
- Stack scanning is Win32 specific:
- :c:func:`ThreadScan()` calls :c:func:`StackScan()` if the thread is current.
:c:func:`GetThreadContext()` doesn't work on the current thread (the
context would not necessarily have the values which were in the
saved registers on entry to the MM).
- Otherwise it calls :c:func:`GetThreadContext()` to get the root
registers and the stack pointer.
- The thread's registers are dumped into a :c:macro:`CONTEXT` structure and
fixed in memory.
- Scan the stack (getting the stack pointer from ``CONTEXT.Rsp``).
Issues
------
#. Scanning after exceptions. :c:func:`StackScan()` relies on the
non-preserved registers having been pushed on the stack. If we want
to scan after a fault we must make sure that these registers are
either already stored on the stack, or, have an extra function to
do this explicitly.
#. Multiple registration. It is not clear whether a thread should be
allowed to be registered multiple times. We do not provide a
mechanism for knowing whether a thread is already registered with
an arena.