sem_post.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* sem_post -- post to a POSIX semaphore. Generic futex-using version.
  2. Copyright (C) 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <sysdep.h>
  18. #include <lowlevellock.h>
  19. #include <internaltypes.h>
  20. #include <semaphore.h>
  21. #include <tls.h>
  22. int
  23. sem_post (sem_t *sem)
  24. {
  25. struct new_sem *isem = (struct new_sem *) sem;
  26. __typeof (isem->value) cur;
  27. do
  28. {
  29. cur = isem->value;
  30. if (isem->value == SEM_VALUE_MAX)
  31. {
  32. __set_errno (EOVERFLOW);
  33. return -1;
  34. }
  35. }
  36. while (atomic_compare_and_exchange_bool_acq (&isem->value, cur + 1, cur));
  37. atomic_full_barrier ();
  38. if (isem->nwaiters > 0)
  39. {
  40. int err = lll_futex_wake (&isem->value, 1,
  41. isem->private ^ FUTEX_PRIVATE_FLAG);
  42. if (__builtin_expect (err, 0) < 0)
  43. {
  44. __set_errno (-err);
  45. return -1;
  46. }
  47. }
  48. return 0;
  49. }