STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
seed.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/common/seed.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2007, 2008 Andreas Beckmann <[email protected]>
7  * Copyright (C) 2008 Roman Dementiev <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #include <cassert>
15 #include <stxxl/bits/config.h>
17 #include <stxxl/bits/namespace.h>
18 
19 #if STXXL_WINDOWS
20  #ifndef NOMINMAX
21  #define NOMINMAX
22  #endif
23  #include <io.h>
24  #include <windows.h>
25 #else
26  #include <unistd.h>
27  #include <sys/time.h>
28 #endif
29 
30 
32 
33 inline unsigned initial_seed();
34 
35 struct seed_generator_t {
36  unsigned seed;
37  mutex mtx;
38 
39  seed_generator_t(unsigned s) : seed(s)
40  { }
41 };
42 
43 seed_generator_t & seed_generator()
44 {
45  static seed_generator_t sg(initial_seed());
46  return sg;
47 }
48 
49 inline unsigned initial_seed()
50 {
51 #ifndef NDEBUG
52  static bool initialized = false;
53  assert(!initialized); // this should only be called once!
54 
55  initialized = true;
56 #endif // NDEBUG
57 #if STXXL_WINDOWS
58  // GetTickCount(): ms since system start
59  return GetTickCount() ^ GetCurrentProcessId();
60 #else
61  struct timeval tv;
62  gettimeofday(&tv, 0);
63 
64  return tv.tv_sec ^ tv.tv_usec ^ (getpid() << 16);
65 #endif
66 }
67 
68 void set_seed(unsigned seed)
69 {
71  seed_generator().seed = seed;
72 }
73 
74 unsigned get_next_seed()
75 {
77  return seed_generator().seed++;
78 }
79 
unsigned initial_seed()
Definition: seed.cpp:49
unsigned get_next_seed()
get a seed value for prng initialization, subsequent calls return a sequence of different values ...
Definition: seed.cpp:74
seed_generator_t & seed_generator()
Definition: seed.cpp:43
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void set_seed(unsigned seed)
set the global stxxl seed value
Definition: seed.cpp:68
#define STXXL_END_NAMESPACE
Definition: namespace.h:17