Process.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_PROCESS_H
00014 #define _SUPPORT_PROCESS_H
00015 
00021 #include <support/SupportDefs.h>
00022 #include <support/Atom.h>
00023 #include <support/ConditionVariable.h>
00024 #include <support/IBinder.h>
00025 #include <support/Package.h>
00026 #include <support/KeyedVector.h>
00027 #include <support/Locker.h>
00028 #include <support/String.h>
00029 #include <support/Vector.h>
00030 #include <support/InstantiateComponent.h>
00031 #include <support/Value.h>
00032 
00033 #if !LIBBE_BOOTSTRAP
00034 #include <support/IProcess.h>
00035 #endif
00036 
00037 #if _SUPPORTS_NAMESPACE
00038 namespace palmos {
00039 namespace support {
00040 #endif
00041 
00046 class SHandler;
00047 class IVirtualMachine;
00048 class SLooper;
00049 
00050 extern int32_t this_team();
00051 
00052 /*----------------------------------------------------------------*/
00053 
00054 enum {
00055     B_FORGET_CURRENT_ENVIRONMENT    = 0x00000001
00056 };
00057 
00059 class BProcess
00060 #if !LIBBE_BOOTSTRAP
00061     : public BnProcess
00062 #endif
00063 {
00064 public:
00065 
00066                                 BProcess(team_id tid);
00067 
00068             team_id             ID() const;
00069     
00070 #if !LIBBE_BOOTSTRAP
00073     static  sptr<IProcess>          Spawn(  const SString& name = SString(),
00074                                         const SValue& env = B_UNDEFINED_VALUE,
00075                                         uint32_t flags = 0);
00076     
00078     static  sptr<IBinder>       SpawnFile(  const SString& file_path,
00079                                             const SValue& env = B_UNDEFINED_VALUE,
00080                                             uint32_t flags = 0);
00081 
00083     virtual sptr<IBinder>       InstantiateComponent(   const sptr<INode>& node,
00084                                                         const SValue& componentInfo,
00085                                                         const SString& component,
00086                                                         const SValue& args,
00087                                                         status_t* outError = NULL);
00088     virtual int32_t             AtomMarkLeakReport();
00089     virtual void                AtomLeakReport(int32_t mark, int32_t last, uint32_t flags);
00090 
00091     virtual void                PrintBinderReferences(void);
00092 #endif
00093 
00094     virtual void                RestartMallocProfiling(void);
00095     virtual void                SetMallocProfiling(bool enabled, int32_t dumpPeriod, int32_t maxItems, int32_t stackDepth);
00096     virtual void                RestartVectorProfiling(void);
00097     virtual void                SetVectorProfiling(bool enabled, int32_t dumpPeriod, int32_t maxItems, int32_t stackDepth);
00098     virtual void                RestartMessageIPCProfiling(void);
00099     virtual void                SetMessageIPCProfiling(bool enabled, int32_t dumpPeriod, int32_t maxItems, int32_t stackDepth);
00100     virtual void                RestartBinderIPCProfiling(void);
00101     virtual void                SetBinderIPCProfiling(bool enabled, int32_t dumpPeriod, int32_t maxItems, int32_t stackDepth);
00102     virtual void                PrintBinderIPCProfiling(void);
00103 
00104     typedef void                (*catchReleaseFunc)(IBinder* obj);
00105             void                CatchHandleRelease(const sptr<IBinder>& remoteObject, catchReleaseFunc callbackFunc);
00106             
00107             nsecs_t             GetNextEventTime() const;
00108             void                SetHandlerConcurrency(int32_t maxConcurrency);
00109             void                BatchPutReferences();
00110             
00111     // ----------- The remaining methods are not for public use -----------
00112     
00113             void                DispatchMessage(SLooper* looper);
00114 
00115             sptr<IBinder>       GetStrongProxyForHandle(int32_t handle);
00116             wptr<IBinder>       GetWeakProxyForHandle(int32_t handle);
00117             void                ExpungeHandle(int32_t handle, IBinder* binder);
00118             void                StrongHandleGone(IBinder* binder);
00119             
00120             void                Shutdown();
00121             bool                IsShuttingDown() const;
00122             nsecs_t             GetEventDelayTime(SLooper* caller) const;
00123 
00124             class ComponentImage : public BSharedObject
00125             {
00126             public:
00127                 
00128                 inline  ComponentImage(const SValue& file, const SPackage& package, bool fake, attach_func attach)
00129                     : BSharedObject(file, package, attach)
00130                     , m_instantiate(NULL)
00131                     , m_fake(fake)
00132                     , m_numPendingExpunge(1)
00133                     , m_expunged(0)
00134                 {
00135                 }
00136 
00137                 // Ask this .so to instantiate a component
00138                 inline  sptr<IBinder>   InstantiateComponent(const SString& component, const SContext& context, const SValue &args) const;
00139                 
00140                 inline status_t         InitCheck() const { return (m_instantiate || m_fake) ? B_OK : B_NO_INIT; }
00141                 
00142                 inline  int32_t         DecPending() { return SysAtomicDec32(&m_numPendingExpunge); }
00143                 inline  void            MakeExpunged() { SysAtomicOr32(&m_expunged, 1); }
00144                 
00145             protected:
00146                 virtual                 ~ComponentImage() { }
00147                 
00148                 virtual void            InitAtom();
00149                 virtual status_t        FinishAtom(const void* id);
00150                 virtual status_t        IncStrongAttempted(uint32_t flags, const void* id);
00151                 
00152             private:
00153                 instantiate_component_func  m_instantiate;
00154                 bool                        m_fake;
00155                 volatile int32_t            m_numPendingExpunge;
00156                 volatile uint32_t           m_expunged;
00157 
00158             private:
00159                 // copy constructor not supported
00160                 ComponentImage(const ComponentImage& rhs);
00161             };
00162 
00163             bool                ExpungePackage(const wptr<ComponentImage>& image);
00164             
00165 protected:
00166     virtual                     ~BProcess();
00167     virtual void                InitAtom();
00168 
00169 private:
00170 
00171     friend  class SHandler;
00172     friend  class SLooper;
00173     friend  class ProcessFreeKey;
00174     
00175     static  bool                ResumingScheduling();
00176     static  void                ClearSchedulingResumed();
00177     static  bool                InsertHandler(SHandler **handlerP, SHandler *handler);
00178             void                UnscheduleHandler(SHandler* h, bool lock=true);
00179             bool                ScheduleNextEvent();
00180             void                ScheduleHandler(SHandler* h);
00181             // This function is called with the lock held, and returns with it released.
00182             void                ScheduleNextHandler();
00183             IBinder* &          BinderForHandle(int32_t handle);
00184             sptr<ComponentImage>    get_shared_object(const SValue& file, const SValue& info, bool fake=false, attach_func attach = NULL);
00185             sptr<IBinder>       DoInstantiate(  const SContext& context,
00186                                                 const SValue &componentInfo,
00187                                                 const SString &component,
00188                                                 SVector<SString> &vmIDs,
00189                                                 const SValue &args,
00190                                                 status_t* outError);
00191             void                GetRemoteHostInfo(int32_t handle);
00192             void                ReleaseRemoteReferences();
00193 
00194             enum {
00195                 MAX_LOOPERS_PER_TEAM= 63
00196             };
00197     
00198             const team_id               m_id;
00199     mutable SLocker                     m_lock;
00200             SHandler*                   m_pendingHandlers;
00201             nsecs_t                     m_nextEventTime;
00202             int32_t                     m_maxEventConcurrency;
00203             int32_t                     m_currentEventConcurrency;
00204             bool                        m_shutdown;
00205             
00206             // Information about loaded component images.
00207             SNestedLocker               m_imageLock;
00208             struct ImageData;
00209             ImageData*                  m_imageData;
00210             
00211             // Information about remote binders.
00212     mutable SNestedLocker               m_handleRefLock;
00213             bool                        m_remoteRefsReleased;
00214             SVector<IBinder*>           m_handleRefs;
00215             SKeyedVector<IBinder*, catchReleaseFunc>
00216                                         m_catchers;
00217     
00218     // -----------------------------------------------
00219     // These are used for scheduling handlers when
00220     // there is no driver available to do so.
00221             
00223             nsecs_t             GetIdleTimeout() const;
00225             bool                PopLooper(SLooper* looper, nsecs_t when);
00227             void                PushLooper(SLooper* looper);
00229             bool                RemoveLooper(SLooper* looper);
00231             SLooper*            UnsafePopLooper();
00233             int32_t             LooperCount() const;
00235             int32_t             MaximumLoopers() const;
00237             int32_t             MinimumLoopers() const;
00239             void                IncrementLoopers();
00241             void                DecrementLoopers();
00243             void                CheckIntegrity() const;
00244 
00245             int32_t                     m_idleCount;
00246             SLooper*                    m_idleLooper;
00247             nsecs_t                     m_idleTimeout;
00248             int32_t                     m_loopers;
00249             int32_t                     m_maxLoopers;
00250             int32_t                     m_minLoopers;
00251 };
00252 
00255 inline sptr<IBinder>
00256 BProcess::ComponentImage::InstantiateComponent(const SString& component, const SContext& context, const SValue &args) const
00257 {
00258     if (m_instantiate)
00259         return m_instantiate(component, context, args);
00260     else
00261         return NULL;
00262 }
00263 
00264 #if _SUPPORTS_NAMESPACE
00265 } } // namespace palmos::support
00266 #endif
00267 
00268 #endif /* _SUPPORT_PROCESS_H */