KasperskyOS Community Edition 1.3

Managing threads (high-level API thread.h)

The API is defined in the header file sysroot-*-kos/include/kos/thread.h from the KasperskyOS SDK.

Main capabilities of the API:

  • Create, terminate, and lock threads.
  • Resume execution of locked threads.
  • Register functions that are called when creating and terminating threads.

Information about API functions is provided in the table below.

The libkos library also provides a low-level API for thread management. This API is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK. The low-level API should be used only if the high-level API does not have sufficient capabilities.

Creating threads

To create a thread, call the KosThreadCreate() or KosThreadCreateDetached() function. These functions create a standard thread with priority ranging from 0–15. (For details about standard threads, see Managing threads (low-level API thread_api.h)).

Terminating threads

When a thread is terminated, it is permanently deleted from the scheduler. A thread is terminated in the following cases:

  • The function executed by the thread is exited.

    The root function (not a nested function) performed by the thread must be exited by the return operator.

  • The KosThreadTerminate() or KosThreadExit() function is called.

    The KosThreadExit() function terminates the thread even if the function is called from a nested function instead of the root function executed by the thread.

  • The process was terminated or switched to the "frozen" state.

    For details about the "frozen" state, see Managing processes (low-level API task_api.h).

Thread exit codes are defined by the developer of the KasperskyOS-based solution. These codes should be specified in the exitCode parameter of the KosThreadTerminate() and KosThreadExit() functions, and when calling the return operator in the function executed by the thread. To get the thread exit code, call the KosThreadWait() function. If successful, this function returns the thread exit code. Otherwise, it returns -1. Therefore, thread exit codes must differ from -1 to avoid ambiguity.

Registering functions that are called when creating and terminating threads

To register a function that is called when threads of a process are created and terminated, call the KosThreadCallbackRegister() function from this process.

The registered function is called in the following cases:

  • A thread is created by calling the KosThreadCreate() function.
  • The thread that was created by calling the KosThreadCreate() function is terminated as a result of exiting a function executed by this thread.
  • The thread is terminated by calling the KosThreadExit() function.

You can register multiple functions, and each of them will be called when a thread is created and terminated. When a thread is created, the registered function is called with the KosThreadCallbackReasonCreate argument in the reason parameter. When the thread is terminated, the registered function is called with the KosThreadCallbackReasonDestroy argument in the reason parameter.

Guaranteeing that a function can only be called once

The callback function defined through the initRoutine parameter is called only when the KosThreadOnce() function is called for the first time. This does not occur on repeated calls of the KosThreadOnce() function (even from other threads), and the KosThreadOnce() function simply returns control. For example, this ensures that a driver is initialized only once when multiple software components use this driver and start its initialization irrespective of each other.

Special considerations for threads attached to interrupts

After a thread is attached to an interrupt, the thread becomes a real-time thread with a FIFO scheduler class and a priority higher than 31. (For details about attaching a thread to an interrupt, see Managing interrupt processing (irq.h). For details about the FIFO real-time thread scheduler class, see Managing threads (low-level API thread_api.h)).

Many API functions cannot be applied for threads that are attached to interrupts. These functions include KosThreadCreate(), KosThreadSuspend(), KosThreadResume(), KosThreadTerminate(), KosThreadWait(), KosThreadSleep(), and KosThreadYield().

Defining and getting the base address of the TLS for threads

A thread local storage (TLS) is process memory in which a thread can store data in isolation from other threads. The base address for TLS is defined and received by the KosThreadTlsSet() and KosThreadTlsGet() functions, respectively. These functions are intended for use by the libc library.

Getting the base address and stack limit of threads

The KosThreadGetStack() function gets the base address and stack limit of the thread. This function is intended for use by the libc library.

Freeing resources of terminated threads

Resources of a thread include its stack, context, and TCB (for details about a TCB, see Managing threads (low-level API thread_api.h)). To release thread resources after the thread terminates, call the KosThreadWait() function to wait for completion of the thread. (Exceptions to this case are the initial thread of a process and a thread created by the KosThreadCreateDetached() function.) Thread resources are also released upon termination of the process that includes these threads.

Information about API functions

thread.h functions

Function

Information about the function

KosThreadCallbackRegister()

Purpose

Registers the function that is called when creating and terminating threads of the calling process.

Parameters

  • [in] callback – pointer to the function.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadCallbackUnregister()

Purpose

Deregisters the function that is called when creating and terminating threads of the calling process.

Parameters

  • [in] callback – pointer to the function.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadCreate()

Purpose

Creates a thread.

Parameters

  • [out] handle – pointer to the thread handle.
  • [in] priority – thread priority.
  • [in,optional] stackSize – thread stack limit (in bytes), or 0 to use the default size that was defined when the process was created.
  • [in] routine – pointer to the function executed by the thread. The parameter type is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK.
  • [in,optional] context – pointer to the parameters passed to the function defined through the routine parameter, or RTL_NULL if parameters do not need to be passed to this function.
  • [in] suspended – value defining whether the thread will be created in a locked state (1) or unlocked state (0).

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadCreateDetached()

Purpose

Creates an unlocked thread and closes its handle.

Parameters

  • [in] priority – thread priority.
  • [in,optional] stackSize – thread stack limit (in bytes), or 0 to use the default size that was defined when the process was created.
  • [in] routine – pointer to the function executed by the thread. The parameter type is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK.
  • [in,optional] context – pointer to the parameters passed to the function defined through the routine parameter, or RTL_NULL if parameters do not need to be passed to this function.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadCurrentId()

Purpose

Gets the ID (TID) of the calling thread.

Parameters

N/A

Returned values

Thread ID. The ID type is defined in the header file sysroot-*-kos/include/thread/tidtype.h from the KasperskyOS SDK.

KosThreadSuspend()

Purpose

Locks the calling thread.

Parameters

  • [in] handle – thread handle.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadResume()

Purpose

Resumes execution of a locked thread.

Parameters

  • [in] handle – thread handle.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadExit()

Purpose

Terminates the calling thread.

Parameters

  • [in] exitCode – thread exit code.

Returned values

N/A

KosThreadWait()

Purpose

Locks the calling thread until the defined thread is terminated.

Parameters

  • [in] handle – thread handle.
  • [in] timeout – timeout (in milliseconds) for the termination of a thread, or INFINITE_TIMEOUT to define an unlimited timeout.

Returned values

If successful, the function returns the thread exit code, otherwise it returns -1.

KosThreadSleep()

Purpose

Locks the calling thread for the specified duration.

Parameters

  • [in] mdelay – thread lockout duration (in milliseconds).

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadYield()

Purpose

Gives up the time slice of the calling thread to the next thread in the queue.

Parameters

N/A

Returned values

N/A

Additional information

The KosThreadYield() function call is identical to the KosThreadSleep() function call with a zero value for the mdelay parameter.

KosThreadTerminate()

Purpose

Terminates a thread.

Parameters

  • [in] handle – thread handle.
  • [in] exitCode – thread exit code.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadTlsGet()

Purpose

Gets the base address of the thread local storage (TLS) for the calling thread.

Parameters

N/A

Returned values

Pointer to the TLS, or RTL_NULL if the thread does not have a TLS.

KosThreadTlsSet()

Purpose

Defines the base address of the thread local storage (TLS) for the calling thread.

Parameters

  • [in] tls – pointer to the TLS.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosThreadGetStack()

Purpose

Gets the base address and stack limit of the thread.

Parameters

  • [in] handle – thread handle.
  • [out] size – pointer to the stack limit (in bytes).

Returned values

Pointer to the thread stack.

KosThreadOnce()

Purpose

Guarantees that the defined function will be called only once.

Parameters

  • [in] onceControl – pointer to the variable that indicates whether or not the defined function was already called. This variable must be initialized with the KOS_THREAD_ONCE_INIT value.
  • [in] initRoutine – pointer to the function that must be called only one time.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.