semaphore.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. wait_condition_t condition = { .type = WAIT_UNTIL_NOT_LESS, .pointer = &semaphore->count, .value = count };
  93. wait_result_t result = scheduler_wait(&condition, timeout);
  94. if (result == WAIT_TIMED_OUT) return ERR_TIMEOUT;
  95. else if (result == WAIT_CANCELED) return ERR_CANCELED;
  96. }
  97. semaphore->count -= count;
  98. return ERR_SUCCESS;
  99. }
  100. dword_t release_semaphore(semaphore_t *semaphore, dword_t count)
  101. {
  102. if ((semaphore->count + count) <= semaphore->max_count)
  103. {
  104. semaphore->count += count;
  105. if (scheduler_enabled) syscall_yield_quantum();
  106. return ERR_SUCCESS;
  107. }
  108. else
  109. {
  110. return ERR_INVALID;
  111. }
  112. }
  113. dword_t semaphore_pre_wait(object_t *obj, void *parameter, wait_condition_t *condition)
  114. {
  115. semaphore_t *semaphore = (semaphore_t*)obj;
  116. dword_t count = parameter ? (dword_t)parameter : 1;
  117. if (count > semaphore->max_count) return ERR_INVALID;
  118. condition->type = WAIT_UNTIL_NOT_LESS;
  119. condition->pointer = &semaphore->count;
  120. condition->value = count;
  121. return ERR_SUCCESS;
  122. }
  123. void semaphore_post_wait(object_t *obj, void *parameter, wait_result_t result)
  124. {
  125. semaphore_t *semaphore = (semaphore_t*)obj;
  126. semaphore->count -= (dword_t)parameter;
  127. }
  128. sysret_t syscall_release_semaphore(handle_t semaphore, dword_t count)
  129. {
  130. semaphore_t *obj;
  131. if (!reference_by_handle(semaphore, OBJECT_SEMAPHORE, (object_t**)&obj)) return ERR_INVALID;
  132. release_semaphore(obj, count);
  133. dereference(&obj->header);
  134. return ERR_SUCCESS;
  135. }