Debian Codegraph @

runtime/linux/probe_lock.h

raw
1 /* probe locking header file
2 * Copyright (C) 2009-2019 Red Hat Inc.
3 *
4 * This file is part of systemtap, and is free software. You can
5 * redistribute it and/or modify it under the terms of the GNU General
6 * Public License (GPL); either version 2, or (at your option) any
7 * later version.
8 */
9
10 #ifndef _STAPLINUX_PROBE_LOCK_H
11 #define _STAPLINUX_PROBE_LOCK_H
12
13 #include <linux/spinlock.h>
14
15 // XXX: old 2.6 kernel hack
16 #ifndef read_trylock
17 #define read_trylock(x) ({ read_lock(x); 1; })
18 #endif
19
20 struct stp_probe_lock {
21 #ifdef STP_TIMING
22 atomic_t *skipped;
23 atomic_t *contention;
24 #endif
25 stp_rwlock_t *lock;
26 unsigned write_p;
27 };
28
29
30 static void
31 stp_unlock_probe(const struct stp_probe_lock *locks, unsigned num_locks)
32 {
33 unsigned i;
34 if (num_locks == 0) return; /* defeat a gcc9 warning */
35 for (i = num_locks; i-- > 0;) {
36 if (locks[i].write_p)
37 stp_write_unlock(locks[i].lock);
38 else
39 stp_read_unlock(locks[i].lock);
40 }
41 }
42
43
44 static unsigned
45 stp_lock_probe(const struct stp_probe_lock *locks, unsigned num_locks)
46 {
47 unsigned i, retries = 0;
48 for (i = 0; i < num_locks; ++i) {
49 if (locks[i].write_p)
50 while (!stp_write_trylock(locks[i].lock)) {
51 #if !defined(STAP_SUPPRESS_TIME_LIMITS_ENABLE)
52 if (++retries > MAXTRYLOCK)
53 goto skip;
54 #endif
55 #ifdef STP_TIMING
56 atomic_inc(locks[i].contention);
57 #endif
58 udelay (TRYLOCKDELAY);
59 }
60 else
61 while (!stp_read_trylock(locks[i].lock)) {
62 #if !defined(STAP_SUPPRESS_TIME_LIMITS_ENABLE)
63 if (++retries > MAXTRYLOCK)
64 goto skip;
65 #endif
66 #ifdef STP_TIMING
67 atomic_inc(locks[i].contention);
68 #endif
69 udelay (TRYLOCKDELAY);
70 }
71 }
72 return 1;
73
74 skip:
75 atomic_inc(skipped_count());
76 #ifdef STP_TIMING
77 atomic_inc(locks[i].skipped);
78 #endif
79 stp_unlock_probe(locks, i);
80 return 0;
81 }
82
83
84 #endif /* _STAPLINUX_PROBE_LOCK_H */