summaryrefslogtreecommitdiffstats
path: root/chrome_frame/scoped_initialization_manager.h
blob: 0a22f4ffedd5e04a396ef76dcb411fd69f69da54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_FRAME_SCOPED_INITIALIZATION_MANAGER_H_
#define CHROME_FRAME_SCOPED_INITIALIZATION_MANAGER_H_

#include "base/basictypes.h"
#include "base/lazy_instance.h"
#include "base/synchronization/lock.h"

namespace chrome_frame {

// A class intended to be instantiated on the stack in a dyanmically loaded
// shared object to initialize and shutdown the object's dependencies. |Traits|
// must be a type with two public static void(void) functions named Initialize
// and Shutdown. Traits::Initialize will be invoked when the first instance of
// this class is created and Traits::Shutdown will be invoked when the last one
// is destroyed.
template<class Traits>
class ScopedInitializationManager {
 public:
  ScopedInitializationManager() { AddRef(); }
  ~ScopedInitializationManager() { Release(); }

 private:
  static void AddRef() {
    base::AutoLock auto_lock(lock_.Get());
    DCHECK_LT(ref_count_, kuint32max);
    if (++ref_count_ == 1)
      Traits::Initialize();
  }

  static void Release() {
    base::AutoLock auto_lock(lock_.Get());
    DCHECK_GT(ref_count_, 0U);
    if (--ref_count_ == 0)
      Traits::Shutdown();
  }

  static base::LazyInstance<base::Lock>::Leaky lock_;
  static uint32 ref_count_;
  DISALLOW_COPY_AND_ASSIGN(ScopedInitializationManager);
};

template<class Traits> base::LazyInstance<base::Lock>::Leaky
    ScopedInitializationManager<Traits>::lock_ = LAZY_INSTANCE_INITIALIZER;

template<class Traits> uint32
    ScopedInitializationManager<Traits>::ref_count_ = 0;

}  // namespace chrome_frame

#endif  // CHROME_FRAME_SCOPED_INITIALIZATION_MANAGER_H_