#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Threading; using HeuristicLab.Clients.Hive.SlaveCore.Properties; namespace HeuristicLab.Clients.Hive.SlaveCore { /// /// Every Executor gets an ExecutorQueue in which it can push messages. /// These messages are then read and processed from outside the appdomain. /// public class ExecutorQueue : MarshalByRefObject { private Queue queue = null; private Semaphore semaphore = null; public ExecutorQueue() { queue = new Queue(); semaphore = new Semaphore(0, Settings.Default.QueuesMaxThreads); } /// /// Returns the oldest ExecutorMessage Object from the Queue. /// /// the oldest ExecutorMessage Object public ExecutorMessage GetMessage() { semaphore.WaitOne(); lock (this) { if (queue.Count > 0) { return queue.Dequeue(); } } return null; } /// /// Adds a ExecutorMessage Object to the Queue /// /// the ExecutorMessage public void AddMessage(ExecutorMessage message) { lock (this) { queue.Enqueue(message); semaphore.Release(); } } /// /// Adds a message to the Queue. The ExecutorMessage Object is built in the Method /// /// the Message public void AddMessage(ExecutorMessageType message) { lock (this) { queue.Enqueue(new ExecutorMessage(message)); semaphore.Release(); } } } }