Line data Source code
1 : /* gen-posix-lock-obj.c - Build tool to construct the lock object.
2 : Copyright (C) 2014 g10 Code GmbH
3 :
4 : This file is part of libgpg-error.
5 :
6 : libgpg-error is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Lesser General Public License
8 : as published by the Free Software Foundation; either version 2.1 of
9 : the License, or (at your option) any later version.
10 :
11 : libgpg-error is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public
17 : License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #if HAVE_CONFIG_H
21 : #include <config.h>
22 : #endif
23 :
24 : #ifdef HAVE_W32_SYSTEM
25 : # error This module may not be build for Windows.
26 : #endif
27 :
28 : #include <stdlib.h>
29 : #include <string.h>
30 : #include <stdio.h>
31 : #include <errno.h>
32 : #include <pthread.h>
33 :
34 : #include "posix-lock-obj.h"
35 :
36 : #define PGM "gen-posix-lock-obj"
37 :
38 : /* Check that configure did its job. */
39 : #ifdef USE_POSIX_THREADS
40 : #if SIZEOF_PTHREAD_MUTEX_T < 4
41 : # error sizeof pthread_mutex_t is not known.
42 : #endif
43 : #endif
44 :
45 : /* Special requirements for certain platforms. */
46 : #if defined(__hppa__) && defined(__linux__)
47 : # define USE_16BYTE_ALIGNMENT 1
48 : #else
49 : # define USE_16BYTE_ALIGNMENT 0
50 : #endif
51 :
52 :
53 : #if USE_16BYTE_ALIGNMENT && !HAVE_GCC_ATTRIBUTE_ALIGNED
54 : # error compiler is not able to enforce a 16 byte alignment
55 : #endif
56 :
57 : #ifdef USE_POSIX_THREADS
58 : static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
59 : #endif
60 :
61 : int
62 1 : main (void)
63 : {
64 : #ifdef USE_POSIX_THREADS
65 : unsigned char *p;
66 : int i;
67 : #endif
68 : struct {
69 : long vers;
70 : #ifdef USE_POSIX_THREADS
71 : pthread_mutex_t mtx;
72 : #endif
73 : } dummyobj;
74 :
75 :
76 : #ifdef USE_POSIX_THREADS
77 : if (sizeof mtx != SIZEOF_PTHREAD_MUTEX_T)
78 : {
79 : fprintf (stderr, PGM ": pthread_mutex_t mismatch\n");
80 : exit (1);
81 : }
82 : #endif /*USE_POSIX_THREADS*/
83 :
84 : if (sizeof (dummyobj) != sizeof (_gpgrt_lock_t))
85 : {
86 : fprintf (stderr, PGM ": internal and external lock object mismatch\n");
87 : exit (1);
88 : }
89 :
90 1 : printf ("## lock-obj-pub.%s.h%s\n"
91 : "## File created by " PGM " - DO NOT EDIT\n"
92 : "## To be included by mkheader into gpg-error.h\n"
93 : "\n",
94 : HOST_TRIPLET_STRING,
95 : #ifdef USE_POSIX_THREADS
96 : ""
97 : #else
98 : " - NO LOCK SUPPORT"
99 : #endif
100 : );
101 :
102 : #ifdef USE_POSIX_THREADS
103 :
104 : /* To force a probably suitable alignment of the structure we use a
105 : union and include a long and a pointer to a long. */
106 1 : printf ("typedef struct\n"
107 : "{\n"
108 : " long _vers;\n"
109 : " union {\n"
110 : " volatile char _priv[%d];\n"
111 : "%s"
112 : " long _x_align;\n"
113 : " long *_xp_align;\n"
114 : " } u;\n"
115 : "} gpgrt_lock_t;\n"
116 : "\n"
117 : "#define GPGRT_LOCK_INITIALIZER {%d,{{",
118 : SIZEOF_PTHREAD_MUTEX_T,
119 : # if USE_16BYTE_ALIGNMENT
120 : " int _x16_align __attribute__ ((aligned (16)));\n",
121 : # else
122 : "",
123 : # endif
124 : LOCK_ABI_VERSION);
125 : p = (unsigned char *)&mtx;
126 41 : for (i=0; i < sizeof mtx; i++)
127 : {
128 40 : if (i && !(i % 8))
129 4 : printf (" \\\n%*s", 36, "");
130 40 : printf ("%u", p[i]);
131 40 : if (i < sizeof mtx - 1)
132 : putchar (',');
133 : }
134 1 : fputs ("}}}\n", stdout);
135 :
136 : #else /*!USE_POSIX_THREADS*/
137 :
138 : printf ("/* Dummy object - no locking available. */\n"
139 : "typedef struct\n"
140 : "{\n"
141 : " long _vers;\n"
142 : "} gpgrt_lock_t;\n"
143 : "\n"
144 : "#define GPGRT_LOCK_INITIALIZER {%d}\n",
145 : LOCK_ABI_VERSION);
146 :
147 : #endif /*!USE_POSIX_THREADS*/
148 :
149 1 : fputs ("##\n"
150 : "## Loc" "al Variables:\n"
151 : "## mode: c\n"
152 : "## buffer-read-only: t\n"
153 : "## End:\n"
154 : "##\n", stdout);
155 :
156 1 : if (ferror (stdout))
157 : {
158 0 : fprintf (stderr, PGM ": error writing to stdout: %s\n", strerror (errno));
159 0 : return 1;
160 : }
161 :
162 : return 0;
163 : }
|