blob: ee25a01e1530e43072c847889cc716231e394be0 [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
13namespace leveldb {
14namespace port {
15
16// TODO(jorlow): Many of these belong more in the environment class rather than
17// here. We should try moving them and see if it affects perf.
18
19// The following boolean constant must be true on a little-endian machine
20// and false otherwise.
21static const bool kLittleEndian = true /* or some other expression */;
22
23// ------------------ Threading -------------------
24
25// A Mutex represents an exclusive lock.
26class Mutex {
27 public:
28 Mutex();
29 ~Mutex();
30
31 // Lock the mutex. Waits until other lockers have exited.
32 // Will deadlock if the mutex is already locked by this thread.
33 void Lock();
34
35 // Unlock the mutex.
36 // REQUIRES: This mutex was locked by this thread.
37 void Unlock();
38
39 // Optionally crash if this thread does not hold this mutex.
40 // The implementation must be fast, especially if NDEBUG is
41 // defined. The implementation is allowed to skip all checks.
42 void AssertHeld();
43};
44
45class CondVar {
46 public:
47 explicit CondVar(Mutex* mu);
48 ~CondVar();
49
50 // Atomically release *mu and block on this condition variable until
51 // either a call to SignalAll(), or a call to Signal() that picks
52 // this thread to wakeup.
53 // REQUIRES: this thread holds *mu
54 void Wait();
55
56 // If there are some threads waiting, wake up at least one of them.
57 void Signal();
58
59 // Wake up all waiting threads.
60 void SignallAll();
61};
62
63// A type that holds a pointer that can be read or written atomically
64// (i.e., without word-tearing.)
65class AtomicPointer {
66 private:
67 intptr_t rep_;
68 public:
69 // Initialize to arbitrary value
70 AtomicPointer();
71
72 // Initialize to hold v
73 explicit AtomicPointer(void* v) : rep_(v) { }
74
75 // Read and return the stored pointer with the guarantee that no
76 // later memory access (read or write) by this thread can be
77 // reordered ahead of this read.
78 void* Acquire_Load() const;
79
80 // Set v as the stored pointer with the guarantee that no earlier
81 // memory access (read or write) by this thread can be reordered
82 // after this store.
83 void Release_Store(void* v);
84
85 // Read the stored pointer with no ordering guarantees.
86 void* NoBarrier_Load() const;
87
88 // Set va as the stored pointer with no ordering guarantees.
89 void NoBarrier_Store(void* v);
90};
91
92// ------------------ Checksumming -------------------
93
94// Store a 160-bit hash of "data[0..len-1]" in "hash_array[0]..hash_array[19]"
95extern void SHA1_Hash(const char* data, size_t len, char* hash_array);
96
97// ------------------ Compression -------------------
98
99// Store the lightweight compression of "input[0,input_length-1]" in *output.
100extern void Lightweight_Compress(const char* input, size_t input_length,
101 std::string* output);
102
103// Attempt to lightweight uncompress input[0,input_length-1] into *output.
104// Returns true if successful, false if the input is invalid lightweight
105// compressed data.
106extern bool Lightweight_Uncompress(const char* input_data, size_t input_length,
107 std::string* output);
108
109// ------------------ Miscellaneous -------------------
110
111// If heap profiling is not supported, returns false.
112// Else repeatedly calls (*func)(arg, data, n) and then returns true.
113// The concatenation of all "data[0,n-1]" fragments is the heap profile.
114extern bool GetHeapProfile(void (*func)(void*, const char*, int), void* arg);
115
116}
117}
118
119#endif // STORAGE_LEVELDB_PORT_PORT_EXAMPLE_H_