thread.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2009
*
* Bugfixes provided by:
* David Rijsman <david.rijsman@quintiq.com>
*
* 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.
*
*/
#include <cstddef>
#ifdef GECODE_THREADS_WINDOWS
#ifndef NOMINMAX
# define NOMINMAX
#endif
#ifndef _WIN32_WINNT
# define _WIN32_WINNT 0x400
#endif
#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
#ifdef GECODE_THREADS_PTHREADS
#include <pthread.h>
#ifdef GECODE_THREADS_OSX_UNFAIR
#include <os/lock.h>
#include <libkern/OSAtomic.h>
#endif
#endif
namespace Gecode { namespace Support {
class Mutex {
private:
#if defined(GECODE_THREADS_WINDOWS)
CRITICAL_SECTION w_cs;
#elif defined(GECODE_THREADS_OSX_UNFAIR)
union {
os_unfair_lock unfair_lck;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
OSSpinLock spin_lck;
#pragma clang diagnostic pop
} u;
#elif defined(GECODE_THREADS_PTHREADS)
pthread_mutex_t p_m;
#else
#error No suitable mutex implementation found
#endif
public:
Mutex(void);
void acquire(void);
bool tryacquire(void);
void release(void);
~Mutex(void);
static void* operator new(size_t s);
static void operator delete(void* p);
private:
Mutex(const Mutex&) {}
void operator=(const Mutex&) {}
};
#ifndef GECODE_THREADS_PTHREADS_SPINLOCK
typedef Mutex FastMutex;
#else
class FastMutex {
private:
pthread_spinlock_t p_s;
public:
FastMutex(void);
void acquire(void);
bool tryacquire(void);
void release(void);
~FastMutex(void);
static void* operator new(size_t s);
static void operator delete(void* p);
private:
FastMutex(const FastMutex&) {}
void operator=(const FastMutex&) {}
};
#endif
class Lock {
private:
Mutex& m;
public:
Lock(Mutex& m0);
~Lock(void);
private:
Lock(const Lock& l) : m(l.m) {}
void operator=(const Lock&) {}
};
class Event {
private:
#ifdef GECODE_THREADS_WINDOWS
HANDLE w_h;
#endif
#ifdef GECODE_THREADS_PTHREADS
pthread_mutex_t p_m;
pthread_cond_t p_c;
bool p_s;
#endif
public:
Event(void);
void signal(void);
void wait(void);
~Event(void);
private:
Event(const Event&) {}
void operator=(const Event&) {}
};
class Terminator {
public:
virtual ~Terminator() {}
virtual void terminated(void) = 0;
};
class Runnable {
private:
bool d;
public:
Runnable(bool d=true);
void todelete(bool d);
bool todelete(void) const;
virtual Terminator* terminator(void) const { return NULL; }
virtual void run(void) = 0;
virtual ~Runnable(void) {}
static void* operator new(size_t s);
static void operator delete(void* p);
};
class Thread {
public:
class Run {
public:
Run* n;
Runnable* r;
Event e;
Mutex m;
GECODE_SUPPORT_EXPORT Run(Runnable* r);
GECODE_SUPPORT_EXPORT void exec(void);
void run(Runnable* r);
static void* operator new(size_t s);
static void operator delete(void* p);
};
GECODE_SUPPORT_EXPORT static Mutex* m(void);
GECODE_SUPPORT_EXPORT static Run* idle;
public:
static void run(Runnable* r);
static void sleep(unsigned int ms);
static unsigned int npu(void);
private:
Thread(const Thread&) {}
void operator=(const Thread&) {}
};
}}
// STATISTICS: support-any
Gecode::Support::Event
An event for synchronization.
Definition thread.hpp:215
Gecode::Support::Event::wait
void wait(void)
Wait until the event becomes signalled.
Definition none.hpp:61
Gecode::Support::Event::Event
Event(void)
Initialize event.
Definition none.hpp:57
Gecode::Support::Event::~Event
~Event(void)
Delete event.
Definition none.hpp:63
Gecode::Support::Event::signal
void signal(void)
Signal the event.
Definition none.hpp:59
Gecode::Support::Lock::~Lock
~Lock(void)
Leave lock.
Definition thread.hpp:99
Gecode::Support::Lock::Lock
Lock(Mutex &m0)
Enter lock.
Definition thread.hpp:95
Gecode::Support::Mutex
A mutex for mutual exclausion among several threads.
Definition thread.hpp:96
Gecode::Support::Mutex::Mutex
Mutex(void)
Initialize mutex.
Definition none.hpp:40
Gecode::Support::Mutex::~Mutex
~Mutex(void)
Delete mutex.
Definition none.hpp:50
Gecode::Support::Mutex::release
void release(void)
Release the mutex.
Definition none.hpp:48
Gecode::Support::Mutex::tryacquire
bool tryacquire(void)
Try to acquire the mutex, return true if succesful.
Definition none.hpp:44
Gecode::Support::Mutex::acquire
void acquire(void)
Acquire the mutex and possibly block.
Definition none.hpp:42
Gecode::Support::Runnable
An interface for objects that can be run by a thread.
Definition thread.hpp:264
Gecode::Support::Runnable::Runnable
Runnable(bool d=true)
Initialize, d defines whether object is deleted when terminated.
Definition thread.hpp:40
Gecode::Support::Runnable::todelete
bool todelete(void) const
Return whether to be deleted upon termination.
Definition thread.hpp:47
Gecode::Support::Runnable::run
virtual void run(void)=0
The function that is executed when the thread starts.
Gecode::Support::Runnable::terminator
virtual Terminator * terminator(void) const
Return terminator object.
Definition thread.hpp:276
Gecode::Support::Runnable::~Runnable
virtual ~Runnable(void)
Destructor.
Definition thread.hpp:280
Gecode::Support::Terminator
An interface for objects that can be called after a thread has terminated (after running the thread's...
Definition thread.hpp:251
Gecode::Support::Terminator::~Terminator
virtual ~Terminator()
Destructor.
Definition thread.hpp:254
Gecode::Support::Terminator::terminated
virtual void terminated(void)=0
The function that is called when the thread has terminated.
Gecode::Support::Thread::Run
A real thread.
Definition thread.hpp:299
Gecode::Support::Thread::Run::run
void run(Runnable *r)
Run a runnable object.
Definition thread.hpp:108
Gecode::Support::Thread::Run::exec
void exec(void)
Infinite loop for execution.
Definition thread.cpp:50
Gecode::Support::Thread::Run::e
Event e
Event to wait for next runnable object to execute.
Definition thread.hpp:306
Gecode::Support::Thread::Run::n
Run * n
Next idle thread.
Definition thread.hpp:302
Gecode::Support::Thread::Run::r
Runnable * r
Runnable object to execute.
Definition thread.hpp:304
Gecode::Support::Thread::Run::Run
Run(Runnable *r)
Create a new thread.
Definition none.hpp:70
Gecode::Support::Thread::Run::m
Mutex m
Mutex for synchronization.
Definition thread.hpp:308
Gecode::Support::Thread
Simple threads.
Definition thread.hpp:296
Gecode::Support::Thread::idle
static Run * idle
Idle runners.
Definition thread.hpp:323
Gecode::Support::Thread::run
static void run(Runnable *r)
Construct a new thread and run r.
Definition thread.hpp:115
Gecode::Support::Thread::sleep
static void sleep(unsigned int ms)
Put current thread to sleep for ms milliseconds.
Definition none.hpp:74
Gecode::Support::Thread::m
static Mutex * m(void)
Mutex for synchronization.
Definition thread.cpp:42
Gecode::Support::Thread::npu
static unsigned int npu(void)
Return number of processing units (1 if information not available).
Definition none.hpp:76
Gecode::Support
Support algorithms and datastructures
Gecode::Support::FastMutex
Mutex FastMutex
Definition thread.hpp:140
Gecode
Gecode toplevel namespace
GECODE_SUPPORT_EXPORT
#define GECODE_SUPPORT_EXPORT
Definition support.hh:71