utils.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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. #ifdef __MINGW32__
  22. #include <io.h>
  23. #define mkdir(path, mode) mkdir(path)
  24. #endif
  25. /* Function with behaviour like `mkdir -p'. This function was adapted from
  26. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  27. int _starpu_mkpath(const char *s, mode_t mode)
  28. {
  29. char *q, *r = NULL, *path = NULL, *up = NULL;
  30. int rv;
  31. rv = -1;
  32. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0
  33. #ifdef __MINGW32__
  34. /* C:/ or C:\ */
  35. || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3])
  36. #endif
  37. )
  38. return 0;
  39. if ((path = strdup(s)) == NULL)
  40. STARPU_ABORT();
  41. if ((q = strdup(s)) == NULL)
  42. STARPU_ABORT();
  43. if ((r = dirname(q)) == NULL)
  44. goto out;
  45. if ((up = strdup(r)) == NULL)
  46. STARPU_ABORT();
  47. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  48. goto out;
  49. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  50. rv = -1;
  51. else
  52. rv = 0;
  53. out:
  54. if (up)
  55. free(up);
  56. free(q);
  57. free(path);
  58. return rv;
  59. }
  60. int _starpu_check_mutex_deadlock(pthread_mutex_t *mutex)
  61. {
  62. int ret;
  63. ret = pthread_mutex_trylock(mutex);
  64. if (!ret)
  65. {
  66. pthread_mutex_unlock(mutex);
  67. return 0;
  68. }
  69. if (ret == EBUSY)
  70. return 0;
  71. STARPU_ASSERT (ret != EDEADLK);
  72. return 1;
  73. }