Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.


From Wiki
Jump to: navigation, search
Article Metadata
Created: pzul_wisner (12 Mar 2008)
Last edited: hamishwillee (27 Jul 2012)

How to make calls to blocking Open C functions from an Active Object

It is often necessary to mix Open C with Active Objects. In particular, user interfaces are normally done with Active Objects. But if you want to call a blocking Open C function, such as the network communications functions, then you are faced with a problem. The blocking call will freeze your user interface until the call returns. If there is a non-blocking version of the call you could use a CTimer to repeatadly poll the function. But it is not battery efficient to have the periodic CPU activity involved in polling.

So you must turn to threads.

Okay.... here's pseudo code for a wrapper class around blocking calls named do_blocking_read_call and do_blocking_write_call. CMyCaller could be declared as a subclass of CBase.

The general idea is that the blocking calls are made from a new thread. The thread initializes then goes into a RSemapore::Wait. The main thread sets a command variable iCallerState and then calls RSemaphore::Signal to trigger the thread to execute the command. The two versions of RThread::Rendezvous are used together to coordinate with an Active Object.

After each Signal() the thread returns to the Wait state until the next Signal is given. A single thread with a repeating loop is used instead of creating and deleting a new thread each time.

class CMyCaller : public CBase
// usual NewL construction stuff omitted
void ConstructL();
void DoCallAsynch(TRequestStatus& aStatus)
void CallerThreadFn();
RSemaphore iRequestSignal;
RThread iCallerThread;
TInt iCallerState;

The ConstructL function launches the new thread. The thread initializes, uses Rendezvous(TInt) to release User::WaitForRequest, and then Waits for a Signal.

void CMyCaller::ConstructL()
TRequestStatus status;
iRequestSignal.CreateLocal(0,EOwnerProcess); // 0=wait for first Signal()
iCallerThread.Create(_L("FN_Caller"), caller_thread_fn, stackSize, minHeap, maxHeap, this, EOwnerProcess);
iCallerState = ECallerInitializing;
iCallerThread.Rendezvous(status); // request that status get signaled
// here you could check status for initialization failure.

The thread function(s).

TInt caller_thread_fn(TAny *aAddrSelf)
CMyCaller* self = (CMyCaller *)aAddrSelf;
return 0; // the docs don't say whether this value has any meaning
void CMyCaller::CallerThreadFn()
// NOTE: This code runs in its own thread with a different heap.
int ret;
TBuf8<8> port;
ret = init_server();
if (ret <0)
iCallerState = EInitializingFailed;
else {
iCallerState = ECallerReader;
int success;
iCallerState = ECallerReady;
switch (iCallerState) {
case ECallingReadFunction:
success = do_blocking_read_call(iSharedBuffer);
case ECallingWriteFunction:
success = do_blocking_write_call(iSharedBuffer);
if (success) RThread().Rendezvous (KErrNone);
else RThread().Rendezvous (KErrGeneral);

Now add an asynchronous function to be called from your Active Object.

// Call from an active object
void CMyCaller::DoCallAsynch(TRequestStatus& aStatus)
// Rendezvous(TRequestStatus) requests that the AO's RunL
// get called later when the thread calls Rendezvous(TInt).
// Tell the thread function which operation do execute
iCallerState = ECallingReadFunction;
// Release the thread from its Wait call.

Note that because CallerThreadFn runs in a different thread, it does not manipulate any Symbian objects from another thread. It also is important to use the version of RThread::Create that will create its own heap. Otherwise you may have multiple threads manipulating the same heap - since the heap is not thread safe it can become corrupted. Don't allocate a heap object and then pass its ownership to another thread because it may not be safe if it later gets deleted by a thread with a different heap.

In this pattern all data is passed by copying bytes into a shared buffer.

Note: It's not documented that every RThread must be given a unique name in the Create function.

This page was last modified on 27 July 2012, at 04:16.
46 page views in the last 30 days.