Generated on Fri Aug 24 2012 04:52:14 for Gecode by doxygen 1.8.1.2
pthreads.hpp
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: 2011-04-28 22:05:18 +1000 (Thu, 28 Apr 2011) $ by $Author: tack $
11  * $Revision: 11968 $
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 #ifdef GECODE_HAS_UNISTD_H
39 #include <unistd.h>
40 #endif
41 
42 namespace Gecode { namespace Support {
43 
44  /*
45  * Mutex
46  */
48  Mutex::Mutex(void) {
49  if (pthread_mutex_init(&p_m,NULL) != 0)
50  throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]");
51  }
52  forceinline void
53  Mutex::acquire(void) {
54  if (pthread_mutex_lock(&p_m) != 0)
55  throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]");
56  }
57  forceinline bool
58  Mutex::tryacquire(void) {
59  return pthread_mutex_trylock(&p_m) == 0;
60  }
61  forceinline void
62  Mutex::release(void) {
63  if (pthread_mutex_unlock(&p_m) != 0)
64  throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]");
65  }
67  Mutex::~Mutex(void) {
68  if (pthread_mutex_destroy(&p_m) != 0)
69  throw OperatingSystemError("Mutex::~Mutex[pthread_mutex_destroy]");
70  }
71 
72 
73  /*
74  * Event
75  */
77  Event::Event(void) : p_s(false) {
78  if (pthread_mutex_init(&p_m,NULL) != 0)
79  throw OperatingSystemError("Event::Event[pthread_mutex_init]");
80  if (pthread_cond_init(&p_c,NULL) != 0)
81  throw OperatingSystemError("Event::Event[pthread_cond_init]");
82  }
83  forceinline void
84  Event::signal(void) {
85  if (pthread_mutex_lock(&p_m) != 0)
86  throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
87  if (!p_s) {
88  p_s = true;
89  if (pthread_cond_signal(&p_c) != 0)
90  throw OperatingSystemError("Event::signal[pthread_cond_signal]");
91  }
92  if (pthread_mutex_unlock(&p_m) != 0)
93  throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
94  }
95  forceinline void
96  Event::wait(void) {
97  if (pthread_mutex_lock(&p_m) != 0)
98  throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
99  while (!p_s)
100  if (pthread_cond_wait(&p_c,&p_m) != 0)
101  throw OperatingSystemError("Event::wait[pthread_cond_wait]");
102  p_s = false;
103  if (pthread_mutex_unlock(&p_m) != 0)
104  throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
105  }
107  Event::~Event(void) {
108  if (pthread_cond_destroy(&p_c) != 0)
109  throw OperatingSystemError("Event::~Event[pthread_cond_destroy]");
110  if (pthread_mutex_destroy(&p_m) != 0)
111  throw OperatingSystemError("Event::~Event[pthread_mutex_destroy]");
112  }
113 
114 
115  /*
116  * Thread
117  */
118  forceinline void
119  Thread::sleep(unsigned int ms) {
120 #ifdef GECODE_HAS_UNISTD_H
121  unsigned int s = ms / 1000;
122  ms -= 1000 * s;
123  if (s > 0) {
124  // More than one million microseconds, use sleep
125  ::sleep(s);
126  }
127  usleep(ms * 1000);
128 #endif
129  }
130  forceinline unsigned int
131  Thread::npu(void) {
132 #ifdef GECODE_HAS_UNISTD_H
133  int n=static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
134  return (n>1) ? n : 1;
135 #else
136  return 1;
137 #endif
138  }
139 
140 }}
141 
142 // STATISTICS: support-any