semaphore.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 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. (void)pshared;
  29. winPthreadAssertWindows(*sem = CreateSemaphore(NULL, value, MAXLONG, NULL));
  30. return 0;
  31. }
  32. static __inline int do_sem_wait(sem_t *sem, DWORD timeout) {
  33. switch (WaitForSingleObject(*sem, timeout)) {
  34. default:
  35. case WAIT_FAILED:
  36. setSystemErrno();
  37. return -1;
  38. case WAIT_TIMEOUT:
  39. errno = EAGAIN;
  40. return -1;
  41. case WAIT_ABANDONED:
  42. case WAIT_OBJECT_0:
  43. return 0;
  44. }
  45. }
  46. #define sem_wait(sem) do_sem_wait(sem, INFINITE)
  47. #define sem_trywait(sem) do_sem_wait(sem, 0)
  48. static __inline int sem_post(sem_t *sem) {
  49. winPthreadAssertWindows(ReleaseSemaphore(*sem, 1, NULL));
  50. return 0;
  51. }
  52. static __inline int sem_destroy(sem_t *sem) {
  53. winPthreadAssertWindows(CloseHandle(*sem));
  54. return 0;
  55. }
  56. #endif /* __STARPU_SEMAPHORE_H__ */