Reviewed-by: Benoît Canet <ben...@scylladb.com>

On Sun, Aug 28, 2016 at 3:02 PM, Nadav Har'El <n...@scylladb.com> wrote:

> Implement pthread_key_delete().
>
> Currently, it will not mark the key available for re-use.
> The danger in allowing re-use is that if we leave data with that key (the
> pthread_key_delete(3P) manpage explains why this happens), we can get a new
> destructor running on old unrelated data.
>
> The not-as-great side of not re-using keys is that our current
> implementation
> is limited to 128 keys so we can run out of them. But this patch doesn't
> make
> this any worse than before.
>
> Signed-off-by: Nadav Har'El <n...@scylladb.com>
> ---
>  libc/pthread.cc | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/libc/pthread.cc b/libc/pthread.cc
> index 62db224..781ff3a 100644
> --- a/libc/pthread.cc
> +++ b/libc/pthread.cc
> @@ -275,8 +275,19 @@ extern "C" {
>
>  int pthread_key_delete(pthread_key_t key)
>  {
> -    WARN_STUBBED();
> -    return EINVAL;
> +    std::lock_guard<mutex> guard(tsd_key_mutex);
> +    if (key < 0 || key >= (int)tsd_used_keys.size() ||
> !tsd_used_keys[key]) {
> +        return EINVAL;
> +    }
> +    tsd_dtor[key] = nullptr;
> +    // TODO: Currently, we keep tsd_used_keys[key] at true so the key will
> +    // not be reused. Since pthread_key_delete cannot get rid of existing
> +    // data, reusing the key may causes us to later call a new destructor
> +    // for old unrelated data. The cost of not reusing keys is that we can
> +    // run out of them if many keys are created and deleted (e.g., a
> shared
> +    // object is loaded and unloaded)..
> +    //tsd_used_keys[key] = false;
> +    return 0;
>  }
>
>  void* pthread_getspecific(pthread_key_t key)
> --
> 2.7.4
>
> --
> You received this message because you are subscribed to the Google Groups
> "OSv Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to osv-dev+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to