summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Graf <tgraf@suug.ch>2012-11-15 21:30:57 +0100
committerThomas Graf <tgraf@suug.ch>2012-11-15 21:30:57 +0100
commit20efa14e5c188738c15a013e092af51b780fde61 (patch)
treec2c3ef38df936c557d78b9069d1ab4dab756eb47
parent3ed1f9ab5fba2b43fb3c67ac03e0da90ac042ed0 (diff)
downloadlibnl-20efa14e5c188738c15a013e092af51b780fde61.tar.gz
lock abstraction layer
Hide pthread availability and enablement behind internal API Signed-off-by: Thomas Graf <tgraf@suug.ch>
-rw-r--r--include/netlink-local.h50
-rw-r--r--lib/socket.c28
2 files changed, 56 insertions, 22 deletions
diff --git a/include/netlink-local.h b/include/netlink-local.h
index 01c611a..1f580c6 100644
--- a/include/netlink-local.h
+++ b/include/netlink-local.h
@@ -53,6 +53,10 @@
#include <linux/ipv6.h>
#include <linux/snmp.h>
+#ifndef DISABLE_PTHREADS
+#include <pthread.h>
+#endif
+
#include <netlink/netlink.h>
#include <netlink/handlers.h>
#include <netlink/cache.h>
@@ -210,4 +214,50 @@ static inline int build_sysconf_path(char **strp, const char *filename)
return asprintf(strp, "%s/%s", sysconfdir, filename);
}
+#ifndef DISABLE_PTHREADS
+#define NL_LOCK(NAME) pthread_mutex_t (NAME) = PTHREAD_MUTEX_INITIALIZER
+#define NL_RW_LOCK(NAME) pthread_rwlock_t (NAME) = PTHREAD_RWLOCK_INITIALIZER
+
+static inline void nl_lock(pthread_mutex_t *lock)
+{
+ pthread_mutex_lock(lock);
+}
+
+static inline void nl_unlock(pthread_mutex_t *lock)
+{
+ pthread_mutex_unlock(lock);
+}
+
+static inline void nl_read_lock(pthread_rwlock_t *lock)
+{
+ pthread_rwlock_rdlock(lock);
+}
+
+static inline void nl_read_unlock(pthread_rwlock_t *lock)
+{
+ pthread_rwlock_unlock(lock);
+}
+
+static inline void nl_write_lock(pthread_rwlock_t *lock)
+{
+ pthread_rwlock_wrlock(lock);
+}
+
+static inline void nl_write_unlock(pthread_rwlock_t *lock)
+{
+ pthread_rwlock_unlock(lock);
+}
+
+#else
+#define NL_LOCK(NAME) int __unused_lock_ ##NAME __attribute__((unused))
+#define NL_RW_LOCK(NAME) int __unused_lock_ ##NAME __attribute__((unused))
+
+#define nl_lock(LOCK) do { } while(0)
+#define nl_unlock(LOCK) do { } while(0)
+#define nl_read_lock(LOCK) do { } while(0)
+#define nl_read_unlock(LOCK) do { } while(0)
+#define nl_write_lock(LOCK) do { } while(0)
+#define nl_write_unlock(LOCK) do { } while(0)
+#endif
+
#endif
diff --git a/lib/socket.c b/lib/socket.c
index 0335f07..b30058b 100644
--- a/lib/socket.c
+++ b/lib/socket.c
@@ -29,10 +29,6 @@
#include "defs.h"
-#ifndef DISABLE_PTHREADS
-#include <pthread.h>
-#endif
-
#include <netlink-local.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
@@ -61,18 +57,14 @@ static void __init init_default_cb(void)
}
static uint32_t used_ports_map[32];
-#ifndef DISABLE_PTHREADS
-static pthread_mutex_t port_map_mutex = PTHREAD_MUTEX_INITIALIZER;
-#endif
+static NL_RW_LOCK(port_map_lock);
static uint32_t generate_local_port(void)
{
int i, n;
uint32_t pid = getpid() & 0x3FFFFF;
-#ifndef DISABLE_PTHREADS
- pthread_mutex_lock(&port_map_mutex);
-#endif
+ nl_write_lock(&port_map_lock);
for (i = 0; i < 32; i++) {
if (used_ports_map[i] == 0xFFFFFFFF)
@@ -88,17 +80,13 @@ static uint32_t generate_local_port(void)
/* PID_MAX_LIMIT is currently at 2^22, leaving 10 bit
* to, i.e. 1024 unique ports per application. */
-#ifndef DISABLE_PTHREADS
- pthread_mutex_unlock(&port_map_mutex);
-#endif
+ nl_write_unlock(&port_map_lock);
return pid + (n << 22);
}
}
-#ifndef DISABLE_PTHREADS
- pthread_mutex_unlock(&port_map_mutex);
-#endif
+ nl_write_unlock(&port_map_lock);
/* Out of sockets in our own PID namespace, what to do? FIXME */
return UINT_MAX;
@@ -113,13 +101,9 @@ static void release_local_port(uint32_t port)
nr = port >> 22;
-#ifndef DISABLE_PTHREADS
- pthread_mutex_lock(&port_map_mutex);
-#endif
+ nl_write_lock(&port_map_lock);
used_ports_map[nr / 32] &= ~(1 << (nr % 32));
-#ifndef DISABLE_PTHREADS
- pthread_mutex_unlock(&port_map_mutex);
-#endif
+ nl_write_unlock(&port_map_lock);
}
/**