semaphore.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 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. /* This is a minimal pthread implementation based on windows functions.
  17. * It is *not* intended to be complete - just complete enough to get
  18. * StarPU running.
  19. */
  20. #ifndef __STARPU_SEMAPHORE_H__
  21. #define __STARPU_SEMAPHORE_H__
  22. #include "pthread.h"
  23. /**************
  24. * semaphores *
  25. **************/
  26. typedef HANDLE sem_t;
  27. static inline int sem_init(sem_t *sem, int pshared, unsigned int value) {
  28. winPthreadAssertWindows(*sem = CreateSemaphore(NULL, value, MAXLONG, NULL));
  29. return 0;
  30. }
  31. static inline int do_sem_wait(sem_t *sem, DWORD timeout) {
  32. switch (WaitForSingleObject(*sem, timeout)) {
  33. default:
  34. case WAIT_FAILED:
  35. setSystemErrno();
  36. return -1;
  37. case WAIT_TIMEOUT:
  38. errno = EAGAIN;
  39. return -1;
  40. case WAIT_ABANDONED:
  41. case WAIT_OBJECT_0:
  42. return 0;
  43. }
  44. }
  45. #define sem_wait(sem) do_sem_wait(sem, INFINITE)
  46. #define sem_trywait(sem) do_sem_wait(sem, 0)
  47. static inline int sem_post(sem_t *sem) {
  48. winPthreadAssertWindows(ReleaseSemaphore(*sem, 1, NULL));
  49. return 0;
  50. }
  51. static inline int sem_destroy(sem_t *sem) {
  52. winPthreadAssertWindows(CloseHandle(*sem));
  53. return 0;
  54. }
  55. #endif /* __STARPU_SEMAPHORE_H__ */