semaphore.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * semaphore.c
  3. *
  4. * Copyright (C) 2016 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <semaphore.h>
  20. #include <thread.h>
  21. #include <heap.h>
  22. void init_semaphore(semaphore_t *semaphore, dword_t init_count, dword_t max_count)
  23. {
  24. ASSERT(init_count <= max_count);
  25. semaphore->count = init_count;
  26. semaphore->max_count = max_count;
  27. }
  28. sysret_t syscall_create_semaphore(const char *name, dword_t init_count, dword_t max_count, handle_t *handle)
  29. {
  30. handle_t safe_handle;
  31. char *safe_name = NULL;
  32. if ((max_count == 0) || (init_count > max_count)) return ERR_INVALID;
  33. semaphore_t *semaphore = (semaphore_t*)malloc(sizeof(semaphore_t));
  34. if (semaphore == NULL) return ERR_NOMEMORY;
  35. if (name != NULL)
  36. {
  37. if (get_previous_mode() == USER_MODE) safe_name = copy_user_string(name);
  38. else safe_name = (char*)name;
  39. }
  40. init_semaphore(semaphore, init_count, max_count);
  41. init_object(&semaphore->header, name, OBJECT_SEMAPHORE);
  42. dword_t ret = create_object(&semaphore->header);
  43. if (ret != ERR_SUCCESS)
  44. {
  45. if (semaphore->header.name) free(semaphore->header.name);
  46. free(semaphore);
  47. return ret;
  48. }
  49. ret = open_object(&semaphore->header, 0, &safe_handle);
  50. dereference(&semaphore->header);
  51. if (ret == ERR_SUCCESS)
  52. {
  53. EH_TRY *handle = safe_handle;
  54. EH_CATCH ret = ERR_BADPTR;
  55. EH_DONE;
  56. }
  57. if (get_previous_mode() == USER_MODE) free(safe_name);
  58. return ret;
  59. }
  60. sysret_t syscall_open_semaphore(const char *name, handle_t *handle)
  61. {
  62. handle_t safe_handle;
  63. char *safe_name = NULL;
  64. if (get_previous_mode() == USER_MODE)
  65. {
  66. dword_t name_length = 0;
  67. EH_TRY name_length = strlen(name);
  68. EH_CATCH EH_ESCAPE(return ERR_BADPTR);
  69. EH_DONE;
  70. if (!check_usermode(name, name_length + 1)) return ERR_BADPTR;
  71. if (!check_usermode(handle, sizeof(handle_t))) return ERR_BADPTR;
  72. safe_name = copy_user_string(name);
  73. if (safe_name == NULL) return ERR_BADPTR;
  74. }
  75. else safe_name = (char*)name;
  76. dword_t ret = open_object_by_name(safe_name, OBJECT_SEMAPHORE, 0, &safe_handle);
  77. EH_TRY *handle = safe_handle;
  78. EH_CATCH
  79. {
  80. syscall_close_object(safe_handle);
  81. ret = ERR_BADPTR;
  82. }
  83. EH_DONE;
  84. if (get_previous_mode() == USER_MODE) free(safe_name);
  85. return ret;
  86. }
  87. dword_t wait_semaphore(semaphore_t *semaphore, dword_t count, dword_t timeout)
  88. {
  89. if (count > semaphore->max_count) return ERR_INVALID;
  90. if (semaphore->count < count)
  91. {
  92. dword_t ret = scheduler_wait(WAIT_UNTIL_NOT_LESS, timeout, &semaphore->count, count);
  93. if (ret == WAIT_TIMED_OUT) return ERR_TIMEOUT;
  94. else if (ret == WAIT_CANCELED) return ERR_CANCELED;
  95. }
  96. semaphore->count -= count;
  97. return ERR_SUCCESS;
  98. }
  99. dword_t release_semaphore(semaphore_t *semaphore, dword_t count)
  100. {
  101. if ((semaphore->count + count) <= semaphore->max_count)
  102. {
  103. semaphore->count += count;
  104. if (scheduler_enabled) syscall_yield_quantum();
  105. return ERR_SUCCESS;
  106. }
  107. else
  108. {
  109. return ERR_INVALID;
  110. }
  111. }
  112. sysret_t syscall_wait_semaphore(handle_t semaphore, dword_t count, dword_t timeout)
  113. {
  114. semaphore_t *obj;
  115. if (!reference_by_handle(semaphore, OBJECT_SEMAPHORE, (object_t**)&obj)) return ERR_INVALID;
  116. dword_t ret = wait_semaphore(obj, count, timeout);
  117. dereference(&obj->header);
  118. return ret;
  119. }
  120. sysret_t syscall_release_semaphore(handle_t semaphore, dword_t count)
  121. {
  122. semaphore_t *obj;
  123. if (!reference_by_handle(semaphore, OBJECT_SEMAPHORE, (object_t**)&obj)) return ERR_INVALID;
  124. release_semaphore(obj, count);
  125. dereference(&obj->header);
  126. return ERR_SUCCESS;
  127. }