usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-localtime_r-mt.c
blob096c83eb70cb3bd401305cf2af292c29ce78cf0f
1 /* Multithread-safety test for localtime_r().
2 Copyright (C) 2024 Free Software Foundation, Inc.
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 3 of the License, or
7 (at your option) any later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2024. */
19 #include <config.h>
21 /* Work around GCC bug 44511. */
22 #if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
23 # pragma GCC diagnostic ignored "-Wreturn-type"
24 #endif
26 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
28 /* Specification. */
29 #include <time.h>
31 #include <stdio.h>
32 #include <stdlib.h>
34 #include "glthread/thread.h"
35 #include "macros.h"
38 /* Some common time zone name. */
40 #if defined _WIN32 && !defined __CYGWIN__
41 /* Cf. <https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/default-time-zones>
42 or <https://ss64.com/timezones.html> */
43 # define FRENCH_TZ "Romance Standard Time"
44 #else
45 /* Cf. <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones> */
46 # define FRENCH_TZ "Europe/Paris"
47 #endif
50 static void *
51 thread1_func (void *arg)
53 for (;;)
56 time_t t = 1178467200; /* 2007-05-06 18:00:00 */
57 struct tm tm;
58 struct tm *result = localtime_r (&t, &tm);
59 ASSERT (result == &tm);
60 if (!(result->tm_sec == 0
61 && result->tm_min == 0
62 && result->tm_hour == 18
63 && result->tm_mday == 6
64 && result->tm_mon == 5 - 1
65 && result->tm_year == 2007 - 1900
66 && result->tm_wday == 0
67 && result->tm_yday == 125
68 && result->tm_isdst == 1))
70 fprintf (stderr, "thread1 disturbed by thread2!\n"); fflush (stderr);
71 abort ();
75 /*NOTREACHED*/
78 static void *
79 thread2_func (void *arg)
81 for (;;)
83 time_t t = 1336320000; /* 2012-05-06 18:00:00 */
84 struct tm tm;
85 struct tm *result = localtime_r (&t, &tm);
86 ASSERT (result == &tm);
87 if (!(result->tm_sec == 0
88 && result->tm_min == 0
89 && result->tm_hour == 18
90 && result->tm_mday == 6
91 && result->tm_mon == 5 - 1
92 && result->tm_year == 2012 - 1900
93 && result->tm_wday == 0
94 && result->tm_yday == 126
95 && result->tm_isdst == 1))
97 fprintf (stderr, "thread2 disturbed by thread1!\n"); fflush (stderr);
98 abort ();
102 /*NOTREACHED*/
106 main (int argc, char *argv[])
108 setenv ("TZ", FRENCH_TZ, 1);
110 /* Check that this TZ works. */
112 time_t t = 0; /* 1970-01-01 01:00:00 */
113 struct tm *result = localtime (&t);
114 if (! (result
115 && result->tm_sec == 0
116 && result->tm_min == 0
117 && result->tm_hour == 1
118 && result->tm_mday == 1
119 && result->tm_mon == 1 - 1
120 && result->tm_year == 1970 - 1900
121 && result->tm_wday == 4
122 && result->tm_yday == 0
123 && result->tm_isdst == 0))
125 fputs ("Skipping test: TZ='" FRENCH_TZ "' is not Paris time\n",
126 stderr);
127 return 77;
131 /* Create the threads. */
132 gl_thread_create (thread1_func, NULL);
133 gl_thread_create (thread2_func, NULL);
135 /* Let them run for 1 second. */
137 struct timespec duration;
138 duration.tv_sec = (argc > 1 ? atoi (argv[1]) : 1);
139 duration.tv_nsec = 0;
141 nanosleep (&duration, NULL);
144 return test_exit_status;
147 #else
149 /* No multithreading available. */
151 #include <stdio.h>
154 main ()
156 fputs ("Skipping test: multithreading not enabled\n", stderr);
157 return 77;
160 #endif