2
* Copyright 2011 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: morlovich@google.com (Maksim Orlovich)
19
#ifndef PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_LOCK_MANAGER_H_
20
#define PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_LOCK_MANAGER_H_
24
#include "pagespeed/kernel/base/basictypes.h"
25
#include "pagespeed/kernel/base/named_lock_manager.h"
26
#include "pagespeed/kernel/base/scoped_ptr.h"
27
#include "pagespeed/kernel/base/string.h"
28
#include "pagespeed/kernel/base/string_util.h"
30
namespace net_instaweb {
32
class AbstractSharedMem;
33
class AbstractSharedMemSegment;
38
namespace SharedMemLockData {
42
} // namespace SharedMemLockData
44
// A simple shared memory named locking manager, which uses scheduler alarms
45
// (via SchedulerBasedAbstractLock) when it needs to block.
47
// TODO(morlovich): Implement condvars?
48
class SharedMemLockManager : public NamedLockManager {
50
// Note that you must call Initialize() in the root process, and Attach in
51
// child processes to finish the initialization.
53
// Locks created by this object must not live after it dies.
55
AbstractSharedMem* shm, const GoogleString& path, Scheduler* scheduler,
56
Hasher* hasher, MessageHandler* handler);
57
virtual ~SharedMemLockManager();
59
// Sets up our shared state for use of all child processes. Returns
60
// whether successful.
63
// Connects to already initialized state from a child process.
64
// Returns whether successful.
67
// This should be called from the root process as it is about to exit,
68
// with the same value as were passed to the constructor of any
69
// instance on which Initialize() was called, except the message_handler
70
// may be different (if for example the original one is no longer available
71
// due to the cleanup sequence).
72
static void GlobalCleanup(AbstractSharedMem* shm, const GoogleString& path,
73
MessageHandler* message_handler);
75
virtual NamedLock* CreateNamedLock(const StringPiece& name);
78
friend class SharedMemLock;
80
SharedMemLockData::Bucket* Bucket(size_t bucket);
82
// Offset of mutex wrt to segment base.
83
size_t MutexOffset(SharedMemLockData::Bucket*);
85
AbstractSharedMem* shm_runtime_;
88
scoped_ptr<AbstractSharedMemSegment> seg_;
89
Scheduler* scheduler_;
91
MessageHandler* handler_;
94
DISALLOW_COPY_AND_ASSIGN(SharedMemLockManager);
97
} // namespace net_instaweb
99
#endif // PAGESPEED_KERNEL_SHAREDMEM_SHARED_MEM_LOCK_MANAGER_H_