jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 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 | // This file contains the specification, but not the implementations, |
| 6 | // of the types/operations/etc. that should be defined by a platform |
| 7 | // specific port_<platform>.h file. Use this file as a reference for |
| 8 | // how to port this package to a new platform. |
| 9 | |
| 10 | #ifndef STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_ |
| 11 | #define STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_ |
| 12 | |
costan | 41172a2 | 2018-02-13 22:31:50 -0800 | [diff] [blame] | 13 | #include "port/thread_annotations.h" |
| 14 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 15 | namespace leveldb { |
| 16 | namespace port { |
| 17 | |
| 18 | // TODO(jorlow): Many of these belong more in the environment class rather than |
| 19 | // here. We should try moving them and see if it affects perf. |
| 20 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 21 | // ------------------ Threading ------------------- |
| 22 | |
| 23 | // A Mutex represents an exclusive lock. |
costan | 41172a2 | 2018-02-13 22:31:50 -0800 | [diff] [blame] | 24 | class LOCKABLE Mutex { |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 25 | public: |
| 26 | Mutex(); |
| 27 | ~Mutex(); |
| 28 | |
| 29 | // Lock the mutex. Waits until other lockers have exited. |
| 30 | // Will deadlock if the mutex is already locked by this thread. |
costan | 41172a2 | 2018-02-13 22:31:50 -0800 | [diff] [blame] | 31 | void Lock() EXCLUSIVE_LOCK_FUNCTION(); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 32 | |
| 33 | // Unlock the mutex. |
| 34 | // REQUIRES: This mutex was locked by this thread. |
costan | 41172a2 | 2018-02-13 22:31:50 -0800 | [diff] [blame] | 35 | void Unlock() UNLOCK_FUNCTION(); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 36 | |
| 37 | // Optionally crash if this thread does not hold this mutex. |
| 38 | // The implementation must be fast, especially if NDEBUG is |
| 39 | // defined. The implementation is allowed to skip all checks. |
costan | 41172a2 | 2018-02-13 22:31:50 -0800 | [diff] [blame] | 40 | void AssertHeld() ASSERT_EXCLUSIVE_LOCK(); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class CondVar { |
| 44 | public: |
| 45 | explicit CondVar(Mutex* mu); |
| 46 | ~CondVar(); |
| 47 | |
| 48 | // Atomically release *mu and block on this condition variable until |
| 49 | // either a call to SignalAll(), or a call to Signal() that picks |
| 50 | // this thread to wakeup. |
| 51 | // REQUIRES: this thread holds *mu |
| 52 | void Wait(); |
| 53 | |
| 54 | // If there are some threads waiting, wake up at least one of them. |
| 55 | void Signal(); |
| 56 | |
| 57 | // Wake up all waiting threads. |
Jayice | 9e85005 | 2021-03-28 16:38:37 +0800 | [diff] [blame] | 58 | void SignalAll(); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 61 | // ------------------ Compression ------------------- |
| 62 | |
jorlow@chromium.org | 8303bb1 | 2011-03-22 23:24:02 +0000 | [diff] [blame] | 63 | // Store the snappy compression of "input[0,input_length-1]" in *output. |
| 64 | // Returns false if snappy is not supported by this port. |
costan | aece206 | 2018-03-12 09:14:44 -0700 | [diff] [blame] | 65 | bool Snappy_Compress(const char* input, size_t input_length, |
| 66 | std::string* output); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 67 | |
gabor@google.com | 60bd801 | 2011-07-21 02:40:18 +0000 | [diff] [blame] | 68 | // If input[0,input_length-1] looks like a valid snappy compressed |
| 69 | // buffer, store the size of the uncompressed data in *result and |
| 70 | // return true. Else return false. |
costan | aece206 | 2018-03-12 09:14:44 -0700 | [diff] [blame] | 71 | bool Snappy_GetUncompressedLength(const char* input, size_t length, |
| 72 | size_t* result); |
gabor@google.com | 60bd801 | 2011-07-21 02:40:18 +0000 | [diff] [blame] | 73 | |
jorlow@chromium.org | 8303bb1 | 2011-03-22 23:24:02 +0000 | [diff] [blame] | 74 | // Attempt to snappy uncompress input[0,input_length-1] into *output. |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 75 | // Returns true if successful, false if the input is invalid lightweight |
| 76 | // compressed data. |
gabor@google.com | 60bd801 | 2011-07-21 02:40:18 +0000 | [diff] [blame] | 77 | // |
| 78 | // REQUIRES: at least the first "n" bytes of output[] must be writable |
| 79 | // where "n" is the result of a successful call to |
| 80 | // Snappy_GetUncompressedLength. |
costan | aece206 | 2018-03-12 09:14:44 -0700 | [diff] [blame] | 81 | bool Snappy_Uncompress(const char* input_data, size_t input_length, |
| 82 | char* output); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 83 | |
| 84 | // ------------------ Miscellaneous ------------------- |
| 85 | |
| 86 | // If heap profiling is not supported, returns false. |
| 87 | // Else repeatedly calls (*func)(arg, data, n) and then returns true. |
| 88 | // The concatenation of all "data[0,n-1]" fragments is the heap profile. |
costan | aece206 | 2018-03-12 09:14:44 -0700 | [diff] [blame] | 89 | bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg); |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 90 | |
costan | ea175e2 | 2017-02-27 14:29:18 -0800 | [diff] [blame] | 91 | // Extend the CRC to include the first n bytes of buf. |
| 92 | // |
| 93 | // Returns zero if the CRC cannot be extended using acceleration, else returns |
| 94 | // the newly extended CRC value (which may also be zero). |
| 95 | uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size); |
| 96 | |
Hans Wennborg | 36a5f8e | 2011-10-31 17:22:06 +0000 | [diff] [blame] | 97 | } // namespace port |
| 98 | } // namespace leveldb |
jorlow@chromium.org | f67e15e | 2011-03-18 22:37:00 +0000 | [diff] [blame] | 99 | |
| 100 | #endif // STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_ |