| 1 |
/* Multithread-safety test for localtime_r(). |
| 2 |
Copyright (C) 2024-2025 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 3 of the License, or |
| 7 |
(at your option) 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 |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2024. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
/* Work around GCC bug 44511. */ |
| 22 |
#if _GL_GNUC_PREREQ (4, 3) |
| 23 |
# pragma GCC diagnostic ignored "-Wreturn-type" |
| 24 |
#endif |
| 25 |
|
| 26 |
#if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS |
| 27 |
|
| 28 |
/* Specification. */ |
| 29 |
#include <time.h> |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
|
| 34 |
#include "glthread/thread.h" |
| 35 |
#include "macros.h" |
| 36 |
|
| 37 |
|
| 38 |
/* Some common time zone name. */ |
| 39 |
|
| 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 |
| 48 |
|
| 49 |
|
| 50 |
static void * |
| 51 |
thread1_func (void *arg) |
| 52 |
{ |
| 53 |
for (;;) |
| 54 |
{ |
| 55 |
|
| 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)) |
| 69 |
{ |
| 70 |
fprintf (stderr, "thread1 disturbed by thread2!\n"); fflush (stderr); |
| 71 |
abort (); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/*NOTREACHED*/ |
| 76 |
} |
| 77 |
|
| 78 |
static void * |
| 79 |
thread2_func (void *arg) |
| 80 |
{ |
| 81 |
for (;;) |
| 82 |
{ |
| 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)) |
| 96 |
{ |
| 97 |
fprintf (stderr, "thread2 disturbed by thread1!\n"); fflush (stderr); |
| 98 |
abort (); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/*NOTREACHED*/ |
| 103 |
} |
| 104 |
|
| 105 |
int |
| 106 |
main (int argc, char *argv[]) |
| 107 |
{ |
| 108 |
setenv ("TZ", FRENCH_TZ, 1); |
| 109 |
|
| 110 |
/* Check that this TZ works. */ |
| 111 |
{ |
| 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)) |
| 124 |
{ |
| 125 |
fputs ("Skipping test: TZ='" FRENCH_TZ "' is not Paris time\n", |
| 126 |
stderr); |
| 127 |
return 77; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/* Create the threads. */ |
| 132 |
gl_thread_create (thread1_func, NULL); |
| 133 |
gl_thread_create (thread2_func, NULL); |
| 134 |
|
| 135 |
/* Let them run for 1 second. */ |
| 136 |
{ |
| 137 |
struct timespec duration; |
| 138 |
duration.tv_sec = (argc > 1 ? atoi (argv[1]) : 1); |
| 139 |
duration.tv_nsec = 0; |
| 140 |
|
| 141 |
nanosleep (&duration, NULL); |
| 142 |
} |
| 143 |
|
| 144 |
return test_exit_status; |
| 145 |
} |
| 146 |
|
| 147 |
#else |
| 148 |
|
| 149 |
/* No multithreading available. */ |
| 150 |
|
| 151 |
#include <stdio.h> |
| 152 |
|
| 153 |
int |
| 154 |
main () |
| 155 |
{ |
| 156 |
fputs ("Skipping test: multithreading not enabled\n", stderr); |
| 157 |
return 77; |
| 158 |
} |
| 159 |
|
| 160 |
#endif |