utils.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <common/config.h>
  19. #include <common/utils.h>
  20. #include <libgen.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #ifdef __MINGW32__
  25. #include <io.h>
  26. #include <sys/locking.h>
  27. #define mkdir(path, mode) mkdir(path)
  28. #endif
  29. /* Function with behaviour like `mkdir -p'. This function was adapted from
  30. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  31. int _starpu_mkpath(const char *s, mode_t mode)
  32. {
  33. int olderrno;
  34. char *q, *r = NULL, *path = NULL, *up = NULL;
  35. int rv;
  36. rv = -1;
  37. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0
  38. #ifdef __MINGW32__
  39. /* C:/ or C:\ */
  40. || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3])
  41. #endif
  42. )
  43. return 0;
  44. if ((path = strdup(s)) == NULL)
  45. STARPU_ABORT();
  46. if ((q = strdup(s)) == NULL)
  47. STARPU_ABORT();
  48. if ((r = dirname(q)) == NULL)
  49. goto out;
  50. if ((up = strdup(r)) == NULL)
  51. STARPU_ABORT();
  52. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  53. goto out;
  54. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  55. rv = -1;
  56. else
  57. rv = 0;
  58. out:
  59. olderrno = errno;
  60. if (up)
  61. free(up);
  62. free(q);
  63. free(path);
  64. errno = olderrno;
  65. return rv;
  66. }
  67. void _starpu_mkpath_and_check(const char *path, mode_t mode)
  68. {
  69. int ret;
  70. ret = _starpu_mkpath(path, mode);
  71. if (ret == -1)
  72. {
  73. if (errno != EEXIST)
  74. {
  75. fprintf(stderr,"Error making StarPU directory %s:\n", path);
  76. perror("mkdir");
  77. STARPU_ABORT();
  78. }
  79. /* make sure that it is actually a directory */
  80. struct stat sb;
  81. stat(path, &sb);
  82. if (!S_ISDIR(sb.st_mode))
  83. {
  84. fprintf(stderr,"Error: %s is not a directory:\n", path);
  85. STARPU_ABORT();
  86. }
  87. }
  88. }
  89. int _starpu_ftruncate(FILE *file)
  90. {
  91. return ftruncate(fileno(file), 0);
  92. }
  93. int _starpu_frdlock(FILE *file)
  94. {
  95. #ifdef __MINGW32__
  96. int ret;
  97. do {
  98. ret = _locking(fileno(file), _LK_RLCK, 10);
  99. } while (ret == EDEADLOCK);
  100. return ret;
  101. #else
  102. struct flock lock = {
  103. .l_type = F_RDLCK,
  104. .l_whence = SEEK_SET,
  105. .l_start = 0,
  106. .l_len = 0
  107. };
  108. return fcntl(fileno(file), F_SETLKW, &lock);
  109. #endif
  110. }
  111. int _starpu_frdunlock(FILE *file)
  112. {
  113. #ifdef __MINGW32__
  114. # ifndef _LK_UNLCK
  115. # define _LK_UNLCK _LK_UNLOCK
  116. # endif
  117. return _locking(fileno(file), _LK_UNLCK, 10);
  118. #else
  119. struct flock lock = {
  120. .l_type = F_UNLCK,
  121. .l_whence = SEEK_SET,
  122. .l_start = 0,
  123. .l_len = 0
  124. };
  125. return fcntl(fileno(file), F_SETLKW, &lock);
  126. #endif
  127. }
  128. int _starpu_fwrlock(FILE *file)
  129. {
  130. #ifdef __MINGW32__
  131. int ret;
  132. do {
  133. ret = _locking(fileno(file), _LK_LOCK, 10);
  134. } while (ret == EDEADLOCK);
  135. return ret;
  136. #else
  137. struct flock lock = {
  138. .l_type = F_WRLCK,
  139. .l_whence = SEEK_SET,
  140. .l_start = 0,
  141. .l_len = 0
  142. };
  143. return fcntl(fileno(file), F_SETLKW, &lock);
  144. #endif
  145. }
  146. int _starpu_fwrunlock(FILE *file)
  147. {
  148. return _starpu_frdunlock(file);
  149. }
  150. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  151. {
  152. int ret;
  153. ret = starpu_pthread_mutex_trylock(mutex);
  154. if (!ret)
  155. {
  156. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  157. return 0;
  158. }
  159. if (ret == EBUSY)
  160. return 0;
  161. STARPU_ASSERT (ret != EDEADLK);
  162. return 1;
  163. }
  164. char *_starpu_get_home_path(void)
  165. {
  166. char *path = getenv("XDG_CACHE_HOME");
  167. if (!path)
  168. path = getenv("STARPU_HOME");
  169. if (!path)
  170. path = getenv("HOME");
  171. if (!path)
  172. path = getenv("USERPROFILE");
  173. if (!path) {
  174. static int warn;
  175. if (!warn) {
  176. warn = 1;
  177. _STARPU_DISP("couldn't find a $STARPU_HOME place to put .starpu data, using /tmp\n");
  178. }
  179. path = "/tmp";
  180. }
  181. return path;
  182. }
  183. void _starpu_gethostname(char *hostname, size_t size)
  184. {
  185. char *forced_hostname = getenv("STARPU_HOSTNAME");
  186. if (forced_hostname && forced_hostname[0])
  187. {
  188. snprintf(hostname, size-1, "%s", forced_hostname);
  189. hostname[size-1] = 0;
  190. }
  191. else
  192. {
  193. char *c;
  194. gethostname(hostname, size-1);
  195. hostname[size-1] = 0;
  196. c = strchr(hostname, '.');
  197. if (c)
  198. *c = 0;
  199. }
  200. }
  201. void _starpu_sleep(struct timespec ts)
  202. {
  203. #ifdef STARPU_SIMGRID
  204. MSG_process_sleep(ts.tv_sec + ts.tv_nsec / 1000000000.);
  205. #elif defined(STARPU_HAVE_WINDOWS)
  206. Sleep((ts.tv_sec * 1000) + (ts.tv_nsec / 1000000));
  207. #else
  208. struct timespec req, rem;
  209. req = ts;
  210. while (nanosleep(&req, &rem))
  211. req = rem;
  212. #endif
  213. }