utils.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 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 <common/thread.h>
  21. #include <libgen.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #ifdef __MINGW32__
  25. #include <io.h>
  26. #define mkdir(path, mode) mkdir(path)
  27. #endif
  28. /* Function with behaviour like `mkdir -p'. This function was adapted from
  29. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  30. int _starpu_mkpath(const char *s, mode_t mode)
  31. {
  32. int olderrno;
  33. char *q, *r = NULL, *path = NULL, *up = NULL;
  34. int rv;
  35. rv = -1;
  36. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0
  37. #ifdef __MINGW32__
  38. /* C:/ or C:\ */
  39. || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3])
  40. #endif
  41. )
  42. return 0;
  43. if ((path = strdup(s)) == NULL)
  44. STARPU_ABORT();
  45. if ((q = strdup(s)) == NULL)
  46. STARPU_ABORT();
  47. if ((r = dirname(q)) == NULL)
  48. goto out;
  49. if ((up = strdup(r)) == NULL)
  50. STARPU_ABORT();
  51. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  52. goto out;
  53. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  54. rv = -1;
  55. else
  56. rv = 0;
  57. out:
  58. olderrno = errno;
  59. if (up)
  60. free(up);
  61. free(q);
  62. free(path);
  63. errno = olderrno;
  64. return rv;
  65. }
  66. void _starpu_mkpath_and_check(const char *path, mode_t mode)
  67. {
  68. int ret;
  69. ret = _starpu_mkpath(path, mode);
  70. if (ret == -1)
  71. {
  72. if (errno != EEXIST)
  73. {
  74. fprintf(stderr,"Error making StarPU directory %s:\n", path);
  75. perror("mkdir");
  76. STARPU_ABORT();
  77. }
  78. /* make sure that it is actually a directory */
  79. struct stat sb;
  80. stat(path, &sb);
  81. if (!S_ISDIR(sb.st_mode))
  82. {
  83. fprintf(stderr,"Error: %s is not a directory:\n", path);
  84. STARPU_ABORT();
  85. }
  86. }
  87. }
  88. int _starpu_check_mutex_deadlock(starpu_pthread_mutex_t *mutex)
  89. {
  90. int ret;
  91. ret = starpu_pthread_mutex_trylock(mutex);
  92. if (!ret)
  93. {
  94. STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  95. return 0;
  96. }
  97. if (ret == EBUSY)
  98. return 0;
  99. STARPU_ASSERT (ret != EDEADLK);
  100. return 1;
  101. }
  102. char *_starpu_get_home_path(void)
  103. {
  104. char *path = getenv("XDG_CACHE_HOME");
  105. if (!path)
  106. path = getenv("STARPU_HOME");
  107. if (!path)
  108. path = getenv("HOME");
  109. if (!path)
  110. path = getenv("USERPROFILE");
  111. if (!path)
  112. _STARPU_ERROR("couldn't find a home place to put starpu data\n");
  113. return path;
  114. }
  115. void _starpu_gethostname(char *hostname, size_t size)
  116. {
  117. char *forced_hostname = getenv("STARPU_HOSTNAME");
  118. if (forced_hostname && forced_hostname[0])
  119. {
  120. snprintf(hostname, size-1, "%s", forced_hostname);
  121. hostname[size-1] = 0;
  122. }
  123. else
  124. {
  125. char *c;
  126. gethostname(hostname, size-1);
  127. hostname[size-1] = 0;
  128. c = strchr(hostname, '.');
  129. if (c)
  130. *c = 0;
  131. }
  132. }