From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <alviro.iskandar@gmail.com>
Received: from mail-qv1-f65.google.com (mail-qv1-f65.google.com [209.85.219.65])
	by gnuweeb.org (Postfix) with ESMTPS id C5FD67ECC4
	for <timl@vger.teainside.org>; Sat, 22 Jan 2022 05:06:40 +0000 (UTC)
Received: by mail-qv1-f65.google.com with SMTP id s6so5862074qvv.11
        for <timl@vger.teainside.org>; Fri, 21 Jan 2022 21:06:40 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20210112;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=KuLyycNzm2TguMFQS4S3Ii4Dw1qRkgKq/0ZI3zTnaXg=;
        b=NPVscItKWGqkFS4rtYhT+LIZ+Wcf6Zv5Fipep8u2vHIy8DykggKu9sXzIONhjKYAHY
         c9lnHFhRZb91mEv/ZOkHtmXEzkEZmxqHpFKG4CoGckUTVB2g3Vp43SzMl18LzGqF5gZn
         rMhL4WkUr3exe+dYY/BI0y3v959FgQ1e9IFNpYHURExT0r4fxxKQHj1MwgYpg0SmdLbI
         o/ln8UjRb3nx8mWKSwIH7SgjWiYpAV+3HXgMSpx7dZGFNFbjBKEAMoFE1D/f017GFm2C
         i0k3HgWmz9RCCgctVEzrlyYy0kZ67VP63tfKyQou45kktaKYeBJ/mBjeHSgUgyCpVPLs
         IQuQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20210112;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=KuLyycNzm2TguMFQS4S3Ii4Dw1qRkgKq/0ZI3zTnaXg=;
        b=YPB5aRZ2+0XQ8eZKG4nfO9xXeijGXF2gE49jlSXGdEyF3p1v13AhppAQNdOWGJNyk/
         Q4DTKAGt4NkoiLtnjVWwawiKDQ+Qh9/RSvxAs7pP1QfVeO2je09gvPjk7Lu3GXrhVZA5
         lME5rI+c+Gh90A/X1OFf/qj72hKWiahnHDK1FYpKrBBQibVt7wjgM6UjrFZ6D2HD01ne
         dq1gMFlFRT4mgaEfEM13IP4tPPf27Wf6A2Wiil+NXSAdG4gglwmWZFJ3Pan59BCEOj4y
         +JDzxIoNuOrKRuNpMqSMWrSMiSjDTLdTYBFGHZB9I3/jCBm8NaJsHZhmXr16GOe9g01z
         oN/g==
X-Gm-Message-State: AOAM533Sq4VHqvZCyw+k4v9evxpWir2mi7FjnIf0b47o+o55Lrb5Cces
	S1TRNGWEdTB3+9qT3VjcqYkhWlwbP4o86snn+2ElaeMA
X-Google-Smtp-Source: ABdhPJz/5XIwUPicr0WhqsFcuCuammZSUD9BBoslKXGxxru6qK80Vc+qxppH+3w0oN3SuIWExjk+JQ==
X-Received: by 2002:ad4:5f8a:: with SMTP id jp10mr6762217qvb.86.1642828000044;
        Fri, 21 Jan 2022 21:06:40 -0800 (PST)
Received: from localhost.localdomain ([2604:a880:cad:d0::c97:1])
        by smtp.gmail.com with ESMTPSA id v21sm4012686qtx.13.2022.01.21.21.06.39
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Fri, 21 Jan 2022 21:06:39 -0800 (PST)
From: Alviro Iskandar Setiawan <alviro.iskandar@gmail.com>
To: Ammar Faizi <ammarfaizi2@gmail.com>
Cc: Tea Inside Mailing List <timl@vger.teainside.org>,
	Alviro Iskandar Setiawan <alviro.iskandar@gmail.com>
Subject: [PATCH teavpn2 3/5] mutex: Refactor mutex helpers
Date: Sat, 22 Jan 2022 05:06:31 +0000
Message-Id: <20220122050633.920866-4-alviro.iskandar@gmail.com>
X-Mailer: git-send-email 2.27.0
In-Reply-To: <20220122050633.920866-1-alviro.iskandar@gmail.com>
References: <20220122050633.920866-1-alviro.iskandar@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
List-Id: <timl.vger.teainside.org>

Refactor mutex helpers, make it simpler. The old code was too verbose
and harder to read.

Signed-off-by: Alviro Iskandar Setiawan <alviro.iskandar@gmail.com>
---
 src/teavpn2/mutex.h | 89 +++++++++++++++++----------------------------
 1 file changed, 34 insertions(+), 55 deletions(-)

diff --git a/src/teavpn2/mutex.h b/src/teavpn2/mutex.h
index cd86779..96abc0d 100644
--- a/src/teavpn2/mutex.h
+++ b/src/teavpn2/mutex.h
@@ -10,99 +10,78 @@
 #include <pthread.h>
 #include <teavpn2/common.h>
 
-#define MUTEX_LEAK_ASSERT 0
-#define MUTEX_LOCK_ASSERT 0
+#ifndef __MUTEX_LEAK_ASSERT
+#define __MUTEX_LEAK_ASSERT 0
+#endif
+
 
 struct tmutex {
 	pthread_mutex_t			mutex;
-#if MUTEX_LEAK_ASSERT
+
+#if __MUTEX_LEAK_ASSERT
 	union {
 		void			*__leak_assert;
-		uint64_t		need_destroy;
+		uintptr_t		need_destroy;
 	};
 #else
 	bool				need_destroy;
-#endif /* #if MUTEX_LEAK_ASSERT */
+#endif
 };
 
 
-static __always_inline void mutex_init_mark(struct tmutex *m)
-{
-#if MUTEX_LEAK_ASSERT
-	m->__leak_assert = malloc(1);
-	if (unlikely(!m->__leak_assert))
-		panic("Cannot initialize __leak_assert for mutex_init_mark");
-#else
-	m->need_destroy = 1;
-#endif /* #if MUTEX_LEAK_ASSERT */
+#define MUTEX_INITIALIZER			\
+{						\
+	.mutex = PTHREAD_MUTEX_INITIALIZER	\
 }
 
+#define DEFINE_MUTEX(V) struct tmutex V = MUTEX_INITIALIZER
 
-static __cold __always_inline int mutex_init(struct tmutex *m,
-					     const pthread_mutexattr_t *attr)
+
+static __always_inline int mutex_init(struct tmutex *m,
+				      const pthread_mutexattr_t *attr)
 {
 	int ret;
 
-	memset(m, 0, sizeof(*m));
 	ret = pthread_mutex_init(&m->mutex, attr);
 	if (unlikely(ret)) {
-		ret = errno;
 		pr_err("pthread_mutex_init(): " PRERF, PREAR(ret));
 		return -ret;
 	}
-	mutex_init_mark(m);
-	return ret;
-}
-
 
-static __hot __always_inline int mutex_lock(struct tmutex *m)
-{
-	int ret;
-	__asm__ volatile("":"+r"(m)::"memory");
-	ret = pthread_mutex_lock(&m->mutex);
-#if MUTEX_LOCK_ASSERT
-	BUG_ON(ret != 0);
+#if __MUTEX_LEAK_ASSERT
+	m->__leak_assert = malloc(1);
+	BUG_ON(!m->__leak_assert);
+#else
+	m->need_destroy = true;
 #endif
+
 	return ret;
 }
 
-
-static __hot __always_inline int mutex_unlock(struct tmutex *m)
+static __always_inline int mutex_lock(struct tmutex *m)
 {
-	int ret;
-	__asm__ volatile("":"+r"(m)::"memory");
-	ret = pthread_mutex_unlock(&m->mutex);
-#if MUTEX_LOCK_ASSERT
-	BUG_ON(ret != 0);
-#endif
-	return ret;
+	return pthread_mutex_lock(&m->mutex);
 }
 
+static __always_inline int mutex_unlock(struct tmutex *m)
+{
+	return pthread_mutex_unlock(&m->mutex);
+}
 
 static __always_inline int mutex_trylock(struct tmutex *m)
 {
-	__asm__ volatile("":"+r"(m)::"memory");
 	return pthread_mutex_trylock(&m->mutex);
 }
 
-
-static __cold inline int mutex_destroy(struct tmutex *m)
+static __always_inline int mutex_destroy(struct tmutex *m)
 {
-	if (m->need_destroy) {
-		int ret = pthread_mutex_destroy(&m->mutex);
-		if (unlikely(ret)) {
-			pr_err("pthread_mutex_destroy(): " PRERF, PREAR(ret));
-			return -ret;
-		}
-
-#if MUTEX_LEAK_ASSERT
-		free(m->__leak_assert);
-		m->__leak_assert = NULL;
-#else
-		m->need_destroy = 0;
+	BUG_ON(!m->need_destroy);
+
+#if __MUTEX_LEAK_ASSERT
+	free(m->__leak_assert);
 #endif
-	}
-	return 0;
+
+	return pthread_mutex_destroy(&m->mutex);
 }
 
 #endif /* #ifndef TEAVPN2__MUTEX_H */
-- 
2.32.0