小组成员:谢瑞阳、徐翔宇
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.

112 lines
2.4 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. #ifdef SNAPPY
  11. #include <snappy.h>
  12. #endif
  13. #include <stdint.h>
  14. #include <string>
  15. #include <cstdatomic>
  16. #include <cstring>
  17. namespace leveldb {
  18. namespace port {
  19. static const bool kLittleEndian = (__BYTE_ORDER == __LITTLE_ENDIAN);
  20. class CondVar;
  21. class Mutex {
  22. public:
  23. Mutex();
  24. ~Mutex();
  25. void Lock();
  26. void Unlock();
  27. void AssertHeld() { }
  28. private:
  29. friend class CondVar;
  30. pthread_mutex_t mu_;
  31. // No copying
  32. Mutex(const Mutex&);
  33. void operator=(const Mutex&);
  34. };
  35. class CondVar {
  36. public:
  37. explicit CondVar(Mutex* mu);
  38. ~CondVar();
  39. void Wait();
  40. void Signal();
  41. void SignalAll();
  42. private:
  43. pthread_cond_t cv_;
  44. Mutex* mu_;
  45. };
  46. // Storage for a lock-free pointer
  47. class AtomicPointer {
  48. private:
  49. std::atomic<void*> rep_;
  50. public:
  51. AtomicPointer() { }
  52. explicit AtomicPointer(void* v) : rep_(v) { }
  53. inline void* Acquire_Load() const {
  54. return rep_.load(std::memory_order_acquire);
  55. }
  56. inline void Release_Store(void* v) {
  57. rep_.store(v, std::memory_order_release);
  58. }
  59. inline void* NoBarrier_Load() const {
  60. return rep_.load(std::memory_order_relaxed);
  61. }
  62. inline void NoBarrier_Store(void* v) {
  63. rep_.store(v, std::memory_order_relaxed);
  64. }
  65. };
  66. inline bool Snappy_Compress(const char* input, size_t input_length,
  67. std::string* output) {
  68. #ifdef SNAPPY
  69. output->resize(snappy::MaxCompressedLength(input_length));
  70. size_t outlen;
  71. snappy::RawCompress(input, input_length, &(*output)[0], &outlen);
  72. output->resize(outlen);
  73. return true;
  74. #endif
  75. return false;
  76. }
  77. inline bool Snappy_Uncompress(const char* input_data, size_t input_length,
  78. std::string* output) {
  79. #ifdef SNAPPY
  80. size_t ulength;
  81. if (!snappy::GetUncompressedLength(input_data, ulength, &ulength)) {
  82. return false;
  83. }
  84. output->resize(ulength);
  85. return snappy::RawUncompress(input_data, input_length, &(*output)[0]);
  86. #endif
  87. return false;
  88. }
  89. inline bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg) {
  90. return false;
  91. }
  92. }
  93. }
  94. #endif // STORAGE_LEVELDB_PORT_PORT_POSIX_H_