[llvm-commits] [llvm] r156237 - in /llvm/trunk: include/llvm/Support/Process.h lib/Support/Process.cpp

Daniel Dunbar daniel at zuster.org
Sat May 5 09:36:21 PDT 2012


Author: ddunbar
Date: Sat May  5 11:36:20 2012
New Revision: 156237

URL: http://llvm.org/viewvc/llvm-project?rev=156237&view=rev
Log:
[Support] Add sys::Process::GetRandomNumber().
 - Primitive API, but we rarely have need for random numbers.

Modified:
    llvm/trunk/include/llvm/Support/Process.h
    llvm/trunk/lib/Support/Process.cpp

Modified: llvm/trunk/include/llvm/Support/Process.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Process.h?rev=156237&r1=156236&r2=156237&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Process.h (original)
+++ llvm/trunk/include/llvm/Support/Process.h Sat May  5 11:36:20 2012
@@ -1,4 +1,4 @@
-//===- llvm/Support/Process.h ------------------------------------*- C++ -*-===//
+//===- llvm/Support/Process.h -----------------------------------*- C++ -*-===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -142,6 +142,10 @@
 
       /// Resets the terminals colors, or returns an escape sequence to do so.
       static const char *ResetColor();
+
+      /// Get the result of a process wide random number generator. The
+      /// generator will be automatically seeded in non-deterministic fashion.
+      static unsigned GetRandomNumber();
     /// @}
   };
 }

Modified: llvm/trunk/lib/Support/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Process.cpp?rev=156237&r1=156236&r2=156237&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Process.cpp (original)
+++ llvm/trunk/lib/Support/Process.cpp Sat May  5 11:36:20 2012
@@ -22,6 +22,15 @@
 //===          independent code.
 //===----------------------------------------------------------------------===//
 
+unsigned llvm::sys::Process::GetRandomNumber() {
+#if defined(HAVE_ARC4RANDOM)
+  return arc4random();
+#else
+  static int x = (::srand(::time(NULL)), 0);
+  return rand();
+#endif
+}
+
 }
 
 // Include the platform-specific parts of this class.





More information about the llvm-commits mailing list