xref: /AOO41X/main/sal/inc/internal/once.h (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef INCLUDED_SAL_INTERNAL_ONCE_H
29 #define INCLUDED_SAL_INTERNAL_ONCE_H
30 
31 /** sal_once_type
32  *  (platform dependent)
33  */
34 
35 #if defined(SAL_UNX) || defined(SAL_OS2)
36 
37 #include <pthread.h>
38 
39 typedef pthread_once_t sal_once_type;
40 
41 #define SAL_ONCE_INIT PTHREAD_ONCE_INIT
42 #define SAL_ONCE(once, init) pthread_once((once), (init))
43 
44 #elif defined(SAL_W32)
45 
46 #define WIN32_LEAN_AND_MEAN
47 #pragma warning(push,1) /* disable warnings within system headers */
48 #include <windows.h>
49 #pragma warning(pop)
50 
51 typedef struct sal_once_st sal_once_type;
52 struct sal_once_st
53 {
54 	LONG volatile m_done;
55 	LONG volatile m_lock;
56 };
57 
58 #define SAL_ONCE_INIT { 0, 0 }
59 #define SAL_ONCE(once, init) \
60 { \
61 	sal_once_type * control = (once); \
62 	if (!(control->m_done)) \
63 	{ \
64 		while (InterlockedExchange(&(control->m_lock), 1) == 1) Sleep(0); \
65 		if (!(control->m_done)) \
66 		{ \
67 			void (*init_routine)(void) = (init); \
68 			(*init_routine)(); \
69 			control->m_done = 1; \
70 		} \
71 		InterlockedExchange(&(control->m_lock), 0); \
72 	} \
73 }
74 
75 #else
76 #error Unknown platform
77 #endif /* SAL_UNX | SAL_W32 */
78 
79 #endif /* INCLUDED_SAL_INTERNAL_ONCE_H */
80