utils.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <common/config.h>
  18. #include <common/utils.h>
  19. #include <libgen.h>
  20. #ifdef __MINGW32__
  21. #include <io.h>
  22. #define mkdir(path, mode) mkdir(path)
  23. #endif
  24. /* Function with behaviour like `mkdir -p'. This function was adapted from
  25. * http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/ */
  26. int _starpu_mkpath(const char *s, mode_t mode)
  27. {
  28. char *q, *r = NULL, *path = NULL, *up = NULL;
  29. int rv;
  30. rv = -1;
  31. if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0
  32. #ifdef __MINGW32__
  33. /* C:/ or C:\ */
  34. || (s[0] && s[1] == ':' && (s[2] == '/' || s[2] == '\\') && !s[3])
  35. #endif
  36. )
  37. return 0;
  38. if ((path = strdup(s)) == NULL)
  39. STARPU_ABORT();
  40. if ((q = strdup(s)) == NULL)
  41. STARPU_ABORT();
  42. if ((r = dirname(q)) == NULL)
  43. goto out;
  44. if ((up = strdup(r)) == NULL)
  45. STARPU_ABORT();
  46. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  47. goto out;
  48. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  49. rv = -1;
  50. else
  51. rv = 0;
  52. out:
  53. if (up)
  54. free(up);
  55. free(q);
  56. free(path);
  57. return rv;
  58. }
  59. int _starpu_check_mutex_deadlock(pthread_mutex_t *mutex)
  60. {
  61. int ret;
  62. ret = pthread_mutex_trylock(mutex);
  63. if (!ret)
  64. {
  65. pthread_mutex_unlock(mutex);
  66. return 0;
  67. }
  68. if (ret == EBUSY)
  69. return 0;
  70. STARPU_ASSERT (ret != EDEADLK);
  71. return 1;
  72. }