threaded_array_processor.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**************************************************************************/
  2. /* threaded_array_processor.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/os.h"
  33. #include "core/os/thread.h"
  34. #include "core/os/thread_safe.h"
  35. #include "core/templates/safe_refcount.h"
  36. template <class C, class U>
  37. struct ThreadArrayProcessData {
  38. uint32_t elements;
  39. SafeNumeric<uint32_t> index;
  40. C *instance;
  41. U userdata;
  42. void (C::*method)(uint32_t, U);
  43. void process(uint32_t p_index) {
  44. (instance->*method)(p_index, userdata);
  45. }
  46. };
  47. template <class T>
  48. void process_array_thread(void *ud) {
  49. T &data = *(T *)ud;
  50. while (true) {
  51. uint32_t index = data.index.increment();
  52. if (index >= data.elements) {
  53. break;
  54. }
  55. data.process(index);
  56. }
  57. }
  58. template <class C, class M, class U>
  59. void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
  60. ThreadArrayProcessData<C, U> data;
  61. data.method = p_method;
  62. data.instance = p_instance;
  63. data.userdata = p_userdata;
  64. data.index.set(0);
  65. data.elements = p_elements;
  66. data.process(0); //process first, let threads increment for next
  67. int thread_count = OS::get_singleton()->get_processor_count();
  68. Thread *threads = memnew_arr(Thread, thread_count);
  69. for (int i = 0; i < thread_count; i++) {
  70. threads[i].start(process_array_thread<ThreadArrayProcessData<C, U>>, &data);
  71. }
  72. for (int i = 0; i < thread_count; i++) {
  73. threads[i].wait_to_finish();
  74. }
  75. memdelete_arr(threads);
  76. }
  77. #endif // THREADED_ARRAY_PROCESSOR_H