semaphore.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <limits.h>
  19. #ifdef __USE_XOPEN2K
  20. # define __need_timespec
  21. # include <time.h>
  22. #endif
  23. #ifndef _PTHREAD_DESCR_DEFINED
  24. /* Thread descriptors. Needed for `sem_t' definition. */
  25. typedef struct _pthread_descr_struct *_pthread_descr;
  26. # define _PTHREAD_DESCR_DEFINED
  27. #endif
  28. /* System specific semaphore definition. */
  29. typedef struct
  30. {
  31. struct _pthread_fastlock __sem_lock;
  32. int __sem_value;
  33. _pthread_descr __sem_waiting;
  34. } sem_t;
  35. /* Value returned if `sem_open' failed. */
  36. #define SEM_FAILED ((sem_t *) 0)
  37. /* Maximum value the semaphore can have. */
  38. #ifndef SEM_VALUE_MAX
  39. #define SEM_VALUE_MAX ((int) ((~0u) >> 1))
  40. #endif
  41. __BEGIN_DECLS
  42. /* Initialize semaphore object SEM to VALUE. If PSHARED then share it
  43. with other processes. */
  44. extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value) __THROW;
  45. /* Free resources associated with semaphore object SEM. */
  46. extern int sem_destroy (sem_t *__sem) __THROW;
  47. /* Open a named semaphore NAME with open flags OFLAG. */
  48. extern sem_t *sem_open (const char *__name, int __oflag, ...) __THROW;
  49. /* Close descriptor for named semaphore SEM. */
  50. extern int sem_close (sem_t *__sem) __THROW;
  51. /* Remove named semaphore NAME. */
  52. extern int sem_unlink (const char *__name) __THROW;
  53. /* Wait for SEM being posted.
  54. This function is a cancellation point and therefore not marked with
  55. __THROW. */
  56. extern int sem_wait (sem_t *__sem);
  57. #ifdef __USE_XOPEN2K
  58. /* Similar to `sem_wait' but wait only until ABSTIME.
  59. This function is a cancellation point and therefore not marked with
  60. __THROW. */
  61. extern int sem_timedwait (sem_t *__restrict __sem,
  62. const struct timespec *__restrict __abstime);
  63. #endif
  64. /* Test whether SEM is posted. */
  65. extern int sem_trywait (sem_t *__sem) __THROWNL;
  66. /* Post SEM. */
  67. extern int sem_post (sem_t *__sem) __THROWNL;
  68. /* Get current value of SEM and store it in *SVAL. */
  69. extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval)
  70. __THROW;
  71. __END_DECLS
  72. #endif /* semaphore.h */