小组成员:谢瑞阳、徐翔宇
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.5 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. //
  5. // See port_example.h for documentation for the following types/functions.
  6. #ifndef STORAGE_LEVELDB_PORT_PORT_POSIX_H_
  7. #define STORAGE_LEVELDB_PORT_PORT_POSIX_H_
  8. #include <endian.h>
  9. #include <pthread.h>
  10. #include <stdint.h>
  11. #include <string>
  12. #include <cstdatomic>
  13. #include <cstring>
  14. #include "port/sha1_portable.h"
  15. namespace leveldb {
  16. namespace port {
  17. static const bool kLittleEndian = (__BYTE_ORDER == __LITTLE_ENDIAN);
  18. class CondVar;
  19. class Mutex {
  20. public:
  21. Mutex();
  22. ~Mutex();
  23. void Lock();
  24. void Unlock();
  25. void AssertHeld() { }
  26. private:
  27. friend class CondVar;
  28. pthread_mutex_t mu_;
  29. // No copying
  30. Mutex(const Mutex&);
  31. void operator=(const Mutex&);
  32. };
  33. class CondVar {
  34. public:
  35. explicit CondVar(Mutex* mu);
  36. ~CondVar();
  37. void Wait();
  38. void Signal();
  39. void SignalAll();
  40. private:
  41. pthread_cond_t cv_;
  42. Mutex* mu_;
  43. };
  44. // Storage for a lock-free pointer
  45. class AtomicPointer {
  46. private:
  47. std::atomic<void*> rep_;
  48. public:
  49. AtomicPointer() { }
  50. explicit AtomicPointer(void* v) : rep_(v) { }
  51. inline void* Acquire_Load() const {
  52. return rep_.load(std::memory_order_acquire);
  53. }
  54. inline void Release_Store(void* v) {
  55. rep_.store(v, std::memory_order_release);
  56. }
  57. inline void* NoBarrier_Load() const {
  58. return rep_.load(std::memory_order_relaxed);
  59. }
  60. inline void NoBarrier_Store(void* v) {
  61. rep_.store(v, std::memory_order_relaxed);
  62. }
  63. };
  64. inline void SHA1_Hash(const char* data, size_t len, char* hash_array) {
  65. SHA1_Hash_Portable(data, len, hash_array);
  66. }
  67. /**
  68. * TODO(gabor): Implement actual compress
  69. * This is a hack - it just copies input to output.
  70. * No actual compression occurs.
  71. */
  72. inline void Lightweight_Compress(const char* input, size_t input_length,
  73. std::string* output) {
  74. output->assign(input, input_length);
  75. }
  76. /**
  77. * TODO(gabor): Implement actual uncompress
  78. * This is a hack - it just copies input to output.
  79. * No actual uncompression occurs.
  80. */
  81. inline bool Lightweight_Uncompress(const char* input_data, size_t input_length,
  82. std::string* output) {
  83. output->assign(input_data, input_length);
  84. return true;
  85. }
  86. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  87. return false;
  88. }
  89. }
  90. }
  91. #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_