Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20170624222338.GX1627@brightrain.aerifal.cx>
Date: Sat, 24 Jun 2017 18:23:38 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: [PATCH 4/8] determine the existence of private futexes at
 the first thread creation

On Tue, Jun 20, 2017 at 10:35:28PM +0200, Jens Gustedt wrote:
> diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c
> index ca5c6b90..26945022 100644
> --- a/src/thread/pthread_create.c
> +++ b/src/thread/pthread_create.c
> @@ -10,6 +10,8 @@ void *__mmap(void *, size_t, int, int, int, off_t);
>  int __munmap(void *, size_t);
>  int __mprotect(void *, size_t, int);
>  
> +int __futex_private = 0;
> +
>  static void dummy_0()
>  {
>  }
> @@ -277,7 +279,13 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
>  	new->unblock_cancel = self->cancel;
>  	new->CANARY = self->CANARY;
>  
> -	a_inc(&libc.threads_minus_1);
> +	if (!a_fetch_add(&libc.threads_minus_1, 1)) {
> +          // As long as we only have one thread, test if this supports
> +          // private futexes.
> +          __lock_t dummy = { 0 };
> +          if (__syscall(SYS_futex, dummy.lc, FUTEX_WAKE|FUTEX_PRIVATE, 0) != -ENOSYS)
> +		__futex_private = FUTEX_PRIVATE;
> +        }
>  	ret = __clone((c11 ? start_c11 : start), stack, flags, new, &new->tid, TP_ADJ(new), &new->tid);

For future reference, this is also the wrong way to do "on first
thread creation" -- it will trigger every time the number of threads
changes from 1 to 2. There's already a block at the top of the
function for first thread creation, that sets up stdio for
multithreaded use and such; that would be the place to put this.

I was also momentarily concerned about whether there are cases where a
single-threaded process needs to be able to use the futex ops, but I
think it probably doesn't matter except for the not-private case
(pshared sync objects).

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.