idle_hook.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. #include <core/workers.h>
  17. #include <common/utils.h>
  18. #include <core/idle_hook.h>
  19. #define NMAXHOOKS 16
  20. struct progression_hook
  21. {
  22. unsigned (*func)(void *arg);
  23. void *arg;
  24. unsigned active;
  25. };
  26. /* protect the hook table */
  27. static starpu_pthread_rwlock_t idle_hook_rwlock;
  28. static struct progression_hook idle_hooks[NMAXHOOKS] = {{NULL, NULL, 0}};
  29. static int active_idle_hook_cnt = 0;
  30. /*
  31. * Staticly initializing idle_hook_rwlock seems to lead to weird errors
  32. * on Darwin, so we do it dynamically.
  33. */
  34. void _starpu_init_idle_hooks(void)
  35. {
  36. STARPU_PTHREAD_RWLOCK_INIT(&idle_hook_rwlock, NULL);
  37. STARPU_HG_DISABLE_CHECKING(active_idle_hook_cnt);
  38. }
  39. int starpu_idle_hook_register(unsigned (*func)(void *arg), void *arg)
  40. {
  41. int hook;
  42. STARPU_PTHREAD_RWLOCK_WRLOCK(&idle_hook_rwlock);
  43. for (hook = 0; hook < NMAXHOOKS; hook++)
  44. {
  45. if (!idle_hooks[hook].active)
  46. {
  47. /* We found an empty slot */
  48. idle_hooks[hook].func = func;
  49. idle_hooks[hook].arg = arg;
  50. idle_hooks[hook].active = 1;
  51. active_idle_hook_cnt++;
  52. STARPU_PTHREAD_RWLOCK_UNLOCK(&idle_hook_rwlock);
  53. return hook;
  54. }
  55. }
  56. STARPU_PTHREAD_RWLOCK_UNLOCK(&idle_hook_rwlock);
  57. starpu_wake_all_blocked_workers();
  58. /* We could not find an empty slot */
  59. return -1;
  60. }
  61. void starpu_idle_hook_deregister(int hook_id)
  62. {
  63. STARPU_PTHREAD_RWLOCK_WRLOCK(&idle_hook_rwlock);
  64. if (idle_hooks[hook_id].active)
  65. active_idle_hook_cnt--;
  66. idle_hooks[hook_id].active = 0;
  67. STARPU_PTHREAD_RWLOCK_UNLOCK(&idle_hook_rwlock);
  68. }
  69. unsigned _starpu_execute_registered_idle_hooks(void)
  70. {
  71. if (active_idle_hook_cnt == 0)
  72. return 1;
  73. /* By default, it is possible to block, but if some idle hooks
  74. * requires that it's not blocking, we disable blocking. */
  75. unsigned may_block = 1;
  76. unsigned hook;
  77. for (hook = 0; hook < NMAXHOOKS; hook++)
  78. {
  79. unsigned active;
  80. STARPU_PTHREAD_RWLOCK_RDLOCK(&idle_hook_rwlock);
  81. active = idle_hooks[hook].active;
  82. STARPU_PTHREAD_RWLOCK_UNLOCK(&idle_hook_rwlock);
  83. unsigned may_block_hook = 1;
  84. if (active)
  85. may_block_hook = idle_hooks[hook].func(idle_hooks[hook].arg);
  86. /* As soon as one hook tells that the driver cannot be
  87. * blocking, we don't allow it. */
  88. if (!may_block_hook)
  89. may_block = 0;
  90. }
  91. return may_block;
  92. }