1
0
Fork 0

starting to merge with thread.c

This commit is contained in:
Frank Celler 2011-12-21 15:15:11 +01:00
parent 5ebbeaae82
commit 2fda615a92
9 changed files with 339 additions and 293 deletions

View File

@ -104,9 +104,9 @@ namespace triagens {
ProcessIdentifier ();
public:
Thread::pid_t process;
Thread::tpid_t threadProcess;
Thread::tid_t thread;
TRI_pid_t process;
TRI_tpid_t threadProcess;
TRI_tid_t thread;
};
////////////////////////////////////////////////////////////////////////////////

View File

@ -83,7 +83,8 @@ namespace RandomHelper {
template<int N>
class RandomDeviceDirect : public RandomDevice {
public:
RandomDeviceDirect (string path) {
RandomDeviceDirect (string path)
: fd(1), pos(0) {
fd = TRI_OPEN(path.c_str(), O_RDONLY);
if (fd < 0) {
@ -147,6 +148,8 @@ namespace RandomHelper {
RandomDeviceCombined (string path)
: interval(0, UINT32_MAX),
randomGenerator(mersenneDevice, interval),
fd(0),
pos(0),
rseed(0) {
fd = TRI_OPEN(path.c_str(), O_RDONLY);
@ -255,6 +258,10 @@ namespace RandomHelper {
namespace RandomHelper {
class UniformGenerator {
private:
UniformGenerator (UniformGenerator const&);
UniformGenerator& operator= (UniformGenerator const&);
public:
UniformGenerator (RandomDevice* device)
: device(device) {
@ -262,6 +269,11 @@ namespace RandomHelper {
virtual ~UniformGenerator () {
}
int32_t random (int32_t left, int32_t right) {
if (left >= right) {
return left;

View File

@ -43,11 +43,11 @@ using namespace triagens::basics;
/// @brief constructs a read-write lock
////////////////////////////////////////////////////////////////////////////////
ReadWriteLock::ReadWriteLock () {
ReadWriteLock::ReadWriteLock ()
: _rwlock(),
_writeLocked(false) {
TRI_InitReadWriteLock(&_rwlock);
_writeLocked = false;
#ifdef TRI_READ_WRITE_LOCK_COUNTER
TRI_InitMutex(&_mutex);

View File

@ -1,221 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Abstract Base Class for Threads
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2010-2011 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
/// @author Achim Brandt
/// @author Copyright 2008-2011, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_JUTLAND_BASICS_THREAD_POSIX_H
#define TRIAGENS_JUTLAND_BASICS_THREAD_POSIX_H 1
#include <Basics/Common.h>
////////////////////////////////////////////////////////////////////////////////
/// @defgroup Threads Threads, Locks, and Conditions
////////////////////////////////////////////////////////////////////////////////
namespace triagens {
namespace basics {
class ConditionVariable;
////////////////////////////////////////////////////////////////////////////////
/// @ingroup Threads
/// @brief abstract base class for threads
///
/// Each subclass must implement this method. A thread can be started by start
/// and is stopped either when the method run ends or when stop is called.
////////////////////////////////////////////////////////////////////////////////
class Thread {
public:
Thread (Thread const&);
Thread& operator= (Thread const&);
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief process id
////////////////////////////////////////////////////////////////////////////////
typedef ::pid_t pid_t;
////////////////////////////////////////////////////////////////////////////////
/// @brief thread process identifier
////////////////////////////////////////////////////////////////////////////////
typedef int tpid_t;
////////////////////////////////////////////////////////////////////////////////
/// @brief thread identifier
////////////////////////////////////////////////////////////////////////////////
typedef intptr_t tid_t;
////////////////////////////////////////////////////////////////////////////////
/// @brief thread
////////////////////////////////////////////////////////////////////////////////
typedef ::pthread_t thread_t;
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief thread local storage key
////////////////////////////////////////////////////////////////////////////////
typedef pthread_key_t tlsKey;
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the process id
////////////////////////////////////////////////////////////////////////////////
static pid_t currentProcessId ();
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the thread process id
////////////////////////////////////////////////////////////////////////////////
static tpid_t currentThreadProcessId ();
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the thread id
////////////////////////////////////////////////////////////////////////////////
static tid_t currentThreadId ();
////////////////////////////////////////////////////////////////////////////////
/// @brief creates a thread local storage
////////////////////////////////////////////////////////////////////////////////
static bool createKey (tlsKey& key, void (*destructor)(void*));
////////////////////////////////////////////////////////////////////////////////
/// @brief gets a thread local storage
////////////////////////////////////////////////////////////////////////////////
static void* specific (tlsKey& key);
////////////////////////////////////////////////////////////////////////////////
/// @brief sets a thread local storage
////////////////////////////////////////////////////////////////////////////////
static bool setSpecific (tlsKey& key, void*);
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief constructs a thread
////////////////////////////////////////////////////////////////////////////////
explicit
Thread (const string& name);
////////////////////////////////////////////////////////////////////////////////
/// @brief deletes the thread
////////////////////////////////////////////////////////////////////////////////
virtual ~Thread ();
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief getter for running
////////////////////////////////////////////////////////////////////////////////
bool isRunning ();
////////////////////////////////////////////////////////////////////////////////
/// @brief returns a thread identifier
////////////////////////////////////////////////////////////////////////////////
tid_t threadId ();
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief start the thread
////////////////////////////////////////////////////////////////////////////////
bool start (ConditionVariable * _finishedCondition = 0);
////////////////////////////////////////////////////////////////////////////////
/// @brief stop the thread
////////////////////////////////////////////////////////////////////////////////
void stop ();
////////////////////////////////////////////////////////////////////////////////
/// @brief join the thread
////////////////////////////////////////////////////////////////////////////////
void join ();
////////////////////////////////////////////////////////////////////////////////
/// @brief send signal to thread
////////////////////////////////////////////////////////////////////////////////
void sendSignal (int signal);
protected:
////////////////////////////////////////////////////////////////////////////////
/// @brief allows the descendent object to perform cleanup
////////////////////////////////////////////////////////////////////////////////
virtual void cleanup () {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief the thread program
////////////////////////////////////////////////////////////////////////////////
virtual void run () = 0;
////////////////////////////////////////////////////////////////////////////////
/// @brief allows asynchrounous cancelation
////////////////////////////////////////////////////////////////////////////////
void allowAsynchronousCancelation ();
private:
static void* startThread (void* arg);
void runMe ();
private:
const string _name;
bool _asynchronousCancelation;
pthread_t _thread;
ConditionVariable* _finishedCondition;
volatile sig_atomic_t _started;
volatile sig_atomic_t _running;
};
}
}
#endif

View File

@ -34,6 +34,24 @@
#include <Basics/ConditionLocker.h>
#include <Basics/Logger.h>
using namespace triagens::basics;
void* Thread::startThread (void* arg) {
sigset_t all;
sigfillset(&all);
pthread_sigmask(SIG_SETMASK, &all, 0);
Thread * ptr = (Thread *) arg;
ptr->runMe();
ptr->cleanup();
return 0;
}
namespace triagens {
namespace basics {
@ -41,42 +59,20 @@ namespace triagens {
// static public methods
// -----------------------------------------------------------------------------
bool Thread::createKey (tlsKey& key, void (*destructor)(void*)) {
return pthread_key_create(&key, destructor) == 0;
TRI_pid_t Thread::currentProcessId () {
return TRI_CurrentProcessId();
}
void* Thread::specific (tlsKey& key) {
return pthread_getspecific(key);
TRI_pid_t Thread::currentThreadProcessId () {
return TRI_CurrentThreadProcessId();
}
bool Thread::setSpecific (tlsKey& key, void* value) {
return pthread_setspecific(key, value) == 0;
}
Thread::pid_t Thread::currentProcessId () {
return ::getpid();
}
Thread::pid_t Thread::currentThreadProcessId () {
#ifdef TRI_HAVE_GETTID
return ::gettid();
#else
return ::getpid();
#endif
}
Thread::tid_t Thread::currentThreadId () {
return (intptr_t) pthread_self();
TRI_tid_t Thread::currentThreadId () {
return TRI_CurrentThreadId();
}
// -----------------------------------------------------------------------------
@ -128,6 +124,8 @@ namespace triagens {
return false;
}
_started = 1;
int rc = pthread_create(&_thread, 0, &startThread, this);
if (rc != 0) {
@ -153,7 +151,7 @@ namespace triagens {
void Thread::join () {
pthread_join(_thread, 0);
TRI_JoinThread(&_thread);
}
@ -196,24 +194,6 @@ namespace triagens {
// private methods
// -----------------------------------------------------------------------------
void* Thread::startThread (void* arg) {
sigset_t all;
sigfillset(&all);
pthread_sigmask(SIG_SETMASK, &all, 0);
Thread * ptr = (Thread *) arg;
ptr->_started = 1;
ptr->runMe();
ptr->cleanup();
return 0;
}
void Thread::runMe () {
if (_asynchronousCancelation) {
LOGGER_DEBUG << "set asynchronous cancelation for " << _name;

View File

@ -1,5 +1,5 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Abstract Base Class for Threads
/// @brief threads
///
/// @file
///
@ -26,17 +26,294 @@
/// @author Copyright 2008-2011, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_JUTLAND_BASICS_THREAD_H
#define TRIAGENS_JUTLAND_BASICS_THREAD_H 1
#ifndef TRIAGENS_JUTLAND_BASICS_THREAD_POSIX_H
#define TRIAGENS_JUTLAND_BASICS_THREAD_POSIX_H 1
#include <Basics/Common.h>
#ifdef TRI_HAVE_POSIX_THREADS
#include <Basics/Thread-posix.h>
#endif
#include <BasicsC/threads.h>
#ifdef TRI_HAVE_WIN32_THREADS
#include <Basics/Thread-win32.h>
#endif
// -----------------------------------------------------------------------------
// --SECTION-- class Thread
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
namespace triagens {
namespace basics {
class ConditionVariable;
////////////////////////////////////////////////////////////////////////////////
/// @brief thread
///
/// Each subclass must implement a method run. A thread can be started by
/// start and is stopped either when the method run ends or when stop is
/// called.
////////////////////////////////////////////////////////////////////////////////
class Thread {
public:
Thread (Thread const&);
Thread& operator= (Thread const&);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- static public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the process id
////////////////////////////////////////////////////////////////////////////////
static TRI_pid_t currentProcessId ();
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the thread process id
////////////////////////////////////////////////////////////////////////////////
static TRI_tpid_t currentThreadProcessId ();
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the thread id
////////////////////////////////////////////////////////////////////////////////
static TRI_tid_t currentThreadId ();
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief constructs a thread
////////////////////////////////////////////////////////////////////////////////
explicit
Thread (const string& name);
////////////////////////////////////////////////////////////////////////////////
/// @brief deletes the thread
////////////////////////////////////////////////////////////////////////////////
virtual ~Thread ();
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief getter for running
////////////////////////////////////////////////////////////////////////////////
bool isRunning ();
////////////////////////////////////////////////////////////////////////////////
/// @brief returns a thread identifier
////////////////////////////////////////////////////////////////////////////////
TRI_tid_t threadId ();
////////////////////////////////////////////////////////////////////////////////
/// @brief starts the thread
////////////////////////////////////////////////////////////////////////////////
bool start (ConditionVariable * _finishedCondition = 0);
////////////////////////////////////////////////////////////////////////////////
/// @brief stops the thread
////////////////////////////////////////////////////////////////////////////////
void stop ();
////////////////////////////////////////////////////////////////////////////////
/// @brief joins the thread
////////////////////////////////////////////////////////////////////////////////
void join ();
////////////////////////////////////////////////////////////////////////////////
/// @brief send signal to thread
////////////////////////////////////////////////////////////////////////////////
void sendSignal (int signal);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- protected methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief allows asynchrounous cancelation
////////////////////////////////////////////////////////////////////////////////
void allowAsynchronousCancelation ();
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- virtual protected methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
protected:
////////////////////////////////////////////////////////////////////////////////
/// @brief allows the sub-class to perform cleanup
////////////////////////////////////////////////////////////////////////////////
virtual void cleanup () {
}
////////////////////////////////////////////////////////////////////////////////
/// @brief the thread program
////////////////////////////////////////////////////////////////////////////////
virtual void run () = 0;
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- static private methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief static started with access to the private variables
////////////////////////////////////////////////////////////////////////////////
static void* startThread (void* arg);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- private methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief runner
////////////////////////////////////////////////////////////////////////////////
void runMe ();
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Threading
/// @{
////////////////////////////////////////////////////////////////////////////////
private:
////////////////////////////////////////////////////////////////////////////////
/// @brief name of the thread
////////////////////////////////////////////////////////////////////////////////
const string _name;
////////////////////////////////////////////////////////////////////////////////
/// @brief asynchronous cancelation is allowed
////////////////////////////////////////////////////////////////////////////////
bool _asynchronousCancelation;
////////////////////////////////////////////////////////////////////////////////
/// @brief thread pointer
////////////////////////////////////////////////////////////////////////////////
TRI_thread_t _thread;
////////////////////////////////////////////////////////////////////////////////
/// @brief condition variable for done
////////////////////////////////////////////////////////////////////////////////
ConditionVariable* _finishedCondition;
////////////////////////////////////////////////////////////////////////////////
/// @brief status
////////////////////////////////////////////////////////////////////////////////
volatile sig_atomic_t _started;
////////////////////////////////////////////////////////////////////////////////
/// @brief status
////////////////////////////////////////////////////////////////////////////////
volatile sig_atomic_t _running;
};
}
}
#endif

View File

@ -36,12 +36,12 @@ namespace triagens {
struct TimingImpl {
TimingImpl (Timing::TimingType type)
: type(type) {
: tv(), tv2(), type(type) {
fill(&tv);
}
TimingImpl (timeval tv, Timing::TimingType type)
: tv(tv), type(type) {
: tv(tv), tv2(), type(type) {
}
void fill (::timeval* t);

View File

@ -55,7 +55,8 @@ namespace triagens {
// constructors and destructors
// -----------------------------------------------------------------------------
VariantArray::VariantArray () {
VariantArray::VariantArray ()
: attributes(), mapping(), nextKey() {
}

View File

@ -43,12 +43,9 @@ namespace triagens {
VariantBlob::VariantBlob (char const* pointer, size_t len) {
if (pointer == 0) {
value = 0;
length = 0;
}
else {
VariantBlob::VariantBlob (char const* pointer, size_t len)
: value(0), length(0) {
if (pointer != 0) {
value = new char[len];
memcpy(value, pointer, len);
length = len;