Aptivi - Deprecated Manuals
ProjectsWebsiteBlog
Nitrocid KS v0.1.0 Beta 3 - Manual
Nitrocid KS v0.1.0 Beta 3 - Manual
  • 👋Welcome!
  • Versions and Compatibility
    • 🔱Supported Versions
    • 🕘EOL Versions
    • 🗞️Version Release Notes
      • ✨v0.0.1.x series
      • 🌈v0.0.2.x series
      • 👥v0.0.3.x series
      • 🛠️v0.0.4.x series
      • 🕔v0.0.5.x series
        • ⌚v0.0.5.0 Beta Versions
      • 📕v0.0.6.x series
        • 📄v0.0.6.0 Beta Versions
      • 🔌v0.0.7.x series
        • ⚡v0.0.7.0 Beta Versions
      • 🌌v0.0.8.x series
      • 💌v0.0.9.x series
      • 📜v0.0.10.x series
      • ✏️v0.0.11.x series
      • ⚙️v0.0.12.x series
      • 🌐v0.0.13.x series
      • 🏃‍♀️v0.0.14.x series
      • ⏳v0.0.15.x series
      • ⭐v0.0.16.x series
      • 🌃v0.0.17.x series
      • 🥁v0.0.18.x series
      • 🔧v0.0.19.x series
      • 🍀v0.0.20.x series
      • 🧰v0.0.21.x series
      • 📱v0.0.22.x series
      • 🖥️v0.0.23.x series
      • 👾v0.0.24.x series
      • 🔮v0.1.x.x series
        • ⚠️Known issues for 0.1.0 Beta
    • 👍Compatibility Notes for KS API Revisions
      • 🔼Upgrading from API v1.0
      • 🔼Upgrading from API v1.1
      • 🔼Upgrading from API v1.2
      • 🔼Upgrading from API v1.3
      • 🔼Upgrading from API v2.0
      • 🔼Upgrading from API v2.1
      • 🔼Upgrading to API v3.0
        • ⬆️From 0.0.24.x to 0.1.0 Beta 1
        • ⬆️From 0.1.0 Beta 1 to 0.1.0 Beta 2
        • ⬆️From 0.1.0 Beta 2 to 0.1.0 Beta 3
  • Installation and Maintenance
    • 📀Installing the Kernel
      • 💻Windows
      • 🍎macOS
      • 🐧Linux
      • 📱Android
    • ⏫Upgrading the Kernel
      • 💻Windows
      • 🍎macOS
      • 🐧Linux
      • 📱Android
    • 📦Dependency Information
  • Fundamentals
    • 🌽What is the Kernel?
    • 👾What is Nitrocid KS?
    • ⭐Simulated Kernel Features
      • 🌟Extra Features
        • 🌏More Networking
          • 🗃️FTP Client
          • 🔒SFTP Client
          • 📰RSS Client
          • 🌐HTTP Client
          • 📧Mail Client
        • 🎮Games and Amusements
          • 🔤Hangman
          • 🏇BackRace
          • ☄️Meteor
          • 🎰Russian Roulette
          • ☄️ShipDuet
          • 🐍Snaker
          • 🔢Solver
          • ⌨️SpeedPress
          • 🔠Wordle
        • 🖊️More Editors
          • 🖊️JSON Editor
          • 🗄️SQL Editor
        • 🧰Common Programs
          • 📦Archive
          • ☕Caffeine
          • 🗓️Calendar
          • 🔢Calculator
          • 👥Contacts
          • 👩‍💻Git Shell
          • 🎧Music Player
          • 🗒️Notes
          • 🔌SSH Connection
          • ⏰Stopwatch and Timer
          • ☑️To-do List
          • ⚖️Unit Converter
          • ☀️Weather
        • 🪄ChatGPT Client
        • 🖼️Docking
        • 🌏Language Studio
        • 🎨Theme Studio
      • 👤Accounts
        • 👥Groups
        • 🔓Permissions
      • ✏️Editors
        • 📝Text Editor
        • 💾Hex Editor
      • 🐚Shells
        • 📄Commands List
        • 📄Addon Commands List
      • 📂Files and Folders
      • 🌎Networking
      • 🇺🇸Languages
      • 🌌Screensavers
      • 💿Bootable Simulation
  • Advanced and Power Users
    • ⚒️Building the Kernel
      • 🪟Building on Windows
      • 🍎Building on macOS
      • 🐧Building on Linux
      • 📱Building on Android
    • 🧰Kernel Modifications
      • 🏗️Build your...
        • 🧪Your Mod
        • 🪄Your Splash
      • 📉Analyzers for Mods
      • 📈Analyzer Diagnostics
        • 📉Text - NKS0001
        • 📉ConsoleBase - NKS0002
        • 📉ConsoleBase - NKS0003
        • 📉ConsoleBase - NKS0004
        • 📉ConsoleBase - NKS0005
        • 📉ConsoleBase - NKS0006
        • 📉ConsoleBase - NKS0007
        • 📉ConsoleBase - NKS0008
        • 📉ConsoleBase - NKS0009
        • 📉Files - NKS0010
        • 📉Files - NKS0011
        • 📉Files - NKS0012
        • 📉Files - NKS0013
        • 📉Files - NKS0014
        • 📉Files - NKS0015
        • 📉Files - NKS0016
        • 📉Files - NKS0017
        • 📉Files - NKS0018
        • 📉Files - NKS0019
        • 📉Files - NKS0020
        • 📉Files - NKS0021
        • 📉Files - NKS0022
        • 📉Files - NKS0023
        • 📉Kernel - NKS0024
        • 📉Kernel - NKS0025
        • 📉Kernel - NKS0026
        • 📉Kernel - NKS0027
        • 📉Kernel - NKS0031
        • 📉Kernel - NKS0032
        • 📉Kernel - NKS0033
        • 📉Kernel - NKS0037
        • 📉Kernel - NKS0038
        • 📉Kernel - NKS0039
        • 📉Kernel - NKS0040
        • 📉Kernel - NKS0041
        • 📉Kernel - NKS0042
        • 📉Kernel - NKS0043
        • 📉Kernel - NKS0052
        • 📉Kernel - NKS0053
        • 📉Languages - NKS0044
        • 📉Languages - NKS0045
        • 📉Languages - NKS0046
        • 📉Network - NKS0051
        • 📉Text - NKS0047
        • 📉Text - NKS0048
        • 📉Text - NKS0049
        • 📉Text - NKS0050
        • 📉Text - NKS0054
        • 📉Text - NKS0055
      • 🔧Kernel Modification Management
      • 📞Inter-Mod Communication
      • 📞Inter-Addon Communication
    • 🦠Diagnostics
      • 🔬Debugging
        • 🧬Local Debugging
        • 🛰️Remote Debugging
      • 🧪Testing
      • 💉Other Diagnostics
    • ⚒️Inner Workings
      • 🔧Kernel Settings
        • ⛏️Mechanics of Settings App
        • 🔩Settings Format
        • ⚙️Custom Settings
      • 🐚Shell Structure
        • ❔Help System
        • 🗜️Command Parsing
        • 🔋Command Information
        • ⏲️Command Switch Management
        • 🎚️Command Switch Information
        • ⏳Shell History
        • 📜Shell Scripting
        • 📟Shell Presets
        • 💠Extra Shell Features
      • 🌍Multilingual Kernel
        • 🌐Custom Languages
      • 🖥️Console Manipulation
        • 🧩Console Wrappers
        • 🖥️Console Screen
      • 🖲️Console Input
        • ⌨️Interactive TUI
        • 📽️Presentation System
      • ♦️Inner Essentials
        • 🪧Kernel Placeholders
        • 🔐The Permissions
        • 👥The Users
        • 📞Kernel Threads
        • 🔌Kernel Drivers
        • 🎛️Kernel Arguments
        • 📒Kernel Journaling
        • 📡Remote Procedure
        • 🗃️Nitrocid Filesystem
        • 🌃Screensaver Internals
        • 💦Splash Internals
        • 🖥️Kernel Platform
        • 🎨Theme Internals
        • ⛱️Color Internals
        • 🔏Privacy Consents
        • 🔔System Notifications
        • ✉️MAL and MOTD
        • ⌛Progress Handlers
        • ✒️Assembly Signing
        • 🎰Random Number Generation
        • 🌍Network Tools
      • 🌀Miscellaneous APIs
  • 🧱Project Dependencies
  • Report an issue
  • Source code
  • API Reference
Powered by GitBook
On this page
  • How to make your thread
  • Task manager
  • Controls
  • Thread information
  • Child threads
  • Looping until the thread stops
  • Sleeping
Edit on GitHub
  1. Advanced and Power Users
  2. Inner Workings
  3. Inner Essentials

Kernel Threads

Threading at its best!

Threads are a great way to asynchronously do things in your mod! They play a huge role on preventing the block from happening on the main thread. Single-threaded aplications usually get blocked by long operations, but threads solve this problem.

Nitrocid KS manages the threads that are created by the KernelThread instances. It allows the kernel to manipulate with these threads more efficiently, and they stop each time the kernel is requested to shut down or restart by any power management functions, either locally or remotely by RPC.

ThreadManager provides you a whole set of functions and properties to efficiently manage your threads from listing all active threads to sleeping to benchmarking the sleep function.

How to make your thread

To make your KernelThread, just call its constructor with the following parameters:

  • ThreadName

    • Thread name

  • Background

    • Whether the thread is a background thread

  • Executor

    • A function to execute in the thread

    • It can be either of the type ThreadStart or of the type ParameterizedThreadStart

You can then start the thread using the Start() function for normal threads or the Start(object) function for parameterized threads.

You can't start the kernel thread once it's stopped by Stop(false) until it's regenerated either automatically by Stop() or manually by Regen(), and you can't call Regen() before calling the Stop(false) function.

A KernelThread has the following values:

  • Name

    • Gets the name of the thread

  • IsBackground

    • Checks to see if the thread is a background thread

  • IsAlive

    • Checks to see if the kernel thread is alive

  • IsReady

    • Checks to see if the kernel thread is ready

  • IsCritical

    • Indicates that the kernel thread is critical, which means that it is essential for the kernel. Unkillable by the kernel task manager

  • IsStopping

    • Checks to see whether the kernel thread is stopping

  • ParentThread

    • If the thread is a child thread, this will return its parent. Else, it returns null.

  • ThreadId

    • Managed kernel thread ID

  • NativeThreadId

    • Native thread ID that the OS assigned

NativeThreadId property may return inaccurate numbers and can only represent the current PID of the Nitrocid process. Don't use it unless absolutely necessary.

Task manager

The task manager can be called by taskman in the normal shell. It allows you to list both the Nitrocid KS threads and the unmanaged operating system threads, and it provides you with their information.

The left pane of the task manager shows you a list of threads, and the right pane shows you the selected thread info.

Controls

  • F1

    • Kills a Nitrocid KS thread and regenerates it

  • S

    • Switches between the Nitrocid KS thread listing and the unmanaged OS thread listing

  • ESC

    • Exits the program

The F1 command to kill a selected thread can't be used to kill unmanaged OS threads.

Thread information

The selected thread information can be found on the right pane of your task manager. However, depending on the type of the thread you're currently at, it might show different information.

Nitrocid KS threads

The below information are shown:

  • Task name

    • The kernel thread name

  • Alive

    • Whether the kernel thread is running or not

  • Background

    • Whether the kernel thread is running in the background or not

  • Critical

    • Threads that are crucial to the kernel is usually set to True and thus can't be killed

  • Ready

    • Whether the thread is ready to be started or not

Unmanaged OS threads

The below information are shown:

  • Task ID

    • Unmanaged OS thread number assigned by the operating system

  • Privileged processor time

    • Amount of work a processor is completing while executing in privileged mode

  • User processor time

    • Amount of work a processor is completing while executing in user space

  • Total processor time

    • Total amount of work a processor is completing

  • Task state

  • Priority level

    • The unmanaged thread priority level

  • Task memory address

    • A hexadecimal representation of the memory address for an unmanaged thread assigned by the operating system that points to the thread entry point (start) in this format: 0x00000000

Child threads

Child threads are the threads that run with the parent thread and follow the parent thread's lead to perform operations together. In your thread, to add a child thread, you must call the AddChild() function regardless of whether said child thread takes parameters or not on the parent thread instance to make a new child thread and connect it to the parent thread. For example, to spawn three child threads from the parent thread, you must call the AddChild() function like this:

KernelThread thread = new("Test thread", true, KernelThreadTestData.WriteHello);
thread.AddChild("Test child thread", true, KernelThreadTestData.WriteHello);
thread.AddChild("Test child thread #2", true, KernelThreadTestData.WriteHello);
thread.AddChild("Test child thread #3", true, KernelThreadTestData.WriteHello);
thread.Start();
Thread.Sleep(3000);
thread.Stop();

Starting the parent thread will start all the child threads simultaneously, and stopping the parent thread will stop all the child threads at once.

You can also add extra child threads to the parent thread that's already running using the same function. Example code is provided below:

thread = new KernelThread("Unit test thread #5", true, KernelThreadTestHelper.WriteHelloWithAppendingChild);
thread.AddChild("Unit test child thread #1 for parent thread #5", true, KernelThreadTestHelper.WriteHelloFromAppendingChild);
thread.AddChild("Unit test child thread #2 for parent thread #5", true, KernelThreadTestHelper.WriteHelloFromAppendingChild);
thread.AddChild("Unit test child thread #3 for parent thread #5", true, KernelThreadTestHelper.WriteHelloFromAppendingChild);
thread.Start();
Thread.Sleep(1000);
thread.AddChild("Unit test additional child thread #4 for parent thread #5", true, KernelThreadTestHelper.WriteHelloFromAppendingChild);
thread.AddChild("Unit test additional child thread #5 for parent thread #5", true, KernelThreadTestHelper.WriteHelloFromAppendingChild);
thread.AddChild("Unit test additional child thread #6 for parent thread #5", true, KernelThreadTestHelper.WriteHelloFromAppendingChild);
Thread.Sleep(3000);
thread.Stop();

Looping until the thread stops

If your thread consists of an infinite loop doing something useful, like updating the timer screen, the only viable way to implement such a loop within a KernelThread instance is to put a while clause, polling the condition of (!MyThread.IsStopping). Even better, you should catch a ThreadInterruptedException in case your thread does something that takes a long time. Here's an example of how it's used in the timer update thread (excluding the actual logic inside):

TimerScreen.cs
private static void UpdateTimerElapsedDisplay()
{
    var FigletFont = FigletTools.GetFigletFont(TimerFigletFont);
    while (!TimerUpdate.IsStopping)
    {
        (...)
    }
}

Never use the IsAlive property to implement such loops, or your kernel thread will deadlock 60 seconds after it's told to stop in case the ThreadInterruptedException isn't getting caught.

Be sure to use the correct thread in which you're checking for IsStopping.

If you want to use this property, be sure that you still check for IsStopping somewhere in your logic, or at the end of your logic so that you can break out of the infinite loop with polling for the IsAlive property.

As soon as Stop() is called on your kernel thread, IsStopping will be set to true to notify your kernel threads that it's stopping and that it should take appropriate action to stop. After the thread ends, it'll be reverted to false.

Sleeping

Kernel threads can now delay operations by using one of the following Sleep() functions:

  • SleepUntilInput()

    • The thread suspends all operations until the input is detected

  • SleepUntilInput(long Time)

    • The thread suspends all operations until either the input is detected or the timeout is reached

  • SleepNoBlock(long Time)

    • Sleeps until either the time specified, or the current thread is no longer alive.

  • SleepNoBlock(long Time, Thread ThreadWork)

    • Sleeps until either the time specified, or the specified non-Nitrocid thread is no longer alive.

  • SleepNoBlock(long Time, KernelThread ThreadWork)

    • Sleeps until either the time specified, or the specified Nitrocid thread is no longer alive.

Determining the precise sleep duration

The thread manager contains these functions designed to get the total elapsed ticks, milliseconds, or time span to sleep for a specified milliseconds:

  • GetActualMilliseconds()

  • GetActualTicks()

  • GetActualTimeSpan()

When called, they contain valuable information about the time information, depending on the function used, including the amount of nanoseconds taken to sleep for the specified time in milliseconds.

Last updated 1 year ago

The thread state holding one of the values

⚒️
♦️
📞
ThreadState