KasperskyOS Community Edition 1.1
[Topic libkos_threads]

KosThreadCallback()

The callback function prototype is declared in the kos/thread.h file.

typedef void KosThreadCallback(KosThreadCallbackReason reason);

/* Callback function argument */

typedef enum KosThreadCallbackReason {

KosThreadCallbackReasonCreate,

KosThreadCallbackReasonDestroy,

} KosThreadCallbackReason;

When a new thread is created, all registered callback functions will be called with the KosThreadCallbackReasonCreate argument. When the thread is terminated, they will be called with the KosThreadCallbackReasonDestroy argument.

Page top
[Topic kos_thread_callback]

KosThreadCallbackRegister()

This function is declared in the kos/thread.h file.

Retcode KosThreadCallbackRegister(KosThreadCallback *callback);

This function registers a custom callback function. When a thread is created and terminated, all registered callback functions will be called.

Page top
[Topic kos_thread_callback_register]

KosThreadCallbackUnregister()

This function is declared in the kos/thread.h file.

Retcode KosThreadCallbackUnregister(KosThreadCallback *callback);

This function deregisters the custom callback function (removes it from the list of called functions).

Page top
[Topic kos_thread_callback_unregister]

KosThreadCreate()

This function is declared in the kos/thread.h file.

Retcode KosThreadCreate(Tid *tid,

rtl_uint32_t priority,

rtl_uint32_t stackSize,

ThreadRoutine routine,

void *context,

int suspended);

This function creates a new thread.

Input parameters:

  • priority must be within the interval from 0 to 31; the following priority constants are available: ThreadPriorityLowest (0), ThreadPriorityNormal (15) and ThreadPriorityHighest (31).
  • stackSize is the size of the stack.
  • routine is the function that will be executed in the thread.
  • context is the argument that will be passed to the routine function.
  • suspended lets you create a thread in the suspended state (1 means create suspended, 0 means create not suspended).

Output parameters:

  • tid is the ID of the created thread.

Example

int main(int argc, char **argv)

{

Tid tidB;

Tid tidC;

Retcode rcB;

Retcode rcC;

static ThreadContext threadContext[] = {

{.ddi = "B", .deviceName = "/pci/bus0/dev2/fun0/DDI_B"},

{.ddi = "C", .deviceName = "/pci/bus0/dev2/fun0/DDI_C"},

};

rcB = KosThreadCreate(&tidB, ThreadPriorityNormal,

ThreadStackSizeDefault,

FbHotplugThread,

&threadContext[0], 0);

if (rcB != rcOk)

ERR("Failed to start thread %s", threadContext[0].ddi);

rcC = KosThreadCreate(&tidC, ThreadPriorityNormal,

ThreadStackSizeDefault,

FbHotplugThread,

&threadContext[1], 0);

if (rcC != rcOk)

ERR("Failed to start thread %s", threadContext[1].ddi);

/* Waiting for the threads to complete */

...

}

Page top
[Topic kos_thread_create]

KosThreadCurrentId()

This function is declared in the kos/thread.h file.

Tid KosThreadCurrentId(void);

This function requests the TID of the calling thread.

If successful, the function returns the thread ID (TID).

Page top
[Topic kos_thread_current_id]

KosThreadExit()

This function is declared in the kos/thread.h file.

void KosThreadExit(rtl_int32_t exitCode);

This function forcibly terminates the current thread with the exitCode.

Page top
[Topic kos_thread_exit]

KosThreadGetStack()

This function is declared in the kos/thread.h file.

void *KosThreadGetStack(Tid tid, rtl_uint32_t *size);

This function gets the stack of the thread with the specific tid.

Output parameter size contains the stack size.

If successful, the function returns the pointer to the beginning of the stack.

Page top
[Topic kos_thread_get_stack]

KosThreadOnce()

This function is declared in the kos/thread.h file.

typedef int KosThreadOnceState;

Retcode KosThreadOnce(KosThreadOnceState *onceControl,

void (* initRoutine) (void));

This function lets you call the defined initRoutine procedure precisely one time, even when it is called from multiple threads.

The onceControl parameter is designed to control the one-time call of the procedure.

If the procedure is successfully called, and if it was called previously, the KosThreadOnce() function returns rcOk.

Page top
[Topic kos_thread_once]

KosThreadResume()

This function is declared in the kos/thread.h file.

Retcode KosThreadResume(Tid tid);

This function resumes the thread with the identifier tid that was created in the suspended state.

If successful, the function returns rcOk.

Page top
[Topic kos_thread_resume]

KosThreadSleep()

This function is declared in the kos/thread.h file.

Retcode KosThreadSleep(rtl_uint32_t mdelay);

Suspends execution of the current thread for mdelay (in milliseconds).

If successful, the function returns rcOk.

Page top
[Topic kos_thread_sleep]

KosThreadSuspend()

This function is declared in the kos/thread.h file.

Retcode KosThreadSuspend(Tid tid);

Permanently stops the current thread without finishing it.

The tid parameter must be equal to the identifier of the current thread (a limitation of the current implementation).

If successful, the function returns rcOk.

Page top
[Topic kos_thread_suspend]

KosThreadTerminate()

This function is declared in the kos/thread.h file.

Retcode KosThreadTerminate(Tid tid, rtl_int32_t exitCode);

This function terminates the thread of the calling process. The tid parameter defines the ID of the thread.

If the tid points to the current thread, the exitCode parameter defines the thread exit code.

If successful, the function returns rcOk.

Page top
[Topic kos_thread_terminate]

KosThreadTlsGet()

This function is declared in the kos/thread.h file.

void *KosThreadTlsGet(void);

This function returns the pointer to the local storage of the thread (TLS) or RTL_NULL if there is no TLS.

Page top
[Topic kos_thread_tls_get]

KosThreadTlsSet()

This function is declared in the kos/thread.h file.

Retcode KosThreadTlsSet(void *tls);

This function defines the address of the local storage for the thread (TLS).

Input argument tls contains the TLS address.

Page top
[Topic kos_thread_tls_set]

KosThreadWait()

This function is declared in the kos/thread.h file.

int KosThreadWait(rtl_uint32_t tid, rtl_uint32_t timeout);

This function suspends execution of the current thread until termination of the thread with the identifier tid or until the timeout (in milliseconds).

The KosThreadWait() call with a zero value for timeout is analogous to the KosThreadYield() call .

If successful, the function returns rcOk. In case of timeout, it returns rcTimeout.

Page top
[Topic kos_thread_wait]

KosThreadYield()

This function is declared in the kos/thread.h file.

void KosThreadYield(void);

Passes execution of the thread that called it to the next thread.

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

Page top
[Topic kos_thread_yield]