Skip to content

Commit

Permalink
[core] Removed ThreadName(const char*). (#2113)
Browse files Browse the repository at this point in the history
Now only string& can be passed to avoid checking for NULL.
  • Loading branch information
maxsharabayko authored Sep 2, 2021
1 parent df95ecb commit 2243388
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 32 deletions.
4 changes: 2 additions & 2 deletions apps/srt-tunnel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ class Engine
void Start()
{
Verb() << "START: " << media[DIR_IN]->uri() << " --> " << media[DIR_OUT]->uri();
std::string thrn = media[DIR_IN]->id() + ">" + media[DIR_OUT]->id();
srt::ThreadName tn(thrn.c_str());
const std::string thrn = media[DIR_IN]->id() + ">" + media[DIR_OUT]->id();
srt::ThreadName tn(thrn);

thr = thread([this]() { Worker(); });
}
Expand Down
8 changes: 3 additions & 5 deletions srtcore/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ bool srt::CUDTSocket::readReady()
if (m_UDT.m_bConnected && m_UDT.m_pRcvBuffer->isRcvDataReady())
return true;
if (m_UDT.m_bListening)
{
return m_QueuedSockets.size() > 0;
}
return !m_QueuedSockets.empty();

return broken();
}
Expand Down Expand Up @@ -955,7 +953,7 @@ int srt::CUDTUnited::bind(CUDTSocket* s, UDPSOCKET udpsock)
s->m_Status = SRTS_OPENED;

// copy address information of local node
s->core().m_pSndQueue->m_pChannel->getSockAddr((s->m_SelfAddr));
s->core().m_pSndQueue->m_pChannel->getSockAddr(s->m_SelfAddr);

return 0;
}
Expand Down Expand Up @@ -1535,7 +1533,7 @@ int srt::CUDTUnited::groupConnect(CUDTGroup* pg, SRT_SOCKGROUPCONFIG* targets, i
HLOGC(aclog.Debug, log << "groupConnect: connecting a new socket with ISN=" << isn);
connectIn(ns, target_addr, isn);
}
catch (CUDTException& e)
catch (const CUDTException& e)
{
LOGC(aclog.Error, log << "groupConnect: socket @" << sid << " in group " << pg->id() << " failed to connect");
// We know it does belong to a group.
Expand Down
6 changes: 3 additions & 3 deletions srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9395,10 +9395,10 @@ int srt::CUDT::processData(CUnit* in_unit)

const string& tn = tns2.str();

ThreadName tnkeep(tn.c_str());
const char* thname = tn.c_str();
ThreadName tnkeep(tn);
const string& thname = tn;
#else
const char* thname = "SRT:TsbPd";
const string thname = "SRT:TsbPd";
#endif
if (!StartThread(m_RcvTsbPdThread, CUDT::tsbpd, this, thname))
return -1;
Expand Down
4 changes: 2 additions & 2 deletions srtcore/sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ std::string FormatTimeSys(const steady_clock::time_point& timestamp)


#ifdef ENABLE_STDCXX_SYNC
bool StartThread(CThread& th, ThreadFunc&& f, void* args, const char* name)
bool StartThread(CThread& th, ThreadFunc&& f, void* args, const string& name)
#else
bool StartThread(CThread& th, void* (*f) (void*), void* args, const char* name)
bool StartThread(CThread& th, void* (*f) (void*), void* args, const string& name)
#endif
{
ThreadName tn(name);
Expand Down
4 changes: 2 additions & 2 deletions srtcore/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,9 @@ namespace this_thread
///
#ifdef ENABLE_STDCXX_SYNC
typedef void* (&ThreadFunc) (void*);
bool StartThread(CThread& th, ThreadFunc&& f, void* args, const char* name);
bool StartThread(CThread& th, ThreadFunc&& f, void* args, const std::string& name);
#else
bool StartThread(CThread& th, void* (*f) (void*), void* args, const char* name);
bool StartThread(CThread& th, void* (*f) (void*), void* args, const std::string& name);
#endif

////////////////////////////////////////////////////////////////////////////////
Expand Down
26 changes: 13 additions & 13 deletions srtcore/threadname.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ class ThreadName
#endif
}

ThreadNameImpl(const char* name)
explicit ThreadNameImpl(const char* name)
: reset(false)
{
reset = false;
tid = pthread_self();

if (!get(old_name))
Expand All @@ -140,6 +140,10 @@ class ThreadName
if (tid == pthread_self())
set(old_name);
}

private:
ThreadNameImpl(ThreadNameImpl& other);
ThreadNameImpl& operator=(const ThreadNameImpl& other);

private:
bool reset;
Expand Down Expand Up @@ -202,21 +206,17 @@ class ThreadName
return ret;
}

// note: set can fail if name is too long. The upper limit is platform
// dependent. strlen(name) <= 15 should work on most of the platform.
static bool set(const char* name) { return ThreadNameImpl::set(name); }

static bool set(const std::string& name) { return set(name.c_str()); }

ThreadName(const char* name)
: impl(name)
{
}
static bool set(const std::string& name) { return ThreadNameImpl::set(name.c_str()); }

ThreadName(const std::string& name)
explicit ThreadName(const std::string& name)
: impl(name.c_str())
{
}

private:
ThreadName(const ThreadName&);
ThreadName(const char*);
ThreadName& operator=(const ThreadName& other);
};

} // namespace srt
Expand Down
4 changes: 2 additions & 2 deletions test/test_threadname.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ TEST(ThreadName, GetSet)

TEST(ThreadName, AutoReset)
{
std::string old_name("old");
const std::string old_name("old");
std::string new_name("new-name");
if (ThreadName::DUMMY_IMPL)
{
// just make sure the API is correct
ThreadName t("test");
ThreadName t(std::string("test"));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion testing/srt-test-multiplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class MediaBase
med.name = name;

// Ok, got this, so we can start transmission.
srt::ThreadName tn(thread_name.c_str());
srt::ThreadName tn(thread_name);

med.runner = thread( [&med]() { med.TransmissionLoop(); });
return med;
Expand Down
2 changes: 1 addition & 1 deletion testing/srt-test-relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ void SrtMainLoop::run()

std::ostringstream tns;
tns << "Input:" << this;
srt::ThreadName tn(tns.str().c_str());
srt::ThreadName tn(tns.str());
m_input_thr = thread([this] {
try {
InputRunner();
Expand Down
2 changes: 1 addition & 1 deletion testing/testactivemedia.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct Medium
running = true;
std::ostringstream tns;
tns << typeid(*this).name() << ":" << this;
srt::ThreadName tn(tns.str().c_str());
srt::ThreadName tn(tns.str());
thr = thread( [this] { RunnerBase(); } );
}

Expand Down

0 comments on commit 2243388

Please sign in to comment.