public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Jon Turney <jon.turney@dronecode.org.uk>
To: Corinna Vinschen <corinna-cygwin@cygwin.com>,
	Cygwin Patches <cygwin-patches@cygwin.com>
Subject: Re: [newlib-cygwin] Cygwin: Set threadnames with SetThreadDescription()
Date: Sun, 31 Jul 2022 16:03:13 +0100	[thread overview]
Message-ID: <4863473c-48a0-0da0-7e32-6d1cf5522ead@dronecode.org.uk> (raw)
In-Reply-To: <YuQm5xcBC+1LJSJk@calimero.vinschen.de>

On 29/07/2022 19:28, Corinna Vinschen wrote:
> On Jul 29 15:14, Jon Turney wrote:
>> On 29/07/2022 12:58, Corinna Vinschen wrote:
>>> Hi Jon,
>>>
>>> On Jul 29 11:01, Jon TURNEY via Cygwin-cvs wrote:
>>>> https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=d4689b99c68628d9ec2fc1ac7884906ddbf6a2fc
>>>>
>>>> commit d4689b99c68628d9ec2fc1ac7884906ddbf6a2fc
>>>> Author: Jon Turney <jon.turney@dronecode.org.uk>
>>>> Date:   Thu May 19 17:27:39 2022 +0100
>>>>
>>>>       Cygwin: Set threadnames with SetThreadDescription()
>>>>       [...]
>>>> +      /* SetThreadDescription only exists in a wide-char version, so we must
>>>> +	 convert threadname to wide-char.  The encoding of threadName is
>>>> +	 unclear, so use UTF8 until we know better. */
>>>> +      int bufsize = MultiByteToWideChar (CP_UTF8, 0, threadName, -1, NULL, 0);
>>>> +      WCHAR buf[bufsize];
>>>> +      bufsize = MultiByteToWideChar (CP_UTF8, 0, threadName, -1, buf, bufsize);
>>>
>>> I think this is wrong.  The function should use stock mbstowcs instead
>>> to get the externally used encoding.  Think of SetThreadName called with
>>> program_invocation_short_name in pthread::thread_init_wrapper, or called
>>> from pthread_setname_np with an externally provided thread name.  This
>>> thread name will use the locale of the application code it's called by.
>>
>> I'm not sure.
>>
>> The linux manpage for pthread_setname_np() says "The thread name is a
>> meaningful C language string", which I think means it's ASCII-encoded, not
>> locale-encoded.
> 
> I think this only means, it's a NUL-terminated string. "Meaningful" is
> just trying to nudge developers into using meaningful names, not
> something like "blurb".

Oh yeah, that reading makes more sense!

Still I think the threadname is just really just an opaque NULL 
terminated byte sequence which you can get back with pthread_getname_np().

If there are other mechanisms which make that threadname available to 
other processes (which might have a different locale), it's unclear how 
the encoding is supposed to be handled...

>> (The solaris manpage explicitly says that the thread name is utf8 encoded)
> 
> Ok, that's an interesting point.
> 
>> The encoding for program_invocation_short_name was also unclear to me.
>> (It's the same as argv[0], so I guess it's in whatever encoding the
>> filesystem uses, which doesn't have to match the process locale encoding)
>>
>> Expecting this function to work with non-ASCII names seems optimistic :)
> 
> Well, for Linux it's certainly just an arbitrary, NUL-terminated byte
> stream, but yeah, it's certainly the only portable way to expect
> the portable codeset.
> 
> Anyway, feel free to just keep the code as is.  We're typically using
> UTF-8 anyway and people switching to one of the legacy codesets are
> supposed to know what they are doing.

Yes, I think I'll leave this as is until someone complains! :)

      reply	other threads:[~2022-07-31 15:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20220729110147.4E6F43858424@sourceware.org>
     [not found] ` <YuPLd2hlbaNwxAJ0@calimero.vinschen.de>
     [not found]   ` <78af80e5-baed-5ebb-314f-99d13f2a25ca@sourceware.org>
2022-07-29 18:28     ` Corinna Vinschen
2022-07-31 15:03       ` Jon Turney [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4863473c-48a0-0da0-7e32-6d1cf5522ead@dronecode.org.uk \
    --to=jon.turney@dronecode.org.uk \
    --cc=corinna-cygwin@cygwin.com \
    --cc=cygwin-patches@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).