1 /* pthread_mutex_lock.  Hurd version.
2    Copyright (C) 2016-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
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 
10    The GNU C Library 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 GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library;  if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <pthread.h>
20 #include <stdlib.h>
21 #include <assert.h>
22 #include <pt-internal.h>
23 #include "pt-mutex.h"
24 #include <hurdlock.h>
25 
26 int
__pthread_mutex_lock(pthread_mutex_t * mtxp)27 __pthread_mutex_lock (pthread_mutex_t *mtxp)
28 {
29   struct __pthread *self;
30   int flags = mtxp->__flags & GSYNC_SHARED;
31   int ret = 0;
32 
33   switch (MTX_TYPE (mtxp))
34     {
35     case PT_MTX_NORMAL:
36       lll_lock (mtxp->__lock, flags);
37       break;
38 
39     case PT_MTX_RECURSIVE:
40       self = _pthread_self ();
41       if (mtx_owned_p (mtxp, self, flags))
42 	{
43 	  if (__glibc_unlikely (mtxp->__cnt + 1 == 0))
44 	    return EAGAIN;
45 
46 	  ++mtxp->__cnt;
47 	  return ret;
48 	}
49 
50       lll_lock (mtxp->__lock, flags);
51       mtx_set_owner (mtxp, self, flags);
52       mtxp->__cnt = 1;
53       break;
54 
55     case PT_MTX_ERRORCHECK:
56       self = _pthread_self ();
57       if (mtx_owned_p (mtxp, self, flags))
58 	return EDEADLK;
59 
60       lll_lock (mtxp->__lock, flags);
61       mtx_set_owner (mtxp, self, flags);
62       break;
63 
64     case PT_MTX_NORMAL | PTHREAD_MUTEX_ROBUST:
65     case PT_MTX_RECURSIVE | PTHREAD_MUTEX_ROBUST:
66     case PT_MTX_ERRORCHECK | PTHREAD_MUTEX_ROBUST:
67       self = _pthread_self ();
68       ROBUST_LOCK (self, mtxp, lll_robust_lock, flags);
69       break;
70 
71     default:
72       ret = EINVAL;
73       break;
74     }
75 
76   return ret;
77 }
78 
79 hidden_def (__pthread_mutex_lock)
80 strong_alias (__pthread_mutex_lock, _pthread_mutex_lock)
81 weak_alias (__pthread_mutex_lock, pthread_mutex_lock)
82