Zero  0.1.0
lock_s.h
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2011-2013, Hewlett-Packard Development Company, LP
3  */
4 
5 #ifndef __LOCK_S_H
6 #define __LOCK_S_H
7 
13 extern bool OKVL_EXPERIMENT;
14 
16 extern uint32_t OKVL_INIT_STR_PREFIX_LEN;
17 
19 extern uint32_t OKVL_INIT_STR_UNIQUEFIER_LEN;
20 
21 #include "w_defines.h"
22 #include "w_key.h"
23 #include "w_okvl.h"
24 #include "basics.h"
25 
39 // CS TODO: we could probably make lockid_t with size 64bits instead of 128,
40 // since
41 class lockid_t {
42 private:
49  union {
50  uint64_t l[2];
51  uint32_t w[4];
52  };
53 
54 public:
56  bool operator<(lockid_t const& p) const;
57 
59  bool operator==(const lockid_t& p) const;
60 
62  bool operator!=(const lockid_t& p) const;
63 
64  friend ostream& operator<<(ostream& o, const lockid_t& i);
65 
66 public:
68  uint32_t hash() const; // used by lock_cache
70  void zero();
71 
73  StoreID store() const;
74 
75 private:
76  void set_store(StoreID s);
77 
78 public:
79 
80  // construct vacuous lockid_t
81  lockid_t();
82 
84  lockid_t(StoreID stid, const unsigned char* keystr, int16_t keylen);
85 
87  lockid_t(StoreID stid, const w_keystr_t& key);
88 
90  lockid_t(const lockid_t& i);
91 
93  lockid_t& operator=(const lockid_t& i);
94 
95 private:
96  void _init_for_str(StoreID stid, const unsigned char* keystr, int16_t keylen);
97 
98  // CS TODO replace with std::hash
99  uint64_t hash64(uint32_t seed, const unsigned char* str, int16_t len);
100 };
101 
102 inline StoreID lockid_t::store() const {
103  w_assert9(sizeof(StoreID) == sizeof(w[1]));
104  return w[1];
105 }
106 
107 inline void lockid_t::set_store(StoreID _s) {
108  w_assert9(sizeof(StoreID) == sizeof(w[1]));
109  w[1] = (uint32_t)_s;
110 }
111 
112 inline void lockid_t::zero() {
113  l[0] = l[1] = 0;
114 }
115 
117  zero();
118 }
119 
120 // use fixed seed for repeatability and easier debugging
121 const uint32_t LOCKID_T_HASH_SEED = 0xEE5C61DD;
122 
123 // CS: copied from old w_hashing.h
124 inline uint64_t lockid_t::hash64(uint32_t seed, const unsigned char* str, int16_t len) {
125  // simple iterative multiply-sum method on byte-by-byte (safe on every architecture)
126  w_assert1(len >= 0);
127  uint64_t ret = 0;
128  for (int16_t i = 0; i < len; ++i) {
129  ret = seed * ret + str[i];
130  }
131  return ret;
132 }
133 
134 inline void
135 lockid_t::_init_for_str(StoreID stid, const unsigned char* keystr, int16_t keylen) {
136  zero();
137  set_store(stid);
138 
139  if (OKVL_EXPERIMENT) {
140  // take only the prefix part (logical key)
141  if (OKVL_INIT_STR_PREFIX_LEN > 0 && keylen > (int32_t)OKVL_INIT_STR_PREFIX_LEN) {
142  keylen = OKVL_INIT_STR_PREFIX_LEN;
143  }
144  }
145  l[1] = hash64(LOCKID_T_HASH_SEED, keystr, keylen);
146 }
147 
148 inline
149 lockid_t::lockid_t(StoreID stid, const unsigned char* keystr, int16_t keylen) {
150  _init_for_str(stid, keystr, keylen);
151 }
152 
153 inline
155  _init_for_str(stid, (const unsigned char*)key.buffer_as_keystr(), key.get_length_as_keystr());
156 }
157 
158 inline lockid_t&
160  l[0] = i.l[0];
161  l[1] = i.l[1];
162  return *this;
163 }
164 
165 inline bool
166 lockid_t::operator<(lockid_t const& i) const {
167  if (l[0] < i.l[0]) {
168  return true;
169  }
170  if (l[0] > i.l[0]) {
171  return false;
172  }
173  return (l[1] < i.l[1]);
174 }
175 
176 inline bool
178  return (l[0] == i.l[0] && l[1] == i.l[1]);
179 }
180 
181 inline lockid_t::lockid_t(const lockid_t& i) {
182  (void)this->operator=(i);
183 }
184 
185 inline bool
187  return !(l[0] == i.l[0] && l[1] == i.l[1]);
188 }
189 
190 const uint32_t LOCKID_T_HASH_MULT = 0x35D0B891;
191 
192 inline uint32_t lockid_t::hash() const {
193  uint64_t h = w[0];
194  h = h * LOCKID_T_HASH_MULT + w[1];
195  h = h * LOCKID_T_HASH_MULT + w[2];
196  h = h * LOCKID_T_HASH_MULT + w[3];
197  return ((uint32_t)(h >> 32)) ^ ((uint32_t)(h & 0xFFFFFFFF));
198 }
199 
200 #endif // __LOCK_S_H
#define w_assert1(x)
Level 1 should not add significant extra time.
Definition: w_base.h:198
const uint32_t LOCKID_T_HASH_SEED
Definition: lock_s.h:121
uint32_t OKVL_INIT_STR_PREFIX_LEN
Definition: lock_core.cpp:34
bool OKVL_EXPERIMENT
Definition: lock_core.cpp:32
void _init_for_str(StoreID stid, const unsigned char *keystr, int16_t keylen)
Definition: lock_s.h:135
const uint32_t LOCKID_T_HASH_MULT
Definition: lock_s.h:190
uint32_t StoreID
Definition: basics.h:47
bool operator<(lockid_t const &p) const
comparison operator for lockid_t, used by lock manager
Definition: lock_s.h:166
Key string class which can represent a few special values.
Definition: w_key.h:47
uint64_t hash64(uint32_t seed, const unsigned char *str, int16_t len)
Definition: lock_s.h:124
uint32_t w[4]
Definition: lock_s.h:51
void set_store(StoreID s)
Definition: lock_s.h:107
uint32_t hash() const
Used by lock cache.
Definition: lock_s.h:192
lockid_t()
Definition: lock_s.h:116
uint32_t OKVL_INIT_STR_UNIQUEFIER_LEN
Definition: lock_core.cpp:36
friend ostream & operator<<(ostream &o, const lockid_t &i)
Definition: lock_dump.cpp:57
w_keystr_len_t get_length_as_keystr() const
Definition: w_key.h:508
StoreID store() const
extract store number lockid whose lspace() == t_store or has parent with lspace() == t_store ...
Definition: lock_s.h:102
bool operator!=(const lockid_t &p) const
inequality operator for lockid_t
Definition: lock_s.h:186
The means of identifying a desired or held lock.
Definition: lock_s.h:41
bool operator==(const lockid_t &p) const
equality operator for lockid_t
Definition: lock_s.h:177
#define w_assert9(x)
changing an assert to an assert9 turns it off.
Definition: w_base.h:243
const void * buffer_as_keystr() const
Definition: w_key.h:504
lockid_t & operator=(const lockid_t &i)
copy operator
Definition: lock_s.h:159
uint64_t l[2]
Definition: lock_s.h:50
void zero()
clear out the lockid - initialize to mean nothing
Definition: lock_s.h:112