Generated on Tue Apr 18 10:22:15 2017 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: 2015-08-18 09:57:34 +0200 (Tue, 18 Aug 2015) $ by $Author: schulte $
00011  *     $Revision: 14653 $
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 #include <exception>
00043 
00044 namespace Gecode { namespace Support {
00045 
00046   /*
00047    * Mutex
00048    */
00049   forceinline
00050   Mutex::Mutex(void) {
00051     if (pthread_mutex_init(&p_m,NULL) != 0)
00052       throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]");
00053   }
00054   forceinline void
00055   Mutex::acquire(void) {
00056     if (pthread_mutex_lock(&p_m) != 0)
00057       throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]");
00058   }
00059   forceinline bool
00060   Mutex::tryacquire(void) {
00061     return pthread_mutex_trylock(&p_m) == 0;
00062   }
00063   forceinline void
00064   Mutex::release(void) {
00065     if (pthread_mutex_unlock(&p_m) != 0)
00066       throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]");
00067   }
00068   forceinline
00069   Mutex::~Mutex(void) {
00070     if (pthread_mutex_destroy(&p_m) != 0) {
00071       std::cerr << "Operating system error: "
00072                 << "Mutex::~Mutex[pthread_mutex_destroy]";
00073       std::terminate();
00074     }
00075   }
00076 
00077 #ifdef GECODE_THREADS_OSX
00078 
00079   /*
00080    * FastMutex
00081    */
00082   forceinline
00083   FastMutex::FastMutex(void) : lck(OS_SPINLOCK_INIT) {}
00084   forceinline void
00085   FastMutex::acquire(void) {
00086     OSSpinLockLock(&lck);
00087   }
00088   forceinline bool
00089   FastMutex::tryacquire(void) {
00090     return OSSpinLockTry(&lck);
00091   }
00092   forceinline void
00093   FastMutex::release(void) {
00094     OSSpinLockUnlock(&lck);
00095   }
00096   forceinline
00097   FastMutex::~FastMutex(void) {}
00098 
00099 #endif
00100 
00101 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK
00102 
00103   /*
00104    * FastMutex
00105    */
00106   forceinline
00107   FastMutex::FastMutex(void) {
00108     if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
00109       throw OperatingSystemError("FastMutex::FastMutex[pthread_spin_init]");
00110   }
00111   forceinline void
00112   FastMutex::acquire(void) {
00113     if (pthread_spin_lock(&p_s) != 0)
00114       throw OperatingSystemError("FastMutex::acquire[pthread_spin_lock]");
00115   }
00116   forceinline bool
00117   FastMutex::tryacquire(void) {
00118     return pthread_spin_trylock(&p_s) == 0;
00119   }
00120   forceinline void
00121   FastMutex::release(void) {
00122     if (pthread_spin_unlock(&p_s) != 0)
00123       throw OperatingSystemError("FastMutex::release[pthread_spin_unlock]");
00124   }
00125   forceinline
00126   FastMutex::~FastMutex(void) {
00127     if (pthread_spin_destroy(&p_s) != 0) {
00128       std::cerr << "Operating system error: "
00129                 << "FastMutex::~FastMutex[pthread_spin_destroy]";
00130       std::terminate();
00131     }
00132   }
00133 
00134 #endif
00135 
00136   /*
00137    * Event
00138    */
00139   forceinline
00140   Event::Event(void) : p_s(false) {
00141     if (pthread_mutex_init(&p_m,NULL) != 0)
00142       throw OperatingSystemError("Event::Event[pthread_mutex_init]");
00143     if (pthread_cond_init(&p_c,NULL) != 0)
00144       throw OperatingSystemError("Event::Event[pthread_cond_init]");
00145   }
00146   forceinline void
00147   Event::signal(void) {
00148     if (pthread_mutex_lock(&p_m) != 0)
00149       throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
00150     if (!p_s) {
00151       p_s = true;
00152       if (pthread_cond_signal(&p_c) != 0)
00153         throw OperatingSystemError("Event::signal[pthread_cond_signal]");
00154     }
00155     if (pthread_mutex_unlock(&p_m) != 0)
00156       throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
00157   }
00158   forceinline void
00159   Event::wait(void) {
00160     if (pthread_mutex_lock(&p_m) != 0)
00161       throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
00162     while (!p_s)
00163       if (pthread_cond_wait(&p_c,&p_m) != 0)
00164         throw OperatingSystemError("Event::wait[pthread_cond_wait]");
00165     p_s = false;
00166     if (pthread_mutex_unlock(&p_m) != 0)
00167       throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
00168   }
00169   forceinline
00170   Event::~Event(void) {
00171     if (pthread_cond_destroy(&p_c) != 0) {
00172       std::cerr << "Operating system error: "
00173                 << "Event::~Event[pthread_cond_destroy]";
00174       std::terminate();
00175     }
00176     if (pthread_mutex_destroy(&p_m) != 0) {
00177       std::cerr << "Operating system error: "
00178                 << "Event::~Event[pthread_mutex_destroy]";
00179       std::terminate();
00180     }
00181   }
00182 
00183 
00184   /*
00185    * Thread
00186    */
00187   forceinline void
00188   Thread::sleep(unsigned int ms) {
00189 #ifdef GECODE_HAS_UNISTD_H
00190     unsigned int s = ms / 1000;
00191     ms -= 1000 * s;
00192     if (s > 0) {
00193       // More than one million microseconds, use sleep
00194       ::sleep(s);
00195     }
00196     usleep(ms * 1000);
00197 #endif
00198   }
00199   forceinline unsigned int
00200   Thread::npu(void) {
00201 #ifdef GECODE_HAS_UNISTD_H
00202     int n=static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
00203     return (n>1) ? n : 1;
00204 #else
00205     return 1;
00206 #endif
00207   }
00208 
00209 }}
00210 
00211 // STATISTICS: support-any