Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Clients.Hive.Slave/3.3/MessageQueue.cs @ 17597

Last change on this file since 17597 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Threading;
25using HeuristicLab.Clients.Hive.SlaveCore.Properties;
26
27namespace HeuristicLab.Clients.Hive.SlaveCore {
28
29  /// <summary>
30  /// Queue for the communication between threads and AppDomains. The Queue is threadsafe and uses a semaphore
31  /// </summary>
32  public class MessageQueue : MarshalByRefObject {
33
34    private static MessageQueue instance = null;
35
36    private Queue<MessageContainer> queue = null;
37    private Semaphore semaphore = null;
38
39    /// <summary>
40    /// Returns the Instance of the MessageQueue. If the instance is null, it will be created.
41    /// This is a Implementation of the Singleton pattern
42    /// </summary>
43    /// <returns>the MessageQueue Instance</returns>
44    public static MessageQueue GetInstance() {
45      if (instance == null) {
46        instance = new MessageQueue();
47      }
48      return instance;
49    }
50
51
52    /// <summary>
53    /// Creates a new MessageQueue Object.
54    /// A new Queue and a Semaphore is created. The Semaphore is set to a max size of 5000.
55    /// </summary>
56    private MessageQueue() {
57      queue = new Queue<MessageContainer>();
58      semaphore = new Semaphore(0, Settings.Default.QueuesMaxThreads);
59    }
60
61    /// <summary>
62    /// Returns the oldest MessageContainer Object from the Queue.
63    /// </summary>
64    /// <returns>the oldest MessageContainer Object</returns>
65    public MessageContainer GetMessage() {
66      semaphore.WaitOne();
67      lock (this) {
68        if (queue.Count > 0) {
69          return queue.Dequeue();
70        }
71      }
72      return null;
73    }
74
75    /// <summary>
76    /// Adds a MessageContainer Object to the Queue
77    /// </summary>
78    /// <param name="messageContainer">the MessageContainer</param>
79    public void AddMessage(MessageContainer messageContainer) {
80      lock (this) {
81        queue.Enqueue(messageContainer);
82        semaphore.Release();
83      }
84    }
85
86    /// <summary>
87    /// Adds a message to the Queue. The MessageContainer Object is built in the Method
88    /// </summary>
89    /// <param name="message">the Message</param>
90    public void AddMessage(MessageContainer.MessageType message) {
91      lock (this) {
92        queue.Enqueue(new MessageContainer(message));
93        semaphore.Release();
94      }
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.