idle_hook.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012 Inria
  4. * Copyright (C) 2010-2014 Université de Bordeaux
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <core/workers.h>
  19. #include <common/utils.h>
  20. #include <core/idle_hook.h>
  21. #define NMAXHOOKS 16
  22. struct progression_hook
  23. {
  24. unsigned (*func)(void *arg);
  25. void *arg;
  26. unsigned active;
  27. };
  28. /* protect the hook table */
  29. static starpu_pthread_rwlock_t idle_hook_rwlock;
  30. static struct progression_hook idle_hooks[NMAXHOOKS] = {{NULL, NULL, 0}};
  31. static int active_idle_hook_cnt = 0;
  32. /*
  33. * Staticly initializing idle_hook_rwlock seems to lead to weird errors
  34. * on Darwin, so we do it dynamically.
  35. */
  36. void _starpu_init_idle_hooks(void)
  37. {
  38. STARPU_PTHREAD_RWLOCK_INIT(&idle_hook_rwlock, NULL);
  39. STARPU_HG_DISABLE_CHECKING(active_idle_hook_cnt);
  40. }
  41. int starpu_idle_hook_register(unsigned (*func)(void *arg), void *arg)
  42. {
  43. int hook;
  44. STARPU_PTHREAD_RWLOCK_WRLOCK(&idle_hook_rwlock);
  45. for (hook = 0; hook < NMAXHOOKS; hook++)
  46. {
  47. if (!idle_hooks[hook].active)
  48. {
  49. /* We found an empty slot */
  50. idle_hooks[hook].func = func;
  51. idle_hooks[hook].arg = arg;
  52. idle_hooks[hook].active = 1;
  53. active_idle_hook_cnt++;
  54. STARPU_PTHREAD_RWLOCK_UNLOCK(&idle_hook_rwlock);
  55. return hook;
  56. }
  57. }
  58. STARPU_PTHREAD_RWLOCK_UNLOCK(&idle_hook_rwlock);
  59. starpu_wake_all_blocked_workers();
  60. /* We could not find an empty slot */
  61. return -1;
  62. }
  63. void starpu_idle_hook_deregister(int hook_id)
  64. {
  65. STARPU_PTHREAD_RWLOCK_WRLOCK(&idle_hook_rwlock);
  66. if (idle_hooks[hook_id].active)
  67. active_idle_hook_cnt--;
  68. idle_hooks[hook_id].active = 0;
  69. STARPU_PTHREAD_RWLOCK_UNLOCK(&idle_hook_rwlock);
  70. }
  71. unsigned _starpu_execute_registered_idle_hooks(void)
  72. {
  73. if (active_idle_hook_cnt == 0)
  74. return 1;
  75. /* By default, it is possible to block, but if some idle hooks
  76. * requires that it's not blocking, we disable blocking. */
  77. unsigned may_block = 1;
  78. unsigned hook;
  79. for (hook = 0; hook < NMAXHOOKS; hook++)
  80. {
  81. unsigned active;
  82. STARPU_PTHREAD_RWLOCK_RDLOCK(&idle_hook_rwlock);
  83. active = idle_hooks[hook].active;
  84. STARPU_PTHREAD_RWLOCK_UNLOCK(&idle_hook_rwlock);
  85. unsigned may_block_hook = 1;
  86. if (active)
  87. may_block_hook = idle_hooks[hook].func(idle_hooks[hook].arg);
  88. /* As soon as one hook tells that the driver cannot be
  89. * blocking, we don't allow it. */
  90. if (!may_block_hook)
  91. may_block = 0;
  92. }
  93. return may_block;
  94. }