semaphore.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010,2012,2013,2016,2017 CNRS
  4. * Copyright (C) 2010,2012,2014 Université de Bordeaux
  5. * Copyright (C) 2012 Inria
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. /* This is a minimal pthread implementation based on windows functions.
  19. * It is *not* intended to be complete - just complete enough to get
  20. * StarPU running.
  21. */
  22. #ifndef __STARPU_SEMAPHORE_H__
  23. #define __STARPU_SEMAPHORE_H__
  24. #include "pthread.h"
  25. /**************
  26. * semaphores *
  27. **************/
  28. typedef HANDLE sem_t;
  29. static __inline int sem_init(sem_t *sem, int pshared, unsigned int value) {
  30. (void)pshared;
  31. winPthreadAssertWindows(*sem = CreateSemaphore(NULL, value, MAXLONG, NULL));
  32. return 0;
  33. }
  34. static __inline int do_sem_wait(sem_t *sem, DWORD timeout) {
  35. switch (WaitForSingleObject(*sem, timeout)) {
  36. default:
  37. case WAIT_FAILED:
  38. setSystemErrno();
  39. return -1;
  40. case WAIT_TIMEOUT:
  41. errno = EAGAIN;
  42. return -1;
  43. case WAIT_ABANDONED:
  44. case WAIT_OBJECT_0:
  45. return 0;
  46. }
  47. }
  48. #define sem_wait(sem) do_sem_wait(sem, INFINITE)
  49. #define sem_trywait(sem) do_sem_wait(sem, 0)
  50. static __inline int sem_post(sem_t *sem) {
  51. winPthreadAssertWindows(ReleaseSemaphore(*sem, 1, NULL));
  52. return 0;
  53. }
  54. static __inline int sem_destroy(sem_t *sem) {
  55. winPthreadAssertWindows(CloseHandle(*sem));
  56. return 0;
  57. }
  58. #endif /* __STARPU_SEMAPHORE_H__ */