progress_hook.c 2.9 KB

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