1 martin 1.5 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.6 //
|
3 martin 1.5 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
|
9 martin 1.6 //
|
10 martin 1.5 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
|
16 martin 1.6 //
|
17 martin 1.5 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.6 //
|
20 martin 1.5 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.6 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.5 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 martin 1.6 //
|
28 martin 1.5 //////////////////////////////////////////////////////////////////////////
|
29 mike 1.2 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_Once_h
33 #define Pegasus_Once_h
34
35 #include <Pegasus/Common/Config.h>
36 #include <Pegasus/Common/Linkage.h>
37 #include <Pegasus/Common/Mutex.h>
38
39 #define PEGASUS_ONCE_INITIALIZER { PEGASUS_MUTEX_INITIALIZER, 0 }
40
41 PEGASUS_NAMESPACE_BEGIN
42
43 /** Once implements the "once" concept as introduced by POSIX threads.
44 That is, it arranges for a function to be called just once in a thread
45 safe manner. The following example shows how to constuct an object of
46 type X the first time any thread reaches the line that calls once().
47
48 static Once _once = PEGASUS_ONCE_INITIALIZER;
49 static static X* _ptr;
50 mike 1.2
51 static void _create_X()
52 {
53 ptr = new X;
54 }
55
56 ...
57
|
58 mike 1.4 once(&_once, _create_X);
|
59 mike 1.2
60 The _create_X() function is called exactly once no matter how many times
61 once() is called on it. Also, once() may be called safely from multiple
62 threads.
63
64 CAUTION: Once instances must always be defined statically.
65 */
66 struct Once
67 {
68 MutexType mutex;
69 int initialized;
70 };
71
72 void PEGASUS_COMMON_LINKAGE __once(Once* once, void (*function)());
73
74 inline void once(Once* once, void (*function)())
75 {
76 if (once->initialized == 0)
77 __once(once, function);
78 }
79
80 mike 1.2 PEGASUS_NAMESPACE_END
81
82 #endif /* Pegasus_Once_h */
|