| 1 |
/* Multithread-safety test for gmtime_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 |
static void * |
| 38 |
thread1_func (void *arg) |
| 39 |
{ |
| 40 |
for (;;) |
| 41 |
{ |
| 42 |
time_t t = 1509000003; /* 2017-10-26 06:40:03 */ |
| 43 |
struct tm tm; |
| 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)) |
| 55 |
{ |
| 56 |
fprintf (stderr, "thread1 disturbed by thread2!\n"); fflush (stderr); |
| 57 |
abort (); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/*NOTREACHED*/ |
| 62 |
} |
| 63 |
|
| 64 |
static void * |
| 65 |
thread2_func (void *arg) |
| 66 |
{ |
| 67 |
for (;;) |
| 68 |
{ |
| 69 |
time_t t = 2000050005; /* 2033-05-18 17:26:45 */ |
| 70 |
struct tm tm; |
| 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)) |
| 82 |
{ |
| 83 |
fprintf (stderr, "thread2 disturbed by thread1!\n"); fflush (stderr); |
| 84 |
abort (); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/*NOTREACHED*/ |
| 89 |
} |
| 90 |
|
| 91 |
int |
| 92 |
main (int argc, char *argv[]) |
| 93 |
{ |
| 94 |
/* Create the threads. */ |
| 95 |
gl_thread_create (thread1_func, NULL); |
| 96 |
gl_thread_create (thread2_func, NULL); |
| 97 |
|
| 98 |
/* Let them run for 1 second. */ |
| 99 |
{ |
| 100 |
struct timespec duration; |
| 101 |
duration.tv_sec = (argc > 1 ? atoi (argv[1]) : 1); |
| 102 |
duration.tv_nsec = 0; |
| 103 |
|
| 104 |
nanosleep (&duration, NULL); |
| 105 |
} |
| 106 |
|
| 107 |
return test_exit_status; |
| 108 |
} |
| 109 |
|
| 110 |
#else |
| 111 |
|
| 112 |
/* No multithreading available. */ |
| 113 |
|
| 114 |
#include <stdio.h> |
| 115 |
|
| 116 |
int |
| 117 |
main () |
| 118 |
{ |
| 119 |
fputs ("Skipping test: multithreading not enabled\n", stderr); |
| 120 |
return 77; |
| 121 |
} |
| 122 |
|
| 123 |
#endif |