progress_hook.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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_mutex_t progression_hook_mutex = PTHREAD_MUTEX_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_MUTEX_LOCK(&progression_hook_mutex);
  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_MUTEX_UNLOCK(&progression_hook_mutex);
  41. return hook;
  42. }
  43. }
  44. PTHREAD_MUTEX_UNLOCK(&progression_hook_mutex);
  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_MUTEX_LOCK(&progression_hook_mutex);
  52. hooks[hook_id].active = 0;
  53. PTHREAD_MUTEX_UNLOCK(&progression_hook_mutex);
  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_MUTEX_LOCK(&progression_hook_mutex);
  65. active = hooks[hook].active;
  66. PTHREAD_MUTEX_UNLOCK(&progression_hook_mutex);
  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. }