Tea Inside Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Alviro Iskandar Setiawan <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Tea Inside Mailing List <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>
Subject: [PATCH teavpn2 3/5] mutex: Refactor mutex helpers
Date: Sat, 22 Jan 2022 05:06:31 +0000	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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

Signed-off-by: Alviro Iskandar Setiawan <[email protected]>
---
 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


  parent reply	other threads:[~2022-01-22  5:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-22  5:06 [PATCH teavpn2 0/5] Update README, Refactor and Fix Config Template Alviro Iskandar Setiawan
2022-01-22  5:06 ` [PATCH teavpn2 1/5] README: Remove RELEASE_MODE var Alviro Iskandar Setiawan
2022-01-22  5:06 ` [PATCH teavpn2 2/5] README: Add instruction to build with GUI Alviro Iskandar Setiawan
2022-01-22  5:06 ` Alviro Iskandar Setiawan [this message]
2022-01-22  5:06 ` [PATCH teavpn2 4/5] config/client: Fix data_dir for client config template Alviro Iskandar Setiawan
2022-01-22  5:06 ` [PATCH teavpn2 5/5] config/{client,server}: Update interface name to teavpn2 Alviro Iskandar Setiawan
2022-01-22  5:14 ` [PATCH teavpn2 0/5] Update README, Refactor and Fix Config Template Ammar Faizi
2022-01-22  5:41   ` Ammar Faizi
2022-01-22  5:44     ` Alviro Iskandar Setiawan
2022-01-22  5:48       ` Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox