1 mike 1.2 //%2006////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
11 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 mike 1.2 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 // Author: Mike Brasher (m.brasher@inovadevelopment.com)
33 //
34 //%/////////////////////////////////////////////////////////////////////////////
35
36 #ifndef Pegasus_Once_h
37 #define Pegasus_Once_h
38
39 #include <Pegasus/Common/Config.h>
40 #include <Pegasus/Common/Linkage.h>
41 #include <Pegasus/Common/Mutex.h>
42
43 mike 1.2 #define PEGASUS_ONCE_INITIALIZER { PEGASUS_MUTEX_INITIALIZER, 0 }
44
45 PEGASUS_NAMESPACE_BEGIN
46
47 /** Once implements the "once" concept as introduced by POSIX threads.
48 That is, it arranges for a function to be called just once in a thread
49 safe manner. The following example shows how to constuct an object of
50 type X the first time any thread reaches the line that calls once().
51
52 static Once _once = PEGASUS_ONCE_INITIALIZER;
53 static static X* _ptr;
54
55 static void _create_X()
56 {
57 ptr = new X;
58 }
59
60 ...
61
62 once(&_once, _create_X());
63
64 mike 1.2 The _create_X() function is called exactly once no matter how many times
65 once() is called on it. Also, once() may be called safely from multiple
66 threads.
67
68 CAUTION: Once instances must always be defined statically.
69 */
70 struct Once
71 {
72 MutexType mutex;
73 int initialized;
74 };
75
76 void PEGASUS_COMMON_LINKAGE __once(Once* once, void (*function)());
77
78 inline void once(Once* once, void (*function)())
79 {
80 if (once->initialized == 0)
81 __once(once, function);
82 }
83
84 PEGASUS_NAMESPACE_END
85 mike 1.2
86 #endif /* Pegasus_Once_h */
|