prio_list.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #define _STARPU_MALLOC(p, s) do {p = malloc(s);} while (0)
  2. #define STARPU_ATTRIBUTE_UNUSED __attribute((__unused__))
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <limits.h>
  6. #include <common/list.h>
  7. #include <common/prio_list.h>
  8. #include <simgrid/msg.h>
  9. #include <simgrid/modelchecker.h>
  10. #include <xbt/synchro.h>
  11. #define N 2 /* number of threads */
  12. #define M 3 /* number of elements */
  13. xbt_mutex_t mutex;
  14. LIST_TYPE(foo,
  15. unsigned prio;
  16. unsigned back; /* Push at back instead of front? */
  17. );
  18. PRIO_LIST_TYPE(foo, prio);
  19. struct foo_prio_list mylist;
  20. void check_list_prio(struct foo_prio_list *list)
  21. {
  22. struct foo *cur;
  23. unsigned lastprio = UINT_MAX;
  24. unsigned back = 0;
  25. for (cur = foo_prio_list_begin(list);
  26. cur != foo_prio_list_end(list);
  27. cur = foo_prio_list_next(list, cur))
  28. {
  29. if (cur->prio == lastprio)
  30. /* For same prio, back elements should never get before
  31. * front elements */
  32. MC_assert(!(back && !cur->back));
  33. else
  34. MC_assert(lastprio > cur->prio);
  35. lastprio = cur->prio;
  36. back = cur->back;
  37. }
  38. }
  39. int worker(int argc STARPU_ATTRIBUTE_UNUSED, char *argv[] STARPU_ATTRIBUTE_UNUSED)
  40. {
  41. unsigned i, n;
  42. struct foo *elem;
  43. for (i = 0; i < M; i++) {
  44. elem = malloc(sizeof(*elem));
  45. elem->prio = lrand48()%10;
  46. elem->back = lrand48()%2;
  47. xbt_mutex_acquire(mutex);
  48. if (elem->back)
  49. foo_prio_list_push_back(&mylist, elem);
  50. else
  51. foo_prio_list_push_front(&mylist, elem);
  52. check_list_prio(&mylist);
  53. xbt_mutex_release(mutex);
  54. }
  55. for (i = 0; i < M; i++) {
  56. n = lrand48()%(M-i);
  57. xbt_mutex_acquire(mutex);
  58. for (elem = foo_prio_list_begin(&mylist);
  59. n--;
  60. elem = foo_prio_list_next(&mylist, elem))
  61. ;
  62. foo_prio_list_erase(&mylist, elem);
  63. check_list_prio(&mylist);
  64. xbt_mutex_release(mutex);
  65. }
  66. return 0;
  67. }
  68. int master(int argc STARPU_ATTRIBUTE_UNUSED, char *argv[] STARPU_ATTRIBUTE_UNUSED)
  69. {
  70. unsigned i;
  71. mutex = xbt_mutex_init();
  72. foo_prio_list_init(&mylist);
  73. for (i = 0; i < N; i++)
  74. MSG_process_create("test", worker, NULL, MSG_host_self());
  75. return 0;
  76. }
  77. int main(int argc, char *argv[]) {
  78. if (argc < 3) {
  79. fprintf(stderr,"usage: %s platform.xml host\n", argv[0]);
  80. exit(EXIT_FAILURE);
  81. }
  82. srand48(0);
  83. MSG_init(&argc, argv);
  84. xbt_cfg_set_int("contexts/stack-size", 128);
  85. //xbt_cfg_set_boolean("model-check/sparse-checkpoint", "true");
  86. MSG_create_environment(argv[1]);
  87. MSG_process_create("master", master, NULL, MSG_get_host_by_name(argv[2]));
  88. MSG_main();
  89. return 0;
  90. }