03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You can use AOs for background processing. Consider the following code sequence, which<br />

can be called from inside an AO:<br />

TRequestStatus* status = &iStatus;<br />

User::RequestComplete(status, KErrNone);<br />

SetActive();<br />

This construct bypasses the idea of a system call indicating completion of an asynchronous<br />

event – it's saying, 'Run me as soon as possible.' Using this sequence, you can get a very<br />

similar effect to using threads.<br />

Using this technique will involve some rewriting; for instance, your engine may need to<br />

become an active object, and you may need an observer class that tells the main application<br />

that the background processing has finished.<br />

Note<br />

One possible problem with converting to the AO/AS scheme is if you want to<br />

wait for input on sockets, the console, or similar. If you use synchronous<br />

calls, which wait until any input is available, then these will block the whole<br />

thread – remember that you can only switch to another active object when<br />

you exit the current RunL()call. With C++ library calls, the solution would be<br />

to use the asynchronous version of the library call, but at face value there<br />

are no asynchronous implementations within STDLIB. To help with this,<br />

there are overloaded versions of the ioctl() call with an extra<br />

TRequestStatus& parameter. These can be used in a variation on a<br />

select() call, where you can be told when data is available on a single file<br />

descriptor, or when data has been written. For more information, see the<br />

STDLIB documentation and the header file estlib.h.<br />

CPosixServer<br />

STDLIB runs quite differently depending on whether a CPosixServer object exists or not. If<br />

you need one of these, it must be created before you start to use STDLIB, or the system will<br />

get confused. If it exists, then all file and socket operations are redirected to the thread that<br />

owns the CPosixServer object. Because all files are owned by a single thread, Symbian<br />

OS is quite happy with this arrangement – indeed, it is essentially oblivious to the fact, as all<br />

it sees is the interthread communication. To create the CPosixServer object, you should<br />

use InstallPosixServerActiveObject( ) or SpawnPosixServerThread() – the<br />

latter creates a special thread for the server object, and is probably preferred. Once created,<br />

the server object is largely invisible to the program and works behind the scenes. On the<br />

other hand, if STDLIB is used without a CPosixServer object having been created, then all<br />

resources are owned by the thread that opened the file, socket, and so on, and cannot be<br />

accessed by other threads.<br />

There are disadvantages with using the POSIX server. Firstly, it adds an additional clientserver<br />

transaction each time you access a shared resource and secondly, this approach only<br />

applies to STDLIB function calls; you will need to do some extra work if your ported code<br />

calls back to C++ code.<br />

The standard recommendation is that you always try to rewrite a thread-based program<br />

using the active object technique. Because active objects are non-preemptive, there are no<br />

problems with mutual exclusion, and no problems with objects being shared. However,<br />

removal of threads completely is not always so simple.<br />

In practice, you will encounter two situations:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!