Copilot commented on code in PR #13882:
URL: https://github.com/apache/trafficserver/pull/13882#discussion_r4517424100
##########
include/iocore/eventsystem/Thread.h:
##########
@@ -67,55 +33,150 @@
class ProxyMutex;
+/**
+ Size in bytes, including the terminating NUL, of a thread name buffer.
+
+ Use it to size buffers that build or read back the names of ATS
+ threads. A thread name holds at most @c MAX_THREAD_NAME_LENGTH - 1
+ bytes; longer names are truncated when the thread is started, without
+ any error being reported.
Review Comment:
The comment makes a strong behavioral claim that longer names are truncated
“without any error being reported”. That behavior is platform- and
implementation-dependent (some OS APIs reject with an error rather than
truncating). Consider rewording to match the project’s actual implementation
(e.g., “ATS truncates before setting” if that’s true), or soften to “may be
truncated” / “excess bytes may be dropped” to avoid documenting behavior that
may not hold on all supported platforms.
##########
include/iocore/eventsystem/Thread.h:
##########
@@ -67,55 +33,150 @@
class ProxyMutex;
+/**
+ Size in bytes, including the terminating NUL, of a thread name buffer.
+
+ Use it to size buffers that build or read back the names of ATS
+ threads. A thread name holds at most @c MAX_THREAD_NAME_LENGTH - 1
+ bytes; longer names are truncated when the thread is started, without
+ any error being reported.
+*/
constexpr int MAX_THREAD_NAME_LENGTH = 16;
-/// The signature of a function to be called by a thread.
+/**
+ Optional entry point for the OS thread spawned by @c Thread::start.
+
+ When non-empty, the spawned thread invokes the callable exactly once,
+ in place of @c Thread::execute, and exits when it returns.
+
+ @note @c Thread::start keeps its own copy of the callable. Anything the
+ callable captures by reference or pointer MUST stay valid until
+ the callable returns.
+
+ @pre The callable MUST NOT exit via an exception.
+
+ @par Thread Safety
+ The callable runs on the spawned thread, concurrently with the caller
+ of @c Thread::start. State it shares with other threads needs its own
+ synchronization.
+*/
using ThreadFunction = std::function<void()>;
/**
- Base class for the threads in the Event System. Thread is the base
- class for all the thread classes in the Event System. Objects of the
- Thread class represent spawned or running threads and provide minimal
- information for its derived classes. Thread objects have a reference
- to a ProxyMutex, that is used for atomic operations internally, and
- an ink_thread member that is used to identify the thread in the system.
-
- You should not create an object of the Thread class, they are typically
- instantiated after some thread startup mechanism exposed by a processor,
- but even then you would probably deal with processor functions and
- not the Thread object itself.
+ Base class for an object that represents one OS thread and holds its
+ per-thread state.
+ A @c Thread represents the OS thread on which it is registered in
+ @c this_thread_ptr.
+
+ @par Lifetime
+ A @c Thread MUST NOT be destroyed while a different OS thread may
+ still use it, for example through @c this_thread_ptr or a running
+ @c execute.
*/
class Thread
{
public:
/**
- System-wide thread identifier. The thread identifier is represented
- by the platform independent type ink_thread and it is the system-wide
- value assigned to each thread. It is exposed as a convenience for
- processors and you should not modify it directly.
-
+ Identifier of the OS thread spawned by @c start.
+
+ Zero until @c start is called, and remains zero for a @c Thread that
+ is registered via @c set_specific without being started. The value
+ is never reset: once the OS thread has been joined, it no longer
+ identifies a live thread and MAY be reused by the platform.
+
+ @par Thread Safety
+ Written by @c start on its calling thread before @c start returns.
+ Safe to read from that thread after @c start returns, and from any
+ thread that synchronizes with it afterward. The spawned thread
+ itself MUST NOT read @c tid without such synchronization; it should
+ obtain its own identifier from the platform instead.
*/
// NOLINTNEXTLINE(modernize-use-nullptr)
ink_thread tid = 0;
/**
- Thread lock to ensure atomic operations. The thread lock available
- to derived classes to ensure atomic operations and protect critical
- regions. Do not modify this member directly.
+ Mutex that the @c Thread holds on its own behalf.
+
+ Non-null and unchanged from construction until destruction; do not
+ assign to it. Once construction completes, this object must hold this
+ mutex until destruction, so a lock taken on the OS thread this
+ @c Thread represents always succeeds immediately. Any object that
+ adopts this mutex as its own lock can therefore be locked, and its
+ work run, only on that OS thread.
+ @note Do not lock it from any other thread. A try-lock always
+ fails, and a blocking lock is undefined.
+
+ @note The @c Thread base class itself does not take the mutex; it
+ must be held by a subclass to satisfy locking requirements.
Review Comment:
The mutex contract is internally inconsistent: it says the `Thread` object
“must hold this mutex until destruction”, but also says the base class “does
not take the mutex; it must be held by a subclass”. Please clarify which
component actually acquires/holds the lock (base vs subclass vs external code)
and under what conditions locking from other threads fails (e.g., deadlock,
assertion, guaranteed failure), rather than describing it as “undefined”.
##########
include/iocore/eventsystem/Thread.h:
##########
@@ -148,33 +209,108 @@ class Thread
#if TS_HAS_CRIPTS == 1
ProxyAllocator criptContextAllocator;
#endif
+ /// @}
- /** Start the underlying thread.
-
- The thread name is set to @a name. The stack for the thread is either @a
stack or, if that is
- @c nullptr a stack of size @a stacksize is allocated and used. If @a f
is present and valid it
- is called in the thread context. Otherwise the method @c execute is
invoked.
+ /**
+ Spawns the OS thread for this @c Thread.
+
+ The new thread calls @c set_specific, sets its name to @p name, and
+ then calls @p f if it is non-empty, or @c execute otherwise. The OS
+ thread exits when that call returns. If the OS thread cannot be
+ created, the process aborts.
Review Comment:
This documents a very specific failure mode (“the process aborts”) that
should precisely match the implementation of `Thread::start` (e.g., `abort()`,
`ink_release_assert`, thrown exception, returned error). If `start` does not
unconditionally abort on failure across platforms/build modes, please adjust
the wording to reflect the actual behavior (or describe it in terms of the
project’s error-handling policy for thread creation failures).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]