/src/mpv/osdep/threads-posix.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv 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 |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include <stdio.h> |
19 | | #include <errno.h> |
20 | | |
21 | | #include "common/common.h" |
22 | | #include "config.h" |
23 | | #include "threads.h" |
24 | | #include "timer.h" |
25 | | |
26 | | #if HAVE_BSD_THREAD_NAME |
27 | | #include <pthread_np.h> |
28 | | #endif |
29 | | |
30 | | int mp_ptwrap_check(const char *file, int line, int res) |
31 | 0 | { |
32 | 0 | if (res && res != ETIMEDOUT) { |
33 | 0 | fprintf(stderr, "%s:%d: internal error: pthread result %d (%s)\n", |
34 | 0 | file, line, res, mp_strerror(res)); |
35 | 0 | abort(); |
36 | 0 | } |
37 | 0 | return res; |
38 | 0 | } |
39 | | |
40 | | int mp_ptwrap_mutex_init(const char *file, int line, pthread_mutex_t *m, |
41 | | const pthread_mutexattr_t *attr) |
42 | 0 | { |
43 | 0 | pthread_mutexattr_t m_attr; |
44 | 0 | if (!attr) { |
45 | 0 | attr = &m_attr; |
46 | 0 | pthread_mutexattr_init(&m_attr); |
47 | | // Force normal mutexes to error checking. |
48 | 0 | pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_ERRORCHECK); |
49 | 0 | } |
50 | 0 | int res = mp_ptwrap_check(file, line, (pthread_mutex_init)(m, attr)); |
51 | 0 | if (attr == &m_attr) |
52 | 0 | pthread_mutexattr_destroy(&m_attr); |
53 | 0 | return res; |
54 | 0 | } |
55 | | |
56 | | int mp_ptwrap_mutex_trylock(const char *file, int line, pthread_mutex_t *m) |
57 | 0 | { |
58 | 0 | int res = (pthread_mutex_trylock)(m); |
59 | |
|
60 | 0 | if (res != EBUSY) |
61 | 0 | mp_ptwrap_check(file, line, res); |
62 | |
|
63 | 0 | return res; |
64 | 0 | } |