[lttng-dev] [PATCH liburcu 5/5] Added implementation for rand_r()

Charles Briere charlesbriere.flatzo at gmail.com
Wed Nov 27 00:08:22 EST 2013


From: Pierre-Luc St-Charles <pierre-luc.st-charles at polymtl.ca>

stdlib.h doesn't implement rand_r() on Android.

Signed-off-by: Charles Briere <charlesbriere.flatzo at gmail.com>
---
 Makefile.am                      |  1 +
 tests/benchmark/test_urcu_hash.h |  1 +
 urcu/rand-compat.h               | 60 ++++++++++++++++++++++++++++++++++++++++
 urcu/static/urcu.h               |  1 +
 4 files changed, 63 insertions(+)
 create mode 100644 urcu/rand-compat.h

diff --git a/Makefile.am b/Makefile.am
index 64073a1..38ff543 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -23,6 +23,7 @@ nobase_dist_include_HEADERS = urcu/compiler.h urcu/hlist.h urcu/list.h \
 		urcu/lfstack.h urcu/syscall-compat.h \
 		$(top_srcdir)/urcu/map/*.h \
 		$(top_srcdir)/urcu/static/*.h \
+		urcu/rand-compat.h \
 		urcu/tls-compat.h
 nobase_nodist_include_HEADERS = urcu/arch.h urcu/uatomic.h urcu/config.h
 
diff --git a/tests/benchmark/test_urcu_hash.h b/tests/benchmark/test_urcu_hash.h
index c0727b7..b35acdb 100644
--- a/tests/benchmark/test_urcu_hash.h
+++ b/tests/benchmark/test_urcu_hash.h
@@ -37,6 +37,7 @@
 #include <signal.h>
 
 #include <urcu/tls-compat.h>
+#include <urcu/rand-compat.h>
 #include "cpuset.h"
 #include "thread-id.h"
 
diff --git a/urcu/rand-compat.h b/urcu/rand-compat.h
new file mode 100644
index 0000000..b9545c8
--- /dev/null
+++ b/urcu/rand-compat.h
@@ -0,0 +1,60 @@
+#ifndef _URCU_RAND_COMPAT_H
+#define _URCU_RAND_COMPAT_H
+
+/*
+ * urcu/rand-compat.h
+ *
+ * Userspace RCU library - rand/rand_r Compatibility Header
+ *
+ * Note: this file is only used to simplify the code required to
+ * use the 'rand_r(...)' system function across multiple platforms,
+ * which might not always be referenced the same way.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifdef __ANDROID__
+/*
+ * Reentrant random function from POSIX.1c.
+ * Copyright (C) 1996, 1999 Free Software Foundation, Inc.
+ * This file is part of the GNU C Library.
+ * Contributed by Ulrich Drepper <drepper at cygnus.com <mailto:drepper at cygnus.com>>, 1996.
+ */
+static inline int rand_r(unsigned int *seed)
+{
+       unsigned int next = *seed;
+       int result;
+
+       next *= 1103515245;
+       next += 12345;
+       result = (unsigned int) (next / 65536) % 2048;
+
+       next *= 1103515245;
+       next += 12345;
+       result <<= 10;
+       result ^= (unsigned int) (next / 65536) % 1024;
+
+       next *= 1103515245;
+       next += 12345;
+       result <<= 10;
+       result ^= (unsigned int) (next / 65536) % 1024;
+
+       *seed = next;
+
+       return result;
+}
+#endif /* __ANDROID__ */
+
+#endif /* _URCU_RAND_COMPAT_H */
diff --git a/urcu/static/urcu.h b/urcu/static/urcu.h
index 53d2610..33545b7 100644
--- a/urcu/static/urcu.h
+++ b/urcu/static/urcu.h
@@ -41,6 +41,7 @@
 #include <urcu/list.h>
 #include <urcu/futex.h>
 #include <urcu/tls-compat.h>
+#include <urcu/rand-compat.h>
 
 #ifdef __cplusplus
 extern "C" {
-- 
1.8.4.2




More information about the lttng-dev mailing list