mailing list of musl libc
 help / color / mirror / code / Atom feed
3a47cec1cae1612cfa321abbe604f0759e7fc58a blob 4363 bytes (raw)

  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
 
/* testing pthread mutex behaviour with various attributes */
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "test.h"

#define T(f) if ((r=(f))) t_error(#f " failed: %s\n", strerror(r))
#define E(f) if (f) t_error(#f " failed: %s\n", strerror(errno))

static void *relock(void *arg)
{
	void **a = arg;
	int r;

	T(pthread_mutex_lock(a[0]));
	E(sem_post(a[1]));
	*(int*)a[2] = pthread_mutex_lock(a[0]);
	E(sem_post(a[1]));

	T(pthread_mutex_unlock(a[0]));
	if (*(int*)a[2] == 0)
		T(pthread_mutex_unlock(a[0]));
	return 0;
}

static int test_relock(int mtype)
{
	struct timespec ts;
	pthread_t t;
	pthread_mutex_t m;
	pthread_mutexattr_t ma;
	sem_t s;
	int i;
	int r;
	void *p;
	void *a[] = {&m,&s,&i};

	T(pthread_mutexattr_init(&ma));
	T(pthread_mutexattr_settype(&ma, mtype));
	T(pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
	T(pthread_mutex_init(a[0], &ma));
	T(pthread_mutexattr_destroy(&ma));
	E(sem_init(a[1], 0, 0));
	T(pthread_create(&t, 0, relock, a));
	E(sem_wait(a[1]));
	E(clock_gettime(CLOCK_REALTIME, &ts));
	ts.tv_nsec += 100*1000*1000;
	if (ts.tv_nsec >= 1000*1000*1000) {
		ts.tv_nsec -= 1000*1000*1000;
		ts.tv_sec += 1;
	}
	r = sem_timedwait(a[1],&ts);
	if (r == -1) {
		if (errno != ETIMEDOUT)
			t_error("sem_timedwait failed with unexpected error: %s\n", strerror(errno));
		/* leave the deadlocked relock thread running */
		return -1;
	}
	T(pthread_join(t, &p));
	T(pthread_mutex_destroy(a[0]));
	E(sem_destroy(a[1]));
	return i;
}

static void *unlock(void *arg)
{
	void **a = arg;

	*(int*)a[1] = pthread_mutex_unlock(a[0]);
	return 0;
}

static int test_unlock(int mtype)
{
	pthread_t t;
	pthread_mutex_t m;
	pthread_mutexattr_t ma;
	int i;
	int r;
	void *p;
	void *a[] = {&m,&i};

	T(pthread_mutexattr_init(&ma));
	T(pthread_mutexattr_settype(&ma, mtype));
	T(pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
	T(pthread_mutex_init(a[0], &ma));
	T(pthread_mutexattr_destroy(&ma));
	T(pthread_create(&t, 0, unlock, a));
	T(pthread_join(t, &p));
	T(pthread_mutex_destroy(a[0]));
	return i;
}

static int test_unlock_other(int mtype)
{
	pthread_t t;
	pthread_mutex_t m;
	pthread_mutexattr_t ma;
	int i;
	int r;
	void *p;
	void *a[] = {&m,&i};

	T(pthread_mutexattr_init(&ma));
	T(pthread_mutexattr_settype(&ma, mtype));
	T(pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
	T(pthread_mutex_init(a[0], &ma));
	T(pthread_mutexattr_destroy(&ma));
	T(pthread_mutex_lock(a[0]));
	T(pthread_create(&t, 0, unlock, a));
	T(pthread_join(t, &p));
	T(pthread_mutex_unlock(a[0]));
	T(pthread_mutex_destroy(a[0]));
	return i;
}

static void test_mutexattr()
{
	pthread_mutex_t m;
	pthread_mutexattr_t a;
	int r;
	int i;

	T(pthread_mutexattr_init(&a));
	T(pthread_mutexattr_gettype(&a, &i));
	if (i != PTHREAD_MUTEX_DEFAULT)
		t_error("default mutex type is %d, wanted PTHREAD_MUTEX_DEFAULT (%d)\n", i, PTHREAD_MUTEX_DEFAULT);
	T(pthread_mutexattr_settype(&a, PTHREAD_MUTEX_ERRORCHECK));
	T(pthread_mutexattr_gettype(&a, &i));
	if (i != PTHREAD_MUTEX_ERRORCHECK)
		t_error("setting error check mutex type failed failed: got %d, wanted %d\n", i, PTHREAD_MUTEX_ERRORCHECK);
	T(pthread_mutexattr_destroy(&a));
}

int main(void)
{
	int i;

	test_mutexattr();

	i = test_relock(PTHREAD_MUTEX_NORMAL);
	if (i != -1)
		t_error("PTHREAD_MUTEX_NORMAL relock did not deadlock, got %s\n", strerror(i));
	i = test_relock(PTHREAD_MUTEX_ERRORCHECK);
	if (i != EDEADLK)
		t_error("PTHREAD_MUTEX_ERRORCHECK relock did not return EDEADLK, got %s\n", i==-1?"deadlock":strerror(i));
	i = test_relock(PTHREAD_MUTEX_RECURSIVE);
	if (i != 0)
		t_error("PTHREAD_MUTEX_RECURSIVE relock did not succed, got %s\n", i==-1?"deadlock":strerror(i));

	i = test_unlock(PTHREAD_MUTEX_ERRORCHECK);
	if (i != EPERM)
		t_error("PTHREAD_MUTEX_ERRORCHECK unlock did not return EPERM, got %s\n", strerror(i));
	i = test_unlock(PTHREAD_MUTEX_RECURSIVE);
	if (i != EPERM)
		t_error("PTHREAD_MUTEX_RECURSIVE unlock did not return EPERM, got %s\n", strerror(i));

	i = test_unlock_other(PTHREAD_MUTEX_ERRORCHECK);
	if (i != EPERM)
		t_error("PTHREAD_MUTEX_ERRORCHECK unlock did not return EPERM, got %s\n", strerror(i));
	i = test_unlock_other(PTHREAD_MUTEX_RECURSIVE);
	if (i != EPERM)
		t_error("PTHREAD_MUTEX_RECURSIVE unlock did not return EPERM, got %s\n", strerror(i));

	return t_status;
}
debug log:

solving 3a47cec ...
found 3a47cec in https://inbox.vuxu.org/musl/20190709183313.GP1506@brightrain.aerifal.cx/

applying [1/1] https://inbox.vuxu.org/musl/20190709183313.GP1506@brightrain.aerifal.cx/
diff --git a/src/functional/pthread_mutex_pi.c b/src/functional/pthread_mutex_pi.c
new file mode 100644
index 0000000..3a47cec

Checking patch src/functional/pthread_mutex_pi.c...
Applied patch src/functional/pthread_mutex_pi.c cleanly.

index at:
100644 3a47cec1cae1612cfa321abbe604f0759e7fc58a	src/functional/pthread_mutex_pi.c

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).