progress_hook.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2013 Université de Bordeaux 1
  4. * Copyright (C) 2010-2013 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU 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.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <core/workers.h>
  18. #include <common/utils.h>
  19. #include <core/progress_hook.h>
  20. #define NMAXHOOKS 16
  21. struct progression_hook
  22. {
  23. unsigned (*func)(void *arg);
  24. void *arg;
  25. unsigned active;
  26. };
  27. /* protect the hook table */
  28. static starpu_pthread_rwlock_t progression_hook_rwlock;
  29. static struct progression_hook hooks[NMAXHOOKS] = {{NULL, NULL, 0}};
  30. static int active_hook_cnt = 0;
  31. /*
  32. * Staticly initializing progression_hook_rwlock seems to lead to weird errors
  33. * on Darwin, so we do it dynamically.
  34. */
  35. void _starpu_init_progression_hooks(void)
  36. {
  37. STARPU_PTHREAD_RWLOCK_INIT(&progression_hook_rwlock, NULL);
  38. }
  39. int starpu_progression_hook_register(unsigned (*func)(void *arg), void *arg)
  40. {
  41. int hook;
  42. STARPU_PTHREAD_RWLOCK_WRLOCK(&progression_hook_rwlock);
  43. for (hook = 0; hook < NMAXHOOKS; hook++)
  44. {
  45. if (!hooks[hook].active)
  46. {
  47. /* We found an empty slot */
  48. hooks[hook].func = func;
  49. hooks[hook].arg = arg;
  50. hooks[hook].active = 1;
  51. active_hook_cnt++;
  52. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  53. return hook;
  54. }
  55. }
  56. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  57. starpu_wake_all_blocked_workers();
  58. /* We could not find an empty slot */
  59. return -1;
  60. }
  61. void starpu_progression_hook_deregister(int hook_id)
  62. {
  63. STARPU_PTHREAD_RWLOCK_WRLOCK(&progression_hook_rwlock);
  64. if (hooks[hook_id].active)
  65. active_hook_cnt--;
  66. hooks[hook_id].active = 0;
  67. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  68. }
  69. unsigned _starpu_execute_registered_progression_hooks(void)
  70. {
  71. /* If there is no hook registered, we short-cut loop. */
  72. if (STARPU_PTHREAD_RWLOCK_TRYRDLOCK(&progression_hook_rwlock))
  73. /* It is busy, do not bother */
  74. return;
  75. int no_hook = (active_hook_cnt == 0);
  76. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  77. if (no_hook)
  78. return 1;
  79. /* By default, it is possible to block, but if some progression hooks
  80. * requires that it's not blocking, we disable blocking. */
  81. unsigned may_block = 1;
  82. unsigned hook;
  83. for (hook = 0; hook < NMAXHOOKS; hook++)
  84. {
  85. unsigned active;
  86. STARPU_PTHREAD_RWLOCK_RDLOCK(&progression_hook_rwlock);
  87. active = hooks[hook].active;
  88. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  89. unsigned may_block_hook = 1;
  90. if (active)
  91. may_block_hook = hooks[hook].func(hooks[hook].arg);
  92. /* As soon as one hook tells that the driver cannot be
  93. * blocking, we don't allow it. */
  94. if (!may_block_hook)
  95. may_block = 0;
  96. }
  97. return may_block;
  98. }