about summary refs log tree commit diff
path: root/sysdeps/htl/sem-timedwait.c
blob: 1b1eec9f59b41c58cb8b1debf91c0e555109c51b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* Wait on a semaphore with a timeout.  Generic version.
   Copyright (C) 2005-2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library;  if not, see
   <https://www.gnu.org/licenses/>.  */

#include <semaphore.h>
#include <errno.h>
#include <assert.h>
#include <time.h>
#include <hurdlock.h>
#include <hurd/hurd.h>
#include <sysdep-cancel.h>

#include <pt-internal.h>

#if !__HAVE_64B_ATOMICS
static void
__sem_wait_32_finish (struct new_sem *isem);
#endif

static void
__sem_wait_cleanup (void *arg)
{
  struct new_sem *isem = arg;

#if __HAVE_64B_ATOMICS
  atomic_fetch_add_relaxed (&isem->data, -((uint64_t) 1 << SEM_NWAITERS_SHIFT));
#else
  __sem_wait_32_finish (isem);
#endif
}

int
__sem_timedwait_internal (sem_t *restrict sem,
			  clockid_t clock_id,
			  const struct timespec *restrict timeout)
{
  struct new_sem *isem = (struct new_sem *) sem;
  int err, ret = 0;
  int flags = isem->pshared ? GSYNC_SHARED : 0;

  __pthread_testcancel ();

  if (__sem_waitfast (isem, 0) == 0)
    return 0;

  int cancel_oldtype = LIBC_CANCEL_ASYNC();

#if __HAVE_64B_ATOMICS
  uint64_t d = atomic_fetch_add_relaxed (&sem->data,
		 (uint64_t) 1 << SEM_NWAITERS_SHIFT);

  pthread_cleanup_push (__sem_wait_cleanup, isem);

  for (;;)
    {
      if ((d & SEM_VALUE_MASK) == 0)
	{
	  /* No token, sleep.  */
	  if (timeout)
	    err = __lll_abstimed_wait_intr (
		      ((unsigned int *) &sem->data) + SEM_VALUE_OFFSET,
		      0, timeout, flags, clock_id);
	  else
	    err = __lll_wait_intr (
		      ((unsigned int *) &sem->data) + SEM_VALUE_OFFSET,
		      0, flags);

	  if (err != 0)
	    {
	      /* Error, interruption or timeout, abort.  */
	      if (err == KERN_TIMEDOUT)
		err = ETIMEDOUT;
	      if (err == KERN_INTERRUPTED)
		err = EINTR;
	      ret = __hurd_fail (err);
	      __sem_wait_cleanup (isem);
	      break;
	    }

	  /* Token changed */
	  d = atomic_load_relaxed (&sem->data);
	}
      else
	{
	  /* Try to acquire and dequeue.  */
	  if (atomic_compare_exchange_weak_acquire (&sem->data,
	      &d, d - 1 - ((uint64_t) 1 << SEM_NWAITERS_SHIFT)))
	    {
	      /* Success */
	      ret = 0;
	      break;
	    }
	}
    }

  pthread_cleanup_pop (0);
#else
  unsigned int v;

  atomic_fetch_add_acquire (&isem->nwaiters, 1);

  pthread_cleanup_push (__sem_wait_cleanup, isem);

  v = atomic_load_relaxed (&isem->value);
  do
    {
      do
	{
	  do
	    {
	      if ((v & SEM_NWAITERS_MASK) != 0)
		break;
	    }
	  while (!atomic_compare_exchange_weak_release (&isem->value,
	      &v, v | SEM_NWAITERS_MASK));

	  if ((v >> SEM_VALUE_SHIFT) == 0)
	    {
	      /* No token, sleep.  */
	      if (timeout)
		err = __lll_abstimed_wait_intr (&isem->value,
			  SEM_NWAITERS_MASK, timeout, flags, clock_id);
	      else
		err = __lll_wait_intr (&isem->value,
			  SEM_NWAITERS_MASK, flags);

	      if (err != 0)
		{
		  /* Error, interruption or timeout, abort.  */
		  if (err == KERN_TIMEDOUT)
		    err = ETIMEDOUT;
		  if (err == KERN_INTERRUPTED)
		    err = EINTR;
		  ret = __hurd_fail (err);
		  goto error;
		}

	      /* Token changed */
	      v = atomic_load_relaxed (&isem->value);
	    }
	}
      while ((v >> SEM_VALUE_SHIFT) == 0);
    }
  while (!atomic_compare_exchange_weak_acquire (&isem->value,
	  &v, v - (1 << SEM_VALUE_SHIFT)));

error:
  pthread_cleanup_pop (0);

  __sem_wait_32_finish (isem);
#endif

  LIBC_CANCEL_RESET (cancel_oldtype);

  return ret;
}

#if !__HAVE_64B_ATOMICS
/* Stop being a registered waiter (non-64b-atomics code only).  */
static void
__sem_wait_32_finish (struct new_sem *isem)
{
  unsigned int wguess = atomic_load_relaxed (&isem->nwaiters);
  if (wguess == 1)
    atomic_fetch_and_acquire (&isem->value, ~SEM_NWAITERS_MASK);

  unsigned int wfinal = atomic_fetch_add_release (&isem->nwaiters, -1);
  if (wfinal > 1 && wguess == 1)
    {
      unsigned int v = atomic_fetch_or_relaxed (&isem->value,
						SEM_NWAITERS_MASK);
      v >>= SEM_VALUE_SHIFT;
      while (v--)
	__lll_wake (&isem->value, isem->pshared ? GSYNC_SHARED : 0);
    }
}
#endif

int
__sem_clockwait (sem_t *sem, clockid_t clockid,
		 const struct timespec *restrict timeout)
{
  return __sem_timedwait_internal (sem, clockid, timeout);
}
weak_alias (__sem_clockwait, sem_clockwait);

int
__sem_timedwait (sem_t *restrict sem, const struct timespec *restrict timeout)
{
  return __sem_timedwait_internal (sem, CLOCK_REALTIME, timeout);
}

weak_alias (__sem_timedwait, sem_timedwait);