Fixes from develop experimental (on top of 1.11.2) (#3017)

* MongoDB::PooledConnection: Prevent unwanted release by disabling copy semantics. Enabled move semantics for C++11.

* Construct MongoDB::Cursor from aggragation cursor.

* Added function to get OS-specific numerical thread ID and %J pattern to use numerical OS thread id in pattern formatter.

Co-authored-by: Tomaz Beltram <tomaz.beltram@topit.si>
This commit is contained in:
Matej Kenda
2022-05-19 00:23:16 +02:00
committed by GitHub
parent 5bac3e303f
commit 8a8c23c352
19 changed files with 102 additions and 6 deletions

View File

@@ -132,6 +132,9 @@ public:
long getTid() const; long getTid() const;
/// Returns the numeric thread identifier for the message. /// Returns the numeric thread identifier for the message.
long getOsTid() const;
/// Returns the numeric thread identifier for the message.
void setPid(long pid); void setPid(long pid);
/// Sets the process identifier for the message. /// Sets the process identifier for the message.
@@ -200,6 +203,7 @@ private:
Priority _prio; Priority _prio;
Timestamp _time; Timestamp _time;
long _tid; long _tid;
long _ostid;
std::string _thread; std::string _thread;
long _pid; long _pid;
const char* _file; const char* _file;
@@ -246,6 +250,10 @@ inline long Message::getTid() const
return _tid; return _tid;
} }
inline long Message::getOsTid() const
{
return _ostid;
}
inline long Message::getPid() const inline long Message::getPid() const
{ {

View File

@@ -43,6 +43,7 @@ class Foundation_API PatternFormatter: public Formatter
/// * %P - message process identifier /// * %P - message process identifier
/// * %T - message thread name /// * %T - message thread name
/// * %I - message thread identifier (numeric) /// * %I - message thread identifier (numeric)
/// * %J - message thread OS identifier (numeric)
/// * %N - node or host name /// * %N - node or host name
/// * %U - message source file path (empty string if not set) /// * %U - message source file path (empty string if not set)
/// * %O - message source file filename (empty string if not set) /// * %O - message source file filename (empty string if not set)

View File

@@ -227,6 +227,9 @@ public:
static TID currentTid(); static TID currentTid();
/// Returns the native thread ID for the current thread. /// Returns the native thread ID for the current thread.
static long currentOsTid();
/// Returns the operating system specific thread ID for the current thread.
protected: protected:
ThreadLocalStorage& tls(); ThreadLocalStorage& tls();
/// Returns a reference to the thread's local storage. /// Returns a reference to the thread's local storage.
@@ -378,6 +381,10 @@ inline Thread::TID Thread::currentTid()
return currentTidImpl(); return currentTidImpl();
} }
inline long Thread::currentOsTid()
{
return currentOsTidImpl();
}
} // namespace Poco } // namespace Poco

View File

@@ -80,6 +80,7 @@ public:
static void yieldImpl(); static void yieldImpl();
static ThreadImpl* currentImpl(); static ThreadImpl* currentImpl();
static TIDImpl currentTidImpl(); static TIDImpl currentTidImpl();
static long currentOsTidImpl();
protected: protected:
static void* runnableEntry(void* pThread); static void* runnableEntry(void* pThread);

View File

@@ -88,6 +88,7 @@ public:
static void yieldImpl(); static void yieldImpl();
static ThreadImpl* currentImpl(); static ThreadImpl* currentImpl();
static TIDImpl currentTidImpl(); static TIDImpl currentTidImpl();
static long currentOsTidImpl();
protected: protected:
static void runnableEntry(void* pThread, int, int, int, int, int, int, int, int, int); static void runnableEntry(void* pThread, int, int, int, int, int, int, int, int, int);

View File

@@ -73,6 +73,7 @@ public:
static void yieldImpl(); static void yieldImpl();
static ThreadImpl* currentImpl(); static ThreadImpl* currentImpl();
static TIDImpl currentTidImpl(); static TIDImpl currentTidImpl();
static long currentOsTidImpl();
protected: protected:
#if defined(_DLL) #if defined(_DLL)

View File

@@ -73,6 +73,7 @@ public:
static void yieldImpl(); static void yieldImpl();
static ThreadImpl* currentImpl(); static ThreadImpl* currentImpl();
static TIDImpl currentTidImpl(); static TIDImpl currentTidImpl();
static long currentOsTidImpl();
protected: protected:
static DWORD WINAPI runnableEntry(LPVOID pThread); static DWORD WINAPI runnableEntry(LPVOID pThread);

View File

@@ -31,7 +31,7 @@ int main(int argc, char** argv)
{ {
// set up two channel chains - one to the // set up two channel chains - one to the
// console and the other one to a log file. // console and the other one to a log file.
FormattingChannel* pFCConsole = new FormattingChannel(new PatternFormatter("%s: %p: %t")); FormattingChannel* pFCConsole = new FormattingChannel(new PatternFormatter("[%O] %s: %p: %t"));
pFCConsole->setChannel(new ConsoleChannel); pFCConsole->setChannel(new ConsoleChannel);
pFCConsole->open(); pFCConsole->open();

View File

@@ -27,6 +27,7 @@ namespace Poco {
Message::Message(): Message::Message():
_prio(PRIO_FATAL), _prio(PRIO_FATAL),
_tid(0), _tid(0),
_ostid(0),
_pid(0), _pid(0),
_file(0), _file(0),
_line(0), _line(0),
@@ -41,6 +42,7 @@ Message::Message(const std::string& source, const std::string& text, Priority pr
_text(text), _text(text),
_prio(prio), _prio(prio),
_tid(0), _tid(0),
_ostid(0),
_pid(0), _pid(0),
_file(0), _file(0),
_line(0), _line(0),
@@ -55,6 +57,7 @@ Message::Message(const std::string& source, const std::string& text, Priority pr
_text(text), _text(text),
_prio(prio), _prio(prio),
_tid(0), _tid(0),
_ostid(0),
_pid(0), _pid(0),
_file(file), _file(file),
_line(line), _line(line),
@@ -70,6 +73,7 @@ Message::Message(const Message& msg):
_prio(msg._prio), _prio(msg._prio),
_time(msg._time), _time(msg._time),
_tid(msg._tid), _tid(msg._tid),
_ostid(msg._ostid),
_thread(msg._thread), _thread(msg._thread),
_pid(msg._pid), _pid(msg._pid),
_file(msg._file), _file(msg._file),
@@ -88,6 +92,7 @@ Message::Message(Message&& msg) noexcept:
_prio(std::move(msg._prio)), _prio(std::move(msg._prio)),
_time(std::move(msg._time)), _time(std::move(msg._time)),
_tid(std::move(msg._tid)), _tid(std::move(msg._tid)),
_ostid(std::move(msg._ostid)),
_thread(std::move(msg._thread)), _thread(std::move(msg._thread)),
_pid(std::move(msg._pid)), _pid(std::move(msg._pid)),
_file(std::move(msg._file)), _file(std::move(msg._file)),
@@ -104,6 +109,7 @@ Message::Message(const Message& msg, const std::string& text):
_prio(msg._prio), _prio(msg._prio),
_time(msg._time), _time(msg._time),
_tid(msg._tid), _tid(msg._tid),
_ostid(msg._ostid),
_thread(msg._thread), _thread(msg._thread),
_pid(msg._pid), _pid(msg._pid),
_file(msg._file), _file(msg._file),
@@ -127,6 +133,7 @@ void Message::init()
#if !defined(POCO_VXWORKS) #if !defined(POCO_VXWORKS)
_pid = Process::id(); _pid = Process::id();
#endif #endif
_ostid = (IntPtr)Thread::currentOsTid();
Thread* pThread = Thread::current(); Thread* pThread = Thread::current();
if (pThread) if (pThread)
{ {
@@ -154,6 +161,7 @@ Message& Message::operator = (Message&& msg) noexcept
_prio = std::move(msg._prio); _prio = std::move(msg._prio);
_time = std::move(msg._time); _time = std::move(msg._time);
_tid = std::move(msg._tid); _tid = std::move(msg._tid);
_ostid = std::move(msg._ostid);
_thread = std::move(msg._thread); _thread = std::move(msg._thread);
_pid = std::move(msg._pid); _pid = std::move(msg._pid);
_file = std::move(msg._file); _file = std::move(msg._file);
@@ -173,6 +181,7 @@ void Message::swap(Message& msg)
swap(_prio, msg._prio); swap(_prio, msg._prio);
swap(_time, msg._time); swap(_time, msg._time);
swap(_tid, msg._tid); swap(_tid, msg._tid);
swap(_ostid, msg._ostid);
swap(_thread, msg._thread); swap(_thread, msg._thread);
swap(_pid, msg._pid); swap(_pid, msg._pid);
swap(_file, msg._file); swap(_file, msg._file);

View File

@@ -80,6 +80,7 @@ void PatternFormatter::format(const Message& msg, std::string& text)
case 'P': NumberFormatter::append(text, msg.getPid()); break; case 'P': NumberFormatter::append(text, msg.getPid()); break;
case 'T': text.append(msg.getThread()); break; case 'T': text.append(msg.getThread()); break;
case 'I': NumberFormatter::append(text, msg.getTid()); break; case 'I': NumberFormatter::append(text, msg.getTid()); break;
case 'J': NumberFormatter::append(text, msg.getOsTid()); break;
case 'N': text.append(Environment::nodeName()); break; case 'N': text.append(Environment::nodeName()); break;
case 'U': text.append(msg.getSourceFile() ? msg.getSourceFile() : ""); break; case 'U': text.append(msg.getSourceFile() ? msg.getSourceFile() : ""); break;
case 'O': text.append(msg.getSourceFile() ? Path(msg.getSourceFile()).getFileName() : ""); break; case 'O': text.append(msg.getSourceFile() ? Path(msg.getSourceFile()).getFileName() : ""); break;

View File

@@ -28,6 +28,12 @@
# include <time.h> # include <time.h>
#endif #endif
#if POCO_OS == POCO_OS_LINUX
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <sys/syscall.h> /* For SYS_xxx definitions */
#endif
// //
// Block SIGPIPE in main thread. // Block SIGPIPE in main thread.
// //
@@ -262,6 +268,16 @@ ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
return pthread_self(); return pthread_self();
} }
long ThreadImpl::currentOsTidImpl()
{
#if POCO_OS == POCO_OS_LINUX
return ::syscall(SYS_gettid);
#elif POCO_OS == POCO_OS_MAC_OS_X
return ::pthread_mach_thread_np(::pthread_self());
#else
return ::pthread_self();
#endif
}
void ThreadImpl::sleepImpl(long milliseconds) void ThreadImpl::sleepImpl(long milliseconds)
{ {

View File

@@ -144,6 +144,10 @@ ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
return taskIdSelf(); return taskIdSelf();
} }
long ThreadImpl::currentOsTidImpl()
{
return taskIdSelf();
}
void ThreadImpl::sleepImpl(long milliseconds) void ThreadImpl::sleepImpl(long milliseconds)
{ {

View File

@@ -189,6 +189,10 @@ ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
return GetCurrentThreadId(); return GetCurrentThreadId();
} }
long ThreadImpl::currentOsTidImpl()
{
return GetCurrentThreadId();
}
#if defined(_DLL) #if defined(_DLL)
DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread) DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread)

View File

@@ -142,6 +142,10 @@ ThreadImpl::TIDImpl ThreadImpl::currentTidImpl()
return GetCurrentThreadId(); return GetCurrentThreadId();
} }
long ThreadImpl::currentOsTidImpl()
{
return GetCurrentThreadId();
}
DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread) DWORD WINAPI ThreadImpl::runnableEntry(LPVOID pThread)
{ {

View File

@@ -23,7 +23,6 @@
#include "Poco/MongoDB/QueryRequest.h" #include "Poco/MongoDB/QueryRequest.h"
#include "Poco/MongoDB/ResponseMessage.h" #include "Poco/MongoDB/ResponseMessage.h"
namespace Poco { namespace Poco {
namespace MongoDB { namespace MongoDB {
@@ -38,6 +37,9 @@ public:
Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags = QueryRequest::QUERY_DEFAULT); Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags = QueryRequest::QUERY_DEFAULT);
/// Creates a Cursor for the given database and collection ("database.collection"), using the specified flags. /// Creates a Cursor for the given database and collection ("database.collection"), using the specified flags.
Cursor(const Document& aggregationResponse);
/// Creates a Cursor for the given agregation query response.
virtual ~Cursor(); virtual ~Cursor();
/// Destroys the Cursor. /// Destroys the Cursor.

View File

@@ -116,7 +116,24 @@ public:
return _connection; return _connection;
} }
#if defined(POCO_ENABLE_CPP11)
// Disable copy to prevent unwanted release of resources: C++11 way
PooledConnection(const PooledConnection&) = delete;
PooledConnection& operator=(const PooledConnection&) = delete;
// Enable move semantics
PooledConnection(PooledConnection&& other) = default;
PooledConnection& operator=(PooledConnection&&) = default;
#endif
private: private:
#if ! defined(POCO_ENABLE_CPP11)
// Disable copy to prevent unwanted release of resources: pre C++11 way
PooledConnection(const PooledConnection&);
PooledConnection& operator=(const PooledConnection&);
#endif
Poco::ObjectPool<Connection, Connection::Ptr>& _pool; Poco::ObjectPool<Connection, Connection::Ptr>& _pool;
Connection::Ptr _connection; Connection::Ptr _connection;
}; };

View File

@@ -36,6 +36,9 @@ public:
ResponseMessage(); ResponseMessage();
/// Creates an empty ResponseMessage. /// Creates an empty ResponseMessage.
ResponseMessage(const Int64& cursorID);
/// Creates an ResponseMessage for existing cursor ID.
virtual ~ResponseMessage(); virtual ~ResponseMessage();
/// Destroys the ResponseMessage. /// Destroys the ResponseMessage.

View File

@@ -33,6 +33,12 @@ Cursor::Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags)
} }
Cursor::Cursor(const Document& aggregationResponse) :
_query(aggregationResponse.get<Poco::MongoDB::Document::Ptr>("cursor")->get<std::string>("ns")),
_response(aggregationResponse.get<Poco::MongoDB::Document::Ptr>("cursor")->get<Int64>("id"))
{
}
Cursor::~Cursor() Cursor::~Cursor()
{ {
try try

View File

@@ -30,6 +30,16 @@ ResponseMessage::ResponseMessage():
} }
ResponseMessage::ResponseMessage(const Int64& cursorID):
Message(MessageHeader::OP_REPLY),
_responseFlags(0),
_cursorID(cursorID),
_startingFrom(0),
_numberReturned(0)
{
}
ResponseMessage::~ResponseMessage() ResponseMessage::~ResponseMessage()
{ {
} }