port_example.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. //
  10. // This file contains the specification, but not the implementations,
  11. // of the types/operations/etc. that should be defined by a platform
  12. // specific port_<platform>.h file. Use this file as a reference for
  13. // how to port this package to a new platform.
  14. #pragma once
  15. namespace ROCKSDB_NAMESPACE {
  16. namespace port {
  17. // TODO(jorlow): Many of these belong more in the environment class rather than
  18. // here. We should try moving them and see if it affects perf.
  19. // The following boolean constant must be true on a little-endian machine
  20. // and false otherwise.
  21. static const bool kLittleEndian = true /* or some other expression */;
  22. // ------------------ Threading -------------------
  23. // A Mutex represents an exclusive lock.
  24. class Mutex {
  25. public:
  26. Mutex();
  27. ~Mutex();
  28. // Lock the mutex. Waits until other lockers have exited.
  29. // Will deadlock if the mutex is already locked by this thread.
  30. void Lock();
  31. // Unlock the mutex.
  32. // REQUIRES: This mutex was locked by this thread.
  33. void Unlock();
  34. // Optionally crash if this thread does not hold this mutex.
  35. // The implementation must be fast, especially if NDEBUG is
  36. // defined. The implementation is allowed to skip all checks.
  37. void AssertHeld();
  38. };
  39. class CondVar {
  40. public:
  41. explicit CondVar(Mutex* mu);
  42. ~CondVar();
  43. // Atomically release *mu and block on this condition variable until
  44. // either a call to SignalAll(), or a call to Signal() that picks
  45. // this thread to wakeup.
  46. // REQUIRES: this thread holds *mu
  47. void Wait();
  48. // If there are some threads waiting, wake up at least one of them.
  49. void Signal();
  50. // Wake up all waiting threads.
  51. void SignallAll();
  52. };
  53. // Thread-safe initialization.
  54. // Used as follows:
  55. // static port::OnceType init_control = LEVELDB_ONCE_INIT;
  56. // static void Initializer() { ... do something ...; }
  57. // ...
  58. // port::InitOnce(&init_control, &Initializer);
  59. typedef intptr_t OnceType;
  60. #define LEVELDB_ONCE_INIT 0
  61. extern void InitOnce(port::OnceType*, void (*initializer)());
  62. // ------------------ Compression -------------------
  63. // Store the snappy compression of "input[0,input_length-1]" in *output.
  64. // Returns false if snappy is not supported by this port.
  65. extern bool Snappy_Compress(const char* input, size_t input_length,
  66. std::string* output);
  67. // If input[0,input_length-1] looks like a valid snappy compressed
  68. // buffer, store the size of the uncompressed data in *result and
  69. // return true. Else return false.
  70. extern bool Snappy_GetUncompressedLength(const char* input, size_t length,
  71. size_t* result);
  72. // Attempt to snappy uncompress input[0,input_length-1] into *output.
  73. // Returns true if successful, false if the input is invalid lightweight
  74. // compressed data.
  75. //
  76. // REQUIRES: at least the first "n" bytes of output[] must be writable
  77. // where "n" is the result of a successful call to
  78. // Snappy_GetUncompressedLength.
  79. extern bool Snappy_Uncompress(const char* input_data, size_t input_length,
  80. char* output);
  81. } // namespace port
  82. } // namespace ROCKSDB_NAMESPACE