Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NativeThreadInfo and blend in Message #3333

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Foundation/include/Poco/NativeThreadInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// NativeThreadInfo.h
//
// Library: Foundation
// Package: Threading
// Module: NativeThreadInfo
//
// Definition of the NativeThreadInfo class.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#ifndef Foundation_NativeThreadInfo_INCLUDED
#define Foundation_NativeThreadInfo_INCLUDED

#include "Poco/Foundation.h"

#if defined(POCO_OS_FAMILY_WINDOWS)
#if defined(_WIN32_WCE)
#include "Poco/NativeThreadInfo_WINCE.h"
#else
#include "Poco/NativeThreadInfo_WIN32.h"
#endif
#elif defined(POCO_VXWORKS)
#include "Poco/NativeThreadInfo_VX.h"
#else
#include "Poco/NativeThreadInfo_POSIX.h"
#endif

namespace Poco {

class Foundation_API NativeThreadInfo: private NativeThreadInfoImpl
/// This class implements a platform independent API to query information about a native thread.
{
public:
typedef NativeThreadInfoImpl::ThreadIdImpl ThreadId;
typedef NativeThreadInfoImpl::HandleImpl Handle;

NativeThreadInfo();
/// Creates a thread info for the current thread.

NativeThreadInfo(Handle handle);
/// Creates a thread info for a specific thread id.

std::string name() const;
/// Returns the name of the thread.

ThreadId id() const;
};

//
// inlines
//
inline std::string NativeThreadInfo::name() const
{
return nameImpl();
}

inline NativeThreadInfo::ThreadId NativeThreadInfo::id() const
{
return idImpl();
}

} // namespace Poco

#endif // Foundation_NativeThreadInfo_INCLUDED
51 changes: 51 additions & 0 deletions Foundation/include/Poco/NativeThreadInfo_POSIX.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// NativeThreadInfo_POSIX.h
//
// Library: Foundation
// Package: Threading
// Module: NativeThreadInfo
//
// Definition of the NativeThreadInfoImpl class for POSIX NativeThreadInfos.
//
// Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#ifndef Foundation_NativeThreadInfo_POSIX_INCLUDED
#define Foundation_NativeThreadInfo_POSIX_INCLUDED


#include "Poco/Foundation.h"
#include <string>
#include <pthread.h>
#include <sys/types.h>

namespace Poco {

class Foundation_API NativeThreadInfoImpl
{
public:
#if (POCO_OS == POCO_OS_MAC_OS_X)
typedef uint64_t ThreadIdImpl;
#else
typedef pid_t ThreadIdImpl;
#endif
typedef pthread_t HandleImpl;

NativeThreadInfoImpl(HandleImpl handle);

std::string nameImpl() const;
ThreadIdImpl idImpl() const;

static HandleImpl currentThreadHandleImpl();

private:
HandleImpl _thread;
};

} // namespace Poco

#endif // Foundation_NativeThreadInfo_POSIX_INCLUDED
46 changes: 46 additions & 0 deletions Foundation/include/Poco/NativeThreadInfo_VX.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// NativeThreadInfo_VX.h
//
// Library: Foundation
// Package: Threading
// Module: NativeThreadInfo
//
// Definition of the NativeThreadInfoImpl class for VX NativeThreadInfos.
//
// Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#ifndef Foundation_NativeThreadInfo_VX_INCLUDED
#define Foundation_NativeThreadInfo_VX_INCLUDED


#include "Poco/Foundation.h"
#include <string>
#include <taskLib.h>

namespace Poco {

class Foundation_API NativeThreadInfoImpl
{
public:
typedef int ThreadIdImpl;
typedef int HandleImpl;

NativeThreadInfoImpl(HandleImpl handle);

std::string nameImpl() const;
ThreadIdImpl idImpl() const;

static HandleImpl currentThreadHandleImpl();

private:
HandleImpl _thread;
};

} // namespace Poco

#endif // Foundation_NativeThreadInfo_VX_INCLUDED
46 changes: 46 additions & 0 deletions Foundation/include/Poco/NativeThreadInfo_WIN32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// NativeThreadInfo_WIN32.h
//
// Library: Foundation
// Package: Threading
// Module: NativeThreadInfo
//
// Definition of the NativeThreadInfoImpl class for WIN32 NativeThreadInfos.
//
// Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#ifndef Foundation_NativeThreadInfo_WIN32_INCLUDED
#define Foundation_NativeThreadInfo_WIN32_INCLUDED


#include "Poco/Foundation.h"
#include <string>
#include "Poco/UnWindows.h"

namespace Poco {

class Foundation_API NativeThreadInfoImpl
{
public:
typedef DWORD ThreadIdImpl;
typedef HANDLE HandleImpl;

NativeThreadInfoImpl(HandleImpl handle);

std::string nameImpl() const;
ThreadIdImpl idImpl() const;

static HandleImpl currentThreadHandleImpl();

private:
HandleImpl _thread;
};

} // namespace Poco

#endif // Foundation_NativeThreadInfo_WIN32_INCLUDED
46 changes: 46 additions & 0 deletions Foundation/include/Poco/NativeThreadInfo_WINCE.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// NativeThreadInfo_WINCE.h
//
// Library: Foundation
// Package: Threading
// Module: NativeThreadInfo
//
// Definition of the NativeThreadInfoImpl class for WINCE NativeThreadInfos.
//
// Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#ifndef Foundation_NativeThreadInfo_WINCE_INCLUDED
#define Foundation_NativeThreadInfo_WINCE_INCLUDED


#include "Poco/Foundation.h"
#include <string>
#include "Poco/UnWindows.h"

namespace Poco {

class Foundation_API NativeThreadInfoImpl
{
public:
typedef DWORD ThreadIdImpl;
typedef DWORD HandleImpl;

NativeThreadInfoImpl(HandleImpl handle);

std::string nameImpl() const;
ThreadIdImpl idImpl() const;

static HandleImpl currentThreadHandleImpl();

private:
HandleImpl _thread;
};

} // namespace Poco

#endif // Foundation_NativeThreadInfo_WINCE_INCLUDED
6 changes: 6 additions & 0 deletions Foundation/src/Message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Poco/Process.h"
#endif
#include "Poco/Thread.h"
#include "Poco/NativeThreadInfo.h"
#include <algorithm>


Expand Down Expand Up @@ -133,6 +134,11 @@ void Message::init()
_tid = pThread->id();
_thread = pThread->name();
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be formatted as

}
else
{

NativeThreadInfo info;
_tid = info.id();
_thread = info.name();
}
}


Expand Down
40 changes: 40 additions & 0 deletions Foundation/src/NativeThreadInfo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// NativeThreadInfo.cpp
//
// Library: Foundation
// Package: Threading
// Module: NativeThreadInfo
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#include "Poco/NativeThreadInfo.h"

#if defined(POCO_OS_FAMILY_WINDOWS)
#if defined(_WIN32_WCE)
#include "NativeThreadInfo_WINCE.cpp"
#else
#include "NativeThreadInfo_WIN32.cpp"
#endif
#elif defined(POCO_VXWORKS)
#include "NativeThreadInfo_VX.cpp"
#else
#include "NativeThreadInfo_POSIX.cpp"
Dismissed Show dismissed Hide dismissed
#endif


namespace Poco {

NativeThreadInfo::NativeThreadInfo(Handle handle):
NativeThreadInfoImpl{handle}
{ }

NativeThreadInfo::NativeThreadInfo():
NativeThreadInfo{ NativeThreadInfoImpl::currentThreadHandleImpl() }
{ }

} // namespace Poco
63 changes: 63 additions & 0 deletions Foundation/src/NativeThreadInfo_POSIX.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// NativeThreadInfo_POSIX.cpp
//
// Library: Foundation
// Package: Threading
// Module: NativeThreadInfoImpl
//
// Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//


#include "Poco/NativeThreadInfo_POSIX.h"
#include <cstring>
#include <unistd.h>
#include <sys/types.h>

#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#ifdef SYS_gettid
#define gettid() syscall(SYS_gettid)
#else
#define gettid() (0)
#endif
#endif

namespace Poco {

NativeThreadInfoImpl::NativeThreadInfoImpl(NativeThreadInfoImpl::HandleImpl handle):
_thread(handle)
{
}

std::string NativeThreadInfoImpl::nameImpl() const
{
std::string result(128, '\0');
int rc = pthread_getname_np(_thread, &result[0], result.size());
if (rc < 0) {
return {};
}
result.resize(std::strlen(result.data()));
return result;
}

NativeThreadInfoImpl::ThreadIdImpl NativeThreadInfoImpl::idImpl() const
{
ThreadIdImpl tid;
#if (POCO_OS == POCO_OS_MAC_OS_X)
pthread_threadid_np(_thread, &tid);
#else
tid = gettid();
#endif
return tid;
}

NativeThreadInfoImpl::HandleImpl NativeThreadInfoImpl::currentThreadHandleImpl()
{
return pthread_self();
}

} // namespace Poco
Loading
Loading