How to perform context switching in symbian
| Thu, 2007-06-21 08:00 | |
|
-------------------------------------------------------------------------------- Hi all, I have a callback function, which is called by lower layer via function pointer to notify the application when an event of interest occurs. Now problem is that - this is a blocking call. The application should return from this call within 5 msec to avoid blocking the caller for an excessively long period of time. Also Callbacks should not return an error to the caller, so if an error occurs, the application shall handle it internally. Would anyone suggest anythink??? how to handle this issue? |
|






Forum posts: 672
Create an active object that is activated as the callback is called. The AO doesn't start processing the callback immediately, so the control returns to the caller. Then when the AO is run, it can handle the callback.
The AO is implemented so that it does not use any resource (R-class) but itself completes the request, as it gets the turn from the scheduler. There are examples of this on this forum and elsewhere.
Forum posts: 1232
CIdle is a nice and easy-to-use active object derived class that implements an AO that behaves like Andreas describe.
Forum posts: 672
True, you could use CIdle. Note, though, that CIdle gets the chance to run every time no other thread (application) or AO is run, so effectively it is active is nothing else is active -- all the time. Consuming the power of the device. So if you use CIdle, remember to destroy it as long as you've done with it. Or create your own as described, that is not activated until necessary (you get the callback called) and is active only if the callback was actually called.
Forum posts: 1232
You control the CIdle with what you return from your callback.
Just return EFalse on the first call and it will not be rescheduled.
No need to destroy the CIdle, if you don't want to destroy it for resource saveing. (It only takes a few bytes memory though)
But could be nice to keep it there for the next time it is needed, to not need OOM-handling in the event callback.
I don't see any reason to re-implement it, your own active object would look and work pretty much identical.
Forum posts: 9
Thanks for ur reply.
But this will not solve my problem because after creating AO the callback still run in caller context.
Actually i want to switch thread.....is there any way to switch between threads???
Problem Description:
There are two modules A and B. Module A will send buffer data for decoding to module B which in turn send this buffer to decoder...after sending data to decoder module B send notification to module A via callback. Now module A and module B both running in different threads. Also when data is decoded module B will send decoded data to module A in the callback and should return from this call within 5 msec. Then module A will send the decoded data to the sink.
Problem is when module B send data to module A in the callback it is called in module B's thread. I want to switch to thread A and return from the callback. And then performed rest of the operation in thread A context.
Hope i explained my doubt clearly.
Forum posts: 672
Try adding User::After(100) or a similar small value in the AO, before it starts processing the callback result. This should enable the system to switch to the other thread, since this thread is now idle for a small amount of time and the turn can be given to others.
Forum posts: 3
"Try adding User::After(100) or a similar small value in the AO, before it starts processing the callback result. This should enable the system to switch to the other thread, since this thread is now idle for a small amount of time and the turn can be given to others."
Does that change thread context? I don't think so. It just suspends the current thread for certain amount of time, and the callback will still be called and executed in thread B.
Anyone has a solution for this? I have a same problem too. I'm quite new to Symbian OS. In .NET programming, there was a Invoke-method, which was used to change the thread context. It relies on the window handle, and i believe it raises some soft of event which is handled in the target thread to change context.
Forum posts: 672
You can also take a look at RThread::Open and RThread::Resume (to find and run the other thread) and RThread::Suspend to suspend a thread.
Forum posts: 3
Even if the thread in question waits for certain time and allows other threads to do their own business, it still continues (in it's turn) to execute the function it should not be executing, am I right? I cannot see how simpy waiting for a bit can make other thread to start executing a function.
I have this kind of problem because i have to use a component which is apparently not thread safe.
So I think my solution would be to send some kind of message to my application's message handling to handle completion event in it's own thread.
Forum posts: 114
Hello Priyanka,
You can use the "RMsgQueue" templated class to send data and for call backs. There are asynchronous methods available in the RMsgQueue class.
Client creates a global queue and waits for the notification for data/message availability on this queue
///Client side code somewhat looks like....RMsgQueue<TMyDataType> imsgQueue;
iMsgQueue.CreateGlobal(KNullDesC, 128); // You can use create local if both the threads are in same process.
iMsgQueue.NotifyDataAvailable(iStatus);
Server(thread2) opens the queue and sends notification/message/data on this queue whenever needed.
//Thread2 code looks something like....RMsgQueue<TMyDataType> iMsgQueue;
iMsgQueue.Open(handle); //Handle sent to this thread from thread1
....
//When data available, notify the client
TMyDataType data;
//Initialize the variable "data"
iMsgQueue.Send(data); //This will send the call back and/or data to the thread1and continues without blocking
In thread1 an AO should be implemented to handle the message notification. This guarantees that the T2 will continue its processing after call back and also the callback will run in T1's context.
Hope this helps...
Chao,
Ravi
Chao,
Raghav