Delivered-To: mlmmj-tyndur-devel
Received: from sv1.famkaufmann.info [176.9.73.67]
	by a9b8c5461646 with IMAP (fetchmail-6.3.26)
	for <mlmmj@localhost> (single-drop); Sat, 17 Apr 2021 22:09:57 +0000 (GMT)
Received: from localhost ([172.29.0.7])
	by sv1.famkaufmann.info with LMTP
	id ENoqNp9ce2BRCgAALHnFKQ
	(envelope-from <hanna.reitz@mailbox.org>)
	for <mlmmj-tyndur-devel>; Sat, 17 Apr 2021 22:09:35 +0000
Delivered-To: tyndur-devel@tyndur.org
Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161])
	by sv1.famkaufmann.info (OpenSMTPD) with ESMTPS id 0e714f32 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO)
	for <tyndur-devel@tyndur.org>;
	Sat, 17 Apr 2021 22:09:35 +0000 (UTC)
Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4FN6gg3PtGzQjv5
	for <tyndur-devel@tyndur.org>; Sun, 18 Apr 2021 00:09:35 +0200 (CEST)
Authentication-Results: hefe.heinlein-support.de (amavisd-new);
	dkim=pass (2048-bit key) reason="pass (just generated, assumed good)"
	header.d=mailbox.org
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mailbox.org; s=mail20150812;
	t=1618697373;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=YNueAsfSnLufXhyJKTBGTQfUVLOsJRcXrE/Y4FXhSYM=;
	b=Jpn5He7jfB1YjP+7FHQJqDeRlFQWZC0lyumyAuaC2NThyZTzadUDMoJ/s9KIgo8+6Aem62
	2QmiU3ZRkcWUl1WT7iEPon7/mInSWZ4g9QWO3RPPFSvbAhH9JHcJBQzItsUhFLw/3NacCj
	fQuGmU75mU00qq0aJvGUBh2ryJu2u3lREYElwQMYbsxw4snDE9z/1RluHhwBNGavpa7vTy
	z9AsY2RrrqQzfifsPqX/Vl2QkaYTUyrHuSLTcbtI4dsy472HStvGOmPToYp1zJuyI/0nCn
	4HdfKGs18UjXrx+QQx2YX5P67Af+cGkK8vZ9xzrR0JAk4AdHrrVMTFoQwPjAuQ==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mailbox.org; h=
	content-transfer-encoding:content-type:content-type:mime-version
	:references:in-reply-to:message-id:date:date:subject:subject
	:from:from:received; s=mail20150812; t=1618697372; bh=9KUSYLXfOu
	SC5gySp4vKdTHymltER+7Cfdlhq0eQQJI=; b=OYarK0pmNQ0YT15UhkSdxkuRl0
	/R+ztTqgJKwfmIn6hA34ftJluic8lRV7qGvuyQC87T7itHoQMN9Ka22emQD6EOzf
	f3U7Xr9O0ftXwKH9r/lFfofAhYlTQ2ifxyKjJOr2PguO2urnvoQuetqMz34QT/Mz
	0bD9Vtp3w6C660/DgRAy4t4EPjcG/i/VKxXs3EoX4EzU9qOwCiPfWWB4HJPbFLsr
	UhvEqh8mfwYZLF8Wiee1s+/2lEbsX+e/sZbdPbkcfvjHLbTfQRXUusihPSon9CLI
	53/3M7/CsnTkmnWp6UTd1a6o7lGcufUxvaHXriq9MN+Sst5EPYhgjx3WzPPw==
X-Virus-Scanned: amavisd-new at heinlein-support.de
Received: from smtp2.mailbox.org ([80.241.60.241])
	by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030)
	with ESMTP id g-SEF0GNAzJU; Sun, 18 Apr 2021 00:09:32 +0200 (CEST)
From: Hanna Reitz <hanna.reitz@mailbox.org>
To: tyndur-devel@tyndur.org
Cc: Hanna Reitz <hanna.reitz@mailbox.org>
Subject: [PATCH 3/3] pthread: Thread-Keys
Date: Sun, 18 Apr 2021 00:09:02 +0200
Message-Id: <20210417220902.151893-4-hanna.reitz@mailbox.org>
In-Reply-To: <20210417220902.151893-1-hanna.reitz@mailbox.org>
References: <20210417220902.151893-1-hanna.reitz@mailbox.org>
Errors-To: toni@tyndur.org
Precedence: list
Reply-To: tyndur-Patches <tyndur-devel@tyndur.org>
List-Id: tyndur-Patches <tyndur-devel.tyndur.org>
List-Unsubscribe: <mailto:tyndur-devel+unsubscribe@tyndur.org>
List-Post: <mailto:tyndur-devel@tyndur.org>
List-Help: <mailto:tyndur-devel+help@tyndur.org>
List-Subscribe: <mailto:tyndur-devel+subscribe@tyndur.org>
List-Owner: <mailto:toni@tyndur.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-MBO-SPAM-Probability: 
X-Rspamd-Score: -8.85 / 15.00 / 15.00
X-Rspamd-Queue-Id: 8FAEF17F9
X-Rspamd-UID: 6c77f8

Ein pthread-Key ist ein globaler Key für den gesamten Prozess, wobei
aber jeder Thread unterschiedliche Daten sieht.

Für einen Key kann eine Destruktorfunktion registriert werden, die beim
Beenden eines Threads aufgerufen wird, um die jeweils gespeicherten
Daten freizugeben.  Dafür brauchen wir einen Notifier, der von
exit_thread() aufgerufen wird.

Signed-off-by: Hanna Reitz <hanna.reitz@mailbox.org>
---
 src/modules/include/pthread.h            |   9 +
 src/modules/lib/posix/pthread/keys.c     | 258 +++++++++++++++++++++++
 src/modules/lib/syscalls/create_thread.c |   5 +
 3 files changed, 272 insertions(+)
 create mode 100644 src/modules/lib/posix/pthread/keys.c

diff --git a/src/modules/include/pthread.h b/src/modules/include/pthread.h
index 517e01c0..1516e39f 100644
--- a/src/modules/include/pthread.h
+++ b/src/modules/include/pthread.h
@@ -51,6 +51,8 @@ typedef struct {
 // pthread_rwlock_init()
 typedef unsigned int pthread_rwlockattr_t;
 
+typedef unsigned int pthread_key_t;
+
 
 int pthread_create(pthread_t *thread,
     __attribute__((unused)) const pthread_attr_t *attr,
@@ -69,4 +71,11 @@ int pthread_rwlock_wrlock(pthread_rwlock_t* rwlock);
 int pthread_rwlock_trywrlock(pthread_rwlock_t* rwlock);
 int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
 
+
+int pthread_key_create(pthread_key_t* key, void (*destructor)(void*));
+int pthread_key_delete(pthread_key_t key);
+
+int pthread_setspecific(pthread_key_t key, const void* value);
+void* pthread_getspecific(pthread_key_t key);
+
 #endif
diff --git a/src/modules/lib/posix/pthread/keys.c b/src/modules/lib/posix/pthread/keys.c
new file mode 100644
index 00000000..6933434f
--- /dev/null
+++ b/src/modules/lib/posix/pthread/keys.c
@@ -0,0 +1,258 @@
+/*
+ * Copyright (c) 2021 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Hanna Reitz.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct thread_key {
+    void (*destructor)(void*);
+    // Index: pthread_self()
+    const void** values;
+    size_t value_count;
+
+    // Bezieht sich nur auf das thread_key-Objekt, nicht auf values[].
+    // Wenn etwas innerhalb von values[] veraendert wird, braucht man
+    // also nur ein rdlock.
+    pthread_rwlock_t rwl;
+};
+
+// Index: pthread_key_t - 1
+// (Ein Key darf wohl nicht 0 sein)
+static struct thread_key** thread_keys;
+static uint32_t thread_key_count;
+
+// Bezieht sich nur auf thread_keys und thread_key_count, nicht auf
+// thread_keys[].  Wenn etwas innerhalb von thread_keys[] veraendert
+// wird, braucht man also nur ein rdlock.  (Fuer *thread_keys[]
+// braucht man das Lock hier nicht mal, nur thread_key.rwl.)
+static pthread_rwlock_t thread_keys_rwl = PTHREAD_RWLOCK_INITIALIZER;
+
+int pthread_key_create(pthread_key_t* key, void (*destructor)(void*))
+{
+    uint32_t ikey;
+    int ret;
+
+    ret = pthread_rwlock_wrlock(&thread_keys_rwl);
+    if (ret) {
+        return ret;
+    }
+
+    ikey = thread_key_count++;
+    thread_keys = realloc(thread_keys,
+                          thread_key_count * sizeof(thread_keys[0]));
+
+    thread_keys[ikey] = malloc(sizeof(*thread_keys[ikey]));
+    *thread_keys[ikey] = (struct thread_key) {
+        .destructor = destructor,
+        .rwl        = PTHREAD_RWLOCK_INITIALIZER,
+    };
+
+    pthread_rwlock_unlock(&thread_keys_rwl);
+
+    *key = ikey + 1;
+    return 0;
+}
+
+int pthread_key_delete(pthread_key_t key)
+{
+    struct thread_key* key_obj;
+    int ret;
+
+    ret = pthread_rwlock_rdlock(&thread_keys_rwl);
+    if (ret) {
+        return ret;
+    }
+
+    key -= 1; // Unsigned, 0 - 1 laeuft also unter
+    if (key >= thread_key_count) {
+        pthread_rwlock_unlock(&thread_keys_rwl);
+        return EINVAL;
+    }
+
+    key_obj = thread_keys[key];
+
+    pthread_rwlock_unlock(&thread_keys_rwl);
+    ret = pthread_rwlock_wrlock(&key_obj->rwl);
+    if (ret) {
+        return ret;
+    }
+
+    free(key_obj->values);
+    key_obj->destructor = NULL;
+    key_obj->values = NULL;
+    key_obj->value_count = 0;
+
+    pthread_rwlock_unlock(&key_obj->rwl);
+
+    return 0;
+}
+
+void __pthread_notify_exit(void)
+{
+    pthread_t self = pthread_self();
+    bool destructors_called;
+    uint32_t key;
+
+    do {
+        destructors_called = false;
+
+        if (pthread_rwlock_rdlock(&thread_keys_rwl)) {
+            // Wenn das nicht klappt, koennen wir auch aufgeben
+            return;
+        }
+
+        for (key = 0; key < thread_key_count; key++) {
+            struct thread_key* key_obj = thread_keys[key];
+            const void* value = NULL;
+            void (*destructor)(void*);
+
+            pthread_rwlock_unlock(&thread_keys_rwl);
+
+            if (pthread_rwlock_rdlock(&key_obj->rwl)) {
+                if (pthread_rwlock_rdlock(&thread_keys_rwl)) {
+                    return;
+                } else {
+                    continue;
+                }
+            }
+
+            destructor = key_obj->destructor;
+
+            // Wenn kein Destruktor gesetzt wurde, vollstaendig ignorieren
+            if (destructor && self < key_obj->value_count) {
+                value = key_obj->values[self];
+                key_obj->values[self] = NULL;
+            }
+
+            pthread_rwlock_unlock(&key_obj->rwl);
+
+            if (value) {
+                destructor((void*) value);
+                destructors_called = true;
+            }
+
+            if (pthread_rwlock_rdlock(&thread_keys_rwl)) {
+                return;
+            }
+        }
+
+        pthread_rwlock_unlock(&thread_keys_rwl);
+    } while (destructors_called);
+}
+
+int pthread_setspecific(pthread_key_t key, const void* value)
+{
+    pthread_t self = pthread_self();
+    struct thread_key* key_obj;
+    int ret;
+
+    ret = pthread_rwlock_rdlock(&thread_keys_rwl);
+    if (ret) {
+        return ret;
+    }
+
+    key -= 1; // Unsigned, 0 - 1 laeuft also unter
+    if (key >= thread_key_count) {
+        pthread_rwlock_unlock(&thread_keys_rwl);
+        return EINVAL;
+    }
+
+    key_obj = thread_keys[key];
+
+    pthread_rwlock_unlock(&thread_keys_rwl);
+    ret = pthread_rwlock_rdlock(&key_obj->rwl);
+    if (ret) {
+        return ret;
+    }
+
+    if (self >= key_obj->value_count) {
+        pthread_rwlock_unlock(&key_obj->rwl);
+        ret = pthread_rwlock_wrlock(&key_obj->rwl);
+        if (ret) {
+            return ret;
+        }
+
+        // Nochmal pruefen, koennte sich ja geaendert haben
+        if (self >= key_obj->value_count) {
+            size_t nvc = self + 1;
+
+            key_obj->values = realloc(key_obj->values,
+                                      nvc * sizeof(key_obj->values[0]));
+            memset(key_obj->values + key_obj->value_count,
+                   0,
+                   (nvc - key_obj->value_count) * sizeof(key_obj->values[0]));
+
+            key_obj->value_count = nvc;
+        }
+    }
+
+    key_obj->values[self] = value;
+
+    pthread_rwlock_unlock(&key_obj->rwl);
+
+    return 0;
+}
+
+void* pthread_getspecific(pthread_key_t key)
+{
+    pthread_t self = pthread_self();
+    struct thread_key* key_obj;
+    const void* value;
+
+    if (pthread_rwlock_rdlock(&thread_keys_rwl)) {
+        return NULL;
+    }
+
+    key -= 1; // Unsigned, 0 - 1 laeuft also unter
+    if (key >= thread_key_count) {
+        pthread_rwlock_unlock(&thread_keys_rwl);
+        return NULL;
+    }
+
+    key_obj = thread_keys[key];
+
+    pthread_rwlock_unlock(&thread_keys_rwl);
+    if (pthread_rwlock_rdlock(&key_obj->rwl)) {
+        return NULL;
+    }
+
+    if (self < key_obj->value_count) {
+        value = key_obj->values[self];
+    } else {
+        value = NULL;
+    }
+
+    pthread_rwlock_unlock(&key_obj->rwl);
+
+    return (void*) value;
+}
diff --git a/src/modules/lib/syscalls/create_thread.c b/src/modules/lib/syscalls/create_thread.c
index 5789df58..111b7ea8 100644
--- a/src/modules/lib/syscalls/create_thread.c
+++ b/src/modules/lib/syscalls/create_thread.c
@@ -30,6 +30,9 @@
 #include <stdlib.h>
 #include "syscall.h"
 
+// Wird von unserer pthread-Implementierung bereitgestellt
+extern void __pthread_notify_exit(void);
+
 /**
  * ID des aktuellen Threads abfragen.
  *
@@ -103,6 +106,8 @@ tid_t create_thread(uint32_t start, void *arg)
 
 void exit_thread(void)
 {
+    __pthread_notify_exit();
+
     asm(
         "mov %0, %%eax;"
         "int $0x30;"
-- 
2.31.0

