Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,155 +0,0 @@
1
-/* 
2
- * Copyright (C) 2007 iptelorg GmbH
3
- *
4
- * Permission to use, copy, modify, and distribute this software for any
5
- * purpose with or without fee is hereby granted, provided that the above
6
- * copyright notice and this permission notice appear in all copies.
7
- *
8
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
- */
16
-/*!
17
-* \file
18
-* \brief Kamailio core :: locks
19
-* \author andrei
20
-* \ingroup core
21
-* Module: \ref core
22
- *
23
- * futex based lock (mutex) implementation  (linux 2.6+ only)
24
- * based on Ulrich Drepper implementation in "Futexes Are Tricky"
25
- * (http://people.redhat.com/drepper/futex.pdf)
26
- *
27
- * Implements:
28
- *   void futex_get(futex_lock_t* lock);     - mutex lock
29
- *   void futex_release(futex_lock_t* lock); - unlock
30
- *   int  futex_try(futex_lock_t* lock);     - tries to get lock, returns 0
31
- *                                              on success and !=0 on failure
32
- *                                              (1 or 2)
33
- *
34
- *  Config defines:
35
- */
36
-
37
-#ifndef _futexlock_h
38
-#define _futexlock_h
39
-
40
-
41
-#include "atomic/atomic_common.h"
42
-#include "atomic/atomic_native.h"
43
-
44
-#ifdef HAVE_ASM_INLINE_ATOMIC_OPS
45
-#define HAVE_FUTEX
46
-#include <sys/types.h> /* hack to workaround some type conflicts 
47
-                          between linux-libc-dev andlibc headers
48
-                          in recent (6.08.2008) x86_64 debian sid
49
-                          installations */
50
-/* hack to work with old linux/futex.h versions, that depend on sched.h in
51
-   __KERNEL__ mode (futex.h < 2.6.20) */
52
-#include <linux/types.h>
53
-typedef __u32 u32;
54
-struct task_struct;
55
-/* end of the hack */
56
-/* another hack this time for OpenSuse 10.2:
57
-   futex.h uses a __user attribute, which is defined in linux/compiler.h
58
-   However linux/compiler.h is not part of the kernel headers package in
59
-   most distributions. Instead they ship a modified linux/futex.h that does
60
-   not include <linux/compile.h> and does not user __user.
61
-*/
62
-#ifndef __user
63
-#define __user
64
-#endif /* __user__*/
65
-/* end of hack */
66
-#include <linux/futex.h>
67
-#include <sys/syscall.h>
68
-#include <unistd.h>
69
-#include "compiler_opt.h"
70
-
71
-/* either syscall directly or #include <sys/linux/syscall.h> and use
72
- * sys_futex directly */
73
-#define sys_futex(addr, op, val, timeout, addr2, val3) \
74
-	syscall(__NR_futex , (addr), (op), (val), (timeout), (addr2), (val3))
75
-
76
-typedef atomic_t futex_lock_t;
77
-
78
-/* the mutex has 3 states: 0 - free/unlocked and nobody waiting
79
- *                         1 - locked and nobody waiting for it
80
- *                         2 - locked w/ 0 or more waiting processes/threads
81
- */
82
-
83
-inline static futex_lock_t* futex_init(futex_lock_t* lock)
84
-{
85
-	atomic_set(lock, 0);
86
-	return lock;
87
-}
88
-
89
-
90
-inline static void futex_get(futex_lock_t* lock)
91
-{
92
-	int v;
93
-#ifdef ADAPTIVE_WAIT
94
-	register int i=ADAPTIVE_WAIT_LOOPS;
95
-	
96
-retry:
97
-#endif
98
-	
99
-	v=atomic_cmpxchg(lock, 0, 1); /* lock if 0 */
100
-	if (likely(v==0)){  /* optimize for the uncontended case */
101
-		/* success */
102
-		membar_enter_lock();
103
-		return;
104
-	}else if (unlikely(v==2)){ /* if contended, optimize for the one waiter
105
-								case */
106
-		/* waiting processes/threads => add ourselves to the queue */
107
-		do{
108
-			sys_futex(&(lock)->val, FUTEX_WAIT, 2, 0, 0, 0);
109
-			v=atomic_get_and_set(lock, 2);
110
-		}while(v);
111
-	}else{
112
-		/* v==1 */
113
-#ifdef ADAPTIVE_WAIT
114
-		if (i>0){
115
-			i--;
116
-			goto retry;
117
-		}
118
-#endif
119
-		v=atomic_get_and_set(lock, 2);
120
-		while(v){
121
-			sys_futex(&(lock)->val, FUTEX_WAIT, 2, 0, 0, 0);
122
-			v=atomic_get_and_set(lock, 2);
123
-		}
124
-	}
125
-	membar_enter_lock();
126
-}
127
-
128
-
129
-inline static void futex_release(futex_lock_t* lock)
130
-{
131
-	int v;
132
-	
133
-	membar_leave_lock();
134
-	v=atomic_get_and_set(lock, 0);
135
-	if (unlikely(v==2)){ /* optimize for the uncontended case */
136
-		sys_futex(&(lock)->val, FUTEX_WAKE, 1, 0, 0, 0);
137
-	}
138
-}
139
-
140
-
141
-static inline int futex_try(futex_lock_t* lock)
142
-{
143
-	int c;
144
-	c=atomic_cmpxchg(lock, 0, 1);
145
-	if (likely(c==0))
146
-		membar_enter_lock();
147
-	return c;
148
-}
149
-
150
-
151
-#else /*HAVE_ASM_INLINE_ATOMIC_OPS*/
152
-#undef USE_FUTEX
153
-#endif /*HAVE_ASM_INLINE_ATOMIC_OPS*/
154
-
155
-#endif /* _futexlocks_h*/
Browse code

core/futex: fix bug in futex_try

atomic_cmpxchg returns the previous value. If the previous value was 0 then it was previously unlocked and we now have acquired the lock.

Thus, it should be c==0 that is considered as the case we enter the lock, not c!=0

As far as I can tell, lock_try (and lock_set_try) are currently no used anywhere in the code, so this fix shoudn't have any impact

tsearle authored on 25/02/2016 08:56:09
Showing 1 changed files
... ...
@@ -142,7 +142,7 @@ static inline int futex_try(futex_lock_t* lock)
142 142
 {
143 143
 	int c;
144 144
 	c=atomic_cmpxchg(lock, 0, 1);
145
-	if (likely(c))
145
+	if (likely(c==0))
146 146
 		membar_enter_lock();
147 147
 	return c;
148 148
 }
Browse code

core : Update include files - delete IDs, update doxygen, delete history

Olle E. Johansson authored on 03/01/2015 10:55:48
Showing 1 changed files
... ...
@@ -1,6 +1,4 @@
1 1
 /* 
2
- * $Id$
3
- * 
4 2
  * Copyright (C) 2007 iptelorg GmbH
5 3
  *
6 4
  * Permission to use, copy, modify, and distribute this software for any
... ...
@@ -15,7 +13,13 @@
15 13
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 14
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 15
  */
18
-/*
16
+/*!
17
+* \file
18
+* \brief Kamailio core :: locks
19
+* \author andrei
20
+* \ingroup core
21
+* Module: \ref core
22
+ *
19 23
  * futex based lock (mutex) implementation  (linux 2.6+ only)
20 24
  * based on Ulrich Drepper implementation in "Futexes Are Tricky"
21 25
  * (http://people.redhat.com/drepper/futex.pdf)
... ...
@@ -29,12 +33,6 @@
29 33
  *
30 34
  *  Config defines:
31 35
  */
32
-/* 
33
- * History:
34
- * --------
35
- *  2007-05-13  created by andrei
36
- *  2007-06-12  added ADAPTIVE_WAIT busy waiting (andrei)
37
- */
38 36
 
39 37
 #ifndef _futexlock_h
40 38
 #define _futexlock_h
Browse code

core: futexlock compilation workaround for OpenSuse 10.2

In some older linux distributions /usr/include/futex.h uses a __user
attribute, which is defined in linux/compiler.h.
However linux/compiler.h is not part of the kernel headers package in most
recent distributions. Instead they ship a modified linux/futex.h that
does not include <linux/compile.h> and does not use __user.
"__user" is used only for checking pointer address space in the kernel
(using the sparse tool and make C=1 or make C=2) so it's safe to define
it as null/empty.

Reported-by: Alexandr Dubovikov shurik start4 info

Andrei Pelinescu-Onciul authored on 14/06/2010 17:11:59
Showing 1 changed files
... ...
@@ -55,6 +55,16 @@
55 55
 typedef __u32 u32;
56 56
 struct task_struct;
57 57
 /* end of the hack */
58
+/* another hack this time for OpenSuse 10.2:
59
+   futex.h uses a __user attribute, which is defined in linux/compiler.h
60
+   However linux/compiler.h is not part of the kernel headers package in
61
+   most distributions. Instead they ship a modified linux/futex.h that does
62
+   not include <linux/compile.h> and does not user __user.
63
+*/
64
+#ifndef __user
65
+#define __user
66
+#endif /* __user__*/
67
+/* end of hack */
58 68
 #include <linux/futex.h>
59 69
 #include <sys/syscall.h>
60 70
 #include <unistd.h>
Browse code

core: futexlock include hack for older futex.h

Older non-fixed linux/futex.h version (<2.6.20) cannot be included from
userspace without additional type declarations (which otherwise
are included only if __KERNEL__ is defined).
Note that most distribution fix this problem by distributing a
modified /usr/include/linux/futex.h and not the default kernel
one. However there are other distributions (like CentOS 5) for
which this hack is needed.

Andrei Pelinescu-Onciul authored on 16/07/2009 12:05:50
Showing 1 changed files
... ...
@@ -49,6 +49,12 @@
49 49
                           between linux-libc-dev andlibc headers
50 50
                           in recent (6.08.2008) x86_64 debian sid
51 51
                           installations */
52
+/* hack to work with old linux/futex.h versions, that depend on sched.h in
53
+   __KERNEL__ mode (futex.h < 2.6.20) */
54
+#include <linux/types.h>
55
+typedef __u32 u32;
56
+struct task_struct;
57
+/* end of the hack */
52 58
 #include <linux/futex.h>
53 59
 #include <sys/syscall.h>
54 60
 #include <unistd.h>
Browse code

- workaround for type declaration conflicts between linux-libc-dev and libc headers on x86_64 debian (in one place declared as long int and in another as long long)

Andrei Pelinescu-Onciul authored on 07/08/2008 13:29:09
Showing 1 changed files
... ...
@@ -45,6 +45,10 @@
45 45
 
46 46
 #ifdef HAVE_ASM_INLINE_ATOMIC_OPS
47 47
 #define HAVE_FUTEX
48
+#include <sys/types.h> /* hack to workaround some type conflicts 
49
+                          between linux-libc-dev andlibc headers
50
+                          in recent (6.08.2008) x86_64 debian sid
51
+                          installations */
48 52
 #include <linux/futex.h>
49 53
 #include <sys/syscall.h>
50 54
 #include <unistd.h>
Browse code

- try first to spin a little and only if this fails go through the futex syscalls (uses ADAPTIVE_WAIT and ADAPTIVE_WAIT_LOOPS like FAST_LOCK). This gives a 5% performance increase compared to the "pure" futex version on my machine (for tm).

Andrei Pelinescu-Onciul authored on 12/06/2007 22:43:19
Showing 1 changed files
... ...
@@ -33,6 +33,7 @@
33 33
  * History:
34 34
  * --------
35 35
  *  2007-05-13  created by andrei
36
+ *  2007-06-12  added ADAPTIVE_WAIT busy waiting (andrei)
36 37
  */
37 38
 
38 39
 #ifndef _futexlock_h
... ...
@@ -49,7 +50,8 @@
49 50
 #include <unistd.h>
50 51
 #include "compiler_opt.h"
51 52
 
52
-
53
+/* either syscall directly or #include <sys/linux/syscall.h> and use
54
+ * sys_futex directly */
53 55
 #define sys_futex(addr, op, val, timeout, addr2, val3) \
54 56
 	syscall(__NR_futex , (addr), (op), (val), (timeout), (addr2), (val3))
55 57
 
... ...
@@ -70,13 +72,18 @@ inline static futex_lock_t* futex_init(futex_lock_t* lock)
70 72
 inline static void futex_get(futex_lock_t* lock)
71 73
 {
72 74
 	int v;
75
+#ifdef ADAPTIVE_WAIT
76
+	register int i=ADAPTIVE_WAIT_LOOPS;
77
+	
78
+retry:
79
+#endif
73 80
 	
74 81
 	v=atomic_cmpxchg(lock, 0, 1); /* lock if 0 */
75 82
 	if (likely(v==0)){  /* optimize for the uncontended case */
76 83
 		/* success */
77 84
 		membar_enter_lock();
78 85
 		return;
79
-	}else if (likely(v==2)){ /* if contended, optimize for the several waiters
86
+	}else if (unlikely(v==2)){ /* if contended, optimize for the one waiter
80 87
 								case */
81 88
 		/* waiting processes/threads => add ourselves to the queue */
82 89
 		do{
... ...
@@ -85,6 +92,12 @@ inline static void futex_get(futex_lock_t* lock)
85 92
 		}while(v);
86 93
 	}else{
87 94
 		/* v==1 */
95
+#ifdef ADAPTIVE_WAIT
96
+		if (i>0){
97
+			i--;
98
+			goto retry;
99
+		}
100
+#endif
88 101
 		v=atomic_get_and_set(lock, 2);
89 102
 		while(v){
90 103
 			sys_futex(&(lock)->val, FUTEX_WAIT, 2, 0, 0, 0);
Browse code

- added futex based locking, used by default on linux 2.5.70+ (to turn it off compile with make use_futex=no) - moved get_sys_ver from io_wait.c into ut.c

Andrei Pelinescu-Onciul authored on 14/05/2007 21:29:46
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,124 @@
1
+/* 
2
+ * $Id$
3
+ * 
4
+ * Copyright (C) 2007 iptelorg GmbH
5
+ *
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
9
+ *
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
+ */
18
+/*
19
+ * futex based lock (mutex) implementation  (linux 2.6+ only)
20
+ * based on Ulrich Drepper implementation in "Futexes Are Tricky"
21
+ * (http://people.redhat.com/drepper/futex.pdf)
22
+ *
23
+ * Implements:
24
+ *   void futex_get(futex_lock_t* lock);     - mutex lock
25
+ *   void futex_release(futex_lock_t* lock); - unlock
26
+ *   int  futex_try(futex_lock_t* lock);     - tries to get lock, returns 0
27
+ *                                              on success and !=0 on failure
28
+ *                                              (1 or 2)
29
+ *
30
+ *  Config defines:
31
+ */
32
+/* 
33
+ * History:
34
+ * --------
35
+ *  2007-05-13  created by andrei
36
+ */
37
+
38
+#ifndef _futexlock_h
39
+#define _futexlock_h
40
+
41
+
42
+#include "atomic/atomic_common.h"
43
+#include "atomic/atomic_native.h"
44
+
45
+#ifdef HAVE_ASM_INLINE_ATOMIC_OPS
46
+#define HAVE_FUTEX
47
+#include <linux/futex.h>
48
+#include <sys/syscall.h>
49
+#include <unistd.h>
50
+#include "compiler_opt.h"
51
+
52
+
53
+#define sys_futex(addr, op, val, timeout, addr2, val3) \
54
+	syscall(__NR_futex , (addr), (op), (val), (timeout), (addr2), (val3))
55
+
56
+typedef atomic_t futex_lock_t;
57
+
58
+/* the mutex has 3 states: 0 - free/unlocked and nobody waiting
59
+ *                         1 - locked and nobody waiting for it
60
+ *                         2 - locked w/ 0 or more waiting processes/threads
61
+ */
62
+
63
+inline static futex_lock_t* futex_init(futex_lock_t* lock)
64
+{
65
+	atomic_set(lock, 0);
66
+	return lock;
67
+}
68
+
69
+
70
+inline static void futex_get(futex_lock_t* lock)
71
+{
72
+	int v;
73
+	
74
+	v=atomic_cmpxchg(lock, 0, 1); /* lock if 0 */
75
+	if (likely(v==0)){  /* optimize for the uncontended case */
76
+		/* success */
77
+		membar_enter_lock();
78
+		return;
79
+	}else if (likely(v==2)){ /* if contended, optimize for the several waiters
80
+								case */
81
+		/* waiting processes/threads => add ourselves to the queue */
82
+		do{
83
+			sys_futex(&(lock)->val, FUTEX_WAIT, 2, 0, 0, 0);
84
+			v=atomic_get_and_set(lock, 2);
85
+		}while(v);
86
+	}else{
87
+		/* v==1 */
88
+		v=atomic_get_and_set(lock, 2);
89
+		while(v){
90
+			sys_futex(&(lock)->val, FUTEX_WAIT, 2, 0, 0, 0);
91
+			v=atomic_get_and_set(lock, 2);
92
+		}
93
+	}
94
+	membar_enter_lock();
95
+}
96
+
97
+
98
+inline static void futex_release(futex_lock_t* lock)
99
+{
100
+	int v;
101
+	
102
+	membar_leave_lock();
103
+	v=atomic_get_and_set(lock, 0);
104
+	if (unlikely(v==2)){ /* optimize for the uncontended case */
105
+		sys_futex(&(lock)->val, FUTEX_WAKE, 1, 0, 0, 0);
106
+	}
107
+}
108
+
109
+
110
+static inline int futex_try(futex_lock_t* lock)
111
+{
112
+	int c;
113
+	c=atomic_cmpxchg(lock, 0, 1);
114
+	if (likely(c))
115
+		membar_enter_lock();
116
+	return c;
117
+}
118
+
119
+
120
+#else /*HAVE_ASM_INLINE_ATOMIC_OPS*/
121
+#undef USE_FUTEX
122
+#endif /*HAVE_ASM_INLINE_ATOMIC_OPS*/
123
+
124
+#endif /* _futexlocks_h*/