Activity objects of Symbian OS

Source: Internet
Author: User
Activity objects of Symbian OSI. Overview: Symbian OS is a multi-task operating system. To achieve multi-task, the system can respond quickly and process events efficiently, it also reduces the workload of application programmers (applying for most time-consuming operations (such as file systems) by the service provider. After the service provider completes the requests submitted by the programmer, will return a successful or failed Signal.), Symbian OS specifically introduces the concept of activity objects. The service provider API has asynchronous and synchronous versions of functions for your applications. Synchronization means that after a customer submits a request, the customer is in the waiting status and waits for the signal of success or failure returned by the service provider to perform other operations. asynchronous means that the request is completed, that is, before the return signal, the caller may continue to perform other processing, or simply wait. The waiting here can also be called "blocking". The signal is an event, and our code is event-driven. To implement multiple tasks, we generally use Asynchronous APIs. Generally, the operating system uses multiple threads to implement multiple tasks. Of course, Symbian also supports multiple threads. However, switching between active objects running in the same thread costs less than switching the thread context, which makes Symbian OS with tight resources, this makes the activity object more suitable for event-driven multitasking. Note:(1) The difference in speed between context switching between threads and passing control between the active objects of the same thread may be 10 times worse. In addition, A thread has a space overhead of about 4 kb in the kernel and 8 kb in the user space for the program stack. the overhead of an active object may be several hundred bytes or even smaller. (2) Although the active objects in a thread run collaboratively in non-preemptible mode, in Symbian, their threads are indeed preemptible. 2. Concept: An activity object must be derived from the base class cactiveclass cactive: Public cbase {public: Enum tpriority {epriorityidle =-100; eprioritylow =-20; eprioritystandard = 0; epriorityuserinput = 10; epriorityhigh = 20;} public: import_c ~ Cactive (); import_c void cancel (); // Delete the function of the unfinished request ...... Import_c void setpriority (tint priority ority); inline tbool isactive () const ;...... Protected: import_c cactive (tint priority ority); import_c void setactive (); Virtual void docancel () = 0 ;// Two pure virtual functions, which must be implemented by the inherited class Virtual void runl () = 0;// Processing function import_c virtual tint runerror (tint aerror); Public: trequeststatus istatus; // indicates the Request status ....... PRIVATE: tbool iactive ;.......} The preceding cactive statement shows that the active object is similar to the thread, and there is a priority value during the construction to determine how they are scheduled. Generally, a standard priority eprioritystandard is provided for the active object. When the asynchronous service of the active object response is completed, an event is generated. The activity scheduler detects the event and determines the activity object corresponding to each event. Then, it calls an appropriate activity object to process the event. When an activity object processes an event and the event processing function returns to the activity scheduler, the object cannot be preemptible, That is to say, Runl () An event handler is an atomic operation.In Symbian OS, the active objects collaborate with each other to implement multiple tasks in sequence, and do not need to protect shared resources synchronously. In addition, because the active objects run in the same thread, it is easier to share the memory and objects. Although the active objects exist in the same thread, they still run independently, this is like the threads in the same process are running independently. Iii. Description of the base class cactive of the activity object: refer to the above cactive Statement 1. setactive () must be called when a request is published (), otherwise, the activity object planner ignores the completed activity object when searching for it, resulting in an error. It should be noted that in the base class cactive, there is no actual function used to publish asynchronous requests. We must compile this function ourselves, usually named startl (). 2. () is a pure virtual function, which must be implemented to provide the functions required for incomplete requests. However, you must note that you should never directly call this function. You should always use cancel (). This function calls docancel () and ensures that the required flag is set to indicate that the request has been completed. Docancel3 and () are atomic operations. When it is scheduled by the activity scheduler, no other runl () can run in the same thread until this runl () complete and return, so This method must be briefOtherwise, the user will feel that the waiting event is long and the mobile phone seems to have crashed. Runl4. If runl () Exits abnormally, call runerror () (called by the activity planner), which provides an opportunity for the activity object to handle its own errors. If the error can be handled, runerror () should return kerrnone; otherwise, it should only return the error code passed as the parameter. In this case, pass the error to the error () function of the active planner. The default behavior is to cause a serious error. 5, It is actually an encapsulated integer.Indicates the status or error code returned by the asynchronous service provider. After the activity object sends a request, The first task of the service provider isIstatusSet to krequestpendingWhen the requested service is complete, the service provider sets the value of istatus to kerrnone (if the request is successful) or error code. IstatusIV. Implementation steps of the activity object: 1. Constructing the activity object almost always requires two-stage construction, because the activity Object usually needs to be connected to their asynchronous service provider, and the connection process may fail. Select the priority that best suits the activity object. This priority only affects the order of the activity object in the list of the activity planner. In actual development, priority other than eprioritystandard is rarely used. Tags: tags (RFS & AFS, celementlist & aelementlist, tags & aobserver): cactive (eprioritystandard) {} void ccsvfileloader: constructl (const tdesc & afilename) {ifilename = afilename; user: leaveiferror (ifile. open (IFS, ifilename, efileread); User: leaveiferror (itimewaster. createlocal (); cactiveschedmer: add (this); // Add the activity object to the activity planner. This statement can also be placed in the Stage 1 constructor.} use the rtimer handle. And rfile link to the two required asynchronous servers. Warning you must add all activity objects to the activity planner only once. Failed to add will cause a serious error of lost requests.2. Start activity object void ccsvfileloader: Start () {tint delay = (ifilename. size () % 10) * 100000; itimewaster. after (istatus, delay); setactive (); // set iactive to the etrue} asynchronous service provider (running in another thread or process) the following two things will be done to use the signal notification thread (the thread of the active object ). (1) Increase the semaphore of the thread (this semaphore is generally not used. Only when the thread to which the activity object belongs is suspended, the thread is restarted by adding a semaphore, And the thread is suspended through the waitforanyrequest () function ()). (2) set the given trequeststatus to a value different from that of krequestpending (if everything runs well, It is likely kerrnone ). 3. () The runl function is complicated and will execute a large number of tasks: (1) determine what the next iteration should do (loading data or wasting time) (2) check the last iteration status and report the status to the observer (3) the runl () method that processes all loaded data for general active objects to complete one or more of the preceding tasks. Here, you can call start () Again, that is, you can publish a service request again. 4. The error handling activity object in runerror () allows the runl () to exit unexpectedly. The end is "L", indicating this. If the function exits unexpectedly, the error code when it exits unexpectedly will be passed to runerror (). Tint ccsvfileloader: runerror (tint aerror) {iobserver. notifyloadcompleted (aerror, * This); Return kerrnone;} the error handling here is very simple. Only the error is passed to the observer. 5. You must implement a docancel () method to delete all activity objects in an incomplete request. Void ccsvfileloader: docancel () {If (iwastingtime |! Ihavetriedtoload) {itimewaster. cancel () ;}} cactive: Cancel calls docancel () and cannot overwrite cactive: Cancel () itself (it is not a virtual function in any case ), this function has completed a lot of important work. (1) check whether the activity object is actually active. If not, it will return without doing anything. (2) Call docancel () (3) wait until the request is completed, which must be completed as much as possible (the original request may have been completed before it is deleted) (4) set iactive to false. Note: You can override the docancel () method, but you cannot override the cancel () method. This method will automatically call docancel (). In addition, we cannot directly call the docancel () method, this is also important. 6. destructor ccsvfileloader ::~ Ccsvfileloader () {cancel (); ifile. close (); itimewaster. close ();} the first step of any activity object destructor is to call cancel () to delete any unfinished requests. If you delete an activity object with an incomplete request, a serious error is returned. You must disable any handle of the asynchronous service provider to avoid resource leakage. Base cactive The Destructor automatically calls deque. () To remove the active object from the list of active schedulers.7. Start the activity Scheduler UI The framework automatically creates, installs, and starts the activity scheduler. For this reason, if you do not compress the file into .exe (Console application or Symbian OS Server) or DLL (You can skip these steps if you need to explicitly start the activity scheduler. Before starting the event SchedulerYou must complete the following steps: (1) instantiate the active scheduler (2) install it in the thread (3) create an active object and add it to the active scheduler (4) send a request void doexamplel () {cactivescheduler * scheduler = new (eleave) cactivescheduler; cleanupstack: pushl (schedhl); cactivescheduler: Install (schedhl); optional * elementengine = Principal:: newlc (* console); elementengine-> loadfromcsvfilesl (); // send the request cactivescheduler: Start (); // start the activity scheduler cleanupstack: popanddestroy (2, sched);} 5. Common activity object errors: 1. You forgot to call cactivescheduler: add () before starting the activity object () 2. setactive () is not called when an asynchronous request is published or re-published () 3. Pass the same istatus to the two service providers at the same time (therefore, there are multiple incomplete requests on the same activity object)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.