Handler.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_HANDLER_H
00014 #define _SUPPORT_HANDLER_H
00015 
00021 #include <support/Atom.h>
00022 #include <support/Locker.h>
00023 #include <support/Message.h>
00024 #include <support/MessageList.h>
00025 #include <support/SupportDefs.h>
00026 
00027 // XXX remove
00028 #include <support/Context.h>
00029 
00030 #if _SUPPORTS_NAMESPACE
00031 namespace palmos {
00032 namespace support {
00033 #endif
00034 
00039 class BProcess;
00040 class SHandler;
00041 
00042 typedef SHandler BHandler;
00043 
00045 nsecs_t approx_SysGetRunTime();
00047 nsecs_t exact_SysGetRunTime();  
00048 
00049 /*----------------------------------------------------------------*/
00050 /*----- SHandler class --------------------------------------------*/
00051 
00053 
00083 class SHandler : public virtual SAtom
00084 {
00085 public:
00086     // --------------------------------------------------------------
00090             
00091                             SHandler();
00093                             SHandler(SLocker* externalLock);
00094 
00096                             SHandler(const SContext& context);
00097 
00098 protected:
00099     virtual                 ~SHandler();
00100 public:
00101 
00103 
00104     // --------------------------------------------------------------
00108 
00110             enum {
00112 
00119                 POST_REMOVE_DUPLICATES  = 0x0001,
00120 
00122 
00135                 POST_KEEP_UNIQUE            = 0x0002
00136             };
00137 
00139 
00141             status_t        PostMessage(        const SMessage &message,
00142                                                 uint32_t flags = 0);
00144 
00146             status_t        PostDelayedMessage( const SMessage &message,
00147                                                 nsecs_t delay,
00148                                                 uint32_t flags = 0);
00150 
00166             status_t        PostMessageAtTime(  const SMessage &message,
00167                                                 nsecs_t absoluteTime,
00168                                                 uint32_t flags = 0);
00170 
00174             status_t        EnqueueMessage(     const SMessage& message,
00175                                                 nsecs_t time,
00176                                                 uint32_t flags = 0);
00177 
00179 
00180     // --------------------------------------------------------------
00184 
00186     virtual status_t        HandleMessage(const SMessage &msg);
00187 
00189 
00194             void            ResumeScheduling();
00195 
00197 
00198     // --------------------------------------------------------------
00202 
00204             enum filter_action {
00205                 FILTER_REMOVE           = 0,            
00206                 FILTER_KEEP             = 1,            
00207                 FILTER_STOP             = 2             
00208             };
00209 
00211             enum {
00212                 FILTER_REVERSE_FLAG     = 0x0001,       
00213                 FILTER_FUTURE_FLAG      = 0x0002        
00214             };
00215 
00217 
00218             typedef filter_action   (*filter_func)(const SMessage* message, void* data);
00219 
00221 
00222             class filter_functor_base
00223             {
00224             public:
00225                 virtual filter_action operator()(const SMessage* message, void* user) const = 0;
00226                 virtual ~filter_functor_base() { }
00227             };
00228 
00229             template<class T>
00230             class filter_functor : public filter_functor_base
00231             {
00232             public:
00233                 typedef filter_action (T::*method_ptr_t)(const SMessage* message, void* user) const;
00234                 filter_functor(const T& o, method_ptr_t m) : object(o), method(m) { }
00235                 filter_functor(const T* o, method_ptr_t m) : object(*o), method(m) { }
00236                 virtual filter_action operator()(const SMessage* message, void* user) const {
00237                     return (object.*method)(message, user);
00238                 }
00239             private:
00240                 const T&        object;
00241                 method_ptr_t    method;
00242             };
00243 
00244         
00246 
00254             void            FilterMessages( filter_func func, uint32_t flags,
00255                                             void* data,
00256                                             SMessageList* outRemoved = NULL);
00257 
00258             void            FilterMessages( const filter_functor_base& functor, uint32_t flags,
00259                                             void* data,
00260                                             SMessageList* outRemoved = NULL);
00261             
00263 
00264             void            RemoveMessages( uint32_t what, uint32_t flags,
00265                                             SMessageList* outRemoved = NULL);
00266             
00268 
00269             void            RemoveAllMessages(SMessageList* outRemoved = NULL);
00270 
00272             int32_t         CountMessages(uint32_t what=B_ANY_WHAT);
00273 
00275 
00276     // --------------------------------------------------------------
00282 
00284             nsecs_t         NextMessageTime(int32_t* out_priority);
00285             
00287 
00290             SMessage*       DequeueMessage(uint32_t what);
00291             
00293             void            DispatchAllMessages();
00294             
00296 
00297 private:
00298     friend  class           BProcess;
00299 
00300                             SHandler(const SHandler&);
00301             SHandler        operator=(const SHandler&);
00302     
00303             enum scheduling {
00304                 CANCEL_SCHEDULE = 0,
00305                 DO_SCHEDULE,
00306                 DO_RESCHEDULE
00307             };
00308 
00309             status_t        enqueue_unique_message(const SMessage& message, nsecs_t time, uint32_t flags);
00310 
00311             void            defer_scheduling();
00312             void            unschedule();
00313             scheduling      start_schedule();
00314             void            done_schedule();
00315 
00316             status_t        dispatch_message();
00317 
00318             sptr<BProcess>  m_team;
00319             SLocker         m_lock;
00320             SMessageList    m_msgQueue;
00321             uint32_t        m_state;
00322             
00323             SHandler *      m_next;
00324             SHandler *      m_prev;
00325 
00326             SLocker*        m_externalLock;
00327 };
00328 
00329 enum {
00330     B_POST_REMOVE_DUPLICATES    = SHandler::POST_REMOVE_DUPLICATES, 
00331     B_POST_KEEP_UNIQUE          = SHandler::POST_KEEP_UNIQUE,       
00332 
00334     B_POST_UNIQUE_MESSAGE       = B_POST_REMOVE_DUPLICATES
00335 };
00336 
00338 enum {
00339     B_FILTER_REVERSE_FLAG       = SHandler::FILTER_REVERSE_FLAG,
00340     B_FILTER_FUTURE_FLAG        = SHandler::FILTER_FUTURE_FLAG  
00341 };
00342 
00343 
00344 /**************************************************************************************/
00345 
00348 #if _SUPPORTS_NAMESPACE
00349 } } // namespace palmos::support
00350 #endif
00351 
00352 #endif /* _SUPPORT_HANDLER_H */