prio_list.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017 Université de Bordeaux
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #define _STARPU_MALLOC(p, s) do {p = malloc(s);} while (0)
  17. #define STARPU_ATTRIBUTE_UNUSED __attribute((__unused__))
  18. #define _GNU_SOURCE
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <limits.h>
  23. #include <common/list.h>
  24. #include <common/prio_list.h>
  25. #include <simgrid/msg.h>
  26. #include <simgrid/modelchecker.h>
  27. #include <xbt/synchro.h>
  28. #define N 2 /* number of threads */
  29. #define M 4 /* number of elements */
  30. // MC_ignore
  31. xbt_mutex_t mutex;
  32. LIST_TYPE(foo,
  33. unsigned prio;
  34. unsigned back; /* Push at back instead of front? */
  35. );
  36. PRIO_LIST_TYPE(foo, prio);
  37. struct foo_prio_list mylist;
  38. void check_list_prio(struct foo_prio_list *list)
  39. {
  40. struct foo *cur;
  41. unsigned lastprio = UINT_MAX;
  42. unsigned back = 0;
  43. for (cur = foo_prio_list_begin(list);
  44. cur != foo_prio_list_end(list);
  45. cur = foo_prio_list_next(list, cur))
  46. {
  47. if (cur->prio == lastprio)
  48. /* For same prio, back elements should never get before
  49. * front elements */
  50. MC_assert(!(back && !cur->back));
  51. else
  52. MC_assert(lastprio > cur->prio);
  53. lastprio = cur->prio;
  54. back = cur->back;
  55. }
  56. }
  57. int worker(int argc, char *argv[])
  58. {
  59. unsigned i, n;
  60. struct foo *elem;
  61. struct drand48_data buffer;
  62. long res;
  63. srand48_r(atoi(argv[0]), &buffer);
  64. for (i = 0; i < M; i++)
  65. {
  66. elem = malloc(sizeof(*elem));
  67. lrand48_r(&buffer, &res);
  68. elem->prio = res%10;
  69. lrand48_r(&buffer, &res);
  70. elem->back = res%2;
  71. xbt_mutex_acquire(mutex);
  72. if (elem->back)
  73. foo_prio_list_push_back(&mylist, elem);
  74. else
  75. foo_prio_list_push_front(&mylist, elem);
  76. check_list_prio(&mylist);
  77. xbt_mutex_release(mutex);
  78. }
  79. for (i = 0; i < M; i++)
  80. {
  81. lrand48_r(&buffer, &res);
  82. n = res%(M-i);
  83. xbt_mutex_acquire(mutex);
  84. for (elem = foo_prio_list_begin(&mylist);
  85. n--;
  86. elem = foo_prio_list_next(&mylist, elem))
  87. ;
  88. foo_prio_list_erase(&mylist, elem);
  89. check_list_prio(&mylist);
  90. xbt_mutex_release(mutex);
  91. }
  92. return 0;
  93. }
  94. int master(int argc STARPU_ATTRIBUTE_UNUSED, char *argv[] STARPU_ATTRIBUTE_UNUSED)
  95. {
  96. unsigned i;
  97. mutex = xbt_mutex_init();
  98. foo_prio_list_init(&mylist);
  99. for (i = 0; i < N; i++)
  100. {
  101. char *s;
  102. asprintf(&s, "%d\n", i);
  103. char **args = malloc(sizeof(char*)*2);
  104. args[0] = s;
  105. args[1] = NULL;
  106. MSG_process_create_with_arguments("test", worker, NULL, MSG_host_self(), 1, args);
  107. }
  108. return 0;
  109. }
  110. int main(int argc, char *argv[])
  111. {
  112. if (argc < 3)
  113. {
  114. fprintf(stderr,"usage: %s platform.xml host\n", argv[0]);
  115. exit(EXIT_FAILURE);
  116. }
  117. srand48(0);
  118. MSG_init(&argc, argv);
  119. xbt_cfg_set_int("contexts/stack-size", 128);
  120. MSG_create_environment(argv[1]);
  121. MSG_process_create("master", master, NULL, MSG_get_host_by_name(argv[2]));
  122. MSG_main();
  123. return 0;
  124. }