semaphore.h 1.8 KB

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