semaphore.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. #ifndef _SEMAPHORE_H
  15. #define _SEMAPHORE_H 1
  16. #include <features.h>
  17. #include <sys/types.h>
  18. #ifdef __USE_XOPEN2K
  19. # define __need_timespec
  20. # include <time.h>
  21. #endif
  22. #ifndef _PTHREAD_DESCR_DEFINED
  23. /* Thread descriptors. Needed for `sem_t' definition. */
  24. typedef struct _pthread_descr_struct *_pthread_descr;
  25. # define _PTHREAD_DESCR_DEFINED
  26. #endif
  27. /* System specific semaphore definition. */
  28. typedef struct
  29. {
  30. struct _pthread_fastlock __sem_lock;
  31. int __sem_value;
  32. _pthread_descr __sem_waiting;
  33. } sem_t;
  34. /* Value returned if `sem_open' failed. */
  35. #define SEM_FAILED ((sem_t *) 0)
  36. /* Maximum value the semaphore can have. */
  37. #define SEM_VALUE_MAX ((int) ((~0u) >> 1))
  38. __BEGIN_DECLS
  39. /* Initialize semaphore object SEM to VALUE. If PSHARED then share it
  40. with other processes. */
  41. extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value) __THROW;
  42. /* Free resources associated with semaphore object SEM. */
  43. extern int sem_destroy (sem_t *__sem) __THROW;
  44. /* Open a named semaphore NAME with open flaot OFLAG. */
  45. extern sem_t *sem_open (__const char *__name, int __oflag, ...) __THROW;
  46. /* Close descriptor for named semaphore SEM. */
  47. extern int sem_close (sem_t *__sem) __THROW;
  48. /* Remove named semaphore NAME. */
  49. extern int sem_unlink (__const char *__name) __THROW;
  50. /* Wait for SEM being posted. */
  51. extern int sem_wait (sem_t *__sem) __THROW;
  52. #ifdef __USE_XOPEN2K
  53. /* Similar to `sem_wait' but wait only until ABSTIME. */
  54. extern int sem_timedwait (sem_t *__restrict __sem,
  55. __const struct timespec *__restrict __abstime)
  56. __THROW;
  57. #endif
  58. /* Test whether SEM is posted. */
  59. extern int sem_trywait (sem_t *__sem) __THROW;
  60. /* Post SEM. */
  61. extern int sem_post (sem_t *__sem) __THROW;
  62. /* Get current value of SEM and store it in *SVAL. */
  63. extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval)
  64. __THROW;
  65. __END_DECLS
  66. #endif /* semaphore.h */