~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/thread/pthread_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_THREAD_PTHREAD_SHARED_MEM_H_
18
 
#define PAGESPEED_KERNEL_THREAD_PTHREAD_SHARED_MEM_H_
19
 
 
20
 
#include <cstddef>
21
 
#include <map>
22
 
#include <utility>
23
 
 
24
 
#include "pagespeed/kernel/base/abstract_shared_mem.h"
25
 
#include "pagespeed/kernel/base/basictypes.h"
26
 
#include "pagespeed/kernel/base/string.h"
27
 
 
28
 
namespace net_instaweb {
29
 
class MessageHandler;
30
 
 
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.
35
 
//
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 {
39
 
 public:
40
 
  PthreadSharedMem();
41
 
  virtual ~PthreadSharedMem();
42
 
 
43
 
  virtual size_t SharedMutexSize() const;
44
 
 
45
 
  virtual AbstractSharedMemSegment* CreateSegment(
46
 
      const GoogleString& name, size_t size, MessageHandler* handler);
47
 
 
48
 
  virtual AbstractSharedMemSegment* AttachToSegment(
49
 
      const GoogleString& name, size_t size, MessageHandler* handler);
50
 
 
51
 
  virtual void DestroySegment(const GoogleString& name,
52
 
                              MessageHandler* handler);
53
 
 
54
 
  // Frees all lazy-initialized memory used to track shared-memory segments.
55
 
  static void Terminate();
56
 
 
57
 
 private:
58
 
  typedef std::map<GoogleString, std::pair<char*, size_t> > SegmentBaseMap;
59
 
 
60
 
  // Accessor for below. Note that the segment_bases_lock will be held at exit.
61
 
  static SegmentBaseMap* AcquireSegmentBases();
62
 
 
63
 
  static void UnlockSegmentBases();
64
 
 
65
 
  // Prefixes the passed in segment name with the current instance number.
66
 
  GoogleString PrefixSegmentName(const GoogleString& name);
67
 
 
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_;
72
 
 
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_;
80
 
 
81
 
  DISALLOW_COPY_AND_ASSIGN(PthreadSharedMem);
82
 
};
83
 
 
84
 
}  // namespace net_instaweb
85
 
 
86
 
#endif  // PAGESPEED_KERNEL_THREAD_PTHREAD_SHARED_MEM_H_