Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
6.3 kB · 227 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Christian Schulte <schulte@gecode.org> * * Copyright: * Christian Schulte, 2009 * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */
#ifdef GECODE_HAS_UNISTD_H#include <unistd.h>#endif
#include <exception>
namespace Gecode { namespace Support {
#ifdef GECODE_THREADS_OSX_UNFAIR
#pragma clang diagnostic push#pragma clang diagnostic ignored "-Wdeprecated-declarations" /* * Mutex */ forceinline Mutex::Mutex(void) { if (&os_unfair_lock_lock != nullptr) u.unfair_lck = OS_UNFAIR_LOCK_INIT; else u.spin_lck = OS_SPINLOCK_INIT; } forceinline void Mutex::acquire(void) { if (&os_unfair_lock_lock != nullptr) { os_unfair_lock_lock(&u.unfair_lck); } else { OSSpinLockLock(&u.spin_lck); } } forceinline bool Mutex::tryacquire(void) { if (&os_unfair_lock_trylock != nullptr) return os_unfair_lock_trylock(&u.unfair_lck); else return OSSpinLockTry(&u.spin_lck); } forceinline void Mutex::release(void) { if (&os_unfair_lock_unlock != nullptr) os_unfair_lock_unlock(&u.unfair_lck); else OSSpinLockUnlock(&u.spin_lck); } forceinline Mutex::~Mutex(void) {}
#pragma clang diagnostic pop
#else
/* * Mutex */ forceinline Mutex::Mutex(void) { if (pthread_mutex_init(&p_m,nullptr) != 0) throw OperatingSystemError("Mutex::Mutex[pthread_mutex_init]"); } forceinline void Mutex::acquire(void) { if (pthread_mutex_lock(&p_m) != 0) throw OperatingSystemError("Mutex::acquire[pthread_mutex_lock]"); } forceinline bool Mutex::tryacquire(void) { return pthread_mutex_trylock(&p_m) == 0; } forceinline void Mutex::release(void) { if (pthread_mutex_unlock(&p_m) != 0) throw OperatingSystemError("Mutex::release[pthread_mutex_unlock]"); } forceinline Mutex::~Mutex(void) { if (pthread_mutex_destroy(&p_m) != 0) { std::cerr << "Operating system error: " << "Mutex::~Mutex[pthread_mutex_destroy]"; std::terminate(); } }#endif #ifdef GECODE_THREADS_PTHREADS_SPINLOCK
/* * FastMutex */ forceinline FastMutex::FastMutex(void) { if (pthread_spin_init(&p_s,PTHREAD_PROCESS_PRIVATE) != 0) throw OperatingSystemError("FastMutex::FastMutex[pthread_spin_init]"); } forceinline void FastMutex::acquire(void) { if (pthread_spin_lock(&p_s) != 0) throw OperatingSystemError("FastMutex::acquire[pthread_spin_lock]"); } forceinline bool FastMutex::tryacquire(void) { return pthread_spin_trylock(&p_s) == 0; } forceinline void FastMutex::release(void) { if (pthread_spin_unlock(&p_s) != 0) throw OperatingSystemError("FastMutex::release[pthread_spin_unlock]"); } forceinline FastMutex::~FastMutex(void) { if (pthread_spin_destroy(&p_s) != 0) { std::cerr << "Operating system error: " << "FastMutex::~FastMutex[pthread_spin_destroy]"; std::terminate(); } }
#endif
/* * Event */ forceinline Event::Event(void) : p_s(false) { if (pthread_mutex_init(&p_m,nullptr) != 0) throw OperatingSystemError("Event::Event[pthread_mutex_init]"); if (pthread_cond_init(&p_c,nullptr) != 0) throw OperatingSystemError("Event::Event[pthread_cond_init]"); } forceinline void Event::signal(void) { if (pthread_mutex_lock(&p_m) != 0) throw OperatingSystemError("Event::signal[pthread_mutex_lock]"); if (!p_s) { p_s = true; if (pthread_cond_signal(&p_c) != 0) throw OperatingSystemError("Event::signal[pthread_cond_signal]"); } if (pthread_mutex_unlock(&p_m) != 0) throw OperatingSystemError("Event::signal[pthread_mutex_unlock]"); } forceinline void Event::wait(void) { if (pthread_mutex_lock(&p_m) != 0) throw OperatingSystemError("Event::wait[pthread_mutex_lock]"); while (!p_s) if (pthread_cond_wait(&p_c,&p_m) != 0) throw OperatingSystemError("Event::wait[pthread_cond_wait]"); p_s = false; if (pthread_mutex_unlock(&p_m) != 0) throw OperatingSystemError("Event::wait[pthread_mutex_unlock]"); } forceinline Event::~Event(void) { if (pthread_cond_destroy(&p_c) != 0) { std::cerr << "Operating system error: " << "Event::~Event[pthread_cond_destroy]"; std::terminate(); } if (pthread_mutex_destroy(&p_m) != 0) { std::cerr << "Operating system error: " << "Event::~Event[pthread_mutex_destroy]"; std::terminate(); } }
/* * Thread */ forceinline void Thread::sleep(unsigned int ms) {#ifdef GECODE_HAS_UNISTD_H unsigned int s = ms / 1000; ms -= 1000 * s; if (s > 0) { // More than one million microseconds, use sleep ::sleep(s); } usleep(ms * 1000);#endif } forceinline unsigned int Thread::npu(void) {#ifdef GECODE_HAS_UNISTD_H unsigned int n=static_cast<unsigned int>(sysconf(_SC_NPROCESSORS_ONLN)); return (n>1U) ? n : 1U;#else return 1U;#endif }
}}
// STATISTICS: support-any