Friday, October 24, 2008

Symbian OS Thread - Internals

-----------------------------------Symbian OS Thread Internals----------------------------------


This small code tells how to create a thread. From the example code we will explore how it works.


//code for creating thread.
RThread thread;
thread.Create(KThreadName, threadFunction, 4096, KMinHeapSize, 256*KMinHeapSize, NULL);
thread.Resume();


//This function is called when thread created successfully.
TInt threadFunction(TAny *aPtr)
{
RDebug::Printf(“Thread created successfully”);
RDebug::Printf(“You can add Trap and active scheduler support for this thread”);
}


---->RThread is the class for creating thread in the user side. Create function of the RThread class is responsible for Generating software interrupt. Before generating software interrupt, it has to construct SThreadCreateInfo8 structure object. This object will contain stack size, min heap size and max heap size and call back function for this thread creation. This call back function is called when thread created successfully.


struct SThreadCreateInfo8
{
TAny* iHandle;
TInt iType;
TThreadFunction iFunction;
TAny* iPtr;
TAny* iSupervisorStack;
TInt iSupervisorStackSize;
TAny* iUserStack;
TInt iUserStackSize;
TInt iInitialThreadPriority;
TPtrC8 iName;
TInt iTotalSize;
};


Role of Create Function:


1. Checks stack is negative or not if it is negative, it will panic.
2. Checks the minimum heap size is more than 0x100 bytes or not else it will panic.
3. Checks the max heap size is less than min heap size or not else it will panic.
4. Validates the Thread name is proper or not else it will return error.


ExecHandler::ThreadCreate


---->This function is the actual code for creating thread for our process. This will call NewThread() function of currently running DThread object’s member variable DProcess . How the kernel will know the current process’s DProcess Object? Kernel gets this information using TheCurrentThread macro this macro will contain current Thread’s DThread object. Using this, kernel creates the DThread object and kernel assigns new handle for this particular thread. All these stuff will run under critical section. So kernel should enter into critical section before doing all these stuff. once it done it should leave using NKern::ThreadEnterCS() and NKern::ThreadLeavesCS(); Finally this function returns the Handle to the caller(Exec::ThreadCreate()).


No comments: