evq_event.h

Go to the documentation of this file.
00001 // Sometimes one requires a way to perform callbacks in the main
00002 // thread from a worker thread, and this component implements a
00003 // mechanism for that. The approach is to store each callback in a
00004 // queue processed by the main thread. The task of this generic
00005 // callback is to call the actual, event-type specific callback.
00006 // 
00007 // This interface provides the functionality required to get such
00008 // callbacks happening in different scenarios. Note that a typical
00009 // Symbian application already comes with a thread that has the
00010 // necessary mechanisms in place, and hence this interface is
00011 // typically not required in such applications.
00012 // 
00013 // We have two variants of this interface, one intended for situations
00014 // where there are no errors that should cause the event loop to exit.
00015 // There is not much difference between the two, and hence we use the
00016 // same component and header file for both; use the
00017 // EVENT_CALLBACK_WITH_GERROR compile-time definition to indicate
00018 // which it is that you want. The EVENT_CALLBACK_WITH_GERROR==1
00019 // version provides a superset of the functionality, essentially, but
00020 // slightly less efficiently.
00021 // 
00022 // One issue here naturally is the difference in the callback APIs,
00023 // which basically require adjustments in every event source. Again,
00024 // the EVENT_CALLBACK_WITH_GERROR #define can be checked in all event
00025 // sources that must support both variants.
00026 
00027 #ifndef __EVQ_EVENT_H__
00028 #define __EVQ_EVENT_H__
00029 
00030 #include "application_config.h"
00031 
00032 #if EVENT_CALLBACK_WITH_GERROR
00033 #include <glib.h>
00034 #endif
00035 
00036 #if EVENT_CALLBACK_WITH_GERROR
00037 #define MAYBE_ERROR_RTYPE gboolean
00038 #define MAYBE_ERROR_PARAM , GError** error
00039 #define MAYBE_ERROR_SOLE_PARAM GError** error
00040 #define MAYBE_ERROR_SOLE_ARG error
00041 #define MAYBE_ERROR_INVOKE(func,args...) { if (!(func(args,error))) return FALSE; }
00042 #define MAYBE_ERROR_RETURN return TRUE;
00043 #else
00044 #define MAYBE_ERROR_RTYPE void
00045 #define MAYBE_ERROR_PARAM
00046 #define MAYBE_ERROR_SOLE_PARAM
00047 #define MAYBE_ERROR_SOLE_ARG
00048 #define MAYBE_ERROR_INVOKE(func,args...) (func(args));
00049 #define MAYBE_ERROR_RETURN return;
00050 #endif
00051 
00052 #ifdef __cplusplus
00053 extern "C" {
00054 #endif
00055 
00056 #ifdef __EPOC32__
00057 typedef void AoLoop;
00058 
00059 typedef struct {
00060   AoLoop* aoLoop; // CActiveSchedulerWait* or NULL
00061 } EventQueue;
00062 #else
00063 #include "common/evq_queue.h"
00064 
00065 typedef struct _Event {
00066   QueueItem item;
00067   MAYBE_ERROR_RTYPE (*callback)(struct _Event* event MAYBE_ERROR_PARAM);
00068   // Any event specific data goes here.
00069 } Event;
00070 
00071 typedef Queue EventQueue;
00072 #endif
00073 
00074 // On POSIX, initializes an event queue.
00075 // 
00076 // On Symbian, installs a (new) active scheduler for the calling
00077 // thread. This is essentially a call to CActiveScheduler::Install.
00078 void event_init(EventQueue* queue);
00079 
00080 // Processes events until requested otherwise, or until a callback
00081 // produces an error.
00082 // 
00083 // On POSIX, this runs the event queue.
00084 // 
00085 // On Symbian, this creates a nested scheduling loop in the current
00086 // active scheduler, using a CActiveSchedulerWait to control the loop;
00087 // this call essentially invokes CActiveSchedulerWait::Start.
00088 // 
00089 // Returns if the loop is exited due to a call to "event_loop_stop".
00090 //
00091 // The EVENT_CALLBACK_WITH_GERROR variant will also exit due to an an error produced by a
00092 // callback, in which case that error is returned.
00093 MAYBE_ERROR_RTYPE event_loop(EventQueue* queue MAYBE_ERROR_PARAM);
00094 
00095 #ifndef __EPOC32__
00096 // Adds an event into the event queue.
00097 // 
00098 // This function is not implemented for Symbian, where you must
00099 // instead make requests to active objects to add events.
00100 void event_put(EventQueue* queue, Event* event);
00101 
00102 // Removes an event from the event queue (if it is there).
00103 //
00104 // Again, this function is not implemented for Symbian.
00105 void event_remove(EventQueue* queue, Event* event);
00106 #endif
00107 
00108 // Stops the event loop.
00109 // 
00110 // On POSIX, this simply stops running the event queue.
00111 // 
00112 // On Symbian, this essentially invokes
00113 // CActiveSchedulerWait::AsyncStop.
00114 void event_loop_stop(EventQueue* queue);
00115 
00116 // Frees up any resources associated with an event queue.
00117 // 
00118 // On Symbian, uninstalls and deletes (the current) active scheduler
00119 // of the calling thread.
00120 void event_close(EventQueue* queue);
00121 
00122 // These can be used for active scheduler installation on Symbian if
00123 // required. They presently have no effect on other platforms.
00124 void event_scheduler_install(MAYBE_ERROR_SOLE_PARAM);
00125 void event_scheduler_uninstall();
00126 void event_scheduler_replace(MAYBE_ERROR_SOLE_PARAM);
00127 
00128 #ifdef __cplusplus
00129 } /* extern "C" */
00130 #endif
00131 
00132 #endif /* __EVQ_EVENT_H__ */
00133 
00134 /**
00135 
00136 evq_event.h
00137 
00138 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00139 and the authors. All rights reserved.
00140 
00141 Authors: Tero Hasu <tero.hasu@hut.fi>
00142 
00143 Permission is hereby granted, free of charge, to any person
00144 obtaining a copy of this software and associated documentation files
00145 (the "Software"), to deal in the Software without restriction,
00146 including without limitation the rights to use, copy, modify, merge,
00147 publish, distribute, sublicense, and/or sell copies of the Software,
00148 and to permit persons to whom the Software is furnished to do so,
00149 subject to the following conditions:
00150 
00151 The above copyright notice and this permission notice shall be
00152 included in all copies or substantial portions of the Software.
00153 
00154 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00155 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00156 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00157 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00158 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00159 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00160 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00161 SOFTWARE.
00162 
00163  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:52 2011 by Doxygen 1.6.1