Generated on Sun Aug 26 2012 08:43:18 for Gecode by doxygen 1.8.1.1
engine.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2009
8  *
9  * Last modified:
10  * $Date: 2009-08-11 23:05:41 +1000 (Tue, 11 Aug 2009) $ by $Author: schulte $
11  * $Revision: 9585 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/support.hh>
39 
40 #ifdef GECODE_HAS_THREADS
41 
43 
44 namespace Gecode { namespace Search { namespace Parallel {
45 
46  /*
47  * Engine: search control
48  */
49  Space*
50  Engine::next(void) {
51  // Invariant: the worker holds the wait mutex
52  m_search.acquire();
53  if (!solutions.empty()) {
54  // No search needs to be done, take leftover solution
55  Space* s = solutions.pop();
56  m_search.release();
57  return s;
58  }
59  // We ignore stopped (it will be reported again if needed)
60  has_stopped = false;
61  // No more solutions?
62  if (n_busy == 0) {
63  m_search.release();
64  return NULL;
65  }
66  m_search.release();
67  // Okay, now search has to continue, make the guys work
68  release(C_WORK);
69 
70  /*
71  * Wait until a search related event has happened. It might be that
72  * the event has already been signalled in the last run, but the
73  * solution has been removed. So we have to try until there has
74  * something new happened.
75  */
76  while (true) {
77  e_search.wait();
78  m_search.acquire();
79  if (!solutions.empty()) {
80  // Report solution
81  Space* s = solutions.pop();
82  m_search.release();
83  // Make workers wait again
84  block();
85  return s;
86  }
87  // No more solutions or stopped?
88  if ((n_busy == 0) || has_stopped) {
89  m_search.release();
90  // Make workers wait again
91  block();
92  return NULL;
93  }
94  m_search.release();
95  }
97  return NULL;
98  }
99 
100  bool
101  Engine::stopped(void) const {
102  return has_stopped;
103  }
104 
105 
106  /*
107  * Termination and deletion
108  */
110  delete cur;
111  path.reset();
112  }
113 
114 }}}
115 
116 #endif
117 
118 // STATISTICS: search-parallel