blob: 704aa2419eaf60b50d60ceb48d0f17ed433fc628 [file] [log] [blame]
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +00001// 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
costan41172a22018-02-13 22:31:50 -080013#include "port/thread_annotations.h"
14
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000015namespace leveldb {
16namespace 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.orgf67e15e2011-03-18 22:37:00 +000021// ------------------ Threading -------------------
22
23// A Mutex represents an exclusive lock.
costan41172a22018-02-13 22:31:50 -080024class LOCKABLE Mutex {
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000025 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.
costan41172a22018-02-13 22:31:50 -080031 void Lock() EXCLUSIVE_LOCK_FUNCTION();
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000032
33 // Unlock the mutex.
34 // REQUIRES: This mutex was locked by this thread.
costan41172a22018-02-13 22:31:50 -080035 void Unlock() UNLOCK_FUNCTION();
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000036
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.
costan41172a22018-02-13 22:31:50 -080040 void AssertHeld() ASSERT_EXCLUSIVE_LOCK();
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000041};
42
43class 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.
Jayice9e850052021-03-28 16:38:37 +080058 void SignalAll();
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000059};
60
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000061// ------------------ Compression -------------------
62
jorlow@chromium.org8303bb12011-03-22 23:24:02 +000063// Store the snappy compression of "input[0,input_length-1]" in *output.
64// Returns false if snappy is not supported by this port.
costanaece2062018-03-12 09:14:44 -070065bool Snappy_Compress(const char* input, size_t input_length,
66 std::string* output);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000067
gabor@google.com60bd8012011-07-21 02:40:18 +000068// 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.
costanaece2062018-03-12 09:14:44 -070071bool Snappy_GetUncompressedLength(const char* input, size_t length,
72 size_t* result);
gabor@google.com60bd8012011-07-21 02:40:18 +000073
jorlow@chromium.org8303bb12011-03-22 23:24:02 +000074// Attempt to snappy uncompress input[0,input_length-1] into *output.
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000075// Returns true if successful, false if the input is invalid lightweight
76// compressed data.
gabor@google.com60bd8012011-07-21 02:40:18 +000077//
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.
costanaece2062018-03-12 09:14:44 -070081bool Snappy_Uncompress(const char* input_data, size_t input_length,
82 char* output);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000083
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.
costanaece2062018-03-12 09:14:44 -070089bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg);
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000090
costanea175e22017-02-27 14:29:18 -080091// 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).
95uint32_t AcceleratedCRC32C(uint32_t crc, const char* buf, size_t size);
96
Hans Wennborg36a5f8e2011-10-31 17:22:06 +000097} // namespace port
98} // namespace leveldb
jorlow@chromium.orgf67e15e2011-03-18 22:37:00 +000099
100#endif // STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_