utils.c 3.2 KB

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