orsl-lite.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <limits.h>
  29. #include <assert.h>
  30. #include "orsl-lite/include/orsl-lite.h"
  31. #define DISABLE_SYMBOL_VERSIONING
  32. #if defined(__linux__) && !defined(DISABLE_SYMBOL_VERSIONING)
  33. #define symver(src, tgt, verstr) __asm__(".symver " #src "," #tgt verstr)
  34. symver(ORSLReserve0, ORSLReserve, "@@ORSL_0.0");
  35. symver(ORSLTryReserve0, ORSLTryReserve, "@@ORSL_0.0");
  36. symver(ORSLReservePartial0, ORSLReservePartial, "@@ORSL_0.0");
  37. symver(ORSLRelease0, ORSLRelease, "@@ORSL_0.0");
  38. #else
  39. #define ORSLReserve0 ORSLReserve
  40. #define ORSLTryReserve0 ORSLTryReserve
  41. #define ORSLReservePartial0 ORSLReservePartial
  42. #define ORSLRelease0 ORSLRelease
  43. #endif
  44. #ifdef __linux__
  45. #include <pthread.h>
  46. static pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER;
  47. static pthread_cond_t release_cond = PTHREAD_COND_INITIALIZER;
  48. #endif
  49. #ifdef _WIN32
  50. #include <windows.h>
  51. #pragma intrinsic(_ReadWriteBarrier)
  52. static SRWLOCK global_mutex = SRWLOCK_INIT;
  53. static volatile int release_cond_initialized = 0;
  54. static CONDITION_VARIABLE release_cond;
  55. static void state_lazy_init_sync()
  56. {
  57. if (!release_cond_initialized) {
  58. AcquireSRWLockExclusive(&global_mutex);
  59. _ReadWriteBarrier();
  60. if (!release_cond_initialized) {
  61. InitializeConditionVariable(&release_cond);
  62. release_cond_initialized = 1;
  63. }
  64. ReleaseSRWLockExclusive(&global_mutex);
  65. }
  66. }
  67. #endif
  68. static int state_lock()
  69. {
  70. #ifdef __linux__
  71. return pthread_mutex_lock(&global_mutex);
  72. #endif
  73. #ifdef _WIN32
  74. AcquireSRWLockExclusive(&global_mutex);
  75. return 0;
  76. #endif
  77. }
  78. static int state_unlock()
  79. {
  80. #ifdef __linux__
  81. return pthread_mutex_unlock(&global_mutex);
  82. #endif
  83. #ifdef _WIN32
  84. ReleaseSRWLockExclusive(&global_mutex);
  85. return 0;
  86. #endif
  87. }
  88. static int state_wait_for_release()
  89. {
  90. #ifdef __linux__
  91. return pthread_cond_wait(&release_cond, &global_mutex);
  92. #endif
  93. #ifdef _WIN32
  94. return SleepConditionVariableSRW(&release_cond,
  95. &global_mutex, INFINITE, 0) == 0 ? 1 : 0;
  96. #endif
  97. }
  98. static int state_signal_release()
  99. {
  100. #ifdef __linux__
  101. return pthread_cond_signal(&release_cond);
  102. #endif
  103. #ifdef _WIN32
  104. WakeConditionVariable(&release_cond);
  105. return 0;
  106. #endif
  107. }
  108. static struct {
  109. char owner[ORSL_MAX_TAG_LEN + 1];
  110. unsigned long rsrv_cnt;
  111. } rsrv_data[ORSL_MAX_CARDS];
  112. static int check_args(const int n, const int *__restrict inds,
  113. const ORSLBusySet *__restrict bsets,
  114. const ORSLTag __restrict tag)
  115. {
  116. int i;
  117. int card_specified[ORSL_MAX_CARDS];
  118. if (tag == NULL) return -1;
  119. if (strlen((char *)tag) > ORSL_MAX_TAG_LEN) return -1;
  120. if (n < 0 || n >= ORSL_MAX_CARDS) return -1;
  121. if (n != 0 && (inds == NULL || bsets == NULL)) return -1;
  122. for (i = 0; i < ORSL_MAX_CARDS; i++)
  123. card_specified[i] = 0;
  124. for (i = 0; i < n; i++) {
  125. int ind = inds[i];
  126. if (ind < 0 || ind >= ORSL_MAX_CARDS) return -1;
  127. if (card_specified[ind]) return -1;
  128. card_specified[ind] = 1;
  129. }
  130. return 0;
  131. }
  132. static int check_bsets(const int n, const ORSLBusySet *bsets)
  133. {
  134. int i;
  135. for (i = 0; i < n; i++)
  136. if (bsets[i].type == BUSY_SET_PARTIAL) return -1;
  137. return 0;
  138. }
  139. static int can_reserve_card(int card, const ORSLBusySet *__restrict bset,
  140. const ORSLTag __restrict tag)
  141. {
  142. assert(tag != NULL);
  143. assert(bset != NULL);
  144. assert(strlen((char *)tag) < ORSL_MAX_TAG_LEN);
  145. assert(bset->type != BUSY_SET_PARTIAL);
  146. return (bset->type == BUSY_SET_EMPTY ||
  147. ((rsrv_data[card].rsrv_cnt == 0 ||
  148. strncmp((char *)tag,
  149. rsrv_data[card].owner, ORSL_MAX_TAG_LEN) == 0) &&
  150. rsrv_data[card].rsrv_cnt < ULONG_MAX)) ? 0 : - 1;
  151. }
  152. static void reserve_card(int card, const ORSLBusySet *__restrict bset,
  153. const ORSLTag __restrict tag)
  154. {
  155. assert(tag != NULL);
  156. assert(bset != NULL);
  157. assert(strlen((char *)tag) < ORSL_MAX_TAG_LEN);
  158. assert(bset->type != BUSY_SET_PARTIAL);
  159. if (bset->type == BUSY_SET_EMPTY)
  160. return;
  161. assert(rsrv_data[card].rsrv_cnt == 0 ||
  162. strncmp((char *)tag,
  163. rsrv_data[card].owner, ORSL_MAX_TAG_LEN) == 0);
  164. assert(rsrv_data[card].rsrv_cnt < ULONG_MAX);
  165. if (rsrv_data[card].rsrv_cnt == 0)
  166. strncpy(rsrv_data[card].owner, (char *)tag, ORSL_MAX_TAG_LEN);
  167. rsrv_data[card].owner[ORSL_MAX_TAG_LEN] = '\0';
  168. rsrv_data[card].rsrv_cnt++;
  169. }
  170. static int can_release_card(int card, const ORSLBusySet *__restrict bset,
  171. const ORSLTag __restrict tag)
  172. {
  173. assert(tag != NULL);
  174. assert(bset != NULL);
  175. assert(strlen((char *)tag) < ORSL_MAX_TAG_LEN);
  176. assert(bset->type != BUSY_SET_PARTIAL);
  177. return (bset->type == BUSY_SET_EMPTY || (rsrv_data[card].rsrv_cnt > 0 &&
  178. strncmp((char *)tag,
  179. rsrv_data[card].owner, ORSL_MAX_TAG_LEN) == 0)) ? 0 : 1;
  180. }
  181. static void release_card(int card, const ORSLBusySet *__restrict bset,
  182. const ORSLTag __restrict tag)
  183. {
  184. assert(tag != NULL);
  185. assert(bset != NULL);
  186. assert(strlen((char *)tag) < ORSL_MAX_TAG_LEN);
  187. assert(bset->type != BUSY_SET_PARTIAL);
  188. if (bset->type == BUSY_SET_EMPTY)
  189. return;
  190. assert(strncmp((char *)tag,
  191. rsrv_data[card].owner, ORSL_MAX_TAG_LEN) == 0);
  192. assert(rsrv_data[card].rsrv_cnt > 0);
  193. rsrv_data[card].rsrv_cnt--;
  194. }
  195. int ORSLReserve0(const int n, const int *__restrict inds,
  196. const ORSLBusySet *__restrict bsets,
  197. const ORSLTag __restrict tag)
  198. {
  199. int i, ok;
  200. if (n == 0) return 0;
  201. if (check_args(n, inds, bsets, tag) != 0) return EINVAL;
  202. if (check_bsets(n, bsets) != 0) return ENOSYS;
  203. state_lock();
  204. /* Loop until we find that all the resources we want are available */
  205. do {
  206. ok = 1;
  207. for (i = 0; i < n; i++)
  208. if (can_reserve_card(inds[i], &bsets[i], tag) != 0) {
  209. ok = 0;
  210. /* Wait for someone to release some resources */
  211. state_wait_for_release();
  212. break;
  213. }
  214. } while (!ok);
  215. /* At this point we are good to reserve_card the resources we want */
  216. for (i = 0; i < n; i++)
  217. reserve_card(inds[i], &bsets[i], tag);
  218. state_unlock();
  219. return 0;
  220. }
  221. int ORSLTryReserve0(const int n, const int *__restrict inds,
  222. const ORSLBusySet *__restrict bsets,
  223. const ORSLTag __restrict tag)
  224. {
  225. int i, rc = EBUSY;
  226. if (n == 0) return 0;
  227. if (check_args(n, inds, bsets, tag) != 0) return EINVAL;
  228. if (check_bsets(n, bsets) != 0) return ENOSYS;
  229. state_lock();
  230. /* Check resource availability once */
  231. for (i = 0; i < n; i++)
  232. if (can_reserve_card(inds[i], &bsets[i], tag) != 0)
  233. goto bail_out;
  234. /* At this point we are good to reserve the resources we want */
  235. for (i = 0; i < n; i++)
  236. reserve_card(inds[i], &bsets[i], tag);
  237. rc = 0;
  238. bail_out:
  239. state_unlock();
  240. return rc;
  241. }
  242. int ORSLReservePartial0(const ORSLPartialGranularity gran, const int n,
  243. const int *__restrict inds, ORSLBusySet *__restrict bsets,
  244. const ORSLTag __restrict tag)
  245. {
  246. int rc = EBUSY;
  247. int i, num_avail = n;
  248. if (n == 0) return 0;
  249. if (gran != GRAN_CARD && gran != GRAN_THREAD) return EINVAL;
  250. if (gran != GRAN_CARD) return EINVAL;
  251. if (check_args(n, inds, bsets, tag) != 0) return EINVAL;
  252. if (check_bsets(n, bsets) != 0) return ENOSYS;
  253. state_lock();
  254. /* Check resource availability once; remove unavailable resources from the
  255. * user-provided list */
  256. for (i = 0; i < n; i++)
  257. if (can_reserve_card(inds[i], &bsets[i], tag) != 0) {
  258. num_avail--;
  259. bsets[i].type = BUSY_SET_EMPTY;
  260. }
  261. if (num_avail == 0)
  262. goto bail_out;
  263. /* At this point we are good to reserve the resources we want */
  264. for (i = 0; i < n; i++)
  265. reserve_card(inds[i], &bsets[i], tag);
  266. rc = 0;
  267. bail_out:
  268. state_unlock();
  269. return rc;
  270. }
  271. int ORSLRelease0(const int n, const int *__restrict inds,
  272. const ORSLBusySet *__restrict bsets,
  273. const ORSLTag __restrict tag)
  274. {
  275. int i, rc = EPERM;
  276. if (n == 0) return 0;
  277. if (check_args(n, inds, bsets, tag) != 0) return EINVAL;
  278. if (check_bsets(n, bsets) != 0) return ENOSYS;
  279. state_lock();
  280. /* Check that we can release all the resources */
  281. for (i = 0; i < n; i++)
  282. if (can_release_card(inds[i], &bsets[i], tag) != 0)
  283. goto bail_out;
  284. /* At this point we are good to release the resources we want */
  285. for (i = 0; i < n; i++)
  286. release_card(inds[i], &bsets[i], tag);
  287. state_signal_release();
  288. rc = 0;
  289. bail_out:
  290. state_unlock();
  291. return rc;
  292. }
  293. /* vim:set et: */