1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Clients.Hive.SlaveCore.Properties;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
28 | /// <summary>
|
---|
29 | /// Every Executor gets an ExecutorQueue in which it can push messages.
|
---|
30 | /// These messages are then read and processed from outside the appdomain.
|
---|
31 | /// </summary>
|
---|
32 | public class ExecutorQueue : MarshalByRefObject {
|
---|
33 | private Queue<ExecutorMessage> queue = null;
|
---|
34 | private Semaphore semaphore = null;
|
---|
35 |
|
---|
36 | public ExecutorQueue() {
|
---|
37 | queue = new Queue<ExecutorMessage>();
|
---|
38 | semaphore = new Semaphore(0, Settings.Default.QueuesMaxThreads);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// Returns the oldest ExecutorMessage Object from the Queue.
|
---|
43 | /// </summary>
|
---|
44 | /// <returns>the oldest ExecutorMessage Object</returns>
|
---|
45 | public ExecutorMessage GetMessage() {
|
---|
46 | semaphore.WaitOne();
|
---|
47 | lock (this) {
|
---|
48 | if (queue.Count > 0) {
|
---|
49 | return queue.Dequeue();
|
---|
50 | }
|
---|
51 | }
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Adds a ExecutorMessage Object to the Queue
|
---|
57 | /// </summary>
|
---|
58 | /// <param name="message">the ExecutorMessage</param>
|
---|
59 | public void AddMessage(ExecutorMessage message) {
|
---|
60 | lock (this) {
|
---|
61 | queue.Enqueue(message);
|
---|
62 | semaphore.Release();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// Adds a message to the Queue. The ExecutorMessage Object is built in the Method
|
---|
68 | /// </summary>
|
---|
69 | /// <param name="message">the Message</param>
|
---|
70 | public void AddMessage(ExecutorMessageType message) {
|
---|
71 | lock (this) {
|
---|
72 | queue.Enqueue(new ExecutorMessage(message));
|
---|
73 | semaphore.Release();
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|