threaded_array_processor.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*************************************************************************/
  2. /* threaded_array_processor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef THREADED_ARRAY_PROCESSOR_H
  31. #define THREADED_ARRAY_PROCESSOR_H
  32. #include "core/os/mutex.h"
  33. #include "core/os/os.h"
  34. #include "core/os/thread.h"
  35. #include "core/os/thread_safe.h"
  36. #include "core/safe_refcount.h"
  37. template <class C, class U>
  38. struct ThreadArrayProcessData {
  39. uint32_t elements;
  40. uint32_t index;
  41. C *instance;
  42. U userdata;
  43. void (C::*method)(uint32_t, U);
  44. void process(uint32_t p_index) {
  45. (instance->*method)(p_index, userdata);
  46. }
  47. };
  48. #ifndef NO_THREADS
  49. template <class T>
  50. void process_array_thread(void *ud) {
  51. T &data = *(T *)ud;
  52. while (true) {
  53. uint32_t index = atomic_increment(&data.index);
  54. if (index >= data.elements)
  55. break;
  56. data.process(index);
  57. }
  58. }
  59. template <class C, class M, class U>
  60. void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  61. ThreadArrayProcessData<C, U> data;
  62. data.method = p_method;
  63. data.instance = p_instance;
  64. data.userdata = p_userdata;
  65. data.index = 0;
  66. data.elements = p_elements;
  67. data.process(data.index); //process first, let threads increment for next
  68. Vector<Thread *> threads;
  69. threads.resize(OS::get_singleton()->get_processor_count());
  70. for (int i = 0; i < threads.size(); i++) {
  71. threads.write[i] = Thread::create(process_array_thread<ThreadArrayProcessData<C, U> >, &data);
  72. }
  73. for (int i = 0; i < threads.size(); i++) {
  74. Thread::wait_to_finish(threads[i]);
  75. memdelete(threads[i]);
  76. }
  77. }
  78. #else
  79. template <class C, class M, class U>
  80. void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  81. ThreadArrayProcessData<C, U> data;
  82. data.method = p_method;
  83. data.instance = p_instance;
  84. data.userdata = p_userdata;
  85. data.index = 0;
  86. data.elements = p_elements;
  87. for (uint32_t i = 0; i < p_elements; i++) {
  88. data.process(i);
  89. }
  90. }
  91. #endif
  92. #endif // THREADED_ARRAY_PROCESSOR_H