Skip to content

Commit

Permalink
Refactor out set_current_thread_name to Util.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Oct 25, 2020
1 parent 9c7bd20 commit 5e891c0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -283,6 +283,7 @@ target_sources(granite PRIVATE
util/timer.hpp util/timer.cpp
util/small_vector.hpp
util/timeline_trace_file.hpp util/timeline_trace_file.cpp
util/thread_name.hpp util/thread_name.cpp

vulkan/texture_format.cpp vulkan/texture_format.hpp
vulkan/context.cpp vulkan/context.hpp vulkan/vulkan_headers.hpp
Expand Down
19 changes: 3 additions & 16 deletions threading/thread_group.cpp
Expand Up @@ -28,10 +28,7 @@
#include "thread_id.hpp"
#include "string_helpers.hpp"
#include "timeline_trace_file.hpp"

#ifdef __linux__
#include <pthread.h>
#endif
#include "thread_name.hpp"

using namespace std;

Expand Down Expand Up @@ -123,26 +120,16 @@ TaskGroup::~TaskGroup()
flush();
}

void ThreadGroup::set_current_thread_name(const char *name)
{
#ifdef __linux__
pthread_setname_np(pthread_self(), name);
#else
// TODO: Kinda messy.
(void)name;
#endif
}

static void set_main_thread_name()
{
ThreadGroup::set_current_thread_name("MainThread");
Util::set_current_thread_name("MainThread");
Util::TimelineTraceFile::set_tid("main");
}

static void set_worker_thread_name(unsigned index)
{
auto name = Util::join("WorkerThread-", index);
ThreadGroup::set_current_thread_name(name.c_str());
Util::set_current_thread_name(name.c_str());
Util::TimelineTraceFile::set_tid(std::to_string(index + 1).c_str());
}

Expand Down
2 changes: 0 additions & 2 deletions threading/thread_group.hpp
Expand Up @@ -165,8 +165,6 @@ class ThreadGroup
void wait_idle();
bool is_idle();

static void set_current_thread_name(const char *tag);

Util::TimelineTraceFile *get_timeline_trace_file();
void refresh_global_timeline_trace_file();

Expand Down
18 changes: 18 additions & 0 deletions util/thread_name.cpp
@@ -0,0 +1,18 @@
#include "thread_name.hpp"

#ifdef __linux__
#include <pthread.h>
#endif

namespace Util
{
void set_current_thread_name(const char *name)
{
#ifdef __linux__
pthread_setname_np(pthread_self(), name);
#else
// TODO: Kinda messy.
(void)name;
#endif
}
}
28 changes: 28 additions & 0 deletions util/thread_name.hpp
@@ -0,0 +1,28 @@
/* Copyright (c) 2017-2020 Hans-Kristian Arntzen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

namespace Util
{
void set_current_thread_name(const char *name);
}
4 changes: 2 additions & 2 deletions util/timeline_trace_file.cpp
Expand Up @@ -22,8 +22,8 @@

#include "logging.hpp"
#include "timeline_trace_file.hpp"
#include "thread_name.hpp"
#include "timer.hpp"
#include "thread_group.hpp"
#include <string.h>
#include <stdio.h>

Expand Down Expand Up @@ -98,7 +98,7 @@ TimelineTraceFile::TimelineTraceFile(const std::string &path)

void TimelineTraceFile::looper(std::string path)
{
Granite::ThreadGroup::set_current_thread_name("json-trace-io");
set_current_thread_name("json-trace-io");

FILE *file = fopen(path.c_str(), "w");
if (!file)
Expand Down

0 comments on commit 5e891c0

Please sign in to comment.