1
// Copyright 2011 Google Inc.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
15
// Author: morlovich@google.com (Maksim Orlovich)
17
#ifndef PAGESPEED_KERNEL_BASE_ABSTRACT_SHARED_MEM_H_
18
#define PAGESPEED_KERNEL_BASE_ABSTRACT_SHARED_MEM_H_
21
#include "pagespeed/kernel/base/basictypes.h"
22
#include "pagespeed/kernel/base/string.h"
24
namespace net_instaweb {
29
// This represents a region of memory shared between between multiple processes
30
// that may contain mutexes.
31
class AbstractSharedMemSegment {
33
AbstractSharedMemSegment() {}
35
// Destroying the segment object detaches from it, making all pointers into it
37
virtual ~AbstractSharedMemSegment();
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
42
virtual volatile char* Base() = 0;
44
// Returns the number of bytes a mutex inside shared memory takes.
45
virtual size_t SharedMutexSize() const = 0;
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.
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;
61
DISALLOW_COPY_AND_ASSIGN(AbstractSharedMemSegment);
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.
69
// The basic flow here is as follows:
71
// Single process/thread startup stage:
73
// InitializeSharedMutex -----+
76
// process/thread: process/thread:
77
// AttachToSegment AttachToSegment
78
// AttachToSharedMutex AttachToSharedMutex
81
// |------------------------------------/
83
// single process/thread cleanup stage:
86
class AbstractSharedMem {
88
AbstractSharedMem() {}
89
virtual ~AbstractSharedMem();
91
// Size of mutexes inside shared memory segments.
92
virtual size_t SharedMutexSize() const = 0;
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.
99
// May return NULL on failure.
100
virtual AbstractSharedMemSegment* CreateSegment(
101
const GoogleString& name, size_t size, MessageHandler* handler) = 0;
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;
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;
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; }
119
DISALLOW_COPY_AND_ASSIGN(AbstractSharedMem);
122
} // namespace net_instaweb
124
#endif // PAGESPEED_KERNEL_BASE_ABSTRACT_SHARED_MEM_H_