2
* Copyright 2011 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: fangfei@google.com (Fangfei Zhou)
19
#ifndef PAGESPEED_KERNEL_SHAREDMEM_SHARED_CIRCULAR_BUFFER_H_
20
#define PAGESPEED_KERNEL_SHAREDMEM_SHARED_CIRCULAR_BUFFER_H_
22
#include "pagespeed/kernel/base/basictypes.h"
23
#include "pagespeed/kernel/base/scoped_ptr.h"
24
#include "pagespeed/kernel/base/string.h"
25
#include "pagespeed/kernel/base/string_util.h"
27
namespace net_instaweb {
29
class AbstractSharedMem;
30
class AbstractSharedMemSegment;
36
// Shared memory circular buffer, the content of its shared memory segment is a
37
// Mutex and a CircularBuffer.
38
// In parent process, we initialize a shared memory segment. Then we create a
39
// SharedCircularBuffer object in each process and attach it to the segment by
40
// calling InitSegment(true, handler) once in the parent process and calling
41
// InitSegment(false, handler) in each child.
43
class SharedCircularBuffer {
45
// Construct with shared memory, data buffer capacity, filename_prefix and
46
// filename_suffix. filename_prefix and filename_suffix are used to name
47
// segment for the shared circular buffer.
48
SharedCircularBuffer(AbstractSharedMem* shm_runtime,
49
const int buffer_capacity,
50
const GoogleString& filename_prefix,
51
const GoogleString& filename_suffix);
52
virtual ~SharedCircularBuffer();
53
// Initialize the shared memory segment.
54
// parent = true if this is invoked in root process -- initialize the shared
55
// memory; parent = false if this is invoked in child process -- attach to
57
bool InitSegment(bool parent, MessageHandler* handler);
58
// Reset circular buffer.
60
// Write content to circular buffer.
61
bool Write(const StringPiece& message);
62
// Write content of data in buffer to writer, without clearing the buffer.
63
bool Dump(Writer* writer, MessageHandler* handler);
64
// Return data content as string. This is for test purposes.
65
GoogleString ToString(MessageHandler* handler);
66
// This should be called from the root process as it is about to exit, when no
67
// future children are expected to start.
68
void GlobalCleanup(MessageHandler* handler);
71
bool InitMutex(MessageHandler* handler);
72
GoogleString SegmentName() const;
74
// SegmentName looks like:
75
// filename_prefix/SharedCircularBuffer.filename_suffix.
76
AbstractSharedMem* shm_runtime_;
77
// Capacity of circular buffer.
78
const int buffer_capacity_;
80
CircularBuffer* buffer_;
81
const GoogleString filename_prefix_;
82
// filename_suffix_ is used to distinguish SharedCircularBuffer.
83
const GoogleString filename_suffix_;
85
scoped_ptr<AbstractMutex> mutex_;
86
// Shared memory segment.
87
scoped_ptr<AbstractSharedMemSegment> segment_;
89
DISALLOW_COPY_AND_ASSIGN(SharedCircularBuffer);
92
} // namespace net_instaweb
94
#endif // PAGESPEED_KERNEL_SHAREDMEM_SHARED_CIRCULAR_BUFFER_H_