shared_mem_access_posix.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* shared_mem_access_posix.cpp */
  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. #include "shared_mem_access_posix.h"
  31. #if defined(UNIX_ENABLED)
  32. //#include "core/os/memory.h"
  33. #include <sys/mman.h>
  34. #include <sys/stat.h>
  35. #include <unistd.h>
  36. //#include <stdio.h>
  37. #include <fcntl.h>
  38. //#include <sys/mman.h>
  39. //#include <sys/stat.h>
  40. SharedMemAccess *SharedMemAccessPosix::create_func_posix(const String &p_name) {
  41. return memnew(SharedMemAccessPosix(p_name));
  42. }
  43. void SharedMemAccessPosix::make_default() {
  44. create_func = create_func_posix;
  45. }
  46. Error SharedMemAccessPosix::open() {
  47. ERR_EXPLAIN("Already open.");
  48. ERR_FAIL_COND_V(fd != -1, ERR_ALREADY_IN_USE);
  49. fd = shm_open(("/" + name).utf8().get_data(), O_CREAT | O_RDWR, 0600);
  50. if (fd == -1) {
  51. ERR_EXPLAIN("Cannot create/open.");
  52. ERR_FAIL_V(ERR_CANT_OPEN);
  53. }
  54. return OK;
  55. }
  56. Error SharedMemAccessPosix::close() {
  57. ERR_EXPLAIN("Not open.");
  58. ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED);
  59. if (!lock()) {
  60. ERR_EXPLAIN("Cannot lock.");
  61. ERR_FAIL_V(ERR_BUSY);
  62. }
  63. if (is_allocator) {
  64. shm_unlink(("/" + name).utf8().get_data());
  65. is_allocator = false;
  66. }
  67. ::close(fd);
  68. fd = -1;
  69. unlock();
  70. return OK;
  71. }
  72. _FORCE_INLINE_ bool SharedMemAccessPosix::is_open() {
  73. return fd != -1;
  74. }
  75. void *SharedMemAccessPosix::lock() {
  76. ERR_EXPLAIN("Not open.");
  77. ERR_FAIL_COND_V(!is_open(), NULL);
  78. ERR_EXPLAIN("Lock already held.");
  79. ERR_FAIL_COND_V(is_locking(), NULL);
  80. size = lseek(fd, 0, SEEK_END);
  81. lock_addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  82. if (lock_addr == MAP_FAILED) {
  83. ERR_EXPLAIN("Cannot map memory.");
  84. ERR_FAIL_V(NULL);
  85. }
  86. return lock_addr;
  87. }
  88. void SharedMemAccessPosix::unlock() {
  89. ERR_EXPLAIN("Lock not held.");
  90. ERR_FAIL_COND(!is_locking());
  91. munmap(lock_addr, size);
  92. lock_addr = NULL;
  93. size = 0;
  94. }
  95. _FORCE_INLINE_ bool SharedMemAccessPosix::is_locking() {
  96. return lock_addr;
  97. }
  98. void *SharedMemAccessPosix::set_size(uint64_t p_size) {
  99. ERR_EXPLAIN("Lock must be held to set the size.");
  100. ERR_FAIL_COND_V(!is_locking(), NULL);
  101. if (ftruncate(fd, p_size) == -1) {
  102. ERR_EXPLAIN("Cannot set new size. (Out of memory?)");
  103. ERR_FAIL_V(NULL);
  104. }
  105. munmap(lock_addr, size);
  106. // Try to remap at former address
  107. lock_addr = mmap(lock_addr, p_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  108. if (lock_addr == MAP_FAILED) {
  109. unlock();
  110. ERR_EXPLAIN("Cannot remap memory. Lock released.");
  111. ERR_FAIL_V(NULL);
  112. }
  113. return lock_addr;
  114. }
  115. uint64_t SharedMemAccessPosix::get_size() {
  116. ERR_EXPLAIN("Lock must be held to get the size.");
  117. ERR_FAIL_COND_V(!is_locking(), 0);
  118. return size;
  119. }
  120. SharedMemAccessPosix::SharedMemAccessPosix(const String &p_name) :
  121. name(p_name),
  122. is_allocator(false),
  123. size(0),
  124. fd(-1),
  125. lock_addr(NULL) {
  126. }
  127. SharedMemAccessPosix::~SharedMemAccessPosix() {
  128. if (is_open()) {
  129. if (is_locking()) {
  130. unlock();
  131. }
  132. if (close() != OK && is_allocator) {
  133. ERR_PRINTS("Leaking shared memory '" + name + "'.");
  134. }
  135. }
  136. }
  137. #endif