Few fixes after rebase

This commit is contained in:
Gioacchino Mazzurco 2020-01-06 14:46:36 +01:00
parent df87fe53b1
commit f4f7b8a1ea
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
4 changed files with 46 additions and 33 deletions

View File

@ -1,7 +1,8 @@
/*
* RetroShare JSON API
*
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2018-2020 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2019-2020 Asociación Civil Altermundi <info@altermundi.net>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
@ -588,19 +589,12 @@ bool JsonApiServer::restart()
return true;
}
void JsonApiServer::onStopRequested()
{ if(mService->is_up()) mService->stop(); }
bool JsonApiServer::fullstop()
{
if(!mService->is_up()) return true;
mService->stop();
RsThread::ask_for_stop();
while(isRunning())
{
RsDbg() << __PRETTY_FUNCTION__ << " shutting down JSON API service."
<< std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
RsThread::fullstop();
return true;
}
@ -610,7 +604,7 @@ void JsonApiServer::setBindingAddress(const std::string& bindAddress)
{ mBindingAddress = bindAddress; }
std::string JsonApiServer::getBindingAddress() const { return mBindingAddress; }
void JsonApiServer::runloop()
void JsonApiServer::run()
{
auto settings = std::make_shared<restbed::Settings>();
settings->set_port(mListeningPort);

View File

@ -1,7 +1,8 @@
/*
* RetroShare JSON API
*
* Copyright (C) 2018-2019 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2018-2020 Gioacchino Mazzurco <gio@eigenlab.org>
* Copyright (C) 2019-2020 Asociación Civil Altermundi <info@altermundi.net>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
@ -139,6 +140,10 @@ public:
const std::function<bool(const std::string&, const std::string&)>&
callback );
protected:
/// @see RsThread
void onStopRequested() override;
private:
/// @see RsThread
void run() override;
@ -187,9 +192,6 @@ private:
std::reference_wrapper<const JsonApiResourceProvider>,
std::less<const JsonApiResourceProvider> > mResourceProviders;
/// @see RsThread
void runloop() override;
std::shared_ptr<restbed::Service> mService;
uint16_t mListeningPort;

View File

@ -5,6 +5,7 @@
* *
* Copyright (C) 2004-2007 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2016-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2019-2020 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -126,7 +127,17 @@ void RsThread::fullstop()
return;
}
while(!mHasStopped); // Wait for the thread being stopped
// Wait for the thread being stopped
auto i = 1;
while(!mHasStopped)
{
std::this_thread::sleep_for(std::chrono::milliseconds(200));
++i;
if(!(i%5))
RsInfo() << __PRETTY_FUNCTION__ << " " << i*0.2 << " seconds passed"
<< " waiting for thread: " << mTid << " " << mFullName
<< " to stop" << std::endl;
}
}
bool RsThread::start(const std::string& threadName)
@ -140,14 +151,18 @@ bool RsThread::start(const std::string& threadName)
if(pError)
{
RsErr() << __PRETTY_FUNCTION__ << " pthread_create could not create"
<< " a new thread. pError: " << pError << std::endl;
<< " new thread: " << threadName << " pError: " << pError
<< std::endl;
mHasStopped = true;
print_stacktrace();
return false;
}
/* Set thread name which is restricted to 16 characters including the
* terminating null byte */
/* Store thread full name as PThread is not able to keep it entirely */
mFullName = threadName;
/* Set PThread thread name which is restricted to 16 characters
* including the terminating null byte */
if(pthread_setname_np && !threadName.empty())
RS_pthread_setname_np(mTid, threadName.substr(0, 15).c_str());
@ -253,19 +268,13 @@ RsThread::~RsThread()
{
if(isRunning())
{
RsErr() << __PRETTY_FUNCTION__ << " deleting a thread that is still "
RsErr() << __PRETTY_FUNCTION__ << " deleting thread: " << mTid << " "
<< mFullName << " that is still "
<< "running! Something seems very wrong here and RetroShare is "
<< "likely to crash because of this." << std::endl;
print_stacktrace();
askForStop();
while(isRunning())
{
RsErr() << __PRETTY_FUNCTION__ << "waiting 1s for stop..."
<< std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
fullstop();
}
}

View File

@ -4,7 +4,8 @@
* libretroshare: retroshare core library *
* *
* Copyright (C) 2004-2006 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2016-2019 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2016-2020 Gioacchino Mazzurco <gio@eigenlab.org> *
* Copyright (C) 2019-2020 Asociación Civil Altermundi <info@altermundi.net> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -182,8 +183,9 @@ public:
/**
* @brief start the thread and call run() on it.
* @param threadName string containing the name of the thread used for
* debugging purposes, it is truncated to 16 characters
* including \0 at the end of the string.
* debugging purposes, @note inside PThread it is
* truncated to 16 characters including \0 at the end of
* the string.
* @return false on error, true otherwise
*/
bool start(const std::string& threadName = "");
@ -228,6 +230,9 @@ public:
static void async(const std::function<void()>& fn)
{ std::thread(fn).detach(); }
/** @return RsThread full name */
const std::string& threadName() { return mFullName; }
protected:
/**
* This method must be implemented by sublasses, will be called once the
@ -259,6 +264,9 @@ private:
/// Store the id of the corresponding pthread
pthread_t mTid;
/// Store thread full name
std::string mFullName;
};
/**