msvc-inval: Make handler multithread-safe.
[gnulib.git] / lib / msvc-inval.c
1 /* Invalid parameter handler for MSVC runtime libraries.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "msvc-inval.h"
22
23 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
24
25 /* Get declarations of the Win32 API functions.  */
26 # define WIN32_LEAN_AND_MEAN
27 # include <windows.h>
28
29 # if defined _MSC_VER
30
31 static void cdecl
32 gl_msvc_invalid_parameter_handler (const wchar_t *expression,
33                                    const wchar_t *function,
34                                    const wchar_t *file,
35                                    unsigned int line,
36                                    uintptr_t dummy)
37 {
38   RaiseException (STATUS_GNULIB_INVALID_PARAMETER, 0, 0, NULL);
39 }
40
41 # else
42
43 /* An index to thread-local storage.  */
44 static DWORD tls_index;
45 static int tls_initialized /* = 0 */;
46
47 /* Used as a fallback only.  */
48 static struct gl_msvc_inval_per_thread not_per_thread;
49
50 struct gl_msvc_inval_per_thread *
51 gl_msvc_inval_current (void)
52 {
53   if (!tls_initialized)
54     {
55       tls_index = TlsAlloc ();
56       tls_initialized = 1;
57     }
58   if (tls_index == TLS_OUT_OF_INDEXES)
59     /* TlsAlloc had failed.  */
60     return &not_per_thread;
61   else
62     {
63       struct gl_msvc_inval_per_thread *pointer =
64         (struct gl_msvc_inval_per_thread *) TlsGetValue (tls_index);
65       if (pointer == NULL)
66         {
67           /* First call.  Allocate a new 'struct gl_msvc_inval_per_thread'.  */
68           pointer =
69             (struct gl_msvc_inval_per_thread *)
70             malloc (sizeof (struct gl_msvc_inval_per_thread));
71           if (pointer == NULL)
72             /* Could not allocate memory.  Use the global storage.  */
73             pointer = &not_per_thread;
74           TlsSetValue (tls_index, pointer);
75         }
76       return pointer;
77     }
78 }
79
80 static void cdecl
81 gl_msvc_invalid_parameter_handler (const wchar_t *expression,
82                                    const wchar_t *function,
83                                    const wchar_t *file,
84                                    unsigned int line,
85                                    uintptr_t dummy)
86 {
87   struct gl_msvc_inval_per_thread *current = gl_msvc_inval_current ();
88   if (current->restart_valid)
89     longjmp (current->restart, 1);
90   else
91     /* An invalid parameter notification from outside the gnulib code.
92        Give the caller a chance to intervene.  */
93     RaiseException (STATUS_GNULIB_INVALID_PARAMETER, 0, 0, NULL);
94 }
95
96 # endif
97
98 static int gl_msvc_inval_initialized /* = 0 */;
99
100 void
101 gl_msvc_inval_ensure_handler (void)
102 {
103   if (gl_msvc_inval_initialized == 0)
104     {
105       _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler);
106       gl_msvc_inval_initialized = 1;
107     }
108 }
109
110 #endif