Skip to content

Commit

Permalink
lock abstraction layer
Browse files Browse the repository at this point in the history
Hide pthread availability and enablement behind internal API

Signed-off-by: Thomas Graf <[email protected]>
(cherry picked from commit 20efa14)

Conflicts:
	include/netlink-local.h

Signed-off-by: Thomas Graf <[email protected]>
  • Loading branch information
tgraf committed Dec 20, 2012
1 parent dd7097b commit 3ddde65
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
50 changes: 50 additions & 0 deletions include/netlink-local.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
#include <linux/gen_stats.h>
#include <linux/ip_mp_alg.h>

#ifndef DISABLE_PTHREADS
#include <pthread.h>
#endif

#include <netlink/netlink.h>
#include <netlink/handlers.h>
#include <netlink/cache.h>
Expand Down Expand Up @@ -399,4 +403,50 @@ static inline char *nl_cache_name(struct nl_cache *cache)
END_OF_MSGTYPES_LIST, \
}

#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
28 changes: 6 additions & 22 deletions lib/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@

#include "defs.h"

#ifndef DISABLE_PTHREADS
#include <pthread.h>
#endif

#include <netlink-local.h>
#include <netlink/netlink.h>
#include <netlink/utils.h>
Expand Down Expand Up @@ -123,18 +119,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)
Expand All @@ -150,17 +142,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;
Expand All @@ -175,13 +163,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);
}

/**
Expand Down

0 comments on commit 3ddde65

Please sign in to comment.