Generated on Mon Jul 6 18:09:20 2009 for Gecode by doxygen 1.5.9

pthreads.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2009
00008  *
00009  *  Last modified:
00010  *     $Date: 2009-05-20 17:40:57 +0200 (Wed, 20 May 2009) $ by $Author: schulte $
00011  *     $Revision: 9163 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #ifdef GECODE_HAS_UNISTD_H
00039 #include <unistd.h>
00040 #endif
00041 
00042 namespace Gecode { namespace Support {
00043 
00044   /*
00045    * Thread
00046    */
00047   forceinline void
00048   Thread::sleep(unsigned int ms) {
00049 #ifdef GECODE_HAS_UNISTD_H
00050     unsigned int s = ms / 1000;
00051     ms -= 1000 * s;
00052     if (s > 0) {
00053       // More than one million microseconds, use sleep
00054       ::sleep(s);
00055     }
00056     usleep(ms * 1000);
00057 #endif
00058   }
00059   forceinline unsigned int
00060   Thread::npu(void) {
00061 #ifdef GECODE_HAS_UNISTD_H
00062     int n=sysconf(_SC_NPROCESSORS_ONLN);
00063     return (n>1) ? n : 1;
00064 #else
00065     return 1;
00066 #endif
00067   }
00068   forceinline
00069   Thread::~Thread(void) {
00070   }
00071 
00072 
00073   /*
00074    * Mutex
00075    */
00076   forceinline
00077   Mutex::Mutex(void) {
00078     if (pthread_mutex_init(&p_m,NULL) != 0)
00079       throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]");
00080   }
00081   forceinline void
00082   Mutex::acquire(void) {
00083     if (pthread_mutex_lock(&p_m) != 0)
00084       throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]");
00085   }
00086   forceinline bool
00087   Mutex::tryacquire(void) {
00088     return pthread_mutex_trylock(&p_m) == 0;
00089   }
00090   forceinline void
00091   Mutex::release(void) {
00092     if (pthread_mutex_unlock(&p_m) != 0)
00093       throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]");
00094   }
00095   forceinline
00096   Mutex::~Mutex(void) {
00097     if (pthread_mutex_destroy(&p_m) != 0)
00098       throw OperatingSystemError("Mutex::~Mutex[pthread_mutex_destroy]");
00099   }
00100 
00101 
00102   /*
00103    * Event
00104    */
00105   forceinline
00106   Event::Event(void) : p_s(false) {
00107     if (pthread_mutex_init(&p_m,NULL) != 0)
00108       throw OperatingSystemError("Event::Event[pthread_mutex_init]");
00109     if (pthread_cond_init(&p_c,NULL) != 0)
00110       throw OperatingSystemError("Event::Event[pthread_cond_init]");
00111   }
00112   forceinline void
00113   Event::signal(void) {
00114     if (pthread_mutex_lock(&p_m) != 0)
00115       throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
00116     if (!p_s) {
00117       p_s = true;
00118       if (pthread_cond_signal(&p_c) != 0)
00119         throw OperatingSystemError("Event::signal[pthread_cond_signal]");
00120     }
00121     if (pthread_mutex_unlock(&p_m) != 0)
00122       throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
00123   }
00124   forceinline void
00125   Event::wait(void) {
00126     if (pthread_mutex_lock(&p_m) != 0)
00127       throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
00128     while (!p_s)
00129       if (pthread_cond_wait(&p_c,&p_m) != 0)
00130         throw OperatingSystemError("Event::wait[pthread_cond_wait]");
00131     p_s = false;
00132     if (pthread_mutex_unlock(&p_m) != 0)
00133       throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
00134   }
00135   forceinline
00136   Event::~Event(void) {
00137     if (pthread_cond_destroy(&p_c) != 0)
00138       throw OperatingSystemError("Event::~Event[pthread_cond_destroy]");
00139     if (pthread_mutex_destroy(&p_m) != 0)
00140       throw OperatingSystemError("Event::~Event[pthread_mutex_destroy]");
00141   }
00142 
00143 
00144 }}
00145 
00146 // STATISTICS: support-any