progress_hook.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 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 <pthread.h>
  18. #include <core/workers.h>
  19. #include <common/utils.h>
  20. #define NMAXHOOKS 16
  21. struct progression_hook {
  22. unsigned (*func)(void *arg);
  23. void *arg;
  24. unsigned active;
  25. };
  26. /* protect the hook table */
  27. static pthread_rwlock_t progression_hook_rwlock = PTHREAD_RWLOCK_INITIALIZER;
  28. static struct progression_hook hooks[NMAXHOOKS] = {{NULL, NULL, 0}};
  29. static int active_hook_cnt = 0;
  30. int starpu_progression_hook_register(unsigned (*func)(void *arg), void *arg)
  31. {
  32. int hook;
  33. PTHREAD_RWLOCK_WRLOCK(&progression_hook_rwlock);
  34. for (hook = 0; hook < NMAXHOOKS; hook++)
  35. {
  36. if (!hooks[hook].active)
  37. {
  38. /* We found an empty slot */
  39. hooks[hook].func = func;
  40. hooks[hook].arg = arg;
  41. hooks[hook].active = 1;
  42. active_hook_cnt++;
  43. PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  44. return hook;
  45. }
  46. }
  47. PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  48. starpu_wake_all_blocked_workers();
  49. /* We could not find an empty slot */
  50. return -1;
  51. }
  52. void starpu_progression_hook_deregister(int hook_id)
  53. {
  54. PTHREAD_RWLOCK_WRLOCK(&progression_hook_rwlock);
  55. if (hooks[hook_id].active)
  56. active_hook_cnt--;
  57. hooks[hook_id].active = 0;
  58. PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  59. }
  60. unsigned _starpu_execute_registered_progression_hooks(void)
  61. {
  62. /* If there is no hook registered, we short-cut loop. */
  63. PTHREAD_RWLOCK_RDLOCK(&progression_hook_rwlock);
  64. int no_hook = (active_hook_cnt == 0);
  65. PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  66. if (no_hook)
  67. return 1;
  68. /* By default, it is possible to block, but if some progression hooks
  69. * requires that it's not blocking, we disable blocking. */
  70. unsigned may_block = 1;
  71. unsigned hook;
  72. for (hook = 0; hook < NMAXHOOKS; hook++)
  73. {
  74. unsigned active;
  75. PTHREAD_RWLOCK_RDLOCK(&progression_hook_rwlock);
  76. active = hooks[hook].active;
  77. PTHREAD_RWLOCK_UNLOCK(&progression_hook_rwlock);
  78. unsigned may_block_hook = 1;
  79. if (active)
  80. may_block_hook = hooks[hook].func(hooks[hook].arg);
  81. /* As soon as one hook tells that the driver cannot be
  82. * blocking, we don't allow it. */
  83. if (!may_block_hook)
  84. may_block = 0;
  85. }
  86. return may_block;
  87. }