Advertisement
Guest User

Untitled

a guest
Feb 28th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include "pch.h"
  2. #include <thread>
  3. #include <chrono>
  4. #include <vector>
  5. #include <mutex>
  6. #define gmx extern "C" __declspec(dllexport)
  7. using ds_map = int;
  8. using thread = std::thread;
  9. using milliseconds = std::chrono::milliseconds;
  10. const int EVENT_OTHER_SOCIAL = 70;
  11.  
  12. void(*gml_event_perform_async)(ds_map map, int event_type) = nullptr;
  13. int (*gml_ds_map_create)(int n, ...) = nullptr;
  14. bool (*gml_ds_map_add_double)(ds_map map, char* key, double value);
  15. bool (*gml_ds_map_add_string)(ds_map map, char* key, char* value);
  16.  
  17. std::mutex thread_key;
  18. std::vector<thread*> threads;
  19. std::vector<uint32_t*> open_slots;
  20.  
  21. gmx double RegisterCallBacks(char* arg1, char* arg2, char* arg3, char* arg4) {
  22. gml_event_perform_async = (void(*)(ds_map, int)) arg1;
  23. gml_ds_map_create = (int(*)(int, ...)) arg2;
  24. gml_ds_map_add_double = (bool(*)(ds_map, char*, double)) arg3;
  25. gml_ds_map_add_string = (bool(*)(ds_map, char*, char*)) arg4;
  26.  
  27. return 0;
  28. }
  29.  
  30. ds_map ds_map_create() {
  31. return gml_ds_map_create(0);
  32. }
  33.  
  34. void return_double(double time, double type, double value, int handle) {
  35. long t = (long)time;
  36. std::this_thread::sleep_for(milliseconds(t));
  37.  
  38. //Minute 19, point ouT!
  39. //std::lock_guard<std::mutex>(thread_key)
  40.  
  41. thread_key.lock();
  42.  
  43. ds_map map = ds_map_create();
  44. gml_ds_map_add_double(map, (char*)"type", type);
  45. gml_ds_map_add_double(map, (char*)"value", value);
  46. gml_ds_map_add_double(map, (char*)"handle", handle);
  47. gml_event_perform_async(map, EVENT_OTHER_SOCIAL);
  48.  
  49. thread_key.unlock();
  50. }
  51.  
  52. gmx double thread_create(double time, double type, double value) {
  53. int index;
  54. thread_key.lock();
  55.  
  56. if (open_slots.empty()) {
  57. index = threads.size();
  58. threads.push_back(new thread(return_double, time, type, value, index));
  59. }
  60. else {
  61. index = (int)open_slots.back();
  62. open_slots.pop_back();
  63. threads[index] = new thread(return_double, time, type, value, index);
  64. }
  65.  
  66. thread_key.unlock();
  67. return index;
  68. }
  69.  
  70. gmx double thread_kill(double index) {
  71. thread_key.lock();
  72.  
  73. if (threads.size() > index && threads[index] != NULL) {
  74. if (threads[index]->joinable()) {
  75. threads[index]->detach();
  76. }
  77. delete threads[index];
  78. threads[index] = NULL;
  79.  
  80. uint32_t* in = reinterpret_cast<uint32_t*>(&index);
  81. open_slots.push_back(in);
  82. }
  83.  
  84. thread_key.unlock();
  85. return 1;
  86. }
  87.  
  88. gmx double thread_free(double index) {
  89. thread_key.lock();
  90.  
  91. if (threads.size() > index && threads[index] != NULL) {
  92. if (threads[index]->joinable()) {
  93. threads[index]->join();
  94. }
  95. delete threads[index];
  96. threads[index] = NULL;
  97.  
  98. uint32_t* in = reinterpret_cast<uint32_t*>(&index);
  99. open_slots.push_back(in);
  100. }
  101.  
  102. thread_key.unlock();
  103. return 1;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement