]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/callback.h
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / callback.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // Encapsulate the callback mechanism of CSSM.
21 //
22 #ifndef _H_CALLBACK
23 #define _H_CALLBACK
24
25 #include <Security/utilities.h>
26 #include <Security/threading.h>
27 #include <map>
28
29 #ifdef _CPP_CALLBACK
30 #pragma export on
31 #endif
32
33 namespace Security
34 {
35
36 //
37 // A single module-specific callback as requested by the user.
38 //
39 class ModuleCallback {
40 public:
41 ModuleCallback() : mCallback(0), mContext(0) { }
42 ModuleCallback(CSSM_API_ModuleEventHandler callback, void *context)
43 : mCallback(callback), mContext(context) { }
44
45 void operator () (CSSM_MODULE_EVENT event,
46 const Guid &guid, uint32 subId,
47 CSSM_SERVICE_TYPE serviceType) const;
48
49 operator bool () const { return mCallback || mContext; }
50 bool operator ! () const { return !bool(*this); }
51
52 bool operator == (const ModuleCallback &cb) const
53 { return mCallback == cb.mCallback && mContext == cb.mContext; }
54 bool operator < (const ModuleCallback &cb) const
55 { return mCallback < cb.mCallback
56 || mCallback == cb.mCallback && mContext < cb.mContext; }
57
58 private:
59 CSSM_API_ModuleEventHandler mCallback;
60 void *mContext;
61 };
62
63
64 //
65 // A set of callbacks that can be invoked automatically in a thread-safe manner.
66 // THREADS: The set itself is not interlocked by the ModuleCallbackSet class; you
67 // are responsible for ensuring single access to the set object. The class ensures
68 // that any threads it spawns to execute the callbacks will not step on each other
69 // or on you, and that you will not be able to erase() a callback while it has
70 // activity scheduled against it. This also applies to the invocation method
71 // (operator ()) - you must lock against multiple accesses to it until it returns.
72 //
73 class ModuleCallbackSet {
74 public:
75 unsigned int size() const { return callbacks.size(); }
76 void insert(const ModuleCallback &newCallback);
77 void erase(const ModuleCallback &oldCallback);
78
79 void operator () (CSSM_MODULE_EVENT event,
80 const Guid &guid, uint32 subId,
81 CSSM_SERVICE_TYPE serviceType) const;
82
83 private:
84 // note mutex *: we don't want to rely on copy-ability of Mutex objects
85 typedef multimap<ModuleCallback, CountingMutex *> CallbackMap;
86 mutable CallbackMap callbacks;
87
88 struct Runner : public Thread {
89 Runner(CallbackMap &inCallbacks,
90 CSSM_MODULE_EVENT inEvent,
91 const Guid &inGuid,
92 uint32 inSSId,
93 CSSM_SERVICE_TYPE inServiceType)
94 : callbacks(inCallbacks), event(inEvent), guid(inGuid),
95 subserviceId(inSSId), serviceType(inServiceType) { }
96
97 CallbackMap callbacks; // note that we share the CountingMutex * values!
98 const CSSM_MODULE_EVENT event;
99 const Guid guid;
100 const uint32 subserviceId;
101 const CSSM_SERVICE_TYPE serviceType;
102
103 void action();
104 };
105 };
106
107 } // end namespace Security
108
109 #ifdef _CPP_CALLBACK
110 #pragma export off
111 #endif
112
113 #endif //_H_CALLBACK