Debian Codegraph @

src/lib/common/MutexFactory.h

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 MutexFactory.h
29
30 This factory produces OS specific mutex objects
31 *****************************************************************************/
32
33 #ifndef _SOFTHSM_V2_MUTEXFACTORY_H
34 #define _SOFTHSM_V2_MUTEXFACTORY_H
35
36 #include "config.h"
37 #include "osmutex.h"
38 #include "cryptoki.h"
39 #include <memory>
40
41 class Mutex
42 {
43 public:
44 // Constructor
45 Mutex();
46
47 // Destructor
48 virtual ~Mutex();
49
50 // Lock the mutex
51 bool lock();
52
53 // Unlock the mutex
54 void unlock();
55
56 private:
57 // The mutex handle
58 CK_VOID_PTR handle;
59
60 // Is the mutex valid?
61 bool isValid;
62 };
63
64 class MutexLocker
65 {
66 public:
67 // Constructor
68 MutexLocker(Mutex* inMutex);
69
70 // Destructor
71 virtual ~MutexLocker();
72
73 private:
74 // The mutex to lock
75 Mutex* mutex;
76 };
77
78 class MutexFactory
79 {
80 public:
81 // Return the one-and-only instance
82 static MutexFactory* i();
83
84 // Destructor
85 virtual ~MutexFactory();
86
87 // Get a mutex instance
88 Mutex* getMutex();
89
90 // Recycle a mutex instance
91 void recycleMutex(Mutex* mutex);
92
93 // Set the function pointers
94 void setCreateMutex(CK_CREATEMUTEX inCreateMutex);
95 void setDestroyMutex(CK_DESTROYMUTEX inDestroyMutex);
96 void setLockMutex(CK_LOCKMUTEX inLockMutex);
97 void setUnlockMutex(CK_UNLOCKMUTEX inUnlockMutex);
98
99 // Enable/disable mutex handling
100 void enable();
101 void disable();
102
103 private:
104 // Constructor
105 MutexFactory();
106
107 // Mutex operations
108 friend class Mutex;
109
110 CK_RV CreateMutex(CK_VOID_PTR_PTR newMutex);
111 CK_RV DestroyMutex(CK_VOID_PTR mutex);
112 CK_RV LockMutex(CK_VOID_PTR mutex);
113 CK_RV UnlockMutex(CK_VOID_PTR mutex);
114
115 // The one-and-only instance
116 #ifdef HAVE_CXX11
117 static std::unique_ptr<MutexFactory> instance;
118 #else
119 static std::auto_ptr<MutexFactory> instance;
120 #endif
121
122 // The function pointers
123 CK_CREATEMUTEX createMutex;
124 CK_DESTROYMUTEX destroyMutex;
125 CK_LOCKMUTEX lockMutex;
126 CK_UNLOCKMUTEX unlockMutex;
127
128 // Can we do mutex handling?
129 bool enabled;
130 };
131
132 #endif // !_SOFTHSM_V2_MUTEXFACTORY_H
133