watchthreads.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* This testcase is part of GDB, the GNU debugger.
  2. Copyright 2002-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. This file is copied from schedlock.c. */
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <pthread.h>
  18. void *thread_function (void *arg); /* Function executed by each thread. */
  19. #define NUM 5
  20. unsigned int args[NUM+1];
  21. int
  22. main ()
  23. {
  24. int res;
  25. pthread_t threads[NUM];
  26. void *thread_result;
  27. long i;
  28. /* To keep the test determinative, initialize args first,
  29. then start all the threads. Otherwise, the way watchthreads.exp
  30. is written, we have to worry about things like threads[0] getting
  31. to 29 hits of args[0] before args[1] gets changed. */
  32. for (i = 0; i < NUM; i++)
  33. {
  34. /* The call to usleep is so that when the watchpoint triggers,
  35. the pc is still on the same line. */
  36. args[i] = 1; usleep (1); /* Init value. */
  37. }
  38. for (i = 0; i < NUM; i++)
  39. {
  40. res = pthread_create (&threads[i],
  41. NULL,
  42. thread_function,
  43. (void *) i);
  44. }
  45. args[i] = 1;
  46. thread_function ((void *) i);
  47. exit (EXIT_SUCCESS);
  48. }
  49. void *
  50. thread_function (void *arg)
  51. {
  52. int my_number = (long) arg;
  53. int *myp = (int *) &args[my_number];
  54. /* Don't run forever. Run just short of it :) */
  55. while (*myp > 0)
  56. {
  57. (*myp) ++; usleep (1); /* Loop increment. */
  58. }
  59. pthread_exit (NULL);
  60. }