xref: /aoo41x/main/sal/inc/internal/once.h (revision 9eab2a37)
1*9eab2a37SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9eab2a37SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9eab2a37SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9eab2a37SAndrew Rist  * distributed with this work for additional information
6*9eab2a37SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9eab2a37SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9eab2a37SAndrew Rist  * "License"); you may not use this file except in compliance
9*9eab2a37SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9eab2a37SAndrew Rist  *
11*9eab2a37SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9eab2a37SAndrew Rist  *
13*9eab2a37SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9eab2a37SAndrew Rist  * software distributed under the License is distributed on an
15*9eab2a37SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9eab2a37SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9eab2a37SAndrew Rist  * specific language governing permissions and limitations
18*9eab2a37SAndrew Rist  * under the License.
19*9eab2a37SAndrew Rist  *
20*9eab2a37SAndrew Rist  *************************************************************/
21*9eab2a37SAndrew Rist 
22*9eab2a37SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef INCLUDED_SAL_INTERNAL_ONCE_H
25cdf0e10cSrcweir #define INCLUDED_SAL_INTERNAL_ONCE_H
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /** sal_once_type
28cdf0e10cSrcweir  *  (platform dependent)
29cdf0e10cSrcweir  */
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #if defined(SAL_UNX) || defined(SAL_OS2)
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <pthread.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir typedef pthread_once_t sal_once_type;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #define SAL_ONCE_INIT PTHREAD_ONCE_INIT
38cdf0e10cSrcweir #define SAL_ONCE(once, init) pthread_once((once), (init))
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #elif defined(SAL_W32)
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN
43cdf0e10cSrcweir #pragma warning(push,1) /* disable warnings within system headers */
44cdf0e10cSrcweir #include <windows.h>
45cdf0e10cSrcweir #pragma warning(pop)
46cdf0e10cSrcweir 
47cdf0e10cSrcweir typedef struct sal_once_st sal_once_type;
48cdf0e10cSrcweir struct sal_once_st
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	LONG volatile m_done;
51cdf0e10cSrcweir 	LONG volatile m_lock;
52cdf0e10cSrcweir };
53cdf0e10cSrcweir 
54cdf0e10cSrcweir #define SAL_ONCE_INIT { 0, 0 }
55cdf0e10cSrcweir #define SAL_ONCE(once, init) \
56cdf0e10cSrcweir { \
57cdf0e10cSrcweir 	sal_once_type * control = (once); \
58cdf0e10cSrcweir 	if (!(control->m_done)) \
59cdf0e10cSrcweir 	{ \
60cdf0e10cSrcweir 		while (InterlockedExchange(&(control->m_lock), 1) == 1) Sleep(0); \
61cdf0e10cSrcweir 		if (!(control->m_done)) \
62cdf0e10cSrcweir 		{ \
63cdf0e10cSrcweir 			void (*init_routine)(void) = (init); \
64cdf0e10cSrcweir 			(*init_routine)(); \
65cdf0e10cSrcweir 			control->m_done = 1; \
66cdf0e10cSrcweir 		} \
67cdf0e10cSrcweir 		InterlockedExchange(&(control->m_lock), 0); \
68cdf0e10cSrcweir 	} \
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir #else
72cdf0e10cSrcweir #error Unknown platform
73cdf0e10cSrcweir #endif /* SAL_UNX | SAL_W32 */
74cdf0e10cSrcweir 
75cdf0e10cSrcweir #endif /* INCLUDED_SAL_INTERNAL_ONCE_H */
76