From: Nadav Har'El <n...@scylladb.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

pthread: implement pthread_key_delete()

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>
Message-Id: <1472389371-13580-1-git-send-email-...@scylladb.com>
Reviewed-by: Benoît Canet <ben...@scylladb.com>

---
diff --git a/libc/pthread.cc b/libc/pthread.cc
--- a/libc/pthread.cc
+++ b/libc/pthread.cc
@@ -261,8 +261,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)

--
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