progress_hook.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2013 Université de Bordeaux
  4. * Copyright (C) 2010-2013 CNRS
  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. STARPU_HG_DISABLE_CHECKING(active_hook_cnt);
  39. }
  40. int starpu_progression_hook_register(unsigned (*func)(void *arg), void *arg)
  41. {
  42. int hook;
  43. STARPU_PTHREAD_RWLOCK_WRLOCK(&progression_hook_rwlock);
  44. for (hook = 0; hook < NMAXHOOKS; hook++)
  45. {
  46. if (!hooks[hook].active)
  47. {
  48. /* We found an empty slot */
  49. hooks[hook].func = func;
  50. hooks[hook].arg = arg;
  51. hooks[hook].active = 1;
  52. active_hook_cnt++;
  53. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  54. return hook;
  55. }
  56. }
  57. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  58. starpu_wake_all_blocked_workers();
  59. /* We could not find an empty slot */
  60. return -1;
  61. }
  62. void starpu_progression_hook_deregister(int hook_id)
  63. {
  64. STARPU_PTHREAD_RWLOCK_WRLOCK(&progression_hook_rwlock);
  65. if (hooks[hook_id].active)
  66. active_hook_cnt--;
  67. hooks[hook_id].active = 0;
  68. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  69. }
  70. unsigned _starpu_execute_registered_progression_hooks(void)
  71. {
  72. if (active_hook_cnt == 0)
  73. return 1;
  74. /* By default, it is possible to block, but if some progression hooks
  75. * requires that it's not blocking, we disable blocking. */
  76. unsigned may_block = 1;
  77. unsigned hook;
  78. for (hook = 0; hook < NMAXHOOKS; hook++)
  79. {
  80. unsigned active;
  81. STARPU_PTHREAD_RWLOCK_RDLOCK(&progression_hook_rwlock);
  82. active = hooks[hook].active;
  83. STARPU_PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  84. unsigned may_block_hook = 1;
  85. if (active)
  86. may_block_hook = hooks[hook].func(hooks[hook].arg);
  87. /* As soon as one hook tells that the driver cannot be
  88. * blocking, we don't allow it. */
  89. if (!may_block_hook)
  90. may_block = 0;
  91. }
  92. return may_block;
  93. }