Zero  0.1.0
lock_raw.h
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2014, Hewlett-Packard Development Company, LP
3  */
4 #ifndef __LOCK_RAW_H
5 #define __LOCK_RAW_H
6 
87 #include <cstdint>
88 #include <ostream>
89 #include <pthread.h>
90 #include "AtomicCounter.hpp"
91 #include "w_defines.h"
92 #include "w_okvl.h"
93 #include "w_rc.h"
94 #include "w_gc_pool_forest.h"
95 #include "w_markable_pointer.h"
96 #include "lsn.h"
97 
106 inline void atomic_synchronize() {
107  lintel::atomic_thread_fence(lintel::memory_order_seq_cst); // corresponds to full mfence
108 }
109 
110 #ifdef PURE_SPIN_RAWLOCK
111 
112 inline void atomic_synchronize_if_mutex() {}
113 
114 #else // PURE_SPIN_RAWLOCK
116 #endif // PURE_SPIN_RAWLOCK
117 
118 struct RawLockBucket;
119 struct RawLockQueue;
120 struct RawLock;
121 struct RawXct;
122 class sm_options;
123 
133 struct RawLock : public GcPoolEntry {
134  enum LockState {
136  UNUSED = 0,
143  };
144 
146  uint32_t hash;
147 
150 
153 
156 
159 
164 
165  // another doubly linked list for RawXctLockHashMap
168 };
169 
170 std::ostream& operator<<(std::ostream& o, const RawLock& v);
171 
174 
184 struct RawLockQueue {
195  struct Iterator {
202  Iterator(const RawLockQueue* enclosure, RawLock* start_from);
203 
205  bool is_null() const {
206  return current.is_null();
207  }
208 
214  void next(bool& must_retry);
215 
217 
223 
229  };
230 
264  w_error_codes acquire(RawXct* xct, uint32_t hash, const okvl_mode& mode,
265  int32_t timeout_in_ms, bool check, bool wait, bool acquire,
266  RawLock** out);
267 
274  w_error_codes retry_acquire(RawLock** lock, bool wait, bool acquire,
275  int32_t timeout_in_ms);
276 
278  w_error_codes complete_acquire(RawLock** lock, bool wait, bool acquire,
279  int32_t timeout_in_ms);
280 
286  void release(RawLock* lock, const lsn_t& commit_lsn);
287 
289  void update_xlock_tag(const lsn_t& commit_lsn);
290 
296  void atomic_lock_insert(RawLock* new_lock);
297 
299  struct Compatibility {
300  Compatibility(bool grant, bool deadlock, RawXct* block)
301  : can_be_granted(grant),
302  deadlocked(deadlock),
303  blocker(block) {}
304 
306 
308 
310  };
311 
316  Compatibility check_compatiblity(RawLock* lock) const;
317 
326  bool peek_compatiblity(RawXct* xct, uint32_t hash, const okvl_mode& mode) const;
327 
332  w_error_codes wait_for(RawLock* new_lock, int32_t timeout_in_ms);
333 
339  RawLock* find_predecessor(RawLock* lock) const;
340 
347  RawLock* tail() const;
348 
360  bool delink(RawLock* predecessor, RawLock* target, RawLock* successor) const;
361 
362  // Helper function called by RawLockQueue::acquire
363  // Based on the information in Compatibility, if the blocker txn is a loser txn and it is not
364  // in the middle of rolling back, trigger the on_demand UNDO for the loser transaction
365  // Return true if an on_demand UNDO operation was triggered and completed
366  bool trigger_UNDO(Compatibility& compatibility); // In: the current compatibility status of the requested lock
367 
373  mutable RawLock head;
374 
380 
381  // For on_demand and mixed UNDO counting purpose
382  static int loser_count;
383 };
384 
385 std::ostream& operator<<(std::ostream& o, const RawLockQueue& v);
386 
392 const int RAW_XCT_LOCK_HASHMAP_SIZE = 1023;
393 
423 public:
425 
427 
434  okvl_mode get_granted_mode(uint32_t lock_id) const;
435 
437  void reset();
438 
440  void push_front(RawLock* link);
441 
443  void remove(RawLock* link);
444 
445 private:
446 
451 
453  static uint32_t _bucket_id(uint32_t lock_id) {
454  return lock_id % RAW_XCT_LOCK_HASHMAP_SIZE;
455  }
456 };
457 
469 struct RawXct : GcPoolEntry {
470  enum XctState {
472  UNUSED = 0,
477  };
478 
479  void init(gc_thread_id thread_id,
480  GcPoolForest<RawLock>* lock_pool, gc_pointer_raw* lock_pool_next);
481 
482  void uninit();
483 
496  bool is_deadlocked(RawXct* first_blocker);
497 
498  void update_read_watermark(const lsn_t& tag) {
499  if (read_watermark < tag) {
500  read_watermark = tag;
501  }
502  }
503 
508  RawLock* allocate_lock(uint32_t hash,
510 
514  void deallocate_lock(RawLock* lock);
515 
517  void dump_lockinfo(std::ostream& out) const;
518 
520  bool has_locks() const {
521  return private_first != nullptr;
522  }
523 
528 
531 
534 
537 
540 
543 
544 #ifndef PURE_SPIN_RAWLOCK
545 
546  pthread_cond_t lock_wait_cond;
548  pthread_mutex_t lock_wait_mutex;
549 #endif // PURE_SPIN_RAWLOCK
550 
561 
567 
574 
581 };
582 
583 std::ostream& operator<<(std::ostream& o, const RawXct& v);
584 
594 public:
595  RawLockBackgroundThread(const sm_options& options,
596  GcPoolForest<RawLock>* lock_pool, GcPoolForest<RawXct>* xct_pool);
597 
599 
601  void start();
602 
604  void stop_synchronous();
605 
607  void wakeup();
608 
612  void run_main();
613 
617  static void* pthread_main(void* t);
618 
619 protected:
621  pthread_t _thread;
622 
624  pthread_attr_t _join_attr;
625 
627  pthread_mutex_t _interval_mutex;
628 
630  pthread_cond_t _interval_cond;
631 
634 
635  bool _running;
636 
643 
645 
651 
657 
664 
670 
676 
682 
688 
694 
700 
703 
706 };
707 
708 #endif // __LOCK_RAW_H
void atomic_thread_fence(memory_order order)
Definition: AtomicCounter.hpp:223
GcPoolForest< RawLock > * _lock_pool
Definition: lock_raw.h:702
RawLock * xct_next
Definition: lock_raw.h:163
Definition: w_gc_pool_forest.h:460
uint32_t _generation_count
Definition: lock_raw.h:650
bool _running
Definition: lock_raw.h:635
const int RAW_XCT_LOCK_HASHMAP_SIZE
Definition: lock_raw.h:392
void atomic_synchronize_if_mutex()
Definition: lock_raw.h:115
Definition: lock_raw.h:140
RawLock * private_first
Definition: lock_raw.h:573
A shadow transaction object for RAW-style lock manager.
Definition: lock_raw.h:469
MarkablePointer< RawLock > next
Definition: lock_raw.h:152
size_t _lockpool_segsize
Definition: lock_raw.h:693
Definition: lock_raw.h:474
The background thread for pre-allocation and garbage collection of object pools used in RAW-style loc...
Definition: lock_raw.h:593
Header file for lintel::Atomic class.
XctState state
Definition: lock_raw.h:536
bool deadlocked
Definition: lock_raw.h:307
Definition: lock_raw.h:142
pthread_cond_t _interval_cond
Definition: lock_raw.h:630
RawLock * private_last
Definition: lock_raw.h:580
Start-up parameters for the storage engine. See OPTIONS.
Definition: sm_options.h:24
RawXct * owner_xct
Definition: lock_raw.h:155
w_error_codes
Enum of error codes defined in w_error_xmacro.h.
Definition: w_error.h:43
lsn_t x_lock_tag
Definition: lock_raw.h:379
static uint32_t _bucket_id(uint32_t lock_id)
Definition: lock_raw.h:453
RawXct * blocker
Definition: lock_raw.h:542
const RawLockQueue * enclosure
Definition: lock_raw.h:216
gc_thread_id thread_id
Definition: lock_raw.h:527
pthread_cond_t lock_wait_cond
Definition: lock_raw.h:546
RawLock * xct_hashmap_next
Definition: lock_raw.h:167
RawXct * blocker
Definition: lock_raw.h:309
bool has_locks() const
Definition: lock_raw.h:520
Log Sequence Number. See Log Sequence Numbers (LSN).
Definition: lsn.h:243
An RAW-style lock entry in the queue.
Definition: lock_raw.h:133
RawXctLockHashMap private_hash_map
Definition: lock_raw.h:566
uint32_t _free_segment_count
Definition: lock_raw.h:663
Definition: AtomicCounter.hpp:116
Definition: lock_raw.h:476
Safely iterates through lock entires in this queue from the head.
Definition: lock_raw.h:195
bool deadlock_detected_by_others
Definition: lock_raw.h:539
bool is_null() const
Definition: lock_raw.h:205
Definition: lock_raw.h:138
XctState
Definition: lock_raw.h:470
pthread_mutex_t _interval_mutex
Definition: lock_raw.h:627
GcPoolForest< RawLock > * lock_pool
Definition: lock_raw.h:530
A portable and logical pointer with mark-for-death, ABA counter, and generation/segement bits...
Definition: w_gc_pool_forest.h:127
Compatibility(bool grant, bool deadlock, RawXct *block)
Definition: lock_raw.h:300
RawLock * predecessor
Definition: lock_raw.h:222
int _dummy_lsn_lock
Definition: lock_raw.h:642
uint32_t _max_segment_count
Definition: lock_raw.h:669
RawLock * xct_hashmap_previous
Definition: lock_raw.h:166
Definition: lock_raw.h:299
LockState
Definition: lock_raw.h:134
Definition: lock_raw.h:136
Represents a lock mode of one key entry in the OKVL lock manager.
Definition: w_okvl.h:95
uint32_t _lockpool_initseg
Definition: lock_raw.h:681
lsn_t read_watermark
Definition: lock_raw.h:560
uint32_t _init_generation_count
Definition: lock_raw.h:656
GcPoolForest< RawXct > * _xct_pool
Definition: lock_raw.h:705
std::ostream & operator<<(std::ostream &o, const RawLock &v)
Definition: lock_raw.cpp:1259
uint32_t _xctpool_initseg
Definition: lock_raw.h:687
LockState state
Definition: lock_raw.h:149
xct_t * xct()
Definition: smthread.h:575
size_t _xctpool_segsize
Definition: lock_raw.h:699
bool _stop_requested
Definition: lock_raw.h:633
uint64_t gc_thread_id
Definition: w_gc_pool_forest.h:105
pthread_attr_t _join_attr
Definition: lock_raw.h:624
void update_read_watermark(const lsn_t &tag)
Definition: lock_raw.h:498
bool can_be_granted
Definition: lock_raw.h:305
void atomic_synchronize()
Invokes "mfence", which is sfence + lfence.
Definition: lock_raw.h:106
RawLock * xct_previous
Definition: lock_raw.h:161
A hashmap for lock entries in transaction&#39;s private memory.
Definition: lock_raw.h:422
An RAW-style lock queue to hold granted and waiting lock requests (RawLock).
Definition: lock_raw.h:184
RawLock head
Definition: lock_raw.h:373
okvl_mode mode
Definition: lock_raw.h:158
MarkablePointer< RawLock > current
Definition: lock_raw.h:228
uint32_t _internal_milliseconds
Definition: lock_raw.h:675
pthread_mutex_t lock_wait_mutex
Definition: lock_raw.h:548
static int loser_count
Definition: lock_raw.h:382
uint32_t hash
Definition: lock_raw.h:146
const MarkablePointer< RawLock > NULL_RAW_LOCK
Definition: lock_raw.h:173
int _dummy_lsn_xct
Definition: lock_raw.h:644
pthread_t _thread
Definition: lock_raw.h:621
gc_pointer_raw * lock_pool_next
Definition: lock_raw.h:533