[CRIU] [PATCH 35/38] compel: Move lock into compel directly

Cyrill Gorcunov gorcunov at openvz.org
Tue Oct 11 09:04:25 PDT 2016


We will need it anyway.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 compel/include/compel/lock.h | 160 ++++++++++++++++++++++++++++++++++++++++++-
 criu/include/lock.h          | 154 +----------------------------------------
 2 files changed, 160 insertions(+), 154 deletions(-)
 mode change 120000 => 100644 compel/include/compel/lock.h

diff --git a/compel/include/compel/lock.h b/compel/include/compel/lock.h
deleted file mode 120000
index d68447507ebe..000000000000
--- a/compel/include/compel/lock.h
+++ /dev/null
@@ -1 +0,0 @@
-../../../criu/include/lock.h
\ No newline at end of file
diff --git a/compel/include/compel/lock.h b/compel/include/compel/lock.h
new file mode 100644
index 000000000000..69e0f10392cd
--- /dev/null
+++ b/compel/include/compel/lock.h
@@ -0,0 +1,159 @@
+#ifndef COMPEL_LOCK_H__
+#define COMPEL_LOCK_H__
+
+#include <stdint.h>
+#include <linux/futex.h>
+#include <sys/time.h>
+#include <limits.h>
+#include <errno.h>
+
+#include "compel/asm/atomic.h"
+#include "bug.h"
+
+#ifdef CR_NOGLIBC
+# include "uapi/std/syscall.h"
+#else
+# include <unistd.h>
+# include <sys/syscall.h>
+static inline long sys_futex(void *addr1, int op, int val1,
+                            struct timespec *timeout, void *addr2, int val3)
+{
+       int rc = syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
+       if (rc == -1) rc = -errno;
+       return rc;
+}
+#endif
+
+typedef struct {
+	atomic_t raw;
+} futex_t;
+
+#define FUTEX_ABORT_FLAG	(0x80000000)
+#define FUTEX_ABORT_RAW		(-1U)
+
+/* Get current futex @f value */
+static inline uint32_t futex_get(futex_t *f)
+{
+	return atomic_read(&f->raw);
+}
+
+/* Set futex @f value to @v */
+static inline void futex_set(futex_t *f, uint32_t v)
+{
+	atomic_set(&f->raw, (int)v);
+}
+
+#define futex_init(f)	futex_set(f, 0)
+
+/* Wait on futex @__f value @__v become in condition @__c */
+#define futex_wait_if_cond(__f, __v, __cond)						\
+	do {										\
+		int ret;								\
+		uint32_t tmp;								\
+											\
+		while (1) {								\
+			struct timespec to = {.tv_sec = 120};				\
+			tmp = futex_get(__f);						\
+			if ((tmp & FUTEX_ABORT_FLAG) ||					\
+			    (tmp __cond (__v)))						\
+				break;							\
+			ret = sys_futex((uint32_t *)&(__f)->raw.counter, FUTEX_WAIT,	\
+					tmp, &to, NULL, 0);				\
+			if (ret == -ETIMEDOUT) {					\
+				pr_warn("blocked for more than 120 seconds\n");		\
+				continue;						\
+			}								\
+			if (ret == -EINTR || ret == -EWOULDBLOCK)			\
+				continue;						\
+			if (ret < 0) {							\
+				pr_err("futex() returned an unexpected error: %d\n",	\
+				       ret);						\
+				BUG();							\
+			}								\
+		}									\
+	} while (0)
+
+/* Set futex @f to @v and wake up all waiters */
+static inline void futex_set_and_wake(futex_t *f, uint32_t v)
+{
+	atomic_set(&f->raw, (int)v);
+	BUG_ON(sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
+}
+
+/* Mark futex @f as wait abort needed and wake up all waiters */
+static inline void futex_abort_and_wake(futex_t *f)
+{
+	BUILD_BUG_ON(!(FUTEX_ABORT_RAW & FUTEX_ABORT_FLAG));
+	futex_set_and_wake(f, FUTEX_ABORT_RAW);
+}
+
+/* Decrement futex @f value and wake up all waiters */
+static inline void futex_dec_and_wake(futex_t *f)
+{
+	atomic_dec(&f->raw);
+	BUG_ON(sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
+}
+
+/* Increment futex @f value and wake up all waiters */
+static inline void futex_inc_and_wake(futex_t *f)
+{
+	atomic_inc(&f->raw);
+	BUG_ON(sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
+}
+
+/* Plain increment futex @f value */
+static inline void futex_inc(futex_t *f) { atomic_inc(&f->raw); }
+
+/* Plain decrement futex @f value */
+static inline void futex_dec(futex_t *f) { atomic_dec(&f->raw); }
+
+/* Wait until futex @f value become @v */
+#define futex_wait_until(f, v) futex_wait_if_cond(f, v, ==)
+
+/* Wait while futex @f value is greater than @v */
+#define futex_wait_while_gt(f, v) futex_wait_if_cond(f, v, <=)
+
+/* Wait while futex @f value is less than @v */
+#define futex_wait_while_lt(f, v) futex_wait_if_cond(f, v, >=)
+
+/* Wait while futex @f value is equal to @v */
+#define futex_wait_while_eq(f, v) futex_wait_if_cond(f, v, !=)
+
+/* Wait while futex @f value is @v */
+static inline void futex_wait_while(futex_t *f, uint32_t v)
+{
+	while ((uint32_t)atomic_read(&f->raw) == v) {
+		int ret = sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAIT, v, NULL, NULL, 0);
+		BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
+	}
+}
+
+typedef struct {
+	atomic_t	raw;
+} mutex_t;
+
+static inline void mutex_init(mutex_t *m)
+{
+	uint32_t c = 0;
+	atomic_set(&m->raw, (int)c);
+}
+
+static inline void mutex_lock(mutex_t *m)
+{
+	uint32_t c;
+	int ret;
+
+	while ((c = (uint32_t)atomic_inc_return(&m->raw)) != 1) {
+		ret = sys_futex((uint32_t *)&m->raw.counter, FUTEX_WAIT, c, NULL, NULL, 0);
+		BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
+	}
+}
+
+static inline void mutex_unlock(mutex_t *m)
+{
+	uint32_t c = 0;
+	atomic_set(&m->raw, (int)c);
+	BUG_ON(sys_futex((uint32_t *)&m->raw.counter, FUTEX_WAKE, 1, NULL, NULL, 0) < 0);
+}
+
+#endif /* COMPEL_LOCK_H__ */
diff --git a/criu/include/lock.h b/criu/include/lock.h
index 3da1af7538ec..c626d9e2abe2 100644
--- a/criu/include/lock.h
+++ b/criu/include/lock.h
@@ -1,158 +1,6 @@
 #ifndef __CR_LOCK_H__
 #define __CR_LOCK_H__
 
-#include <stdint.h>
-#include <linux/futex.h>
-#include <sys/time.h>
-#include <limits.h>
-#include <errno.h>
-
-#include "compel/asm/atomic.h"
-#include "bug.h"
-
-#ifdef CR_NOGLIBC
-# include "uapi/std/syscall.h"
-#else
-# include <unistd.h>
-# include <sys/syscall.h>
-static inline long sys_futex(void *addr1, int op, int val1,
-                            struct timespec *timeout, void *addr2, int val3)
-{
-       int rc = syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
-       if (rc == -1) rc = -errno;
-       return rc;
-}
-#endif
-
-typedef struct {
-	atomic_t raw;
-} futex_t;
-
-#define FUTEX_ABORT_FLAG	(0x80000000)
-#define FUTEX_ABORT_RAW		(-1U)
-
-/* Get current futex @f value */
-static inline uint32_t futex_get(futex_t *f)
-{
-	return atomic_read(&f->raw);
-}
-
-/* Set futex @f value to @v */
-static inline void futex_set(futex_t *f, uint32_t v)
-{
-	atomic_set(&f->raw, (int)v);
-}
-
-#define futex_init(f)	futex_set(f, 0)
-
-/* Wait on futex @__f value @__v become in condition @__c */
-#define futex_wait_if_cond(__f, __v, __cond)			\
-	do {							\
-		int ret;					\
-		uint32_t tmp;					\
-								\
-		while (1) {					\
-			struct timespec to = {.tv_sec = 120};	\
-			tmp = futex_get(__f);			\
-			if ((tmp & FUTEX_ABORT_FLAG) ||		\
-			    (tmp __cond (__v)))			\
-				break;				\
-			ret = sys_futex((uint32_t *)&(__f)->raw.counter, FUTEX_WAIT,\
-					tmp, &to, NULL, 0);	\
-			if (ret == -ETIMEDOUT) {		\
-				pr_warn("blocked for more than 120 seconds\n"); \
-				continue;			\
-			}					\
-			if (ret == -EINTR || ret == -EWOULDBLOCK) \
-				continue;			\
-			if (ret < 0) {				\
-				pr_err("futex() returned an unexpected error: %d\n", ret); \
-				BUG();				\
-			}					\
-		}						\
-	} while (0)
-
-/* Set futex @f to @v and wake up all waiters */
-static inline void futex_set_and_wake(futex_t *f, uint32_t v)
-{
-	atomic_set(&f->raw, (int)v);
-	BUG_ON(sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
-}
-
-/* Mark futex @f as wait abort needed and wake up all waiters */
-static inline void futex_abort_and_wake(futex_t *f)
-{
-	BUILD_BUG_ON(!(FUTEX_ABORT_RAW & FUTEX_ABORT_FLAG));
-	futex_set_and_wake(f, FUTEX_ABORT_RAW);
-}
-
-/* Decrement futex @f value and wake up all waiters */
-static inline void futex_dec_and_wake(futex_t *f)
-{
-	atomic_dec(&f->raw);
-	BUG_ON(sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
-}
-
-/* Increment futex @f value and wake up all waiters */
-static inline void futex_inc_and_wake(futex_t *f)
-{
-	atomic_inc(&f->raw);
-	BUG_ON(sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
-}
-
-/* Plain increment futex @f value */
-static inline void futex_inc(futex_t *f) { atomic_inc(&f->raw); }
-
-/* Plain decrement futex @f value */
-static inline void futex_dec(futex_t *f) { atomic_dec(&f->raw); }
-
-/* Wait until futex @f value become @v */
-#define futex_wait_until(f, v) futex_wait_if_cond(f, v, ==)
-
-/* Wait while futex @f value is greater than @v */
-#define futex_wait_while_gt(f, v) futex_wait_if_cond(f, v, <=)
-
-/* Wait while futex @f value is less than @v */
-#define futex_wait_while_lt(f, v) futex_wait_if_cond(f, v, >=)
-
-/* Wait while futex @f value is equal to @v */
-#define futex_wait_while_eq(f, v) futex_wait_if_cond(f, v, !=)
-
-/* Wait while futex @f value is @v */
-static inline void futex_wait_while(futex_t *f, uint32_t v)
-{
-	while ((uint32_t)atomic_read(&f->raw) == v) {
-		int ret = sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAIT, v, NULL, NULL, 0);
-		BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
-	}
-}
-
-typedef struct {
-	atomic_t	raw;
-} mutex_t;
-
-static inline void mutex_init(mutex_t *m)
-{
-	uint32_t c = 0;
-	atomic_set(&m->raw, (int)c);
-}
-
-static inline void mutex_lock(mutex_t *m)
-{
-	uint32_t c;
-	int ret;
-
-	while ((c = (uint32_t)atomic_inc_return(&m->raw)) != 1) {
-		ret = sys_futex((uint32_t *)&m->raw.counter, FUTEX_WAIT, c, NULL, NULL, 0);
-		BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
-	}
-}
-
-static inline void mutex_unlock(mutex_t *m)
-{
-	uint32_t c = 0;
-	atomic_set(&m->raw, (int)c);
-	BUG_ON(sys_futex((uint32_t *)&m->raw.counter, FUTEX_WAKE, 1, NULL, NULL, 0) < 0);
-}
+#include "compel/lock.h"
 
 #endif /* __CR_LOCK_H__ */
-- 
2.7.4



More information about the CRIU mailing list