Commit ef73a7f2 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov Committed by Pavel Emelyanov

criu: lock -- Use systemwide uintX types

For portability sake, we gonna share this header.

travis-ci: success for Common headers
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
parent c1e39779
#ifndef __CR_LOCK_H__ #ifndef __CR_LOCK_H__
#define __CR_LOCK_H__ #define __CR_LOCK_H__
#include <stdint.h>
#include <linux/futex.h> #include <linux/futex.h>
#include <sys/time.h> #include <sys/time.h>
#include <limits.h> #include <limits.h>
...@@ -32,13 +33,13 @@ typedef struct { ...@@ -32,13 +33,13 @@ typedef struct {
#define FUTEX_ABORT_RAW (-1U) #define FUTEX_ABORT_RAW (-1U)
/* Get current futex @f value */ /* Get current futex @f value */
static inline u32 futex_get(futex_t *f) static inline uint32_t futex_get(futex_t *f)
{ {
return atomic_read(&f->raw); return atomic_read(&f->raw);
} }
/* Set futex @f value to @v */ /* Set futex @f value to @v */
static inline void futex_set(futex_t *f, u32 v) static inline void futex_set(futex_t *f, uint32_t v)
{ {
atomic_set(&f->raw, (int)v); atomic_set(&f->raw, (int)v);
} }
...@@ -49,7 +50,7 @@ static inline void futex_set(futex_t *f, u32 v) ...@@ -49,7 +50,7 @@ static inline void futex_set(futex_t *f, u32 v)
#define futex_wait_if_cond(__f, __v, __cond) \ #define futex_wait_if_cond(__f, __v, __cond) \
do { \ do { \
int ret; \ int ret; \
u32 tmp; \ uint32_t tmp; \
\ \
while (1) { \ while (1) { \
struct timespec to = {.tv_sec = 120}; \ struct timespec to = {.tv_sec = 120}; \
...@@ -57,7 +58,7 @@ static inline void futex_set(futex_t *f, u32 v) ...@@ -57,7 +58,7 @@ static inline void futex_set(futex_t *f, u32 v)
if ((tmp & FUTEX_ABORT_FLAG) || \ if ((tmp & FUTEX_ABORT_FLAG) || \
(tmp __cond (__v))) \ (tmp __cond (__v))) \
break; \ break; \
ret = sys_futex((u32 *)&(__f)->raw.counter, FUTEX_WAIT,\ ret = sys_futex((uint32_t *)&(__f)->raw.counter, FUTEX_WAIT,\
tmp, &to, NULL, 0); \ tmp, &to, NULL, 0); \
if (ret == -ETIMEDOUT) { \ if (ret == -ETIMEDOUT) { \
pr_warn("blocked for more than 120 seconds\n"); \ pr_warn("blocked for more than 120 seconds\n"); \
...@@ -73,10 +74,10 @@ static inline void futex_set(futex_t *f, u32 v) ...@@ -73,10 +74,10 @@ static inline void futex_set(futex_t *f, u32 v)
} while (0) } while (0)
/* Set futex @f to @v and wake up all waiters */ /* Set futex @f to @v and wake up all waiters */
static inline void futex_set_and_wake(futex_t *f, u32 v) static inline void futex_set_and_wake(futex_t *f, uint32_t v)
{ {
atomic_set(&f->raw, (int)v); atomic_set(&f->raw, (int)v);
BUG_ON(sys_futex((u32 *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0); 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 */ /* Mark futex @f as wait abort needed and wake up all waiters */
...@@ -90,14 +91,14 @@ static inline void futex_abort_and_wake(futex_t *f) ...@@ -90,14 +91,14 @@ static inline void futex_abort_and_wake(futex_t *f)
static inline void futex_dec_and_wake(futex_t *f) static inline void futex_dec_and_wake(futex_t *f)
{ {
atomic_dec(&f->raw); atomic_dec(&f->raw);
BUG_ON(sys_futex((u32 *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0); 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 */ /* Increment futex @f value and wake up all waiters */
static inline void futex_inc_and_wake(futex_t *f) static inline void futex_inc_and_wake(futex_t *f)
{ {
atomic_inc(&f->raw); atomic_inc(&f->raw);
BUG_ON(sys_futex((u32 *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0); BUG_ON(sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAKE, INT_MAX, NULL, NULL, 0) < 0);
} }
/* Plain increment futex @f value */ /* Plain increment futex @f value */
...@@ -119,10 +120,10 @@ static inline void futex_dec(futex_t *f) { atomic_dec(&f->raw); } ...@@ -119,10 +120,10 @@ static inline void futex_dec(futex_t *f) { atomic_dec(&f->raw); }
#define futex_wait_while_eq(f, v) futex_wait_if_cond(f, v, !=) #define futex_wait_while_eq(f, v) futex_wait_if_cond(f, v, !=)
/* Wait while futex @f value is @v */ /* Wait while futex @f value is @v */
static inline void futex_wait_while(futex_t *f, u32 v) static inline void futex_wait_while(futex_t *f, uint32_t v)
{ {
while ((u32)atomic_read(&f->raw) == v) { while ((uint32_t)atomic_read(&f->raw) == v) {
int ret = sys_futex((u32 *)&f->raw.counter, FUTEX_WAIT, v, NULL, NULL, 0); int ret = sys_futex((uint32_t *)&f->raw.counter, FUTEX_WAIT, v, NULL, NULL, 0);
BUG_ON(ret < 0 && ret != -EWOULDBLOCK); BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
} }
} }
...@@ -133,26 +134,26 @@ typedef struct { ...@@ -133,26 +134,26 @@ typedef struct {
static inline void mutex_init(mutex_t *m) static inline void mutex_init(mutex_t *m)
{ {
u32 c = 0; uint32_t c = 0;
atomic_set(&m->raw, (int)c); atomic_set(&m->raw, (int)c);
} }
static inline void mutex_lock(mutex_t *m) static inline void mutex_lock(mutex_t *m)
{ {
u32 c; uint32_t c;
int ret; int ret;
while ((c = (u32)atomic_inc_return(&m->raw)) != 1) { while ((c = (uint32_t)atomic_inc_return(&m->raw)) != 1) {
ret = sys_futex((u32 *)&m->raw.counter, FUTEX_WAIT, c, NULL, NULL, 0); ret = sys_futex((uint32_t *)&m->raw.counter, FUTEX_WAIT, c, NULL, NULL, 0);
BUG_ON(ret < 0 && ret != -EWOULDBLOCK); BUG_ON(ret < 0 && ret != -EWOULDBLOCK);
} }
} }
static inline void mutex_unlock(mutex_t *m) static inline void mutex_unlock(mutex_t *m)
{ {
u32 c = 0; uint32_t c = 0;
atomic_set(&m->raw, (int)c); atomic_set(&m->raw, (int)c);
BUG_ON(sys_futex((u32 *)&m->raw.counter, FUTEX_WAKE, 1, NULL, NULL, 0) < 0); BUG_ON(sys_futex((uint32_t *)&m->raw.counter, FUTEX_WAKE, 1, NULL, NULL, 0) < 0);
} }
#endif /* __CR_LOCK_H__ */ #endif /* __CR_LOCK_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment