Generated on Fri Mar 20 15:56:21 2015 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: 2013-07-01 07:36:45 +0200 (Mon, 01 Jul 2013) $ by $Author: tack $
00011  *     $Revision: 13741 $
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 #ifdef GECODE_THREADS_OSX
00073 
00074   /*
00075    * FastMutex
00076    */
00077   forceinline
00078   FastMutex::FastMutex(void) : lck(OS_SPINLOCK_INIT) {}
00079   forceinline void
00080   FastMutex::acquire(void) {
00081     OSSpinLockLock(&lck);
00082   }
00083   forceinline bool
00084   FastMutex::tryacquire(void) {
00085     return OSSpinLockTry(&lck);
00086   }
00087   forceinline void
00088   FastMutex::release(void) {
00089     OSSpinLockUnlock(&lck);
00090   }
00091   forceinline
00092   FastMutex::~FastMutex(void) {}
00093 
00094 #endif
00095 
00096 #ifdef GECODE_THREADS_PTHREADS_SPINLOCK
00097 
00098   /*
00099    * FastMutex
00100    */
00101   forceinline
00102   FastMutex::FastMutex(void) {
00103     if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0)
00104       throw OperatingSystemError("FastMutex::FastMutex[pthread_spin_init]");
00105   }
00106   forceinline void
00107   FastMutex::acquire(void) {
00108     if (pthread_spin_lock(&p_s) != 0)
00109       throw OperatingSystemError("FastMutex::acquire[pthread_spin_lock]");
00110   }
00111   forceinline bool
00112   FastMutex::tryacquire(void) {
00113     return pthread_spin_trylock(&p_s) == 0;
00114   }
00115   forceinline void
00116   FastMutex::release(void) {
00117     if (pthread_spin_unlock(&p_s) != 0)
00118       throw OperatingSystemError("FastMutex::release[pthread_spin_unlock]");
00119   }
00120   forceinline
00121   FastMutex::~FastMutex(void) {
00122     if (pthread_spin_destroy(&p_s) != 0)
00123       throw OperatingSystemError(
00124         "FastMutex::~FastMutex[pthread_spin_destroy]");
00125   }
00126 
00127 #endif
00128 
00129   /*
00130    * Event
00131    */
00132   forceinline
00133   Event::Event(void) : p_s(false) {
00134     if (pthread_mutex_init(&p_m,NULL) != 0)
00135       throw OperatingSystemError("Event::Event[pthread_mutex_init]");
00136     if (pthread_cond_init(&p_c,NULL) != 0)
00137       throw OperatingSystemError("Event::Event[pthread_cond_init]");
00138   }
00139   forceinline void
00140   Event::signal(void) {
00141     if (pthread_mutex_lock(&p_m) != 0)
00142       throw OperatingSystemError("Event::signal[pthread_mutex_lock]");
00143     if (!p_s) {
00144       p_s = true;
00145       if (pthread_cond_signal(&p_c) != 0)
00146         throw OperatingSystemError("Event::signal[pthread_cond_signal]");
00147     }
00148     if (pthread_mutex_unlock(&p_m) != 0)
00149       throw OperatingSystemError("Event::signal[pthread_mutex_unlock]");
00150   }
00151   forceinline void
00152   Event::wait(void) {
00153     if (pthread_mutex_lock(&p_m) != 0)
00154       throw OperatingSystemError("Event::wait[pthread_mutex_lock]");
00155     while (!p_s)
00156       if (pthread_cond_wait(&p_c,&p_m) != 0)
00157         throw OperatingSystemError("Event::wait[pthread_cond_wait]");
00158     p_s = false;
00159     if (pthread_mutex_unlock(&p_m) != 0)
00160       throw OperatingSystemError("Event::wait[pthread_mutex_unlock]");
00161   }
00162   forceinline
00163   Event::~Event(void) {
00164     if (pthread_cond_destroy(&p_c) != 0)
00165       throw OperatingSystemError("Event::~Event[pthread_cond_destroy]");
00166     if (pthread_mutex_destroy(&p_m) != 0)
00167       throw OperatingSystemError("Event::~Event[pthread_mutex_destroy]");
00168   }
00169 
00170 
00171   /*
00172    * Thread
00173    */
00174   forceinline void
00175   Thread::sleep(unsigned int ms) {
00176 #ifdef GECODE_HAS_UNISTD_H
00177     unsigned int s = ms / 1000;
00178     ms -= 1000 * s;
00179     if (s > 0) {
00180       // More than one million microseconds, use sleep
00181       ::sleep(s);
00182     }
00183     usleep(ms * 1000);
00184 #endif
00185   }
00186   forceinline unsigned int
00187   Thread::npu(void) {
00188 #ifdef GECODE_HAS_UNISTD_H
00189     int n=static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
00190     return (n>1) ? n : 1;
00191 #else
00192     return 1;
00193 #endif
00194   }
00195 
00196 }}
00197 
00198 // STATISTICS: support-any