utils.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. || (s[0] && s[1] == ':' && s[2] == '/' && !s[3])
  34. #endif
  35. )
  36. return 0;
  37. if ((path = strdup(s)) == NULL)
  38. STARPU_ABORT();
  39. if ((q = strdup(s)) == NULL)
  40. STARPU_ABORT();
  41. if ((r = dirname(q)) == NULL)
  42. goto out;
  43. if ((up = strdup(r)) == NULL)
  44. STARPU_ABORT();
  45. if ((_starpu_mkpath(up, mode) == -1) && (errno != EEXIST))
  46. goto out;
  47. if ((mkdir(path, mode) == -1) && (errno != EEXIST))
  48. rv = -1;
  49. else
  50. rv = 0;
  51. out:
  52. if (up)
  53. free(up);
  54. free(q);
  55. free(path);
  56. return rv;
  57. }
  58. int _starpu_check_mutex_deadlock(pthread_mutex_t *mutex)
  59. {
  60. int ret;
  61. ret = pthread_mutex_trylock(mutex);
  62. if (!ret)
  63. {
  64. pthread_mutex_unlock(mutex);
  65. return 0;
  66. }
  67. if (ret == EBUSY)
  68. return 0;
  69. STARPU_ASSERT (ret != EDEADLK);
  70. return 1;
  71. }