~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/sharedmem/shared_circular_buffer.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2011 Google Inc.
3
 
 *
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
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
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.
15
 
 */
16
 
 
17
 
// Author: fangfei@google.com (Fangfei Zhou)
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_SHAREDMEM_SHARED_CIRCULAR_BUFFER_H_
20
 
#define PAGESPEED_KERNEL_SHAREDMEM_SHARED_CIRCULAR_BUFFER_H_
21
 
 
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"
26
 
 
27
 
namespace net_instaweb {
28
 
 
29
 
class AbstractSharedMem;
30
 
class AbstractSharedMemSegment;
31
 
class AbstractMutex;
32
 
class CircularBuffer;
33
 
class MessageHandler;
34
 
class Writer;
35
 
 
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.
42
 
 
43
 
class SharedCircularBuffer {
44
 
 public:
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
56
 
  // existing segment.
57
 
  bool InitSegment(bool parent, MessageHandler* handler);
58
 
  // Reset circular buffer.
59
 
  void Clear();
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);
69
 
 
70
 
 private:
71
 
  bool InitMutex(MessageHandler* handler);
72
 
  GoogleString SegmentName() const;
73
 
 
74
 
  // SegmentName looks like:
75
 
  // filename_prefix/SharedCircularBuffer.filename_suffix.
76
 
  AbstractSharedMem* shm_runtime_;
77
 
  // Capacity of circular buffer.
78
 
  const int buffer_capacity_;
79
 
  // Circular buffer.
80
 
  CircularBuffer* buffer_;
81
 
  const GoogleString filename_prefix_;
82
 
  // filename_suffix_ is used to distinguish SharedCircularBuffer.
83
 
  const GoogleString filename_suffix_;
84
 
  // Mutex for segment.
85
 
  scoped_ptr<AbstractMutex> mutex_;
86
 
  // Shared memory segment.
87
 
  scoped_ptr<AbstractSharedMemSegment> segment_;
88
 
 
89
 
  DISALLOW_COPY_AND_ASSIGN(SharedCircularBuffer);
90
 
};
91
 
 
92
 
}  // namespace net_instaweb
93
 
 
94
 
#endif  // PAGESPEED_KERNEL_SHAREDMEM_SHARED_CIRCULAR_BUFFER_H_