2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
20 // Encapsulate the callback mechanism of CSSM.
25 #include <Security/utilities.h>
26 #include <Security/threading.h>
37 // A single module-specific callback as requested by the user.
39 class ModuleCallback
{
41 ModuleCallback() : mCallback(0), mContext(0) { }
42 ModuleCallback(CSSM_API_ModuleEventHandler callback
, void *context
)
43 : mCallback(callback
), mContext(context
) { }
45 void operator () (CSSM_MODULE_EVENT event
,
46 const Guid
&guid
, uint32 subId
,
47 CSSM_SERVICE_TYPE serviceType
) const;
49 operator bool () const { return mCallback
|| mContext
; }
50 bool operator ! () const { return !bool(*this); }
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
; }
59 CSSM_API_ModuleEventHandler mCallback
;
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.
73 class ModuleCallbackSet
{
75 unsigned int size() const { return callbacks
.size(); }
76 void insert(const ModuleCallback
&newCallback
);
77 void erase(const ModuleCallback
&oldCallback
);
79 void operator () (CSSM_MODULE_EVENT event
,
80 const Guid
&guid
, uint32 subId
,
81 CSSM_SERVICE_TYPE serviceType
) const;
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
;
88 struct Runner
: public Thread
{
89 Runner(CallbackMap
&inCallbacks
,
90 CSSM_MODULE_EVENT inEvent
,
93 CSSM_SERVICE_TYPE inServiceType
)
94 : callbacks(inCallbacks
), event(inEvent
), guid(inGuid
),
95 subserviceId(inSSId
), serviceType(inServiceType
) { }
97 CallbackMap callbacks
; // note that we share the CountingMutex * values!
98 const CSSM_MODULE_EVENT event
;
100 const uint32 subserviceId
;
101 const CSSM_SERVICE_TYPE serviceType
;
107 } // end namespace Security