/*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "netlink.h"
#include "netlink-protocol.h"
#include "ofpbuf.h"
+#include "ovs-thread.h"
#include "poll-loop.h"
#include "socket-util.h"
#include "util.h"
int
nl_sock_create(int protocol, struct nl_sock **sockp)
{
+ static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
struct nl_sock *sock;
struct sockaddr_nl local, remote;
socklen_t local_size;
int rcvbuf;
int retval = 0;
- if (!max_iovs) {
+ if (ovsthread_once_start(&once)) {
int save_errno = errno;
errno = 0;
}
errno = save_errno;
+ ovsthread_once_done(&once);
}
*sockp = NULL;
int n;
};
-static struct nl_pool pools[MAX_LINKS];
+static struct ovs_mutex pool_mutex = OVS_ADAPTIVE_MUTEX_INITIALIZER;
+static struct nl_pool pools[MAX_LINKS] OVS_GUARDED_BY(pool_mutex);
static int
nl_pool_alloc(int protocol, struct nl_sock **sockp)
{
+ struct nl_sock *sock = NULL;
struct nl_pool *pool;
ovs_assert(protocol >= 0 && protocol < ARRAY_SIZE(pools));
+ ovs_mutex_lock(&pool_mutex);
pool = &pools[protocol];
if (pool->n > 0) {
- *sockp = pool->socks[--pool->n];
+ sock = pool->socks[--pool->n];
+ }
+ ovs_mutex_unlock(&pool_mutex);
+
+ if (sock) {
+ *sockp = sock;
return 0;
} else {
return nl_sock_create(protocol, sockp);
if (sock) {
struct nl_pool *pool = &pools[sock->protocol];
+ ovs_mutex_lock(&pool_mutex);
if (pool->n < ARRAY_SIZE(pool->socks)) {
pool->socks[pool->n++] = sock;
- } else {
- nl_sock_destroy(sock);
+ sock = NULL;
}
+ ovs_mutex_unlock(&pool_mutex);
+
+ nl_sock_destroy(sock);
}
}