Ejemplo n.º 1
0
        public virtual void Queue <T>(IBackgroundWorkerParamDelegate <T> workerRunnable, T state)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(delegate(Object myState)
            {
                try
                {
                    workerRunnable.Invoke(state);
                }
                catch (ThreadAbortException)
                {
                    // Intended blank
                }
#if SILVERLIGHT
                catch (Exception e)
                {
                    Log.Error(e);
                    throw;
                }
#else
                catch (Exception e)
                {
                    Log.Error(e);
                }
#endif
                finally
                {
                    ThreadLocalCleanupController.CleanupThreadLocal();
                }
            });
        }
Ejemplo n.º 2
0
        public IBeanContextHolder <I> CreateService <I>(String contextName, IBackgroundWorkerParamDelegate <IBeanContextFactory> registerPhaseDelegate, params Type[] serviceModuleTypes)
        {
            CheckNotDisposed();
            IBeanContextInitializer beanContextInitializer = RegisterBean <BeanContextInitializer>().Finish();

            if (contextName == null && registerPhaseDelegate == null && serviceModuleTypes.Length == 1)
            {
                contextName = serviceModuleTypes[0].Name;
            }
            BeanContextFactory childBeanContextFactory = beanContextFactory.CreateChildContextFactory(beanContextInitializer, this);
            IServiceContext    childContext            = childBeanContextFactory.Create(contextName, this, registerPhaseDelegate, serviceModuleTypes);

            writeLock.Lock();
            try
            {
                if (children == null)
                {
                    children = new IdentityHashSet <IServiceContext>();
                }
                children.Add(childContext);
            }
            finally
            {
                writeLock.Unlock();
            }
            return(new BeanContextHolder <I>(childContext));
        }
Ejemplo n.º 3
0
        public IServiceContext Create(String contextName, ServiceContext parent, IBackgroundWorkerParamDelegate <IBeanContextFactory> registerPhaseDelegate, Type[] serviceModuleTypes)
        {
            ServiceContext context = new ServiceContext(GenerateUniqueContextName(contextName, null), parent);

            if (registerPhaseDelegate != null)
            {
                registerPhaseDelegate(this);
            }
            foreach (Type serviceModuleType in serviceModuleTypes)
            {
                RegisterBean(serviceModuleType);
            }
            IList <IBeanPreProcessor> preProcessors = parent.GetPreProcessors();

            if (preProcessors != null)
            {
                for (int a = 0, size = preProcessors.Count; a < size; a++)
                {
                    context.AddPreProcessor(preProcessors[a]);
                }
            }
            IList <IBeanPostProcessor> postProcessors = parent.GetPostProcessors();

            if (postProcessors != null)
            {
                for (int a = 0, size = postProcessors.Count; a < size; a++)
                {
                    context.AddPostProcessor(postProcessors[a]);
                }
            }
            beanContextInitializer.InitializeBeanContext(context, this);
            return(context);
        }
Ejemplo n.º 4
0
        public void InvokeAndWait <K, V>(IMap <K, V> items, IBackgroundWorkerParamDelegate <Entry <K, V> > itemHandler)
        {
            if (items.Count == 0)
            {
                return;
            }
            if (!IsMultiThreadingAllowed() || items.Count == 1)
            {
                foreach (Entry <K, V> item in items)
                {
                    itemHandler(item);
                }
                return;
            }
            List <Entry <K, V> > itemsList = new List <Entry <K, V> >(items.Count);

            foreach (Entry <K, V> item in items)
            {
                itemsList.Add(item);
            }
            RunnableHandle <Entry <K, V> > runnableHandle = new RunnableHandle <Entry <K, V> >(itemHandler, itemsList, ThreadLocalCleanupController);

            Runnable parallelRunnable = new ParallelRunnable <Entry <K, V> >(runnableHandle, true);
            Runnable mainRunnable     = new ParallelRunnable <Entry <K, V> >(runnableHandle, false);

            QueueAndWait(items.Count - 1, parallelRunnable, mainRunnable, runnableHandle);
        }
Ejemplo n.º 5
0
 public override IServiceContext Start(IBackgroundWorkerParamDelegate <IBeanContextFactory> content)
 {
     return(StartIntern(delegate(IBeanContextFactory childContextFactory)
     {
         if (Content != null)
         {
             Content.Invoke(childContextFactory);
         }
         content.Invoke(childContextFactory);
     }));
 }
Ejemplo n.º 6
0
 public void Use <V>(IBackgroundWorkerParamDelegate <V> runnable, V arg)
 {
     Object[] oldValues = SetThreadLocals();
     try
     {
         runnable(arg);
     }
     finally
     {
         RestoreThreadLocals(oldValues);
     }
 }
Ejemplo n.º 7
0
 protected virtual IServiceContext StartIntern(IBackgroundWorkerParamDelegate <IBeanContextFactory> content)
 {
     writeLock.Lock();
     try
     {
         if (Log.DebugEnabled)
         {
             Log.Debug("Looking for existing child context...");
         }
         IServiceContext childContext = GetChildContext();
         if (childContext == null || childContext.IsDisposed)
         {
             if (Log.DebugEnabled)
             {
                 Log.Debug("No valid child context found. Creating new child context");
             }
             IBackgroundWorkerParamDelegate <IBeanContextFactory> rpd = new IBackgroundWorkerParamDelegate <IBeanContextFactory>(delegate(IBeanContextFactory beanContextFactory)
             {
                 if (content != null)
                 {
                     content.Invoke(beanContextFactory);
                 }
                 beanContextFactory.RegisterBean <ChildContextFoot>().PropertyValue("ContextHandle", this);
             });
             if (ContextFactory != null)
             {
                 childContext = ContextFactory.CreateChildContext(rpd);
             }
             else
             {
                 childContext = BeanContext.CreateService(rpd);
             }
             SetChildContext(childContext);
             childContexts.Add(childContext);
         }
         else if (Log.DebugEnabled)
         {
             Log.Debug("Existing child context found and valid");
         }
         IList <IUpwakingBean> upwakingBeans = childContext.GetImplementingObjects <IUpwakingBean>();
         for (int a = 0, size = upwakingBeans.Count; a < size; a++)
         {
             upwakingBeans[a].WakeUp();
         }
         return(childContext);
     }
     finally
     {
         writeLock.Unlock();
     }
 }
Ejemplo n.º 8
0
        protected void WaitEventToResume(IBackgroundWorkerParamDelegate <IProcessResumeItem> resumeDelegate, IBackgroundWorkerParamDelegate <Exception> errorDelegate)
        {
            IRootCache             rootCache = RootCache;
            IList <IWritableCache> selectedFirstLevelCaches = FirstLevelCacheManager.SelectFirstLevelCaches();

            ISet <Object> collisionSet = new IdentityHashSet <Object>();

            collisionSet.Add(rootCache);
            for (int a = selectedFirstLevelCaches.Count; a-- > 0;)
            {
                collisionSet.Add(selectedFirstLevelCaches[a]);
            }
            // Without the current rootcache we can not handle the event now. We have to block till the rootCache and all childCaches get valid
            EventDispatcher.WaitEventToResume(collisionSet, -1, resumeDelegate, errorDelegate);
        }
Ejemplo n.º 9
0
 public ThreadLocalCleanupController()
 {
     foreignContextHook = new IBackgroundWorkerParamDelegate <IServiceContext>(delegate(IServiceContext state)
     {
         Lock writeLock = listeners.GetWriteLock();
         writeLock.Lock();
         try
         {
             cachedForkStateEntries = null;
         }
         finally
         {
             writeLock.Unlock();
         }
     });
 }
Ejemplo n.º 10
0
        public void InvokeAndWait <V>(IList <V> items, IBackgroundWorkerParamDelegate <V> itemHandler)
        {
            if (items.Count == 0)
            {
                return;
            }
            if (!IsMultiThreadingAllowed() || items.Count == 1)
            {
                for (int a = items.Count; a-- > 0;)
                {
                    V item = items[a];
                    itemHandler(item);
                }
                return;
            }
            RunnableHandle <V> runnableHandle = new RunnableHandle <V>(itemHandler, items, ThreadLocalCleanupController);

            Runnable parallelRunnable = new ParallelRunnable <V>(runnableHandle, true);
            Runnable mainRunnable     = new ParallelRunnable <V>(runnableHandle, false);

            QueueAndWait(items.Count - 1, parallelRunnable, mainRunnable, runnableHandle);
        }
Ejemplo n.º 11
0
 public QueueGroupKey(long queueInterval, IBackgroundWorkerParamDelegate <IList <T> > queuedDelegate)
     : this(queueInterval, false, queuedDelegate)
 {
     // Intended blank
 }
Ejemplo n.º 12
0
        public void Run()
        {
            try
            {
                Thread currentThread = Thread.CurrentThread;
                String oldName       = currentThread.Name;
                if (buildThreadLocals)
                {
                    String name = runnableHandle.createdThread.Name;
                    currentThread.Name = name + " " + oldName;
                }
                try
                {
                    Object     parallelLock          = runnableHandle.parallelLock;
                    IList <V>  items                 = runnableHandle.items;
                    IForkState forkState             = runnableHandle.forkState;
                    ParamHolder <Exception> exHolder = runnableHandle.exHolder;
                    CountDownLatch          latch    = runnableHandle.latch;

                    IBackgroundWorkerParamDelegate <V> run = new IBackgroundWorkerParamDelegate <V>(delegate(V item)
                    {
                        R result = runnableHandle.run(item);
                        IAggregrateResultHandler <R, V> aggregrateResultHandler = runnableHandle.aggregrateResultHandler;
                        if (aggregrateResultHandler != null)
                        {
                            lock (parallelLock)
                            {
                                aggregrateResultHandler(result, item);
                            }
                        }
                    });

                    while (true)
                    {
                        V item;
                        lock (parallelLock)
                        {
                            if (exHolder.Value != null)
                            {
                                // an uncatched error occurred somewhere
                                return;
                            }
                            // pop the last item of the queue
                            item = items[items.Count - 1];
                            items.RemoveAt(items.Count - 1);
                        }
                        if (item == null)
                        {
                            // queue finished
                            return;
                        }
                        try
                        {
                            if (buildThreadLocals)
                            {
                                forkState.Use(run, item);
                            }
                            else
                            {
                                run(item);
                            }
                        }
                        catch (Exception e)
                        {
                            lock (parallelLock)
                            {
                                if (exHolder.Value == null)
                                {
                                    exHolder.Value = e;
                                }
                            }
                        }
                        finally
                        {
                            latch.CountDown();
                        }
                    }
                }
                finally
                {
                    if (buildThreadLocals)
                    {
                        currentThread.Name = oldName;
                    }
                }
            }
            finally
            {
                if (buildThreadLocals)
                {
                    runnableHandle.threadLocalCleanupController.CleanupThreadLocal();
                }
            }
        }
Ejemplo n.º 13
0
 public virtual IServiceContext CreateChildContext(IBackgroundWorkerParamDelegate <IBeanContextFactory> content)
 {
     return(BeanContext.CreateService(content, Modules));
 }
Ejemplo n.º 14
0
 public IBeanContextHolder <I> CreateService <I>(IBackgroundWorkerParamDelegate <IBeanContextFactory> registerPhaseDelegate, params Type[] serviceModuleTypes)
 {
     return(CreateService <I>(null, registerPhaseDelegate, serviceModuleTypes));
 }
Ejemplo n.º 15
0
 public IServiceContext Create(String contextName, ServiceContext parent, IBackgroundWorkerParamDelegate <IBeanContextFactory> registerPhaseDelegate)
 {
     return(Create(contextName, parent, registerPhaseDelegate, emptyServiceModules));
 }
Ejemplo n.º 16
0
 public IServiceContext Create(String contextName, IBackgroundWorkerParamDelegate <IBeanContextFactory> registerPhaseDelegate, IList <IBeanPreProcessor> preProcessors,
                               IList <IBeanPostProcessor> postProcessors)
 {
     return(Create(contextName, registerPhaseDelegate, preProcessors, postProcessors, emptyServiceModules));
 }
Ejemplo n.º 17
0
 public QueueGroupKey(long queueInterval, bool invokeFromGuiThread, IBackgroundWorkerParamDelegate <IList <T> > queuedDelegate) : base(queueInterval, invokeFromGuiThread)
 {
     this.QueuedDelegate = queuedDelegate;
 }
Ejemplo n.º 18
0
 public void RegisterDisposeHook(IBackgroundWorkerParamDelegate <IServiceContext> waitCallback)
 {
     RegisterDisposableIntern(waitCallback, false);
 }
Ejemplo n.º 19
0
 public virtual IServiceContext Start(IBackgroundWorkerParamDelegate <IBeanContextFactory> content)
 {
     throw new NotSupportedException();
 }
Ejemplo n.º 20
0
 public RunnableHandle(IBackgroundWorkerParamDelegate <V> run, IList <V> items, IThreadLocalCleanupController threadLocalCleanupController) : base(items, threadLocalCleanupController)
 {
     this.run = run;
 }
Ejemplo n.º 21
0
        public void WaitEventToResume(Object eventTargetToResume, long maxWaitTime, IBackgroundWorkerParamDelegate <IProcessResumeItem> resumeDelegate, IBackgroundWorkerParamDelegate <Exception> errorDelegate)
        {
            try
            {
                IdentityHashSet <Object> pendingSet = new IdentityHashSet <Object>();
                if (eventTargetToResume is IEnumerable)
                {
                    pendingSet.AddAll((IEnumerable)eventTargetToResume);
                }
                else
                {
                    pendingSet.Add(eventTargetToResume);
                }
                WaitForResumeItem pauseItem = null;
                listenersWriteLock.Lock();
                try
                {
                    IList <Object>             remainingPausedEventTargets    = EvaluatePausedEventTargetsOfForeignThreads();
                    IdentityLinkedSet <Object> remainingPausedEventTargetsSet = new IdentityLinkedSet <Object>(remainingPausedEventTargets);
                    remainingPausedEventTargetsSet.RetainAll(pendingSet);

                    if (remainingPausedEventTargetsSet.Count > 0)
                    {
                        // We should wait now but we have to check if we are in the UI thread, which must never wait
                        if (GuiThreadHelper.IsInGuiThread())
                        {
                            // This is the trick: We "requeue" the current action in the UI pipeline to prohibit blocking
                            GuiThreadHelper.InvokeInGuiLate(delegate()
                            {
                                WaitEventToResume(eventTargetToResume, maxWaitTime, resumeDelegate, errorDelegate);
                            });
                            return;
                        }
                        pauseItem = new WaitForResumeItem(pendingSet);
                        waitForResumeSet.Add(pauseItem);
                    }
                }
                finally
                {
                    listenersWriteLock.Unlock();
                }
                if (pauseItem == null)
                {
                    resumeDelegate.Invoke(null);
                    return;
                }
                CountDownLatch latch = pauseItem.Latch;
                if (maxWaitTime < 0)
                {
                    latch.Await();
                }
                else if (maxWaitTime > 0)
                {
                    latch.Await(TimeSpan.FromMilliseconds(maxWaitTime));
                }
                else
                {
                    throw new System.Exception("Thread should wait but does not want to");
                }
                resumeDelegate.Invoke(pauseItem);
            }
            catch (Exception e)
            {
                if (Log.ErrorEnabled)
                {
                    Log.Error(e);
                }
                if (errorDelegate != null)
                {
                    errorDelegate.Invoke(e);
                }
                throw;
            }
        }