Coverage Report

Created: 2026-05-31 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gettext/gettext-tools/libgettextpo/glthread/once.c
Line
Count
Source
1
/* Once-only initialization in multithreaded situations.
2
   Copyright (C) 2005-2026 Free Software Foundation, Inc.
3
4
   This file is free software: you can redistribute it and/or modify
5
   it under the terms of the GNU Lesser General Public License as
6
   published by the Free Software Foundation; either version 2.1 of the
7
   License, or (at your option) any later version.
8
9
   This file 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 Lesser General Public License for more details.
13
14
   You should have received a copy of the GNU Lesser General Public License
15
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17
/* Written by Bruno Haible <bruno@clisp.org>, 2005.
18
   Based on GCC's gthr-posix.h, gthr-posix95.h.  */
19
20
#include <config.h>
21
22
#include "glthread/once.h"
23
24
#include <errno.h>
25
26
/* ========================================================================= */
27
28
#if USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
29
30
#endif
31
32
/* ========================================================================= */
33
34
#if USE_POSIX_THREADS
35
36
static const pthread_once_t fresh_once = PTHREAD_ONCE_INIT;
37
38
int
39
glthread_once_singlethreaded (pthread_once_t *once_control)
40
0
{
41
  /* We don't know whether pthread_once_t is an integer type, a floating-point
42
     type, a pointer type, or a structure type.  */
43
0
  char *firstbyte = (char *)once_control;
44
0
  if (*firstbyte == *(const char *)&fresh_once)
45
0
    {
46
      /* First time use of once_control.  Invert the first byte.  */
47
0
      *firstbyte = ~ *(const char *)&fresh_once;
48
0
      return 1;
49
0
    }
50
0
  else
51
0
    return 0;
52
0
}
53
54
# if !(PTHREAD_IN_USE_DETECTION_HARD || USE_POSIX_THREADS_WEAK)
55
56
int
57
glthread_once_multithreaded (pthread_once_t *once_control,
58
                             void (*init_function) (void))
59
{
60
  int err = pthread_once (once_control, init_function);
61
  if (err == ENOSYS)
62
    {
63
      /* This happens on FreeBSD 11: The pthread_once function in libc returns
64
         ENOSYS.  */
65
      if (glthread_once_singlethreaded (once_control))
66
        init_function ();
67
      return 0;
68
    }
69
  return err;
70
}
71
72
# endif
73
74
#endif
75
76
/* ========================================================================= */
77
78
#if USE_WINDOWS_THREADS
79
80
#endif
81
82
/* ========================================================================= */