progress_hook.c 2.4 KB

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