summaryrefslogtreecommitdiff
path: root/nptl/pthread_mutex_timedlock.c
blob: d09a8d4ef8ae7c550605ed55edf0b99a74185c2c (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.

   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
   <http://www.gnu.org/licenses/>.  */

#include <assert.h>
#include <errno.h>
#include <time.h>
#include <sys/param.h>
#include <sys/time.h>
#include "pthreadP.h"
#include <atomic.h>
#include <lowlevellock.h>
#include <not-cancel.h>

#include <stap-probe.h>

#ifndef lll_timedlock_elision
#define lll_timedlock_elision(a,dummy,b,c) lll_timedlock(a, b, c)
#endif

#ifndef lll_timedlock_elision64
#define lll_timedlock_elision64(a,dummy,b,c) lll_timedlock64(a, b, c)
#endif

#ifndef lll_trylock_elision
#define lll_trylock_elision(a,t) lll_trylock(a)
#endif

#ifndef FORCE_ELISION
#define FORCE_ELISION(m, s)
#endif

int
__pthread_mutex_timedlock (pthread_mutex_t *mutex,
			   const struct timespec *abstime)
{
  int oldval;
  pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  int result = 0;

  LIBC_PROBE (mutex_timedlock_entry, 2, mutex, abstime);

  /* We must not check ABSTIME here.  If the thread does not block
     abstime must not be checked for a valid value.  */

  switch (__builtin_expect (PTHREAD_MUTEX_TYPE_ELISION (mutex),
			    PTHREAD_MUTEX_TIMED_NP))
    {
      /* Recursive mutex.  */
    case PTHREAD_MUTEX_RECURSIVE_NP|PTHREAD_MUTEX_ELISION_NP:
    case PTHREAD_MUTEX_RECURSIVE_NP:
      /* Check whether we already hold the mutex.  */
      if (mutex->__data.__owner == id)
	{
	  /* Just bump the counter.  */
	  if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
	    /* Overflow of the counter.  */
	    return EAGAIN;

	  ++mutex->__data.__count;

	  goto out;
	}

      /* We have to get the mutex.  */
      result = lll_timedlock (mutex->__data.__lock, abstime,
			      PTHREAD_MUTEX_PSHARED (mutex));

      if (result != 0)
	goto out;

      /* Only locked once so far.  */
      mutex->__data.__count = 1;
      break;

      /* Error checking mutex.  */
    case PTHREAD_MUTEX_ERRORCHECK_NP:
      /* Check whether we already hold the mutex.  */
      if (__glibc_unlikely (mutex->__data.__owner == id))
	return EDEADLK;

      /* Don't do lock elision on an error checking mutex.  */
      goto simple;

    case PTHREAD_MUTEX_TIMED_NP:
      FORCE_ELISION (mutex, goto elision);
    simple:
      /* Normal mutex.  */
      result = lll_timedlock (mutex->__data.__lock, abstime,
			      PTHREAD_MUTEX_PSHARED (mutex));
      break;

    case PTHREAD_MUTEX_TIMED_ELISION_NP:
    elision: __attribute__((unused))
      /* Don't record ownership */
      return lll_timedlock_elision (mutex->__data.__lock,
				    mutex->__data.__spins,
				    abstime,
				    PTHREAD_MUTEX_PSHARED (mutex));


    case PTHREAD_MUTEX_ADAPTIVE_NP:
      if (! __is_smp)
	goto simple;

      if (lll_trylock (mutex->__data.__lock) != 0)
	{
	  int cnt = 0;
	  int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
			     mutex->__data.__spins * 2 + 10);
	  do
	    {
	      if (cnt++ >= max_cnt)
		{
		  result = lll_timedlock (mutex->__data.__lock, abstime,
					  PTHREAD_MUTEX_PSHARED (mutex));
		  break;
		}
	      atomic_spin_nop ();
	    }
	  while (lll_trylock (mutex->__data.__lock) != 0);

	  mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
	}
      break;

    case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
    case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
    case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
    case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
      THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
		     &mutex->__data.__list.__next);
      /* We need to set op_pending before starting the operation.  Also
	 see comments at ENQUEUE_MUTEX.  */
      __asm ("" ::: "memory");

      oldval = mutex->__data.__lock;
      /* This is set to FUTEX_WAITERS iff we might have shared the
	 FUTEX_WAITERS flag with other threads, and therefore need to keep it
	 set to avoid lost wake-ups.  We have the same requirement in the
	 simple mutex algorithm.  */
      unsigned int assume_other_futex_waiters = 0;
      while (1)
	{
	  /* Try to acquire the lock through a CAS from 0 (not acquired) to
	     our TID | assume_other_futex_waiters.  */
	  if (__glibc_likely (oldval == 0))
	    {
	      oldval
	        = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
	            id | assume_other_futex_waiters, 0);
	      if (__glibc_likely (oldval == 0))
		break;
	    }

	  if ((oldval & FUTEX_OWNER_DIED) != 0)
	    {
	      /* The previous owner died.  Try locking the mutex.  */
	      int newval = id | (oldval & FUTEX_WAITERS)
		  | assume_other_futex_waiters;

	      newval
		= atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
						       newval, oldval);
	      if (newval != oldval)
		{
		  oldval = newval;
		  continue;
		}

	      /* We got the mutex.  */
	      mutex->__data.__count = 1;
	      /* But it is inconsistent unless marked otherwise.  */
	      mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;

	      /* We must not enqueue the mutex before we have acquired it.
		 Also see comments at ENQUEUE_MUTEX.  */
	      __asm ("" ::: "memory");
	      ENQUEUE_MUTEX (mutex);
	      /* We need to clear op_pending after we enqueue the mutex.  */
	      __asm ("" ::: "memory");
	      THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);

	      /* Note that we deliberately exit here.  If we fall
		 through to the end of the function __nusers would be
		 incremented which is not correct because the old
		 owner has to be discounted.  */
	      return EOWNERDEAD;
	    }

	  /* Check whether we already hold the mutex.  */
	  if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
	    {
	      int kind = PTHREAD_MUTEX_TYPE (mutex);
	      if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
		{
		  /* We do not need to ensure ordering wrt another memory
		     access.  Also see comments at ENQUEUE_MUTEX. */
		  THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
				 NULL);
		  return EDEADLK;
		}

	      if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
		{
		  /* We do not need to ensure ordering wrt another memory
		     access.  */
		  THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
				 NULL);

		  /* Just bump the counter.  */
		  if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
		    /* Overflow of the counter.  */
		    return EAGAIN;

		  ++mutex->__data.__count;

		  LIBC_PROBE (mutex_timedlock_acquired, 1, mutex);

		  return 0;
		}
	    }

	  /* We are about to block; check whether the timeout is invalid.  */
	  if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
	    return EINVAL;
	  /* Work around the fact that the kernel rejects negative timeout
	     values despite them being valid.  */
	  if (__glibc_unlikely (abstime->tv_sec < 0))
	    return ETIMEDOUT;
#if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
     || !defined lll_futex_timed_wait_bitset)
	  struct timeval tv;
	  struct timespec rt;

	  /* Get the current time.  */
	  (void) __gettimeofday (&tv, NULL);

	  /* Compute relative timeout.  */
	  rt.tv_sec = abstime->tv_sec - tv.tv_sec;
	  rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
	  if (rt.tv_nsec < 0)
	    {
	      rt.tv_nsec += 1000000000;
	      --rt.tv_sec;
	    }

	  /* Already timed out?  */
	  if (rt.tv_sec < 0)
	    return ETIMEDOUT;
#endif

	  /* We cannot acquire the mutex nor has its owner died.  Thus, try
	     to block using futexes.  Set FUTEX_WAITERS if necessary so that
	     other threads are aware that there are potentially threads
	     blocked on the futex.  Restart if oldval changed in the
	     meantime.  */
	  if ((oldval & FUTEX_WAITERS) == 0)
	    {
	      if (atomic_compare_and_exchange_bool_acq (&mutex->__data.__lock,
							oldval | FUTEX_WAITERS,
							oldval)
		  != 0)
		{
		  oldval = mutex->__data.__lock;
		  continue;
		}
	      oldval |= FUTEX_WAITERS;
	    }

	  /* It is now possible that we share the FUTEX_WAITERS flag with
	     another thread; therefore, update assume_other_futex_waiters so
	     that we do not forget about this when handling other cases
	     above and thus do not cause lost wake-ups.  */
	  assume_other_futex_waiters |= FUTEX_WAITERS;

	  /* Block using the futex.  */
#if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
     || !defined lll_futex_timed_wait_bitset)
	  lll_futex_timed wait (&mutex->__data.__lock, oldval,
				&rt, PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
#else
	  int err = lll_futex_timed_wait_bitset (&mutex->__data.__lock,
	      oldval, abstime, FUTEX_CLOCK_REALTIME,
	      PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
	  /* The futex call timed out.  */
	  if (err == -ETIMEDOUT)
	    return -err;
#endif
	  /* Reload current lock value.  */
	  oldval = mutex->__data.__lock;
	}

      /* We have acquired the mutex; check if it is still consistent.  */
      if (__builtin_expect (mutex->__data.__owner
			    == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
	{
	  /* This mutex is now not recoverable.  */
	  mutex->__data.__count = 0;
	  int private = PTHREAD_ROBUST_MUTEX_PSHARED (mutex);
	  lll_unlock (mutex->__data.__lock, private);
	  /* FIXME This violates the mutex destruction requirements.  See
	     __pthread_mutex_unlock_full.  */
	  THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
	  return ENOTRECOVERABLE;
	}

      mutex->__data.__count = 1;
      /* We must not enqueue the mutex before we have acquired it.
	 Also see comments at ENQUEUE_MUTEX.  */
      __asm ("" ::: "memory");
      ENQUEUE_MUTEX (mutex);
      /* We need to clear op_pending after we enqueue the mutex.  */
      __asm ("" ::: "memory");
      THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
      break;

    /* The PI support requires the Linux futex system call.  If that's not
       available, pthread_mutex_init should never have allowed the type to
       be set.  So it will get the default case for an invalid type.  */
#ifdef __NR_futex
    case PTHREAD_MUTEX_PI_RECURSIVE_NP:
    case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
    case PTHREAD_MUTEX_PI_NORMAL_NP:
    case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
    case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
    case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
    case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
    case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
      {
	int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
	int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;

	if (robust)
	  {
	    /* Note: robust PI futexes are signaled by setting bit 0.  */
	    THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
			   (void *) (((uintptr_t) &mutex->__data.__list.__next)
				     | 1));
	    /* We need to set op_pending before starting the operation.  Also
	       see comments at ENQUEUE_MUTEX.  */
	    __asm ("" ::: "memory");
	  }

	oldval = mutex->__data.__lock;

	/* Check whether we already hold the mutex.  */
	if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
	  {
	    if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
	      {
		/* We do not need to ensure ordering wrt another memory
		   access.  */
		THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
		return EDEADLK;
	      }

	    if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
	      {
		/* We do not need to ensure ordering wrt another memory
		   access.  */
		THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);

		/* Just bump the counter.  */
		if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
		  /* Overflow of the counter.  */
		  return EAGAIN;

		++mutex->__data.__count;

		LIBC_PROBE (mutex_timedlock_acquired, 1, mutex);

		return 0;
	      }
	  }

	oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
						      id, 0);

	if (oldval != 0)
	  {
	    /* The mutex is locked.  The kernel will now take care of
	       everything.  The timeout value must be a relative value.
	       Convert it.  */
	    int private = (robust
			   ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
			   : PTHREAD_MUTEX_PSHARED (mutex));
	    INTERNAL_SYSCALL_DECL (__err);

	    int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
				      __lll_private_flag (FUTEX_LOCK_PI,
							  private), 1,
				      abstime);
	    if (INTERNAL_SYSCALL_ERROR_P (e, __err))
	      {
		if (INTERNAL_SYSCALL_ERRNO (e, __err) == ETIMEDOUT)
		  return ETIMEDOUT;

		if (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
		    || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK)
		  {
		    assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
			    || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
				&& kind != PTHREAD_MUTEX_RECURSIVE_NP));
		    /* ESRCH can happen only for non-robust PI mutexes where
		       the owner of the lock died.  */
		    assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH
			    || !robust);

		    /* Delay the thread until the timeout is reached.
		       Then return ETIMEDOUT.  */
		    struct timespec reltime;
		    struct timespec now;

		    INTERNAL_SYSCALL (clock_gettime, __err, 2, CLOCK_REALTIME,
				      &now);
		    reltime.tv_sec = abstime->tv_sec - now.tv_sec;
		    reltime.tv_nsec = abstime->tv_nsec - now.tv_nsec;
		    if (reltime.tv_nsec < 0)
		      {
			reltime.tv_nsec += 1000000000;
			--reltime.tv_sec;
		      }
		    if (reltime.tv_sec >= 0)
		      while (__nanosleep_nocancel (&reltime, &reltime) != 0)
			continue;

		    return ETIMEDOUT;
		  }

		return INTERNAL_SYSCALL_ERRNO (e, __err);
	      }

	    oldval = mutex->__data.__lock;

	    assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
	  }

	if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
	  {
	    atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);

	    /* We got the mutex.  */
	    mutex->__data.__count = 1;
	    /* But it is inconsistent unless marked otherwise.  */
	    mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;

	    /* We must not enqueue the mutex before we have acquired it.
	       Also see comments at ENQUEUE_MUTEX.  */
	    __asm ("" ::: "memory");
	    ENQUEUE_MUTEX_PI (mutex);
	    /* We need to clear op_pending after we enqueue the mutex.  */
	    __asm ("" ::: "memory");
	    THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);

	    /* Note that we deliberately exit here.  If we fall
	       through to the end of the function __nusers would be
	       incremented which is not correct because the old owner
	       has to be discounted.  */
	    return EOWNERDEAD;
	  }

	if (robust
	    && __builtin_expect (mutex->__data.__owner
				 == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
	  {
	    /* This mutex is now not recoverable.  */
	    mutex->__data.__count = 0;

	    INTERNAL_SYSCALL_DECL (__err);
	    INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
			      __lll_private_flag (FUTEX_UNLOCK_PI,
						  PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
			      0, 0);

	    /* To the kernel, this will be visible after the kernel has
	       acquired the mutex in the syscall.  */
	    THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
	    return ENOTRECOVERABLE;
	  }

	mutex->__data.__count = 1;
	if (robust)
	  {
	    /* We must not enqueue the mutex before we have acquired it.
	       Also see comments at ENQUEUE_MUTEX.  */
	    __asm ("" ::: "memory");
	    ENQUEUE_MUTEX_PI (mutex);
	    /* We need to clear op_pending after we enqueue the mutex.  */
	    __asm ("" ::: "memory");
	    THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
	  }
	}
      break;
#endif  /* __NR_futex.  */

    case PTHREAD_MUTEX_PP_RECURSIVE_NP:
    case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
    case PTHREAD_MUTEX_PP_NORMAL_NP:
    case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
      {
	int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;

	oldval = mutex->__data.__lock;

	/* Check whether we already hold the mutex.  */
	if (mutex->__data.__owner == id)
	  {
	    if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
	      return EDEADLK;

	    if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
	      {
		/* Just bump the counter.  */
		if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
		  /* Overflow of the counter.  */
		  return EAGAIN;

		++mutex->__data.__count;

		LIBC_PROBE (mutex_timedlock_acquired, 1, mutex);

		return 0;
	      }
	  }

	int oldprio = -1, ceilval;
	do
	  {
	    int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
			  >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;

	    if (__pthread_current_priority () > ceiling)
	      {
		result = EINVAL;
	      failpp:
		if (oldprio != -1)
		  __pthread_tpp_change_priority (oldprio, -1);
		return result;
	      }

	    result = __pthread_tpp_change_priority (oldprio, ceiling);
	    if (result)
	      return result;

	    ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
	    oldprio = ceiling;

	    oldval
	      = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
						     ceilval | 1, ceilval);

	    if (oldval == ceilval)
	      break;

	    do
	      {
		oldval
		  = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
							 ceilval | 2,
							 ceilval | 1);

		if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
		  break;

		if (oldval != ceilval)
		  {
		    /* Reject invalid timeouts.  */
		    if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
		      {
			result = EINVAL;
			goto failpp;
		      }

		    struct timeval tv;
		    struct timespec rt;

		    /* Get the current time.  */
		    (void) __gettimeofday (&tv, NULL);

		    /* Compute relative timeout.  */
		    rt.tv_sec = abstime->tv_sec - tv.tv_sec;
		    rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
		    if (rt.tv_nsec < 0)
		      {
			rt.tv_nsec += 1000000000;
			--rt.tv_sec;
		      }

		    /* Already timed out?  */
		    if (rt.tv_sec < 0)
		      {
			result = ETIMEDOUT;
			goto failpp;
		      }

		    lll_futex_timed_wait (&mutex->__data.__lock,
					  ceilval | 2, &rt,
					  PTHREAD_MUTEX_PSHARED (mutex));
		  }
	      }
	    while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
							ceilval | 2, ceilval)
		   != ceilval);
	  }
	while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);

	assert (mutex->__data.__owner == 0);
	mutex->__data.__count = 1;
      }
      break;

    default:
      /* Correct code cannot set any other type.  */
      return EINVAL;
    }

  if (result == 0)
    {
      /* Record the ownership.  */
      mutex->__data.__owner = id;
      ++mutex->__data.__nusers;

      LIBC_PROBE (mutex_timedlock_acquired, 1, mutex);
    }

 out:
  return result;
}
weak_alias (__pthread_mutex_timedlock, pthread_mutex_timedlock)

/* 64-bit time version */

int
__pthread_mutex_timedlock64 (pthread_mutex_t *mutex,
			 const struct __timespec64 *abstime)
{
/* Only compile this function if kernel provides clock_gettime64 */
#ifdef __NR_clock_gettime64
  int oldval;
  pid_t id = THREAD_GETMEM (THREAD_SELF, tid);
  int result = 0;
#endif
  struct timespec abstime32;

  LIBC_PROBE (mutex_timedlock_entry, 2, mutex, abstime);

/* Only compile this function if kernel provides clock_gettime64 */
#ifdef __NR_clock_gettime64
  if (__y2038_kernel_support())
    {

      /* We must not check ABSTIME here.  If the thread does not block
         abstime must not be checked for a valid value.  */

      switch (__builtin_expect (PTHREAD_MUTEX_TYPE_ELISION (mutex),
                    PTHREAD_MUTEX_TIMED_NP))
        {
          /* Recursive mutex.  */
        case PTHREAD_MUTEX_RECURSIVE_NP|PTHREAD_MUTEX_ELISION_NP:
        case PTHREAD_MUTEX_RECURSIVE_NP:
          /* Check whether we already hold the mutex.  */
          if (mutex->__data.__owner == id)
        {
          /* Just bump the counter.  */
          if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
            /* Overflow of the counter.  */
            return EAGAIN;

          ++mutex->__data.__count;

          goto out;
        }

          /* We have to get the mutex.  */
          result = lll_timedlock64 (mutex->__data.__lock, abstime,
                          PTHREAD_MUTEX_PSHARED (mutex));

          if (result != 0)
        goto out;

          /* Only locked once so far.  */
          mutex->__data.__count = 1;
          break;

          /* Error checking mutex.  */
        case PTHREAD_MUTEX_ERRORCHECK_NP:
          /* Check whether we already hold the mutex.  */
          if (__glibc_unlikely (mutex->__data.__owner == id))
        return EDEADLK;

          /* Don't do lock elision on an error checking mutex.  */
          goto simple;

        case PTHREAD_MUTEX_TIMED_NP:
          FORCE_ELISION (mutex, goto elision);
        simple:
          /* Normal mutex.  */
          result = lll_timedlock64 (mutex->__data.__lock, abstime,
                          PTHREAD_MUTEX_PSHARED (mutex));
          break;

        case PTHREAD_MUTEX_TIMED_ELISION_NP:
        elision: __attribute__((unused))
          /* Don't record ownership */
          return lll_timedlock_elision64 (mutex->__data.__lock,
                        mutex->__data.__spins,
                        abstime,
                        PTHREAD_MUTEX_PSHARED (mutex));


        case PTHREAD_MUTEX_ADAPTIVE_NP:
          if (! __is_smp)
        goto simple;

          if (lll_trylock (mutex->__data.__lock) != 0)
        {
          int cnt = 0;
          int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
                     mutex->__data.__spins * 2 + 10);
          do
            {
              if (cnt++ >= max_cnt)
            {
              result = lll_timedlock64 (mutex->__data.__lock, abstime,
                              PTHREAD_MUTEX_PSHARED (mutex));
              break;
            }
              atomic_spin_nop ();
            }
          while (lll_trylock (mutex->__data.__lock) != 0);

          mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
        }
          break;

        case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP:
        case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP:
        case PTHREAD_MUTEX_ROBUST_NORMAL_NP:
        case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP:
          THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
                 &mutex->__data.__list.__next);
          /* We need to set op_pending before starting the operation.  Also
         see comments at ENQUEUE_MUTEX.  */
          __asm ("" ::: "memory");

          oldval = mutex->__data.__lock;
          /* This is set to FUTEX_WAITERS iff we might have shared the
         FUTEX_WAITERS flag with other threads, and therefore need to keep it
         set to avoid lost wake-ups.  We have the same requirement in the
         simple mutex algorithm.  */
          unsigned int assume_other_futex_waiters = 0;
          while (1)
        {
          /* Try to acquire the lock through a CAS from 0 (not acquired) to
             our TID | assume_other_futex_waiters.  */
          if (__glibc_likely (oldval == 0))
            {
              oldval
                = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
                    id | assume_other_futex_waiters, 0);
              if (__glibc_likely (oldval == 0))
            break;
            }

          if ((oldval & FUTEX_OWNER_DIED) != 0)
            {
              /* The previous owner died.  Try locking the mutex.  */
              int newval = id | (oldval & FUTEX_WAITERS)
              | assume_other_futex_waiters;

              newval
            = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
                                   newval, oldval);
              if (newval != oldval)
            {
              oldval = newval;
              continue;
            }

              /* We got the mutex.  */
              mutex->__data.__count = 1;
              /* But it is inconsistent unless marked otherwise.  */
              mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;

              /* We must not enqueue the mutex before we have acquired it.
             Also see comments at ENQUEUE_MUTEX.  */
              __asm ("" ::: "memory");
              ENQUEUE_MUTEX (mutex);
              /* We need to clear op_pending after we enqueue the mutex.  */
              __asm ("" ::: "memory");
              THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);

              /* Note that we deliberately exit here.  If we fall
             through to the end of the function __nusers would be
             incremented which is not correct because the old
             owner has to be discounted.  */
              return EOWNERDEAD;
            }

          /* Check whether we already hold the mutex.  */
          if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
            {
              int kind = PTHREAD_MUTEX_TYPE (mutex);
              if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP)
            {
              /* We do not need to ensure ordering wrt another memory
                 access.  Also see comments at ENQUEUE_MUTEX. */
              THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
                     NULL);
              return EDEADLK;
            }

              if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP)
            {
              /* We do not need to ensure ordering wrt another memory
                 access.  */
              THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
                     NULL);

              /* Just bump the counter.  */
              if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
                /* Overflow of the counter.  */
                return EAGAIN;

              ++mutex->__data.__count;

              LIBC_PROBE (mutex_timedlock_acquired, 1, mutex);

              return 0;
            }
            }

          /* We are about to block; check whether the timeout is invalid.  */
          if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
            return EINVAL;
          /* Work around the fact that the kernel rejects negative timeout
             values despite them being valid.  */
          if (__glibc_unlikely (abstime->tv_sec < 0))
            return ETIMEDOUT;
    #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
         || !defined lll_futex_timed_wait_bitset)
          struct timeval tv;
          struct timespec rt;

          /* Get the current time.  */
          (void) __gettimeofday (&tv, NULL);

          /* Compute relative timeout.  */
          rt.tv_sec = abstime->tv_sec - tv.tv_sec;
          rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
          if (rt.tv_nsec < 0)
            {
              rt.tv_nsec += 1000000000;
              --rt.tv_sec;
            }

          /* Already timed out?  */
          if (rt.tv_sec < 0)
            return ETIMEDOUT;
    #endif

          /* We cannot acquire the mutex nor has its owner died.  Thus, try
             to block using futexes.  Set FUTEX_WAITERS if necessary so that
             other threads are aware that there are potentially threads
             blocked on the futex.  Restart if oldval changed in the
             meantime.  */
          if ((oldval & FUTEX_WAITERS) == 0)
            {
              if (atomic_compare_and_exchange_bool_acq (&mutex->__data.__lock,
                                oldval | FUTEX_WAITERS,
                                oldval)
              != 0)
            {
              oldval = mutex->__data.__lock;
              continue;
            }
              oldval |= FUTEX_WAITERS;
            }

          /* It is now possible that we share the FUTEX_WAITERS flag with
             another thread; therefore, update assume_other_futex_waiters so
             that we do not forget about this when handling other cases
             above and thus do not cause lost wake-ups.  */
          assume_other_futex_waiters |= FUTEX_WAITERS;

          /* Block using the futex.  */
    #if (!defined __ASSUME_FUTEX_CLOCK_REALTIME \
         || !defined lll_futex_timed_wait_bitset)
          lll_futex_timed wait_64 (&mutex->__data.__lock, oldval,
                    &rt, PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
    #else
          int err = lll_futex_timed_wait_bitset64 (&mutex->__data.__lock,
              oldval, abstime, FUTEX_CLOCK_REALTIME,
              PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
          /* The futex call timed out.  */
          if (err == -ETIMEDOUT)
            return -err;
    #endif
          /* Reload current lock value.  */
          oldval = mutex->__data.__lock;
        }

          /* We have acquired the mutex; check if it is still consistent.  */
          if (__builtin_expect (mutex->__data.__owner
                    == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
        {
          /* This mutex is now not recoverable.  */
          mutex->__data.__count = 0;
          int private = PTHREAD_ROBUST_MUTEX_PSHARED (mutex);
          lll_unlock (mutex->__data.__lock, private);
          /* FIXME This violates the mutex destruction requirements.  See
             __pthread_mutex_unlock_full.  */
          THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
          return ENOTRECOVERABLE;
        }

          mutex->__data.__count = 1;
          /* We must not enqueue the mutex before we have acquired it.
         Also see comments at ENQUEUE_MUTEX.  */
          __asm ("" ::: "memory");
          ENQUEUE_MUTEX (mutex);
          /* We need to clear op_pending after we enqueue the mutex.  */
          __asm ("" ::: "memory");
          THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
          break;

        /* The PI support requires the Linux futex system call.  If that's not
           available, pthread_mutex_init should never have allowed the type to
           be set.  So it will get the default case for an invalid type.  */
    #ifdef __NR_futex
        case PTHREAD_MUTEX_PI_RECURSIVE_NP:
        case PTHREAD_MUTEX_PI_ERRORCHECK_NP:
        case PTHREAD_MUTEX_PI_NORMAL_NP:
        case PTHREAD_MUTEX_PI_ADAPTIVE_NP:
        case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP:
        case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP:
        case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP:
        case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP:
          {
        int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;
        int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP;

        if (robust)
          {
            /* Note: robust PI futexes are signaled by setting bit 0.  */
            THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending,
                   (void *) (((uintptr_t) &mutex->__data.__list.__next)
                         | 1));
            /* We need to set op_pending before starting the operation.  Also
               see comments at ENQUEUE_MUTEX.  */
            __asm ("" ::: "memory");
          }

        oldval = mutex->__data.__lock;

        /* Check whether we already hold the mutex.  */
        if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id))
          {
            if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
              {
            /* We do not need to ensure ordering wrt another memory
               access.  */
            THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
            return EDEADLK;
              }

            if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
              {
            /* We do not need to ensure ordering wrt another memory
               access.  */
            THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);

            /* Just bump the counter.  */
            if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
              /* Overflow of the counter.  */
              return EAGAIN;

            ++mutex->__data.__count;

            LIBC_PROBE (mutex_timedlock_acquired, 1, mutex);

            return 0;
              }
          }

        oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
                                  id, 0);

        if (oldval != 0)
          {
            /* The mutex is locked.  The kernel will now take care of
               everything.  The timeout value must be a relative value.
               Convert it.  */
            int private = (robust
                   ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex)
                   : PTHREAD_MUTEX_PSHARED (mutex));
            INTERNAL_SYSCALL_DECL (__err);

            int e;
            
            if (abstime->tv_sec > INT_MAX)
            {
              e = EOVERFLOW;
            }
            else
            {
              struct timespec ts;
              ts.tv_sec = abstime->tv_sec;
              ts.tv_nsec = abstime->tv_nsec;
              e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
                          __lll_private_flag (FUTEX_LOCK_PI,
                                  private), 1,
                          &ts);
            }
            if (INTERNAL_SYSCALL_ERROR_P (e, __err))
              {
            if (INTERNAL_SYSCALL_ERRNO (e, __err) == ETIMEDOUT)
              return ETIMEDOUT;

            if (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH
                || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK)
              {
                assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK
                    || (kind != PTHREAD_MUTEX_ERRORCHECK_NP
                    && kind != PTHREAD_MUTEX_RECURSIVE_NP));
                /* ESRCH can happen only for non-robust PI mutexes where
                   the owner of the lock died.  */
                assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH
                    || !robust);

                /* Delay the thread until the timeout is reached.
                   Then return ETIMEDOUT.  */
                struct timespec reltime;
                struct __timespec64 now;

                INTERNAL_SYSCALL (clock_gettime64, __err, 2, CLOCK_REALTIME,
                          &now);
                reltime.tv_sec = abstime->tv_sec - now.tv_sec;
                reltime.tv_nsec = abstime->tv_nsec - now.tv_nsec;
                if (reltime.tv_nsec < 0)
                  {
                reltime.tv_nsec += 1000000000;
                --reltime.tv_sec;
                  }
                if (reltime.tv_sec >= 0)
                  while (__nanosleep_nocancel (&reltime, &reltime) != 0)
                continue;

                return ETIMEDOUT;
              }

            return INTERNAL_SYSCALL_ERRNO (e, __err);
              }

            oldval = mutex->__data.__lock;

            assert (robust || (oldval & FUTEX_OWNER_DIED) == 0);
          }

        if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED))
          {
            atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED);

            /* We got the mutex.  */
            mutex->__data.__count = 1;
            /* But it is inconsistent unless marked otherwise.  */
            mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT;

            /* We must not enqueue the mutex before we have acquired it.
               Also see comments at ENQUEUE_MUTEX.  */
            __asm ("" ::: "memory");
            ENQUEUE_MUTEX_PI (mutex);
            /* We need to clear op_pending after we enqueue the mutex.  */
            __asm ("" ::: "memory");
            THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);

            /* Note that we deliberately exit here.  If we fall
               through to the end of the function __nusers would be
               incremented which is not correct because the old owner
               has to be discounted.  */
            return EOWNERDEAD;
          }

        if (robust
            && __builtin_expect (mutex->__data.__owner
                     == PTHREAD_MUTEX_NOTRECOVERABLE, 0))
          {
            /* This mutex is now not recoverable.  */
            mutex->__data.__count = 0;

            INTERNAL_SYSCALL_DECL (__err);
            INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock,
                      __lll_private_flag (FUTEX_UNLOCK_PI,
                              PTHREAD_ROBUST_MUTEX_PSHARED (mutex)),
                      0, 0);

            /* To the kernel, this will be visible after the kernel has
               acquired the mutex in the syscall.  */
            THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
            return ENOTRECOVERABLE;
          }

        mutex->__data.__count = 1;
        if (robust)
          {
            /* We must not enqueue the mutex before we have acquired it.
               Also see comments at ENQUEUE_MUTEX.  */
            __asm ("" ::: "memory");
            ENQUEUE_MUTEX_PI (mutex);
            /* We need to clear op_pending after we enqueue the mutex.  */
            __asm ("" ::: "memory");
            THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
          }
        }
          break;
    #endif  /* __NR_futex.  */

        case PTHREAD_MUTEX_PP_RECURSIVE_NP:
        case PTHREAD_MUTEX_PP_ERRORCHECK_NP:
        case PTHREAD_MUTEX_PP_NORMAL_NP:
        case PTHREAD_MUTEX_PP_ADAPTIVE_NP:
          {
        int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP;

        oldval = mutex->__data.__lock;

        /* Check whether we already hold the mutex.  */
        if (mutex->__data.__owner == id)
          {
            if (kind == PTHREAD_MUTEX_ERRORCHECK_NP)
              return EDEADLK;

            if (kind == PTHREAD_MUTEX_RECURSIVE_NP)
              {
            /* Just bump the counter.  */
            if (__glibc_unlikely (mutex->__data.__count + 1 == 0))
              /* Overflow of the counter.  */
              return EAGAIN;

            ++mutex->__data.__count;

            LIBC_PROBE (mutex_timedlock_acquired, 1, mutex);

            return 0;
              }
          }

        int oldprio = -1, ceilval;
        do
          {
            int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
                  >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;

            if (__pthread_current_priority () > ceiling)
              {
            result = EINVAL;
              failpp:
            if (oldprio != -1)
              __pthread_tpp_change_priority (oldprio, -1);
            return result;
              }

            result = __pthread_tpp_change_priority (oldprio, ceiling);
            if (result)
              return result;

            ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
            oldprio = ceiling;

            oldval
              = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
                                 ceilval | 1, ceilval);

            if (oldval == ceilval)
              break;

            do
              {
            oldval
              = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
                                 ceilval | 2,
                                 ceilval | 1);

            if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
              break;

            if (oldval != ceilval)
              {
                /* Reject invalid timeouts.  */
                if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
                  {
                result = EINVAL;
                goto failpp;
                  }

                struct timeval tv;
                struct timespec rt;

                /* Get the current time.  */
                (void) __gettimeofday (&tv, NULL);

                /* Compute relative timeout.  */
                rt.tv_sec = abstime->tv_sec - tv.tv_sec;
                rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
                if (rt.tv_nsec < 0)
                  {
                rt.tv_nsec += 1000000000;
                --rt.tv_sec;
                  }

                /* Already timed out?  */
                if (rt.tv_sec < 0)
                  {
                result = ETIMEDOUT;
                goto failpp;
                  }

                lll_futex_timed_wait (&mutex->__data.__lock,
                          ceilval | 2, &rt,
                          PTHREAD_MUTEX_PSHARED (mutex));
              }
              }
            while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
                                ceilval | 2, ceilval)
               != ceilval);
          }
        while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval);

        assert (mutex->__data.__owner == 0);
        mutex->__data.__count = 1;
          }
          break;

        default:
          /* Correct code cannot set any other type.  */
          return EINVAL;
        }

      if (result == 0)
        {
          /* Record the ownership.  */
          mutex->__data.__owner = id;
          ++mutex->__data.__nusers;

          LIBC_PROBE (mutex_timedlock_acquired, 1, mutex);
        }
     out:
      return result;
    }
#endif

  if (abstime->tv_sec > INT_MAX || abstime->tv_sec < INT_MIN)
    {
      return EOVERFLOW;
    }

  abstime32.tv_sec = (time_t) (abstime->tv_sec);
  abstime32.tv_nsec = abstime->tv_nsec;

  return __pthread_mutex_timedlock (mutex, &abstime32);
}