Generated on Thu Mar 22 10:39:45 2012 for Gecode by doxygen 1.6.3

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: 2011-04-28 14:05:18 +0200 (Thu, 28 Apr 2011) $ by $Author: tack $
00011  *     $Revision: 11968 $
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    * Mutex
00046    */
00047   forceinline
00048   Mutex::Mutex(void) {
00049     if (pthread_mutex_init(&p_m,NULL) != 0)
00050       throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]");
00051   }
00052   forceinline void
00053   Mutex::acquire(void) {
00054     if (pthread_mutex_lock(&p_m) != 0)
00055       throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]");
00056   }
00057   forceinline bool
00058   Mutex::tryacquire(void) {
00059     return pthread_mutex_trylock(&p_m) == 0;
00060   }
00061   forceinline void
00062   Mutex::release(void) {
00063     if (pthread_mutex_unlock(&p_m) != 0)
00064       throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]");
00065   }
00066   forceinline
00067   Mutex::~Mutex(void) {
00068     if (pthread_mutex_destroy(&p_m) != 0)
00069       throw OperatingSystemError("Mutex::~Mutex[pthread_mutex_destroy]");
00070   }
00071 
00072 
00073   /*
00074    * Event
00075    */
00076   forceinline
00077   Event::Event(void) : p_s(false) {
00078     if (pthread_mutex_init(&p_m,NULL) != 0)
00079       throw OperatingSystemError("Event::Event[pthread_mutex_init]");
00080     if (pthread_cond_init(&p_c,NULL) != 0)
00081       throw OperatingSystemError("Event::Event[pthread_cond_init]");
00082   }
00083   forceinline void
00084   Event::signal(void) {
00085     if (pthread_mutex_lock(&p_m) != 0)
00086       throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
00087     if (!p_s) {
00088       p_s = true;
00089       if (pthread_cond_signal(&p_c) != 0)
00090         throw OperatingSystemError("Event::signal[pthread_cond_signal]");
00091     }
00092     if (pthread_mutex_unlock(&p_m) != 0)
00093       throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
00094   }
00095   forceinline void
00096   Event::wait(void) {
00097     if (pthread_mutex_lock(&p_m) != 0)
00098       throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
00099     while (!p_s)
00100       if (pthread_cond_wait(&p_c,&p_m) != 0)
00101         throw OperatingSystemError("Event::wait[pthread_cond_wait]");
00102     p_s = false;
00103     if (pthread_mutex_unlock(&p_m) != 0)
00104       throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
00105   }
00106   forceinline
00107   Event::~Event(void) {
00108     if (pthread_cond_destroy(&p_c) != 0)
00109       throw OperatingSystemError("Event::~Event[pthread_cond_destroy]");
00110     if (pthread_mutex_destroy(&p_m) != 0)
00111       throw OperatingSystemError("Event::~Event[pthread_mutex_destroy]");
00112   }
00113 
00114 
00115   /*
00116    * Thread
00117    */
00118   forceinline void
00119   Thread::sleep(unsigned int ms) {
00120 #ifdef GECODE_HAS_UNISTD_H
00121     unsigned int s = ms / 1000;
00122     ms -= 1000 * s;
00123     if (s > 0) {
00124       // More than one million microseconds, use sleep
00125       ::sleep(s);
00126     }
00127     usleep(ms * 1000);
00128 #endif
00129   }
00130   forceinline unsigned int
00131   Thread::npu(void) {
00132 #ifdef GECODE_HAS_UNISTD_H
00133     int n=static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
00134     return (n>1) ? n : 1;
00135 #else
00136     return 1;
00137 #endif
00138   }
00139 
00140 }}
00141 
00142 // STATISTICS: support-any