Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Symbian OS Explained - Effective C++ Programming For Smartphones (2005) [eng].pdf
Скачиваний:
60
Добавлен:
16.08.2013
Размер:
2.62 Mб
Скачать

10

Symbian OS Threads and Processes

Don’t disturb my circles!

Said to be the last words of Archimedes who was drawing geometric figures in the dust and became so absorbed that he snapped at a Roman soldier. The soldier became enraged, pulled out his sword and killed him

Chapters 8 and 9 discussed the role of active objects in multitasking code on Symbian OS. Active objects are preferred to threads for this role because they were designed specifically to suit the resource-limited hardware upon which Symbian OS runs. Multithreaded code has significantly higher run-time requirements compared to active objects: for example, a context switch between threads may be an order of magnitude slower than a similar switch between active objects running in the same thread.1 Threads tend to have a larger size overhead too, typically requiring 4 KB kernel-side and 8 KB user-side for the program stack, compared to active objects, which generally occupy only the size of the C++ object (often less than 1 KB).

One of the main differences between multitasking with threads and active objects is the way in which they are scheduled. Active objects multitask cooperatively within a thread and, once handling an event, an active object cannot be pre-empted by the active scheduler in which it runs. Threads on Symbian OS are scheduled pre-emptively by the kernel.

Pre-emptive scheduling of threads means that data shared by threads must be protected with access synchronization primitives such as mutexes or semaphores. However, despite this additional complexity, pre-emptive scheduling is sometimes necessary. Consider the case of two active objects, one assigned a high priority because it handles user-input events

1 A context switch between threads running in the same process requires the processor registers of the running thread to be stored, and the state of the thread replacing it to be restored. If a reschedule occurs between threads running in two separate processes, the address space accessible to the thread, the process context, must additionally be stored and restored.

152

SYMBIAN OS THREADS AND PROCESSES

and another with a lower priority, which performs increments of a longrunning task when no other active object’s event handler is running on the thread. If the event handler of the lower-priority active object happens to be running when an event for the higher-priority active object completes, it will continue to run to completion. No pre-emption will occur and the low-priority active object effectively ”holds up” the handler of the high-priority object. This can make the user interface sluggish and unresponsive.

Typically, on Symbian OS, problems of this nature are avoided by careful analysis of each active object’s event handler, to ensure that event processing is kept as short as possible (described in Chapters 8 and 9). It would generally not be regarded as reason enough to multithread the code. However, there are occasions where the use of several threads may be necessary, say to perform a task which cannot be split into short-running increments. By implementing it in a separate thread, it can run asynchronously without impacting an application’s response to user interface events.

On Symbian OS, active objects multitask cooperatively within a thread and cannot be pre-empted by the active scheduler; threads are scheduled pre-emptively by the kernel.

10.1Class RThread

On Symbian OS, the class used to manipulate threads is RThread (you’ll notice that it’s an R class, the characteristics of which are described in Chapter 1). An object of type RThread represents a handle to a thread, because the thread itself is a kernel object. An RThread object can be used to create or refer to another thread in order to manipulate it (e.g. suspend, resume, panic or kill it) and to transfer data to or from it.

The RThread class has been modified quite significantly as part of the changes made for the new hard real-time kernel delivered in releases of Symbian OS v8.0. I’ll identify the main differences as I come to them. Most notably, a number of the functions are now restricted for use on threads in the current process only, whereas previous versions of Symbian OS allowed one thread to manipulate any other thread in the system, even those in other processes. The changes to Symbian OS v8.0 have been introduced to protect threads against abuse from potentially malicious code.

At the time of going to press, Symbian identifies the hard real-time kernel as ”EKA2” – which is a historical reference standing for ”EPOC2

2 Symbian OS was previously known as EPOC, and earlier still, EPOC32.

CLASS RThread

153

Kernel Architecture 2” – and refers to the kernel of previous

releases

as EKA1. Throughout this chapter, and the rest of the book, I’ll use this nomenclature to distinguish between versions of Symbian OS v8.0 containing the new kernel and previous versions when discussing any API differences.

On Symbian OS v7.0, class RThread is defined as follows in e32std.h. I’ve included the entire class definition because I’ll mention many of its methods throughout this chapter.

class RThread : public RHandleBase

{

public:

inline RThread();

IMPORT_C TInt Create(const TDesC& aName, TThreadFunction aFunction, TInt aStackSize,TAny* aPtr,RLibrary* aLibrary,RHeap* aHeap, TInt aHeapMinSize,TInt aHeapMaxSize,TOwnerType aType);

IMPORT_C TInt Create(const TDesC& aName, TThreadFunction aFunction,

TInt aStackSize,TInt aHeapMinSize,TInt aHeapMaxSize,

TAny* aPtr,TOwnerType aType=EOwnerProcess);

IMPORT_C TInt Create(const TDesC& aName,TThreadFunction aFunction,

TInt aStackSize,RHeap* aHeap,TAny* aPtr,

TOwnerType aType=EOwnerProcess);

IMPORT_C TInt SetInitialParameter(TAny* aPtr);

IMPORT_C TInt Open(const TDesC& aFullName,

TOwnerType aType=EOwnerProcess);

IMPORT_C TInt Open(TThreadId aID,TOwnerType aType=EOwnerProcess); IMPORT_C TThreadId Id() const;

IMPORT_C void Resume() const;

IMPORT_C void Suspend() const;

IMPORT_C TInt Rename(const TDesC& aName) const;

IMPORT_C void Kill(TInt aReason);

IMPORT_C void Terminate(TInt aReason);

IMPORT_C void Panic(const TDesC& aCategory,TInt aReason); IMPORT_C TInt Process(RProcess& aProcess) const; IMPORT_C TThreadPriority Priority() const;

IMPORT_C void SetPriority(TThreadPriority aPriority) const; IMPORT_C TProcessPriority ProcessPriority() const;

IMPORT_C void SetProcessPriority(TProcessPriority aPriority) const; IMPORT_C TBool System() const;

IMPORT_C void SetSystem(TBool aState) const;

IMPORT_C TBool Protected() const;

IMPORT_C void SetProtected(TBool aState) const;

IMPORT_C TInt RequestCount() const;

IMPORT_C TExitType ExitType() const;

IMPORT_C TInt ExitReason() const;

IMPORT_C TExitCategoryName ExitCategory() const;

IMPORT_C void RequestComplete(TRequestStatus*& aStatus,

TInt aReason) const;

IMPORT_C TInt GetDesLength(const TAny* aPtr) const;

IMPORT_C TInt GetDesMaxLength(const TAny* aPtr) const;

IMPORT_C void ReadL(const TAny* aPtr,TDes8& aDes,

TInt anOffset) const;

IMPORT_C void ReadL(const TAny* aPtr,TDes16 &aDes,

TInt anOffset) const;

IMPORT_C void WriteL(const TAny* aPtr,const TDesC8& aDes, TInt anOffset) const;

154

SYMBIAN OS THREADS AND PROCESSES

 

IMPORT_C void WriteL(const TAny* aPtr,const TDesC16& aDes,

 

TInt anOffset) const;

IMPORT_C void Logon(TRequestStatus& aStatus) const; IMPORT_C TInt LogonCancel(TRequestStatus& aStatus) const; IMPORT_C RHeap* Heap();

IMPORT_C void HandleCount(TInt& aProcessHandleCount,

TInt& aThreadHandleCount) const;

IMPORT_C TExceptionHandler* ExceptionHandler() const; IMPORT_C TInt SetExceptionHandler(TExceptionHandler* aHandler,

TUint32 aMask);

IMPORT_C void ModifyExceptionMask(TUint32 aClearMask,

TUint32 aSetMask);

IMPORT_C TInt RaiseException(TExcType aType);

IMPORT_C TBool IsExceptionHandled(TExcType aType);

IMPORT_C void Context(TDes8& aDes) const;

IMPORT_C TInt GetRamSizes(TInt& aHeapSize,TInt& aStackSize);

IMPORT_C TInt GetCpuTime(TTimeIntervalMicroSeconds& aCpuTime)

const;

inline TInt Open(const TFindThread& aFind, TOwnerType aType=EOwnerProcess);

};

The base class of RThread is RHandleBase, which encapsulates the behavior of a generic handle and is used as a base class throughout Symbian OS to identify a handle to another object, often a kernel object. As Chapter 1 discussed, CBase must always be the base class of a C class (if only indirectly), but RHandleBase is not necessarily always the base of an R class, although you’ll find that a number of Symbian OS R classes do derive from it (e.g. RThread, RProcess, RMutex and RSessionBase). Neither does RHandleBase share the characteristics of class CBase (such as a virtual destructor and zero-initialization through an overloaded operator new). Instead, class RHandleBase encapsulates a 32-bit handle to the object its derived class represents and exports a limited number of public API methods to manipulate that handle, namely

Handle(), SetHandle(), Duplicate() and Close().

You can use the default constructor of RThread to acquire a handle to the thread your code is currently running in, which can be used as follows:

RHeap* myHeap = RThread().Heap(); // Handle to the current thread's heap

// ...or...

_LIT(KClangerPanic, "ClangerThread");

// Panic the current thread with KErrNotFound RThread().Panic(KClangerPanic, KErrNotFound);

The handle created by the default constructor is actually a pseudohandle set to the constant KCurrentThreadHandle, which is treated specially by the kernel. If you want a ”proper” handle to the current thread