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_THREAD_PTHREAD_SHARED_MEM_H_
18
#define PAGESPEED_KERNEL_THREAD_PTHREAD_SHARED_MEM_H_
24
#include "pagespeed/kernel/base/abstract_shared_mem.h"
25
#include "pagespeed/kernel/base/basictypes.h"
26
#include "pagespeed/kernel/base/string.h"
28
namespace net_instaweb {
31
// POSIX shared memory support, using mmap/pthread_mutexattr_setpshared
32
// Supports both processes and threads, but processes that want to access it
33
// must be results of just fork (without exec), and all the CreateSegment
34
// calls must occur before the fork.
36
// This implementation is also not capable of deallocating segments except
37
// at exit, so it should not be used when the set of segments may be dynamic.
38
class PthreadSharedMem : public AbstractSharedMem {
41
virtual ~PthreadSharedMem();
43
virtual size_t SharedMutexSize() const;
45
virtual AbstractSharedMemSegment* CreateSegment(
46
const GoogleString& name, size_t size, MessageHandler* handler);
48
virtual AbstractSharedMemSegment* AttachToSegment(
49
const GoogleString& name, size_t size, MessageHandler* handler);
51
virtual void DestroySegment(const GoogleString& name,
52
MessageHandler* handler);
54
// Frees all lazy-initialized memory used to track shared-memory segments.
55
static void Terminate();
58
typedef std::map<GoogleString, std::pair<char*, size_t> > SegmentBaseMap;
60
// Accessor for below. Note that the segment_bases_lock will be held at exit.
61
static SegmentBaseMap* AcquireSegmentBases();
63
static void UnlockSegmentBases();
65
// Prefixes the passed in segment name with the current instance number.
66
GoogleString PrefixSegmentName(const GoogleString& name);
68
// The root process stores segment locations here. Child processes will
69
// inherit a readonly copy of this map after the fork. Note that this is
70
// initialized in a thread-unsafe manner, given the above assumptions.
71
static SegmentBaseMap* segment_bases_;
73
// Holds the number of times a PthreadSharedMem has been created.
74
static size_t s_instance_count_;
75
// Used to prefix segment names, so that when two runtimes are active at the
76
// same moment they will not have overlapping segment names. This occurs in
77
// ngx_pagespeed during a configuration reload, where first a new factory is
78
// created, before destroying the old one.
79
size_t instance_number_;
81
DISALLOW_COPY_AND_ASSIGN(PthreadSharedMem);
84
} // namespace net_instaweb
86
#endif // PAGESPEED_KERNEL_THREAD_PTHREAD_SHARED_MEM_H_