This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add check for nl_langinfo_l()
[metaconfig.git] / U / threads / d_nl_langinfo_l.U
1 ?RCS: $Id$
2 ?RCS:
3 ?RCS: Copyright (c) 2017 Karl Williamson
4 ?RCS:
5 ?RCS: You may distribute under the terms of either the GNU General Public
6 ?RCS: License or the Artistic License, as specified in the README file.
7 ?RCS:
8 ?MAKE:d_nl_langinfo_l d_thread_safe_nl_langinfo_l: Compile cat rm_try Oldconfig \
9         i_pthread i_stdlib i_langinfo i_locale          \
10         usethreads run
11 ?MAKE:  -pick add $@ %<
12 ?S:d_nl_langinfo_l:
13 ?S:     This variable contains the eventual value of the
14 ?S:     HAS_NL_LANGINFO_L symbol, which indicates if the
15 ?S:     nl_langinfo_l() function exists.
16 ?S:.
17 ?S:d_thread_safe_nl_langinfo_l:
18 ?S:     This variable contains the eventual value of the
19 ?S:     HAS_THREAD_SAFE_NL_LANGINFO_L symbol, which indicates if the
20 ?S:     nl_langinfo_l() function exists and is thread-safe.
21 ?S:.
22 ?C:HAS_NL_LANGINFO_L:
23 ?C:     This symbol, when defined, indicates presence of the nl_langinfo_l()
24 ?C:     function
25 ?C:.
26 ?C:HAS_THREAD_SAFE_NL_LANGINFO_L:
27 ?C:     This symbol, when defined, indicates presence of the nl_langinfo_l()
28 ?C:     function, and that it is thread-safe.
29 ?C:.
30 ?H:#$d_nl_langinfo_l HAS_NL_LANGINFO_L  /**/
31 ?H:#$d_thread_safe_nl_langinfo_l HAS_THREAD_SAFE_NL_LANGINFO_L  /**/
32 ?H:.
33 ?F:!try
34 : check for nl_langinfo_l item
35 $cat <<EOM
36
37 Checking to see if you have nl_langinfo_l(), and that it is thread-safe
38 EOM
39 ?X: Despite claiming that nl_langinfo_l() is thread-safe, the POSIX 2008
40 ?X: standard allows for a non-safe implementation:
41 ?X:
42 ?X:    "nl_langinfo_l() uses an internal per-thread buffer, and nl_langinfo()
43 ?X:     uses (in all threads) the same buffer that nl_langinfo_l() uses in the
44 ?X:     initial thread. There can be interactions, but only when
45 ?X:     nl_langinfo_l() is called in the initial thread"
46 ?X:
47 ?X: This program calls nl_langinfo_l(), then creates a thread, calling plain
48 ?X: nl_langinfo() from within it, then sees if the buffer in the original
49 ?X: thread was changed.
50 $cat >try.c <<EOCP
51 #$i_stdlib I_STDLIB
52 #ifdef I_STDLIB
53 #  include <stdlib.h>
54 #endif
55 #include <string.h>
56 #$i_langinfo I_LANGINFO
57 #ifdef I_LANGINFO
58 #  include <langinfo.h>
59 #endif
60 #$i_pthread I_PTHREAD
61 #ifdef I_PTHREAD
62 #  include <pthread.h>
63 #endif
64 #$i_locale I_LOCALE
65 #ifdef I_LOCALE
66 #  include <locale.h>
67 #endif
68
69 void *
70 thread_start(void * arg)
71 {
72     nl_langinfo(RADIXCHAR);
73 }
74
75 int main() {
76     char * main_buffer;
77     char save_main_buffer[1000];
78     pthread_t subthread;
79     pthread_attr_t attr;
80
81     main_buffer = nl_langinfo_l(CODESET, newlocale(LC_ALL_MASK, "C", 0));
82
83     /* If too large for our generous allowance, just assume we don't have
84      * it. */
85     if (strlen(main_buffer) >= sizeof(save_main_buffer)) {
86         exit(1);
87     }
88
89     strcpy(save_main_buffer, main_buffer);
90
91     if (pthread_attr_init(&attr) != 0) {
92         exit(1);
93     }
94
95     if (pthread_create(&subthread, &attr, thread_start, NULL) != 0) {
96         exit(1);
97     }
98
99     if (pthread_join(subthread, NULL) != 0) {
100         exit(1);
101     }
102
103     exit(! (strcmp(main_buffer, save_main_buffer) == 0));
104 }
105 EOCP
106 case "$usethreads" in
107     define)
108         set try
109         if eval $compile; then
110             echo "Your system has nl_langinfo_l()..." >&4
111             d_nl_langinfo_l="$define"
112             echo "$d_nl_langinfo_l" >&4
113             if $run ./try; then
114                 echo "and it is thread-safe (just as I'd hoped)." >&4
115                 d_thread_safe_nl_langinfo_l="$define"
116                 echo "$d_thread_safe_nl_langinfo_l" >&4
117             else
118                 echo "but it isn't thread-safe." >&4
119             fi
120         else
121             echo "your system does not have nl_langinfo_l()" >&4
122         fi
123         ;;
124     *) echo "Since threads aren't selected, we won't bother looking for nl_langinfo_l()" >&4
125 esac
126 if test X"$d_nl_langinfo_l" = X; then
127         d_nl_langinfo_l="$undef"
128 fi
129 if test X"$d_thread_safe_nl_langinfo_l" = X; then
130         d_thread_safe_nl_langinfo_l="$undef"
131 fi
132 $rm_try
133