| 1 | /* pthread_getattr_np test.
|
|---|
| 2 | Copyright (C) 2003 Free Software Foundation, Inc.
|
|---|
| 3 | This file is part of the GNU C Library.
|
|---|
| 4 | Contributed by Jakub Jelinek <[email protected]>, 2003.
|
|---|
| 5 |
|
|---|
| 6 | The GNU C Library is free software; you can redistribute it and/or
|
|---|
| 7 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 8 | License as published by the Free Software Foundation; either
|
|---|
| 9 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 10 |
|
|---|
| 11 | The GNU C Library is distributed in the hope that it will be useful,
|
|---|
| 12 | but 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 the GNU C Library; if not, write to the Free
|
|---|
| 18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA. */
|
|---|
| 20 |
|
|---|
| 21 | #include <errno.h>
|
|---|
| 22 | #include <error.h>
|
|---|
| 23 | #include <pthread.h>
|
|---|
| 24 | #include <stdio.h>
|
|---|
| 25 | #include <stdlib.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <unistd.h>
|
|---|
| 28 |
|
|---|
| 29 | #include <stackinfo.h>
|
|---|
| 30 |
|
|---|
| 31 | static void *
|
|---|
| 32 | tf (void *arg)
|
|---|
| 33 | {
|
|---|
| 34 | pthread_attr_t a, *ap, a2;
|
|---|
| 35 | int err;
|
|---|
| 36 | void *result = NULL;
|
|---|
| 37 |
|
|---|
| 38 | if (arg == NULL)
|
|---|
| 39 | {
|
|---|
| 40 | ap = &a2;
|
|---|
| 41 | err = pthread_attr_init (ap);
|
|---|
| 42 | if (err)
|
|---|
| 43 | {
|
|---|
| 44 | error (0, err, "pthread_attr_init failed");
|
|---|
| 45 | return tf;
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 | else
|
|---|
| 49 | ap = (pthread_attr_t *) arg;
|
|---|
| 50 |
|
|---|
| 51 | err = pthread_getattr_np (pthread_self (), &a);
|
|---|
| 52 | if (err)
|
|---|
| 53 | {
|
|---|
| 54 | error (0, err, "pthread_getattr_np failed");
|
|---|
| 55 | result = tf;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | int detachstate1, detachstate2;
|
|---|
| 59 | err = pthread_attr_getdetachstate (&a, &detachstate1);
|
|---|
| 60 | if (err)
|
|---|
| 61 | {
|
|---|
| 62 | error (0, err, "pthread_attr_getdetachstate failed");
|
|---|
| 63 | result = tf;
|
|---|
| 64 | }
|
|---|
| 65 | else
|
|---|
| 66 | {
|
|---|
| 67 | err = pthread_attr_getdetachstate (ap, &detachstate2);
|
|---|
| 68 | if (err)
|
|---|
| 69 | {
|
|---|
| 70 | error (0, err, "pthread_attr_getdetachstate failed");
|
|---|
| 71 | result = tf;
|
|---|
| 72 | }
|
|---|
| 73 | else if (detachstate1 != detachstate2)
|
|---|
| 74 | {
|
|---|
| 75 | error (0, 0, "detachstate differs %d != %d",
|
|---|
| 76 | detachstate1, detachstate2);
|
|---|
| 77 | result = tf;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | void *stackaddr;
|
|---|
| 82 | size_t stacksize;
|
|---|
| 83 | err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
|
|---|
| 84 | if (err)
|
|---|
| 85 | {
|
|---|
| 86 | error (0, err, "pthread_attr_getstack failed");
|
|---|
| 87 | result = tf;
|
|---|
| 88 | }
|
|---|
| 89 | else if ((void *) &a < stackaddr
|
|---|
| 90 | || (void *) &a >= stackaddr + stacksize)
|
|---|
| 91 | {
|
|---|
| 92 | error (0, 0, "pthread_attr_getstack returned range does not cover thread's stack");
|
|---|
| 93 | result = tf;
|
|---|
| 94 | }
|
|---|
| 95 | else
|
|---|
| 96 | printf ("thread stack %p-%p (0x%zx)\n", stackaddr, stackaddr + stacksize,
|
|---|
| 97 | stacksize);
|
|---|
| 98 |
|
|---|
| 99 | size_t guardsize1, guardsize2;
|
|---|
| 100 | err = pthread_attr_getguardsize (&a, &guardsize1);
|
|---|
| 101 | if (err)
|
|---|
| 102 | {
|
|---|
| 103 | error (0, err, "pthread_attr_getguardsize failed");
|
|---|
| 104 | result = tf;
|
|---|
| 105 | }
|
|---|
| 106 | else
|
|---|
| 107 | {
|
|---|
| 108 | err = pthread_attr_getguardsize (ap, &guardsize2);
|
|---|
| 109 | if (err)
|
|---|
| 110 | {
|
|---|
| 111 | error (0, err, "pthread_attr_getguardsize failed");
|
|---|
| 112 | result = tf;
|
|---|
| 113 | }
|
|---|
| 114 | else if (guardsize1 != guardsize2)
|
|---|
| 115 | {
|
|---|
| 116 | error (0, 0, "guardsize differs %zd != %zd",
|
|---|
| 117 | guardsize1, guardsize2);
|
|---|
| 118 | result = tf;
|
|---|
| 119 | }
|
|---|
| 120 | else
|
|---|
| 121 | printf ("thread guardsize %zd\n", guardsize1);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | int scope1, scope2;
|
|---|
| 125 | err = pthread_attr_getscope (&a, &scope1);
|
|---|
| 126 | if (err)
|
|---|
| 127 | {
|
|---|
| 128 | error (0, err, "pthread_attr_getscope failed");
|
|---|
| 129 | result = tf;
|
|---|
| 130 | }
|
|---|
| 131 | else
|
|---|
| 132 | {
|
|---|
| 133 | err = pthread_attr_getscope (ap, &scope2);
|
|---|
| 134 | if (err)
|
|---|
| 135 | {
|
|---|
| 136 | error (0, err, "pthread_attr_getscope failed");
|
|---|
| 137 | result = tf;
|
|---|
| 138 | }
|
|---|
| 139 | else if (scope1 != scope2)
|
|---|
| 140 | {
|
|---|
| 141 | error (0, 0, "scope differs %d != %d",
|
|---|
| 142 | scope1, scope2);
|
|---|
| 143 | result = tf;
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | err = pthread_attr_destroy (&a);
|
|---|
| 148 | if (err)
|
|---|
| 149 | {
|
|---|
| 150 | error (0, err, "pthread_attr_destroy failed");
|
|---|
| 151 | result = tf;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | if (ap == &a2)
|
|---|
| 155 | {
|
|---|
| 156 | err = pthread_attr_destroy (ap);
|
|---|
| 157 | if (err)
|
|---|
| 158 | {
|
|---|
| 159 | error (0, err, "pthread_attr_destroy failed");
|
|---|
| 160 | result = tf;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | return result;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | static int
|
|---|
| 169 | do_test (void)
|
|---|
| 170 | {
|
|---|
| 171 | int result = 0;
|
|---|
| 172 | pthread_attr_t a;
|
|---|
| 173 |
|
|---|
| 174 | int err = pthread_attr_init (&a);
|
|---|
| 175 | if (err)
|
|---|
| 176 | {
|
|---|
| 177 | error (0, err, "pthread_attr_init failed");
|
|---|
| 178 | result = 1;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | err = pthread_attr_destroy (&a);
|
|---|
| 182 | if (err)
|
|---|
| 183 | {
|
|---|
| 184 | error (0, err, "pthread_attr_destroy failed");
|
|---|
| 185 | result = 1;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | err = pthread_getattr_np (pthread_self (), &a);
|
|---|
| 189 | if (err)
|
|---|
| 190 | {
|
|---|
| 191 | error (0, err, "pthread_getattr_np failed");
|
|---|
| 192 | result = 1;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | int detachstate;
|
|---|
| 196 | err = pthread_attr_getdetachstate (&a, &detachstate);
|
|---|
| 197 | if (err)
|
|---|
| 198 | {
|
|---|
| 199 | error (0, err, "pthread_attr_getdetachstate failed");
|
|---|
| 200 | result = 1;
|
|---|
| 201 | }
|
|---|
| 202 | else if (detachstate != PTHREAD_CREATE_JOINABLE)
|
|---|
| 203 | {
|
|---|
| 204 | error (0, 0, "initial thread not joinable");
|
|---|
| 205 | result = 1;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | void *stackaddr;
|
|---|
| 209 | size_t stacksize;
|
|---|
| 210 | err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
|
|---|
| 211 | if (err)
|
|---|
| 212 | {
|
|---|
| 213 | error (0, err, "pthread_attr_getstack failed");
|
|---|
| 214 | result = 1;
|
|---|
| 215 | }
|
|---|
| 216 | else if ((void *) &a < stackaddr
|
|---|
| 217 | || (void *) &a >= stackaddr + stacksize)
|
|---|
| 218 | {
|
|---|
| 219 | error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
|
|---|
| 220 | result = 1;
|
|---|
| 221 | }
|
|---|
| 222 | else
|
|---|
| 223 | printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr,
|
|---|
| 224 | stackaddr + stacksize, stacksize);
|
|---|
| 225 |
|
|---|
| 226 | size_t guardsize;
|
|---|
| 227 | err = pthread_attr_getguardsize (&a, &guardsize);
|
|---|
| 228 | if (err)
|
|---|
| 229 | {
|
|---|
| 230 | error (0, err, "pthread_attr_getguardsize failed");
|
|---|
| 231 | result = 1;
|
|---|
| 232 | }
|
|---|
| 233 | else if (guardsize != 0)
|
|---|
| 234 | {
|
|---|
| 235 | error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
|
|---|
| 236 | guardsize);
|
|---|
| 237 | result = 1;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | int scope;
|
|---|
| 241 | err = pthread_attr_getscope (&a, &scope);
|
|---|
| 242 | if (err)
|
|---|
| 243 | {
|
|---|
| 244 | error (0, err, "pthread_attr_getscope failed");
|
|---|
| 245 | result = 1;
|
|---|
| 246 | }
|
|---|
| 247 | else if (scope != PTHREAD_SCOPE_SYSTEM)
|
|---|
| 248 | {
|
|---|
| 249 | error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
|
|---|
| 250 | scope);
|
|---|
| 251 | result = 1;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | int inheritsched;
|
|---|
| 255 | err = pthread_attr_getinheritsched (&a, &inheritsched);
|
|---|
| 256 | if (err)
|
|---|
| 257 | {
|
|---|
| 258 | error (0, err, "pthread_attr_getinheritsched failed");
|
|---|
| 259 | result = 1;
|
|---|
| 260 | }
|
|---|
| 261 | else if (inheritsched != PTHREAD_INHERIT_SCHED)
|
|---|
| 262 | {
|
|---|
| 263 | error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
|
|---|
| 264 | inheritsched);
|
|---|
| 265 | result = 1;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | err = pthread_attr_destroy (&a);
|
|---|
| 269 | if (err)
|
|---|
| 270 | {
|
|---|
| 271 | error (0, err, "pthread_attr_destroy failed");
|
|---|
| 272 | result = 1;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | pthread_t th;
|
|---|
| 276 | err = pthread_create (&th, NULL, tf, NULL);
|
|---|
| 277 | if (err)
|
|---|
| 278 | {
|
|---|
| 279 | error (0, err, "pthread_create #1 failed");
|
|---|
| 280 | result = 1;
|
|---|
| 281 | }
|
|---|
| 282 | else
|
|---|
| 283 | {
|
|---|
| 284 | void *ret;
|
|---|
| 285 | err = pthread_join (th, &ret);
|
|---|
| 286 | if (err)
|
|---|
| 287 | {
|
|---|
| 288 | error (0, err, "pthread_join #1 failed");
|
|---|
| 289 | result = 1;
|
|---|
| 290 | }
|
|---|
| 291 | else if (ret != NULL)
|
|---|
| 292 | result = 1;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | err = pthread_attr_init (&a);
|
|---|
| 296 | if (err)
|
|---|
| 297 | {
|
|---|
| 298 | error (0, err, "pthread_attr_init failed");
|
|---|
| 299 | result = 1;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | err = pthread_create (&th, &a, tf, &a);
|
|---|
| 303 | if (err)
|
|---|
| 304 | {
|
|---|
| 305 | error (0, err, "pthread_create #2 failed");
|
|---|
| 306 | result = 1;
|
|---|
| 307 | }
|
|---|
| 308 | else
|
|---|
| 309 | {
|
|---|
| 310 | void *ret;
|
|---|
| 311 | err = pthread_join (th, &ret);
|
|---|
| 312 | if (err)
|
|---|
| 313 | {
|
|---|
| 314 | error (0, err, "pthread_join #2 failed");
|
|---|
| 315 | result = 1;
|
|---|
| 316 | }
|
|---|
| 317 | else if (ret != NULL)
|
|---|
| 318 | result = 1;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | err = pthread_attr_setguardsize (&a, 16 * sysconf (_SC_PAGESIZE));
|
|---|
| 322 | if (err)
|
|---|
| 323 | {
|
|---|
| 324 | error (0, err, "pthread_attr_setguardsize failed");
|
|---|
| 325 | result = 1;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | err = pthread_create (&th, &a, tf, &a);
|
|---|
| 329 | if (err)
|
|---|
| 330 | {
|
|---|
| 331 | error (0, err, "pthread_create #3 failed");
|
|---|
| 332 | result = 1;
|
|---|
| 333 | }
|
|---|
| 334 | else
|
|---|
| 335 | {
|
|---|
| 336 | void *ret;
|
|---|
| 337 | err = pthread_join (th, &ret);
|
|---|
| 338 | if (err)
|
|---|
| 339 | {
|
|---|
| 340 | error (0, err, "pthread_join #3 failed");
|
|---|
| 341 | result = 1;
|
|---|
| 342 | }
|
|---|
| 343 | else if (ret != NULL)
|
|---|
| 344 | result = 1;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | err = pthread_attr_destroy (&a);
|
|---|
| 348 | if (err)
|
|---|
| 349 | {
|
|---|
| 350 | error (0, err, "pthread_attr_destroy failed");
|
|---|
| 351 | result = 1;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | return result;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | #define TEST_FUNCTION do_test ()
|
|---|
| 358 | #include "../test-skeleton.c"
|
|---|