~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/base/abstract_shared_mem.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
 
// Copyright 2011 Google Inc.
2
 
//
3
 
// Licensed under the Apache License, Version 2.0 (the "License");
4
 
// you may not use this file except in compliance with the License.
5
 
// You may obtain a copy of the License at
6
 
//
7
 
//      http://www.apache.org/licenses/LICENSE-2.0
8
 
//
9
 
// Unless required by applicable law or agreed to in writing, software
10
 
// distributed under the License is distributed on an "AS IS" BASIS,
11
 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 
// See the License for the specific language governing permissions and
13
 
// limitations under the License.
14
 
//
15
 
// Author: morlovich@google.com (Maksim Orlovich)
16
 
 
17
 
#ifndef PAGESPEED_KERNEL_BASE_ABSTRACT_SHARED_MEM_H_
18
 
#define PAGESPEED_KERNEL_BASE_ABSTRACT_SHARED_MEM_H_
19
 
 
20
 
#include <cstddef>
21
 
#include "pagespeed/kernel/base/basictypes.h"
22
 
#include "pagespeed/kernel/base/string.h"
23
 
 
24
 
namespace net_instaweb {
25
 
 
26
 
class AbstractMutex;
27
 
class MessageHandler;
28
 
 
29
 
// This represents a region of memory shared between between multiple processes
30
 
// that may contain mutexes.
31
 
class AbstractSharedMemSegment {
32
 
 public:
33
 
  AbstractSharedMemSegment() {}
34
 
 
35
 
  // Destroying the segment object detaches from it, making all pointers into it
36
 
  // invalid.
37
 
  virtual ~AbstractSharedMemSegment();
38
 
 
39
 
  // Returns the base address of the segment. Note that there is no guarantee
40
 
  // that this address will be the same for other processes attached to the
41
 
  // same segment.
42
 
  virtual volatile char* Base() = 0;
43
 
 
44
 
  // Returns the number of bytes a mutex inside shared memory takes.
45
 
  virtual size_t SharedMutexSize() const = 0;
46
 
 
47
 
  // To use a mutex in shared memory, you first need to dedicate some
48
 
  // [offset, offset + SharedMutexSize()) chunk of memory to it. Then,
49
 
  // exactly one process must call InitializeSharedMutex(offset), and
50
 
  // all users must call AttachToSharedMutex(offset) afterwards.
51
 
  //
52
 
  // InitializeSharedMutex returns whether it succeeded or not.
53
 
  // AttachToSharedMutex returns a fresh object, giving ownership
54
 
  // to the caller. The object returned is outside shared memory,
55
 
  // and acts a helper for referring to the shared state.
56
 
  virtual bool InitializeSharedMutex(size_t offset,
57
 
                                     MessageHandler* handler) = 0;
58
 
  virtual AbstractMutex* AttachToSharedMutex(size_t offset) = 0;
59
 
 
60
 
 private:
61
 
  DISALLOW_COPY_AND_ASSIGN(AbstractSharedMemSegment);
62
 
};
63
 
 
64
 
// Interface for creating and attaching to named shared memory segments.
65
 
// The expectation is that whichever implementation is used at runtime
66
 
// will be able to handle the combination of threads & processes used by
67
 
// the hosting environment.
68
 
//
69
 
// The basic flow here is as follows:
70
 
//
71
 
//            Single process/thread startup stage:
72
 
//            CreateSegment
73
 
//            InitializeSharedMutex -----+
74
 
//           /                           |
75
 
//          /                            |
76
 
//    process/thread:                   process/thread:
77
 
//    AttachToSegment                   AttachToSegment
78
 
//    AttachToSharedMutex               AttachToSharedMutex
79
 
//       |                                     |
80
 
//       |                                     |
81
 
//       |------------------------------------/
82
 
//       |
83
 
//    single process/thread cleanup stage:
84
 
//    DestroySegment
85
 
//
86
 
class AbstractSharedMem {
87
 
 public:
88
 
  AbstractSharedMem() {}
89
 
  virtual ~AbstractSharedMem();
90
 
 
91
 
  // Size of mutexes inside shared memory segments.
92
 
  virtual size_t SharedMutexSize() const = 0;
93
 
 
94
 
  // This should be called upon main process/thread initialization to create
95
 
  // a shared memory segment that will be accessed by other processes/threads
96
 
  // as identified by a unique name (via AttachToSegment). It will remove
97
 
  // any previous segment with the same name. The memory will be zeroed out.
98
 
  //
99
 
  // May return NULL on failure.
100
 
  virtual AbstractSharedMemSegment* CreateSegment(
101
 
      const GoogleString& name, size_t size, MessageHandler* handler) = 0;
102
 
 
103
 
  // Attaches to an existing segment, which must have been created already.
104
 
  // May return NULL on failure
105
 
  virtual AbstractSharedMemSegment* AttachToSegment(
106
 
      const GoogleString& name, size_t size, MessageHandler* handler) = 0;
107
 
 
108
 
  // Cleans up the segment with given name. You should call this after there is
109
 
  // no longer any need for AttachToSegment to succeed.
110
 
  virtual void DestroySegment(const GoogleString& name,
111
 
                              MessageHandler* handler) = 0;
112
 
 
113
 
  // Implementors such as NullSharedMem that don't actually support shared
114
 
  // memory operations should return true.  All real implementations should
115
 
  // leave this as false.
116
 
  virtual bool IsDummy() { return false; }
117
 
 
118
 
 private:
119
 
  DISALLOW_COPY_AND_ASSIGN(AbstractSharedMem);
120
 
};
121
 
 
122
 
}  // namespace net_instaweb
123
 
 
124
 
#endif  // PAGESPEED_KERNEL_BASE_ABSTRACT_SHARED_MEM_H_