utils.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012 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. #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. int _starpu_check_mutex_deadlock(pthread_mutex_t *mutex)
  65. {
  66. int ret;
  67. ret = pthread_mutex_trylock(mutex);
  68. if (!ret)
  69. {
  70. _STARPU_PTHREAD_MUTEX_UNLOCK(mutex);
  71. return 0;
  72. }
  73. if (ret == EBUSY)
  74. return 0;
  75. STARPU_ASSERT (ret != EDEADLK);
  76. return 1;
  77. }