Fix: all lttng-ust source files should be tagged _LGPL_SOURCE
[lttng-ust.git] / liblttng-ust-libc-wrapper / lttng-ust-pthread.c
1 /*
2 * Copyright (C) 2013 Mentor Graphics
3 *
4 * This library 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 * This library 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 GNU
12 * 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 this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #define _GNU_SOURCE
20 #define _LGPL_SOURCE
21 #include <lttng/ust-dlfcn.h>
22 #include <helper.h>
23 #include <pthread.h>
24
25 #define TRACEPOINT_DEFINE
26 #define TRACEPOINT_CREATE_PROBES
27 #define TP_IP_PARAM ip
28 #include "ust_pthread.h"
29
30 static __thread int thread_in_trace;
31
32 int pthread_mutex_lock(pthread_mutex_t *mutex)
33 {
34 static int (*mutex_lock)(pthread_mutex_t *);
35 int retval;
36
37 if (!mutex_lock) {
38 mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock");
39 if (!mutex_lock) {
40 if (thread_in_trace) {
41 abort();
42 }
43 fprintf(stderr, "unable to initialize pthread wrapper library.\n");
44 return EINVAL;
45 }
46 }
47 if (thread_in_trace) {
48 return mutex_lock(mutex);
49 }
50
51 thread_in_trace = 1;
52 tracepoint(lttng_ust_pthread, pthread_mutex_lock_req, mutex,
53 LTTNG_UST_CALLER_IP());
54 retval = mutex_lock(mutex);
55 tracepoint(lttng_ust_pthread, pthread_mutex_lock_acq, mutex,
56 retval, LTTNG_UST_CALLER_IP());
57 thread_in_trace = 0;
58 return retval;
59 }
60
61 int pthread_mutex_trylock(pthread_mutex_t *mutex)
62 {
63 static int (*mutex_trylock)(pthread_mutex_t *);
64 int retval;
65
66 if (!mutex_trylock) {
67 mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock");
68 if (!mutex_trylock) {
69 if (thread_in_trace) {
70 abort();
71 }
72 fprintf(stderr, "unable to initialize pthread wrapper library.\n");
73 return EINVAL;
74 }
75 }
76 if (thread_in_trace) {
77 return mutex_trylock(mutex);
78 }
79
80 thread_in_trace = 1;
81 retval = mutex_trylock(mutex);
82 tracepoint(lttng_ust_pthread, pthread_mutex_trylock, mutex,
83 retval, LTTNG_UST_CALLER_IP());
84 thread_in_trace = 0;
85 return retval;
86 }
87
88 int pthread_mutex_unlock(pthread_mutex_t *mutex)
89 {
90 static int (*mutex_unlock)(pthread_mutex_t *);
91 int retval;
92
93 if (!mutex_unlock) {
94 mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
95 if (!mutex_unlock) {
96 if (thread_in_trace) {
97 abort();
98 }
99 fprintf(stderr, "unable to initialize pthread wrapper library.\n");
100 return EINVAL;
101 }
102 }
103 if (thread_in_trace) {
104 return mutex_unlock(mutex);
105 }
106
107 thread_in_trace = 1;
108 retval = mutex_unlock(mutex);
109 tracepoint(lttng_ust_pthread, pthread_mutex_unlock, mutex,
110 retval, LTTNG_UST_CALLER_IP());
111 thread_in_trace = 0;
112 return retval;
113 }
This page took 0.034398 seconds and 5 git commands to generate.