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 
15 #pragma once
16 
17 namespace ROCKSDB_NAMESPACE {
18 namespace port {
19 
20 // TODO(jorlow): Many of these belong more in the environment class rather than
21 //               here. We should try moving them and see if it affects perf.
22 
23 // The following boolean constant must be true on a little-endian machine
24 // and false otherwise.
25 static const bool kLittleEndian = true /* or some other expression */;
26 
27 // ------------------ Threading -------------------
28 
29 // A Mutex represents an exclusive lock.
30 class Mutex {
31  public:
32   Mutex();
33   ~Mutex();
34 
35   // Lock the mutex.  Waits until other lockers have exited.
36   // Will deadlock if the mutex is already locked by this thread.
37   void Lock();
38 
39   // Unlock the mutex.
40   // REQUIRES: This mutex was locked by this thread.
41   void Unlock();
42 
43   // Optionally crash if this thread does not hold this mutex.
44   // The implementation must be fast, especially if NDEBUG is
45   // defined.  The implementation is allowed to skip all checks.
46   void AssertHeld();
47 };
48 
49 class CondVar {
50  public:
51   explicit CondVar(Mutex* mu);
52   ~CondVar();
53 
54   // Atomically release *mu and block on this condition variable until
55   // either a call to SignalAll(), or a call to Signal() that picks
56   // this thread to wakeup.
57   // REQUIRES: this thread holds *mu
58   void Wait();
59 
60   // If there are some threads waiting, wake up at least one of them.
61   void Signal();
62 
63   // Wake up all waiting threads.
64   void SignallAll();
65 };
66 
67 // Thread-safe initialization.
68 // Used as follows:
69 //      static port::OnceType init_control = LEVELDB_ONCE_INIT;
70 //      static void Initializer() { ... do something ...; }
71 //      ...
72 //      port::InitOnce(&init_control, &Initializer);
73 using OnceType = intptr_t;
74 #define LEVELDB_ONCE_INIT 0
75 extern void InitOnce(port::OnceType*, void (*initializer)());
76 
77 // ------------------ Compression -------------------
78 
79 // Store the snappy compression of "input[0,input_length-1]" in *output.
80 // Returns false if snappy is not supported by this port.
81 extern bool Snappy_Compress(const char* input, size_t input_length,
82                             std::string* output);
83 
84 // If input[0,input_length-1] looks like a valid snappy compressed
85 // buffer, store the size of the uncompressed data in *result and
86 // return true.  Else return false.
87 extern bool Snappy_GetUncompressedLength(const char* input, size_t length,
88                                          size_t* result);
89 
90 // Attempt to snappy uncompress input[0,input_length-1] into *output.
91 // Returns true if successful, false if the input is invalid lightweight
92 // compressed data.
93 //
94 // REQUIRES: at least the first "n" bytes of output[] must be writable
95 // where "n" is the result of a successful call to
96 // Snappy_GetUncompressedLength.
97 extern bool Snappy_Uncompress(const char* input_data, size_t input_length,
98                               char* output);
99 
100 }  // namespace port
101 }  // namespace ROCKSDB_NAMESPACE
102