mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-20 20:31:46 -05:00
74961774cc
* Implemented start/stop interface in udpbitdht and bemanager * added Restart of DHT if it fails to get going. * added start / stop functionality to bdnode and bdstore * added cleanup code to rest of bitdht classes. * reworked NetworkSize calc functions. * added thread debugging (prints at start / stop / join). * TESTS: added utest.h header file for automated unit testing (from libretroshare) * TESTS: bdmetric_test started conversion to automated tests * TESTS: udpbitdht_nettest. Added dht start / stop and network reset (thread join) functionality. * TESTS: fresh bdboot.txt git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3678 b45a01b8-16f6-495d-af2f-9b41ad6348cc
173 lines
3.3 KiB
C++
173 lines
3.3 KiB
C++
/*
|
|
* util/bdthreads.cc
|
|
*
|
|
* BitDHT: An Flexible DHT library.
|
|
*
|
|
* Copyright 2004-2010 by Robert Fernie
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Library General Public
|
|
* License Version 3 as published by the Free Software Foundation.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Library General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Library General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
* USA.
|
|
*
|
|
* Please report all bugs and problems to "bitdht@lunamutt.com".
|
|
*
|
|
*/
|
|
|
|
|
|
|
|
#include "bdthreads.h"
|
|
#include <unistd.h> /* for usleep() */
|
|
|
|
/*******
|
|
* #define DEBUG_THREADS 1
|
|
*******/
|
|
|
|
#define DEBUG_THREADS 1
|
|
|
|
#ifdef DEBUG_THREADS
|
|
#include <iostream>
|
|
#endif
|
|
|
|
extern "C" void* bdthread_init(void* p)
|
|
{
|
|
#ifdef DEBUG_THREADS
|
|
std::cerr << "bdthread_init()";
|
|
std::cerr << std::endl;
|
|
#endif
|
|
|
|
bdThread *thread = (bdThread *) p;
|
|
if (!thread)
|
|
{
|
|
#ifdef DEBUG_THREADS
|
|
std::cerr << "bdthread_init() Error Invalid thread pointer.";
|
|
std::cerr << std::endl;
|
|
#endif
|
|
return 0;
|
|
}
|
|
thread -> run();
|
|
return 0;
|
|
}
|
|
|
|
|
|
pthread_t createThread(bdThread &thread)
|
|
{
|
|
pthread_t tid;
|
|
void *data = (void *) (&thread);
|
|
|
|
#ifdef DEBUG_THREADS
|
|
std::cerr << "createThread() creating a bdThread";
|
|
std::cerr << std::endl;
|
|
#endif
|
|
|
|
thread.mMutex.lock();
|
|
{
|
|
pthread_create(&tid, 0, &bdthread_init, data);
|
|
thread.mTid = tid;
|
|
}
|
|
|
|
#ifdef DEBUG_THREADS
|
|
std::cerr << "createThread() created Thread.mTid: ";
|
|
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
|
std::cerr << "WIN32: Cannot print mTid ";
|
|
#else
|
|
std::cerr << thread.mTid;
|
|
#endif
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
thread.mMutex.unlock();
|
|
|
|
|
|
|
|
return tid;
|
|
|
|
}
|
|
|
|
bdThread::bdThread()
|
|
{
|
|
|
|
#ifdef DEBUG_THREADS
|
|
std::cerr << "bdThread::bdThread()";
|
|
std::cerr << std::endl;
|
|
#endif
|
|
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
|
memset (&mTid, 0, sizeof(mTid));
|
|
#else
|
|
mTid = 0;
|
|
#endif
|
|
}
|
|
|
|
void bdThread::join() /* waits for the the mTid thread to stop */
|
|
{
|
|
#ifdef DEBUG_THREADS
|
|
std::cerr << "bdThread::join() Called! Waiting for Thread.mTid: ";
|
|
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
|
std::cerr << "WIN32: Cannot print mTid ";
|
|
#else
|
|
std::cerr << mTid;
|
|
#endif
|
|
|
|
std::cerr << std::endl;
|
|
#endif
|
|
|
|
mMutex.lock();
|
|
{
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
|
/* Its a struct in Windows compile and the member .p ist checked in the pthreads library */
|
|
#else
|
|
if(mTid > 0)
|
|
#endif
|
|
pthread_join(mTid, NULL);
|
|
|
|
#ifdef DEBUG_THREADS
|
|
std::cerr << "bdThread::join() Joined Thread.mTid: ";
|
|
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
|
std::cerr << "WIN32: Cannot print mTid ";
|
|
#else
|
|
std::cerr << mTid;
|
|
#endif
|
|
|
|
std::cerr << std::endl;
|
|
std::cerr << "bdThread::join() Setting mTid = 0";
|
|
std::cerr << std::endl;
|
|
#endif
|
|
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
|
memset (&mTid, 0, sizeof(mTid));
|
|
#else
|
|
mTid = 0;
|
|
#endif
|
|
|
|
}
|
|
mMutex.unlock();
|
|
|
|
}
|
|
|
|
void bdThread::stop()
|
|
{
|
|
#ifdef DEBUG_THREADS
|
|
std::cerr << "bdThread::stop() Called!";
|
|
std::cerr << std::endl;
|
|
#endif
|
|
|
|
pthread_exit(NULL);
|
|
}
|
|
|
|
|
|
|