1 /* Multithread-safety test for gmtime_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. */
21 /* Work around GCC bug 44511. */
22 #if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
23 # pragma GCC diagnostic ignored "-Wreturn-type"
26 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
34 #include "glthread/thread.h"
38 thread1_func (void *arg
)
42 time_t t
= 1509000003; /* 2017-10-26 06:40:03 */
44 struct tm
*result
= gmtime_r (&t
, &tm
);
45 ASSERT (result
== &tm
);
46 if (!(result
->tm_sec
== 3
47 && result
->tm_min
== 40
48 && result
->tm_hour
== 6
49 && result
->tm_mday
== 26
50 && result
->tm_mon
== 10 - 1
51 && result
->tm_year
== 2017 - 1900
52 && result
->tm_wday
== 4
53 && result
->tm_yday
== 298
54 && result
->tm_isdst
== 0))
56 fprintf (stderr
, "thread1 disturbed by thread2!\n"); fflush (stderr
);
65 thread2_func (void *arg
)
69 time_t t
= 2000050005; /* 2033-05-18 17:26:45 */
71 struct tm
*result
= gmtime_r (&t
, &tm
);
72 ASSERT (result
== &tm
);
73 if (!(result
->tm_sec
== 45
74 && result
->tm_min
== 26
75 && result
->tm_hour
== 17
76 && result
->tm_mday
== 18
77 && result
->tm_mon
== 5 - 1
78 && result
->tm_year
== 2033 - 1900
79 && result
->tm_wday
== 3
80 && result
->tm_yday
== 137
81 && result
->tm_isdst
== 0))
83 fprintf (stderr
, "thread2 disturbed by thread1!\n"); fflush (stderr
);
92 main (int argc
, char *argv
[])
94 /* Create the threads. */
95 gl_thread_create (thread1_func
, NULL
);
96 gl_thread_create (thread2_func
, NULL
);
98 /* Let them run for 1 second. */
100 struct timespec duration
;
101 duration
.tv_sec
= (argc
> 1 ? atoi (argv
[1]) : 1);
102 duration
.tv_nsec
= 0;
104 nanosleep (&duration
, NULL
);
107 return test_exit_status
;
112 /* No multithreading available. */
119 fputs ("Skipping test: multithreading not enabled\n", stderr
);