replaced most pthread_mutex/cond_t by wrapped mutex/condvar_t variant
authorMartin Willi <martin@strongswan.org>
Wed, 5 Nov 2008 11:29:56 +0000 (11:29 -0000)
committerMartin Willi <martin@strongswan.org>
Wed, 5 Nov 2008 11:29:56 +0000 (11:29 -0000)
13 files changed:
src/charon/config/peer_cfg.c
src/charon/network/sender.c
src/charon/plugins/kernel_netlink/kernel_netlink_ipsec.c
src/charon/plugins/kernel_netlink/kernel_netlink_net.c
src/charon/plugins/kernel_netlink/kernel_netlink_shared.c
src/charon/plugins/kernel_pfkey/kernel_pfkey_ipsec.c
src/charon/processing/jobs/callback_job.c
src/charon/processing/processor.c
src/charon/sa/authenticators/eap/eap_manager.c
src/charon/sa/connect_manager.c
src/charon/sa/ike_sa_manager.c
src/charon/sa/mediation_manager.c
src/libstrongswan/plugins/openssl/openssl_plugin.c

index 319be63..34e466b 100644 (file)
  */
 
 #include <string.h>
-#include <pthread.h>
 
 #include "peer_cfg.h"
 
+#include <utils/mutex.h>
 #include <utils/linked_list.h>
 #include <utils/identification.h>
 
@@ -77,7 +77,7 @@ struct private_peer_cfg_t {
        /**
         * mutex to lock access to list of child_cfgs
         */
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
        
        /**
         * id to use to identify us
@@ -197,9 +197,9 @@ static ike_cfg_t* get_ike_cfg(private_peer_cfg_t *this)
  */
 static void add_child_cfg(private_peer_cfg_t *this, child_cfg_t *child_cfg)
 {
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        this->child_cfgs->insert_last(this->child_cfgs, child_cfg);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -207,9 +207,9 @@ static void add_child_cfg(private_peer_cfg_t *this, child_cfg_t *child_cfg)
  */
 static void remove_child_cfg(private_peer_cfg_t *this, enumerator_t *enumerator)
 {
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        this->child_cfgs->remove_at(this->child_cfgs, enumerator);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -219,10 +219,10 @@ static enumerator_t* create_child_cfg_enumerator(private_peer_cfg_t *this)
 {
        enumerator_t *enumerator;
 
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        enumerator = this->child_cfgs->create_enumerator(this->child_cfgs);
        return enumerator_create_cleaner(enumerator,
-                                                                        (void*)pthread_mutex_unlock, &this->mutex);
+                                                                        (void*)this->mutex->unlock, this->mutex);
 }
 
 /**
@@ -480,6 +480,7 @@ static void destroy(private_peer_cfg_t *this)
                DESTROY_IF(this->mediated_by);
                DESTROY_IF(this->peer_id);
 #endif /* ME */
+               this->mutex->destroy(this->mutex);
                free(this->name);
                free(this->pool);
                free(this);
@@ -536,7 +537,7 @@ peer_cfg_t *peer_cfg_create(char *name, u_int ike_version, ike_cfg_t *ike_cfg,
        this->ike_version = ike_version;
        this->ike_cfg = ike_cfg;
        this->child_cfgs = linked_list_create();
-       pthread_mutex_init(&this->mutex, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
        this->my_id = my_id;
        this->other_id = other_id;
        this->cert_policy = cert_policy;
index 10576d3..388dbd8 100644 (file)
@@ -24,6 +24,7 @@
 #include <daemon.h>
 #include <network/socket.h>
 #include <processing/jobs/callback_job.h>
+#include <utils/mutex.h>
 
 
 typedef struct private_sender_t private_sender_t;
@@ -50,17 +51,17 @@ struct private_sender_t {
        /**
         * mutex to synchronize access to list
         */
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
 
        /**
         * condvar to signal for packets added to list
         */
-       pthread_cond_t gotone;
+       condvar_t *got;
        
        /**
         * condvar to signal for packets sent
         */
-       pthread_cond_t sentone;
+       condvar_t *sent;
 };
 
 /**
@@ -74,10 +75,10 @@ static void send_(private_sender_t *this, packet_t *packet)
        dst = packet->get_destination(packet);
        DBG1(DBG_NET, "sending packet: from %#H to %#H", src, dst);
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        this->list->insert_last(this->list, packet);
-       pthread_cond_signal(&this->gotone);
-       pthread_mutex_unlock(&this->mutex);
+       this->got->signal(this->got);
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -88,21 +89,21 @@ static job_requeue_t send_packets(private_sender_t * this)
        packet_t *packet;
        int oldstate;
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        while (this->list->get_count(this->list) == 0)
        {
                /* add cleanup handler, wait for packet, remove cleanup handler */
-               pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&this->mutex);
+               pthread_cleanup_push((void(*)(void*))this->mutex->unlock, this->mutex);
                pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
                
-               pthread_cond_wait(&this->gotone, &this->mutex);
+               this->got->wait(this->got, this->mutex);
                
                pthread_setcancelstate(oldstate, NULL);
                pthread_cleanup_pop(0);
        }
        this->list->remove_first(this->list, (void**)&packet);
-       pthread_cond_signal(&this->sentone);
-       pthread_mutex_unlock(&this->mutex);
+       this->sent->signal(this->sent);
+       this->mutex->unlock(this->mutex);
        
        charon->socket->send(charon->socket, packet);
        packet->destroy(packet);
@@ -115,13 +116,15 @@ static job_requeue_t send_packets(private_sender_t * this)
 static void destroy(private_sender_t *this)
 {
        /* send all packets in the queue */
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        while (this->list->get_count(this->list))
        {
-               pthread_cond_wait(&this->sentone, &this->mutex);
+               this->sent->wait(this->sent, this->mutex);
        }
-       pthread_mutex_unlock(&this->mutex);
-       pthread_mutex_destroy(&this->mutex);
+       this->mutex->unlock(this->mutex);
+       this->got->destroy(this->got);
+       this->sent->destroy(this->sent);
+       this->mutex->destroy(this->mutex);
        this->job->cancel(this->job);
        this->list->destroy(this->list);
        free(this);
@@ -133,19 +136,19 @@ static void destroy(private_sender_t *this)
 sender_t * sender_create()
 {
        private_sender_t *this = malloc_thing(private_sender_t);
-
+       
        this->public.send = (void(*)(sender_t*,packet_t*))send_;
        this->public.destroy = (void(*)(sender_t*)) destroy;
-
+       
        this->list = linked_list_create();
-       pthread_mutex_init(&this->mutex, NULL);
-       pthread_cond_init(&this->gotone, NULL);
-       pthread_cond_init(&this->sentone, NULL);
-
+       this->mutex = mutex_create(MUTEX_DEFAULT);
+       this->got = condvar_create(CONDVAR_DEFAULT);
+       this->sent = condvar_create(CONDVAR_DEFAULT);
+       
        this->job = callback_job_create((callback_job_cb_t)send_packets,
                                                                        this, NULL, NULL);
        charon->processor->queue_job(charon->processor, (job_t*)this->job);
-
+       
        return &this->public;
 }
 
index c53ce3c..4d0e67b 100644 (file)
@@ -37,6 +37,7 @@
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
+#include <utils/mutex.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/acquire_job.h>
@@ -252,7 +253,7 @@ struct private_kernel_netlink_ipsec_t {
        /**
         * mutex to lock access to various lists
         */
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
        
        /**
         * List of installed policies (policy_entry_t)
@@ -1374,7 +1375,7 @@ static status_t add_policy(private_kernel_netlink_ipsec_t *this,
        policy->direction = direction;
        
        /* find the policy, which matches EXACTLY */
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        iterator = this->policies->create_iterator(this->policies, TRUE);
        while (iterator->iterate(iterator, (void**)&current))
        {
@@ -1418,7 +1419,7 @@ static status_t add_policy(private_kernel_netlink_ipsec_t *this,
        policy_info->priority -= policy->sel.sport_mask ? 1 : 0;
        policy_info->action = XFRM_POLICY_ALLOW;
        policy_info->share = XFRM_SHARE_ANY;
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        /* policies don't expire */
        policy_info->lft.soft_byte_limit = XFRM_INF;
@@ -1634,7 +1635,7 @@ static status_t del_policy(private_kernel_netlink_ipsec_t *this,
        policy.direction = direction;
        
        /* find the policy */
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        enumerator = this->policies->create_enumerator(this->policies);
        while (enumerator->enumerate(enumerator, &current))
        {
@@ -1646,7 +1647,7 @@ static status_t del_policy(private_kernel_netlink_ipsec_t *this,
                        {
                                /* is used by more SAs, keep in kernel */
                                DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");
-                               pthread_mutex_unlock(&this->mutex);
+                               this->mutex->unlock(this->mutex);
                                enumerator->destroy(enumerator);
                                return SUCCESS;
                        }
@@ -1655,7 +1656,7 @@ static status_t del_policy(private_kernel_netlink_ipsec_t *this,
                        break;
                }
        }
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        enumerator->destroy(enumerator);
        if (!to_delete)
        {
@@ -1709,6 +1710,7 @@ static void destroy(private_kernel_netlink_ipsec_t *this)
        close(this->socket_xfrm_events);
        this->socket_xfrm->destroy(this->socket_xfrm);
        this->policies->destroy(this->policies);
+       this->mutex->destroy(this->mutex);
        free(this);
 }
 
@@ -1733,7 +1735,7 @@ kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
 
        /* private members */
        this->policies = linked_list_create();
-       pthread_mutex_init(&this->mutex, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
        this->install_routes = lib->settings->get_bool(lib->settings,
                                        "charon.install_routes", TRUE);
        
index af26038..6efba85 100644 (file)
@@ -29,6 +29,7 @@
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
+#include <utils/mutex.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/roam_job.h>
@@ -116,12 +117,12 @@ struct private_kernel_netlink_net_t {
        /**
         * mutex to lock access to various lists
         */
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
        
        /**
         * condition variable to signal virtual IP add/removal
         */
-       pthread_cond_t cond;
+       condvar_t *condvar;
        
        /**
         * Cached list of interfaces and its addresses (iface_entry_t)
@@ -253,7 +254,7 @@ static void process_link(private_kernel_netlink_net_t *this,
                name = "(unknown)";
        }
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        switch (hdr->nlmsg_type)
        {
                case RTM_NEWLINK:
@@ -315,7 +316,7 @@ static void process_link(private_kernel_netlink_net_t *this,
                        break;
                }
        }
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        /* send an update to all IKE_SAs */
        if (update && event)
@@ -373,7 +374,7 @@ static void process_addr(private_kernel_netlink_net_t *this,
                return;
        }
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        ifaces = this->ifaces->create_enumerator(this->ifaces);
        while (ifaces->enumerate(ifaces, &iface))
        {
@@ -431,7 +432,7 @@ static void process_addr(private_kernel_netlink_net_t *this,
                }
        }
        ifaces->destroy(ifaces);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        host->destroy(host);
        
        /* send an update to all IKE_SAs */
@@ -470,10 +471,12 @@ static void process_route(private_kernel_netlink_net_t *this, struct nlmsghdr *h
        }
        if (host)
        {
+               this->mutex->lock(this->mutex);
                if (!get_vip_refcount(this, host))
                {       /* ignore routes added for virtual IPs */
                        fire_roam_job(this, FALSE);
                }
+               this->mutex->unlock(this->mutex);
                host->destroy(host);
        }
 }
@@ -524,12 +527,12 @@ static job_requeue_t receive_events(private_kernel_netlink_net_t *this)
                        case RTM_NEWADDR:
                        case RTM_DELADDR:
                                process_addr(this, hdr, TRUE);
-                               pthread_cond_broadcast(&this->cond);
+                               this->condvar->broadcast(this->condvar);
                                break;
                        case RTM_NEWLINK:
                        case RTM_DELLINK:
                                process_link(this, hdr, TRUE);
-                               pthread_cond_broadcast(&this->cond);
+                               this->condvar->broadcast(this->condvar);
                                break;
                        case RTM_NEWROUTE:
                        case RTM_DELROUTE:
@@ -560,7 +563,7 @@ typedef struct {
  */
 static void address_enumerator_destroy(address_enumerator_t *data)
 {
-       pthread_mutex_unlock(&data->this->mutex);
+       data->this->mutex->unlock(data->this->mutex);
        free(data);
 }
 
@@ -614,7 +617,7 @@ static enumerator_t *create_address_enumerator(private_kernel_netlink_net_t *thi
        data->include_down_ifaces = include_down_ifaces;
        data->include_virtual_ips = include_virtual_ips;
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        return enumerator_create_nested(
                                enumerator_create_filter(this->ifaces->create_enumerator(this->ifaces),
                                                        (void*)filter_interfaces, data, NULL),
@@ -633,7 +636,7 @@ static char *get_interface_name(private_kernel_netlink_net_t *this, host_t* ip)
        
        DBG2(DBG_KNL, "getting interface name for %H", ip);
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        ifaces = this->ifaces->create_enumerator(this->ifaces);
        while (ifaces->enumerate(ifaces, &iface))
        {
@@ -653,7 +656,7 @@ static char *get_interface_name(private_kernel_netlink_net_t *this, host_t* ip)
                }
        }
        ifaces->destroy(ifaces);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        if (name)
        {
@@ -677,7 +680,7 @@ static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
        
        DBG2(DBG_KNL, "getting iface index for %s", name);
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        ifaces = this->ifaces->create_enumerator(this->ifaces);
        while (ifaces->enumerate(ifaces, &iface))
        {
@@ -688,7 +691,7 @@ static int get_interface_index(private_kernel_netlink_net_t *this, char* name)
                }
        }
        ifaces->destroy(ifaces);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
 
        if (ifindex == 0)
        {
@@ -769,6 +772,7 @@ static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
                DBG1(DBG_KNL, "getting address to %H failed", dest);
                return NULL;
        }
+       this->mutex->lock(this->mutex);
        current = out;
        while (NLMSG_OK(current, len))
        {
@@ -846,7 +850,6 @@ static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
                                        else
                                        {
                                                /* no source addr, get one from the interfaces */
-                                               pthread_mutex_lock(&this->mutex);
                                                ifaces = this->ifaces->create_enumerator(this->ifaces);
                                                while (ifaces->enumerate(ifaces, &iface))
                                                {
@@ -870,7 +873,6 @@ static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
                                                        }
                                                }
                                                ifaces->destroy(ifaces);
-                                               pthread_mutex_unlock(&this->mutex);
                                        }
                                }
                                /* FALL through */
@@ -882,6 +884,7 @@ static host_t *get_route(private_kernel_netlink_net_t *this, host_t *dest,
                break;
        }
        free(out);
+       this->mutex->unlock(this->mutex);
        
        if (nexthop)
        {
@@ -957,7 +960,7 @@ static status_t add_ip(private_kernel_netlink_net_t *this,
 
        DBG2(DBG_KNL, "adding virtual IP %H", virtual_ip);
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        ifaces = this->ifaces->create_enumerator(this->ifaces);
        while (ifaces->enumerate(ifaces, &iface))
        {
@@ -977,7 +980,7 @@ static status_t add_ip(private_kernel_netlink_net_t *this,
                                         virtual_ip, iface->ifname);
                                addrs->destroy(addrs);
                                ifaces->destroy(ifaces);
-                               pthread_mutex_unlock(&this->mutex);
+                               this->mutex->unlock(this->mutex);
                                return SUCCESS;
                        }
                }
@@ -998,20 +1001,20 @@ static status_t add_ip(private_kernel_netlink_net_t *this,
                        {
                                while (get_vip_refcount(this, virtual_ip) == 0)
                                {       /* wait until address appears */
-                                       pthread_cond_wait(&this->cond, &this->mutex);
+                                       this->condvar->wait(this->condvar, this->mutex);
                                }
                                ifaces->destroy(ifaces);
-                               pthread_mutex_unlock(&this->mutex);
+                               this->mutex->unlock(this->mutex);
                                return SUCCESS;
                        }
                        ifaces->destroy(ifaces);
-                       pthread_mutex_unlock(&this->mutex);
+                       this->mutex->unlock(this->mutex);
                        DBG1(DBG_KNL, "adding virtual IP %H failed", virtual_ip);
                        return FAILED;
                }
        }
        ifaces->destroy(ifaces);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        DBG1(DBG_KNL, "interface address %H not found, unable to install"
                 "virtual IP %H", iface_ip, virtual_ip);
@@ -1031,7 +1034,7 @@ static status_t del_ip(private_kernel_netlink_net_t *this, host_t *virtual_ip)
 
        DBG2(DBG_KNL, "deleting virtual IP %H", virtual_ip);
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        ifaces = this->ifaces->create_enumerator(this->ifaces);
        while (ifaces->enumerate(ifaces, &iface))
        {
@@ -1049,12 +1052,12 @@ static status_t del_ip(private_kernel_netlink_net_t *this, host_t *virtual_ip)
                                        {       /* wait until the address is really gone */
                                                while (get_vip_refcount(this, virtual_ip) > 0)
                                                {
-                                                       pthread_cond_wait(&this->cond, &this->mutex);
+                                                       this->condvar->wait(this->condvar, this->mutex);
                                                }
                                        }
                                        addrs->destroy(addrs);
                                        ifaces->destroy(ifaces);
-                                       pthread_mutex_unlock(&this->mutex);
+                                       this->mutex->unlock(this->mutex);
                                        return status;
                                }
                                else
@@ -1065,14 +1068,14 @@ static status_t del_ip(private_kernel_netlink_net_t *this, host_t *virtual_ip)
                                         virtual_ip);
                                addrs->destroy(addrs);
                                ifaces->destroy(ifaces);
-                               pthread_mutex_unlock(&this->mutex);
+                               this->mutex->unlock(this->mutex);
                                return SUCCESS;
                        }
                }
                addrs->destroy(addrs);
        }
        ifaces->destroy(ifaces);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        DBG2(DBG_KNL, "virtual IP %H not cached, unable to delete", virtual_ip);
        return FAILED;
@@ -1232,7 +1235,7 @@ static status_t init_address_list(private_kernel_netlink_net_t *this)
        }
        free(out);
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        ifaces = this->ifaces->create_enumerator(this->ifaces);
        while (ifaces->enumerate(ifaces, &iface))
        {
@@ -1248,7 +1251,7 @@ static status_t init_address_list(private_kernel_netlink_net_t *this)
                }
        }
        ifaces->destroy(ifaces);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        return SUCCESS;
 }
 
@@ -1301,6 +1304,8 @@ static void destroy(private_kernel_netlink_net_t *this)
        close(this->socket_events);
        this->socket->destroy(this->socket);
        this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
+       this->condvar->destroy(this->condvar);
+       this->mutex->destroy(this->mutex);
        free(this);
 }
 
@@ -1325,8 +1330,8 @@ kernel_netlink_net_t *kernel_netlink_net_create()
 
        /* private members */
        this->ifaces = linked_list_create();
-       pthread_mutex_init(&this->mutex, NULL);
-       pthread_cond_init(&this->cond, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
+       this->condvar = condvar_create(CONDVAR_DEFAULT);
        timerclear(&this->last_roam);
        this->routing_table = lib->settings->get_int(lib->settings,
                                        "charon.routing_table", IPSEC_ROUTING_TABLE);
index f4af941..c371596 100644 (file)
 #include <linux/rtnetlink.h>
 #include <errno.h>
 #include <unistd.h>
-#include <pthread.h>
 
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
+#include <utils/mutex.h>
 
 typedef struct private_netlink_socket_t private_netlink_socket_t;
 
@@ -40,7 +40,7 @@ struct private_netlink_socket_t {
        /**
         * mutex to lock access to netlink socket
         */
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
 
        /**
         * current sequence number for netlink request
@@ -64,7 +64,7 @@ static status_t netlink_send(private_netlink_socket_t *this, struct nlmsghdr *in
        chunk_t result = chunk_empty, tmp;
        struct nlmsghdr *msg, peek;
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        
        in->nlmsg_seq = ++this->seq;
        in->nlmsg_pid = getpid();
@@ -86,7 +86,7 @@ static status_t netlink_send(private_netlink_socket_t *this, struct nlmsghdr *in
                                /* interrupted, try again */
                                continue;
                        }
-                       pthread_mutex_unlock(&this->mutex);
+                       this->mutex->unlock(this->mutex);
                        DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
                        return FAILED;
                }
@@ -118,14 +118,14 @@ static status_t netlink_send(private_netlink_socket_t *this, struct nlmsghdr *in
                                continue;
                        }
                        DBG1(DBG_KNL, "error reading from netlink socket: %s", strerror(errno));
-                       pthread_mutex_unlock(&this->mutex);
+                       this->mutex->unlock(this->mutex);
                        free(result.ptr);
                        return FAILED;
                }
                if (!NLMSG_OK(msg, len))
                {
                        DBG1(DBG_KNL, "received corrupted netlink message");
-                       pthread_mutex_unlock(&this->mutex);
+                       this->mutex->unlock(this->mutex);
                        free(result.ptr);
                        return FAILED;
                }
@@ -136,7 +136,7 @@ static status_t netlink_send(private_netlink_socket_t *this, struct nlmsghdr *in
                        {
                                continue;
                        }
-                       pthread_mutex_unlock(&this->mutex);
+                       this->mutex->unlock(this->mutex);
                        free(result.ptr);
                        return FAILED;
                }
@@ -162,7 +162,7 @@ static status_t netlink_send(private_netlink_socket_t *this, struct nlmsghdr *in
        *out_len = result.len;
        *out = (struct nlmsghdr*)result.ptr;
        
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        return SUCCESS;
 }
@@ -222,6 +222,7 @@ static status_t netlink_send_ack(private_netlink_socket_t *this, struct nlmsghdr
 static void destroy(private_netlink_socket_t *this)
 {
        close(this->socket);
+       this->mutex->destroy(this->mutex);
        free(this);
 }
 
@@ -239,7 +240,7 @@ netlink_socket_t *netlink_socket_create(int protocol) {
 
        /* private members */
        this->seq = 200;
-       pthread_mutex_init(&this->mutex, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
        
        memset(&addr, 0, sizeof(addr));
        addr.nl_family = AF_NETLINK;
index de6927b..560654e 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <daemon.h>
 #include <utils/host.h>
+#include <utils/mutex.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/acquire_job.h>
 #include <processing/jobs/migrate_job.h>
@@ -83,7 +84,7 @@ struct private_kernel_pfkey_ipsec_t
        /**
         * mutex to lock access to various lists
         */
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
        
        /**
         * List of installed policies (policy_entry_t)
@@ -103,21 +104,18 @@ struct private_kernel_pfkey_ipsec_t
        /**
         * mutex to lock access to the PF_KEY socket
         */
-       pthread_mutex_t mutex_pfkey;
-       
+       mutex_t *mutex_pfkey;
        
        /**
         * PF_KEY socket to communicate with the kernel
         */
        int socket;
        
-       
        /**
         * PF_KEY socket to receive acquire and expire events
         */
        int socket_events;
        
-       
        /**
         * sequence number for messages sent to the kernel
         */
@@ -635,7 +633,7 @@ static status_t pfkey_send_socket(private_kernel_pfkey_ipsec_t *this, int socket
        struct sadb_msg *msg;
        int in_len, len;
        
-       pthread_mutex_lock(&this->mutex_pfkey);
+       this->mutex_pfkey->lock(this->mutex_pfkey);
 
        in->sadb_msg_seq = ++this->seq;
        in->sadb_msg_pid = getpid();
@@ -653,7 +651,7 @@ static status_t pfkey_send_socket(private_kernel_pfkey_ipsec_t *this, int socket
                                /* interrupted, try again */
                                continue;
                        }
-                       pthread_mutex_unlock(&this->mutex_pfkey);
+                       this->mutex_pfkey->unlock(this->mutex_pfkey);
                        DBG1(DBG_KNL, "error sending to PF_KEY socket: %s", strerror(errno));
                        return FAILED;
                }
@@ -675,20 +673,20 @@ static status_t pfkey_send_socket(private_kernel_pfkey_ipsec_t *this, int socket
                                continue;
                        }
                        DBG1(DBG_KNL, "error reading from PF_KEY socket: %s", strerror(errno));
-                       pthread_mutex_unlock(&this->mutex_pfkey);
+                       this->mutex_pfkey->unlock(this->mutex_pfkey);
                        return FAILED;
                }
                if (len < sizeof(struct sadb_msg) ||
                        msg->sadb_msg_len < PFKEY_LEN(sizeof(struct sadb_msg)))
                {
                        DBG1(DBG_KNL, "received corrupted PF_KEY message");
-                       pthread_mutex_unlock(&this->mutex_pfkey);
+                       this->mutex_pfkey->unlock(this->mutex_pfkey);
                        return FAILED;
                }
                if (msg->sadb_msg_len > len / PFKEY_ALIGNMENT)
                {
                        DBG1(DBG_KNL, "buffer was too small to receive the complete PF_KEY message");
-                       pthread_mutex_unlock(&this->mutex_pfkey);
+                       this->mutex_pfkey->unlock(this->mutex_pfkey);
                        return FAILED;
                }
                if (msg->sadb_msg_pid != in->sadb_msg_pid)
@@ -704,7 +702,7 @@ static status_t pfkey_send_socket(private_kernel_pfkey_ipsec_t *this, int socket
                        {
                                continue;
                        }
-                       pthread_mutex_unlock(&this->mutex_pfkey);
+                       this->mutex_pfkey->unlock(this->mutex_pfkey);
                        return FAILED;
                }
                if (msg->sadb_msg_type != in->sadb_msg_type)
@@ -720,7 +718,7 @@ static status_t pfkey_send_socket(private_kernel_pfkey_ipsec_t *this, int socket
        *out = (struct sadb_msg*)malloc(len);
        memcpy(*out, buf, len);
                
-       pthread_mutex_unlock(&this->mutex_pfkey);
+       this->mutex_pfkey->unlock(this->mutex_pfkey);
        
        return SUCCESS;
 }
@@ -764,7 +762,7 @@ static void process_acquire(private_kernel_pfkey_ipsec_t *this, struct sadb_msg*
        }
        
        index = response.x_policy->sadb_x_policy_id;
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        if (this->policies->find_first(this->policies,
                        (linked_list_match_t)policy_entry_match_byindex, (void**)&policy, &index) == SUCCESS)
        {
@@ -777,7 +775,7 @@ static void process_acquire(private_kernel_pfkey_ipsec_t *this, struct sadb_msg*
        }
        src_ts = sadb_address2ts(response.src);
        dst_ts = sadb_address2ts(response.dst);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        DBG1(DBG_KNL, "creating acquire job for policy %R === %R with reqid {%u}",
                                   src_ts, dst_ts, reqid);
@@ -1428,7 +1426,7 @@ static status_t add_policy(private_kernel_pfkey_ipsec_t *this,
        policy = create_policy_entry(src_ts, dst_ts, direction, reqid);
        
        /* find a matching policy */
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        if (this->policies->find_first(this->policies,
                        (linked_list_match_t)policy_entry_equals, (void**)&found, policy) == SUCCESS)
        {
@@ -1507,7 +1505,7 @@ static status_t add_policy(private_kernel_pfkey_ipsec_t *this,
        host2ext(policy->dst.net, addr);
        PFKEY_EXT_ADD(msg, addr);
        
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        if (pfkey_send(this, msg, &out, &len) != SUCCESS)
        {
@@ -1531,14 +1529,14 @@ static status_t add_policy(private_kernel_pfkey_ipsec_t *this,
                return FAILED;
        }
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        
        /* we try to find the policy again and update the kernel index */
        if (this->policies->find_last(this->policies, NULL, (void**)&policy) != SUCCESS)
        {
                DBG2(DBG_KNL, "unable to update index, the policy %R === %R %N is "
                                "already gone, ignoring", src_ts, dst_ts, policy_dir_names, direction);
-               pthread_mutex_unlock(&this->mutex);
+               this->mutex->unlock(this->mutex);
                free(out);
                return SUCCESS;
        }
@@ -1593,7 +1591,7 @@ static status_t add_policy(private_kernel_pfkey_ipsec_t *this,
                }
        }       
        
-       pthread_mutex_unlock(&this->mutex);     
+       this->mutex->unlock(this->mutex);       
        
        return SUCCESS;
 }
@@ -1621,14 +1619,14 @@ static status_t query_policy(private_kernel_pfkey_ipsec_t *this,
        policy = create_policy_entry(src_ts, dst_ts, direction, 0);
        
        /* find a matching policy */
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        if (this->policies->find_first(this->policies,
                        (linked_list_match_t)policy_entry_equals, (void**)&found, policy) != SUCCESS)
        {
                DBG1(DBG_KNL, "querying policy %R === %R %N failed, not found", src_ts,
                                           dst_ts, policy_dir_names, direction);
                policy_entry_destroy(policy);
-               pthread_mutex_unlock(&this->mutex);
+               this->mutex->unlock(this->mutex);
                return NOT_FOUND;
        }
        policy_entry_destroy(policy);
@@ -1664,7 +1662,7 @@ static status_t query_policy(private_kernel_pfkey_ipsec_t *this,
        host2ext(policy->dst.net, addr);
        PFKEY_EXT_ADD(msg, addr);
        
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        if (pfkey_send(this, msg, &out, &len) != SUCCESS)
        {
@@ -1718,7 +1716,7 @@ static status_t del_policy(private_kernel_pfkey_ipsec_t *this,
        policy = create_policy_entry(src_ts, dst_ts, direction, 0);
        
        /* find a matching policy */
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        if (this->policies->find_first(this->policies,
                        (linked_list_match_t)policy_entry_equals, (void**)&found, policy) == SUCCESS)
        {
@@ -1727,7 +1725,7 @@ static status_t del_policy(private_kernel_pfkey_ipsec_t *this,
                        /* is used by more SAs, keep in kernel */
                        DBG2(DBG_KNL, "policy still used by another CHILD_SA, not removed");    
                        policy_entry_destroy(policy);
-                       pthread_mutex_unlock(&this->mutex);
+                       this->mutex->unlock(this->mutex);
                        return SUCCESS;
                }
                /* remove if last reference */
@@ -1740,10 +1738,10 @@ static status_t del_policy(private_kernel_pfkey_ipsec_t *this,
                DBG1(DBG_KNL, "deleting policy %R === %R %N failed, not found", src_ts,
                                           dst_ts, policy_dir_names, direction);
                policy_entry_destroy(policy);
-               pthread_mutex_unlock(&this->mutex);
+               this->mutex->unlock(this->mutex);
                return NOT_FOUND;
        }
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
                
        memset(&request, 0, sizeof(request));
        
@@ -1852,6 +1850,8 @@ static void destroy(private_kernel_pfkey_ipsec_t *this)
        close(this->socket);
        close(this->socket_events);
        this->policies->destroy_function(this->policies, (void*)policy_entry_destroy);
+       this->mutex->destroy(this->mutex);
+       this->mutex_pfkey->destroy(this->mutex_pfkey);
        free(this);
 }
 
@@ -1876,9 +1876,10 @@ kernel_pfkey_ipsec_t *kernel_pfkey_ipsec_create()
 
        /* private members */
        this->policies = linked_list_create();
-       pthread_mutex_init(&this->mutex, NULL);
-       this->install_routes = lib->settings->get_bool(lib->settings, "charon.install_routes", TRUE);
-       pthread_mutex_init(&this->mutex_pfkey, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
+       this->mutex_pfkey = mutex_create(MUTEX_DEFAULT);
+       this->install_routes = lib->settings->get_bool(lib->settings,
+                                                                                               "charon.install_routes", TRUE);
        this->seq = 0;
        
        /* create a PF_KEY socket to communicate with the kernel */
index 9cc4eea..b380945 100644 (file)
@@ -20,6 +20,7 @@
 #include <pthread.h>
 
 #include <daemon.h>
+#include <utils/mutex.h>
 
 typedef struct private_callback_job_t private_callback_job_t;
 
@@ -51,12 +52,12 @@ struct private_callback_job_t {
         * thread ID of the job, if running
         */
        pthread_t thread;
-
+       
        /**
         * mutex to access jobs interna
         */
-       pthread_mutex_t mutex;
-
+       mutex_t *mutex;
+       
        /**
         * list of asociated child jobs
         */
@@ -78,6 +79,7 @@ static void destroy(private_callback_job_t *this)
                this->cleanup(this->data);
        }
        this->children->destroy(this->children);
+       this->mutex->destroy(this->mutex);
        free(this);
 }
 
@@ -91,7 +93,7 @@ static void unregister(private_callback_job_t *this)
                iterator_t *iterator;
                private_callback_job_t *child;
                
-               pthread_mutex_lock(&this->parent->mutex);
+               this->parent->mutex->lock(this->parent->mutex);
                iterator = this->parent->children->create_iterator(this->parent->children, TRUE);
                while (iterator->iterate(iterator, (void**)&child))
                {
@@ -102,7 +104,7 @@ static void unregister(private_callback_job_t *this)
                        }
                }
                iterator->destroy(iterator);
-               pthread_mutex_unlock(&this->parent->mutex);
+               this->parent->mutex->unlock(this->parent->mutex);
        }
 }
 
@@ -113,12 +115,12 @@ static void cancel(private_callback_job_t *this)
 {
        pthread_t thread;
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        thread = this->thread;
        
        /* terminate its children */
        this->children->invoke_offset(this->children, offsetof(callback_job_t, cancel));
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        /* terminate thread */
        if (thread)
@@ -135,9 +137,9 @@ static void execute(private_callback_job_t *this)
 {
        bool cleanup = FALSE;
 
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        this->thread = pthread_self();
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        
        pthread_cleanup_push((void*)destroy, this);
        while (TRUE)
@@ -182,7 +184,7 @@ callback_job_t *callback_job_create(callback_job_cb_t cb, void *data,
        this->public.cancel = (void(*)(callback_job_t*))cancel;
 
        /* private variables */
-       pthread_mutex_init(&this->mutex, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
        this->callback = cb;
        this->data = data;
        this->cleanup = cleanup;
@@ -193,9 +195,9 @@ callback_job_t *callback_job_create(callback_job_cb_t cb, void *data,
        /* register us at parent */
        if (parent)
        {
-               pthread_mutex_lock(&this->parent->mutex);
+               this->parent->mutex->lock(this->parent->mutex);
                this->parent->children->insert_last(this->parent->children, this);
-               pthread_mutex_unlock(&this->parent->mutex);
+               this->parent->mutex->unlock(this->parent->mutex);
        }
        
        return &this->public;
index 336a28b..a5f1833 100644 (file)
@@ -24,6 +24,7 @@
 #include "processor.h"
 
 #include <daemon.h>
+#include <utils/mutex.h>
 #include <utils/linked_list.h>
 
 
@@ -61,17 +62,17 @@ struct private_processor_t {
        /**
         * access to linked_list is locked through this mutex
         */
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
 
        /**
         * Condvar to wait for new jobs
         */
-       pthread_cond_t jobadded;
+       condvar_t *job_added;
        
        /**
         * Condvar to wait for terminated threads
         */
-       pthread_cond_t threadterminated;
+       condvar_t *thread_terminated;
 };
 
 static void process_jobs(private_processor_t *this);
@@ -85,10 +86,10 @@ static void restart(private_processor_t *this)
        
        if (pthread_create(&thread, NULL, (void*)process_jobs, this) != 0)
        {
-               pthread_mutex_lock(&this->mutex);
+               this->mutex->lock(this->mutex);
                this->total_threads--;
-               pthread_cond_broadcast(&this->threadterminated);
-               pthread_mutex_unlock(&this->mutex);
+               this->thread_terminated->broadcast(this->thread_terminated);
+               this->mutex->unlock(this->mutex);
        }
 }
 
@@ -103,7 +104,7 @@ static void process_jobs(private_processor_t *this)
        
        DBG2(DBG_JOB, "started worker thread, thread_ID: %06u", (int)pthread_self());
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        while (this->desired_threads >= this->total_threads)
        {
                job_t *job;
@@ -111,21 +112,21 @@ static void process_jobs(private_processor_t *this)
                if (this->list->get_count(this->list) == 0)
                {
                        this->idle_threads++;
-                       pthread_cond_wait(&this->jobadded, &this->mutex);
+                       this->job_added->wait(this->job_added, this->mutex);
                        this->idle_threads--;
                        continue;
                }
                this->list->remove_first(this->list, (void**)&job);
-               pthread_mutex_unlock(&this->mutex);
+               this->mutex->unlock(this->mutex);
                /* terminated threads are restarted, so we have a constant pool */
                pthread_cleanup_push((void*)restart, this);
                job->execute(job);
                pthread_cleanup_pop(0);
-               pthread_mutex_lock(&this->mutex);
+               this->mutex->lock(this->mutex);
        }
        this->total_threads--;
-       pthread_cond_signal(&this->threadterminated);
-       pthread_mutex_unlock(&this->mutex);
+       this->thread_terminated->signal(this->thread_terminated);
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -134,9 +135,9 @@ static void process_jobs(private_processor_t *this)
 static u_int get_total_threads(private_processor_t *this)
 {
        u_int count;
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        count = this->total_threads; 
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        return count;
 }
 
@@ -146,9 +147,9 @@ static u_int get_total_threads(private_processor_t *this)
 static u_int get_idle_threads(private_processor_t *this)
 {
        u_int count;
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        count = this->idle_threads;
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        return count;
 }
 
@@ -158,9 +159,9 @@ static u_int get_idle_threads(private_processor_t *this)
 static u_int get_job_load(private_processor_t *this)
 {
        u_int load;
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        load = this->list->get_count(this->list);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        return load;
 }
 
@@ -169,10 +170,10 @@ static u_int get_job_load(private_processor_t *this)
  */
 static void queue_job(private_processor_t *this, job_t *job)
 {
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        this->list->insert_last(this->list, job);
-       pthread_cond_signal(&this->jobadded);
-       pthread_mutex_unlock(&this->mutex);
+       this->job_added->signal(this->job_added);
+       this->mutex->unlock(this->mutex);
 }
        
 /**
@@ -180,7 +181,7 @@ static void queue_job(private_processor_t *this, job_t *job)
  */
 static void set_threads(private_processor_t *this, u_int count)
 {
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        if (count > this->total_threads)
        {       /* increase thread count */
                int i;
@@ -200,8 +201,8 @@ static void set_threads(private_processor_t *this, u_int count)
        {       /* decrease thread count */
                this->desired_threads = count;
        }
-       pthread_cond_broadcast(&this->jobadded);
-       pthread_mutex_unlock(&this->mutex);
+       this->job_added->broadcast(this->job_added);
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -210,13 +211,16 @@ static void set_threads(private_processor_t *this, u_int count)
 static void destroy(private_processor_t *this)
 {
        set_threads(this, 0);
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        while (this->total_threads > 0)
        {
-               pthread_cond_broadcast(&this->jobadded);
-               pthread_cond_wait(&this->threadterminated, &this->mutex);
+               this->job_added->broadcast(this->job_added);
+               this->thread_terminated->wait(this->thread_terminated, this->mutex);
        }
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
+       this->thread_terminated->destroy(this->thread_terminated);
+       this->job_added->destroy(this->job_added);
+       this->mutex->destroy(this->mutex);
        this->list->destroy_offset(this->list, offsetof(job_t, destroy));
        free(this);
 }
@@ -236,9 +240,9 @@ processor_t *processor_create(size_t pool_size)
        this->public.destroy = (void(*)(processor_t*))destroy;
        
        this->list = linked_list_create();
-       pthread_mutex_init(&this->mutex, NULL);
-       pthread_cond_init(&this->jobadded, NULL);
-       pthread_cond_init(&this->threadterminated, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
+       this->job_added = condvar_create(CONDVAR_DEFAULT);
+       this->thread_terminated = condvar_create(CONDVAR_DEFAULT);
        this->total_threads = 0;
        this->desired_threads = 0;
        this->idle_threads = 0;
index 8a4d4ee..578d302 100644 (file)
@@ -17,9 +17,8 @@
 
 #include "eap_manager.h"
 
-#include <pthread.h>
-
 #include <utils/linked_list.h>
+#include <utils/mutex.h>
 
 typedef struct private_eap_manager_t private_eap_manager_t;
 typedef struct eap_entry_t eap_entry_t;
@@ -68,7 +67,7 @@ struct private_eap_manager_t {
        /**
         * mutex to lock methods
         */
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
 };
 
 /**
@@ -85,9 +84,9 @@ static void add_method(private_eap_manager_t *this, eap_type_t type,
        entry->role = role;
        entry->constructor = constructor;
 
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        this->methods->insert_last(this->methods, entry);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -98,7 +97,7 @@ static void remove_method(private_eap_manager_t *this, eap_constructor_t constru
        enumerator_t *enumerator;
        eap_entry_t *entry;
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        enumerator = this->methods->create_enumerator(this->methods);
        while (enumerator->enumerate(enumerator, &entry))
        {
@@ -109,7 +108,7 @@ static void remove_method(private_eap_manager_t *this, eap_constructor_t constru
                }
        }
        enumerator->destroy(enumerator);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -124,7 +123,7 @@ static eap_method_t* create_instance(private_eap_manager_t *this,
        eap_entry_t *entry;
        eap_method_t *method = NULL;
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        enumerator = this->methods->create_enumerator(this->methods);
        while (enumerator->enumerate(enumerator, &entry))
        {
@@ -139,7 +138,7 @@ static eap_method_t* create_instance(private_eap_manager_t *this,
                }
        }
        enumerator->destroy(enumerator);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        return method;
 }
 
@@ -149,6 +148,7 @@ static eap_method_t* create_instance(private_eap_manager_t *this,
 static void destroy(private_eap_manager_t *this)
 {
        this->methods->destroy_function(this->methods, free);
+       this->mutex->destroy(this->mutex);
        free(this);
 }
 
@@ -165,7 +165,7 @@ eap_manager_t *eap_manager_create()
        this->public.destroy = (void(*)(eap_manager_t*))destroy;
        
        this->methods = linked_list_create();
-       pthread_mutex_init(&this->mutex, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
        
        return &this->public;
 }
index ef54b83..fa5824b 100644 (file)
 
 #include "connect_manager.h"
 
-#include <pthread.h>
 #include <math.h>
 
 #include <daemon.h>
+#include <utils/mutex.h>
 #include <utils/linked_list.h>
 #include <crypto/hashers/hasher.h>
 
@@ -59,7 +59,7 @@ struct private_connect_manager_t {
         /**
          * Lock for exclusivly accessing the manager.
          */
-        pthread_mutex_t mutex;
+        mutex_t *mutex;
         
         /**
          * Hasher to generate signatures
@@ -845,20 +845,20 @@ static job_requeue_t initiator_finish(callback_data_t *data)
 {
        private_connect_manager_t *this = data->connect_manager;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        check_list_t *checklist;
        if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
        {
                DBG1(DBG_IKE, "checklist with id '%#B' not found, can't finish connectivity checks",
                                &data->connect_id);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return JOB_REQUEUE_NONE;
        }
        
        finish_checks(this, checklist);
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        return JOB_REQUEUE_NONE;
 }
@@ -929,14 +929,14 @@ static job_requeue_t retransmit(callback_data_t *data)
 {
        private_connect_manager_t *this = data->connect_manager;
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        check_list_t *checklist;
        if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
        {
                DBG1(DBG_IKE, "checklist with id '%#B' not found, can't retransmit connectivity check",
                                &data->connect_id);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return JOB_REQUEUE_NONE;
        }
        
@@ -980,7 +980,7 @@ retransmit_end:
                        break;
        }
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        /* we reschedule it manually */
        return JOB_REQUEUE_NONE;
@@ -1078,14 +1078,14 @@ static job_requeue_t sender(callback_data_t *data)
 {
        private_connect_manager_t *this = data->connect_manager;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        check_list_t *checklist;
        if (get_checklist_by_id(this, data->connect_id, &checklist) != SUCCESS)
        {
                DBG1(DBG_IKE, "checklist with id '%#B' not found, can't send connectivity check",
                                &data->connect_id);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return JOB_REQUEUE_NONE;
        }
        
@@ -1100,7 +1100,7 @@ static job_requeue_t sender(callback_data_t *data)
                if (checklist->pairs->find_first(checklist->pairs,
                                (linked_list_match_t)match_waiting_pair, (void**)&pair) != SUCCESS)
                {
-                       pthread_mutex_unlock(&(this->mutex));
+                       this->mutex->unlock(this->mutex);
                        DBG1(DBG_IKE, "no pairs in waiting state, aborting");
                        return JOB_REQUEUE_NONE;
                }
@@ -1126,7 +1126,7 @@ static job_requeue_t sender(callback_data_t *data)
        /* schedule this job again */
        schedule_checks(this, checklist, ME_INTERVAL);
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        /* we reschedule it manually */
        return JOB_REQUEUE_NONE;
@@ -1345,7 +1345,7 @@ static void process_check(private_connect_manager_t *this, message_t *message)
                return;
        }
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        check_list_t *checklist;
        if (get_checklist_by_id(this, check->connect_id, &checklist) != SUCCESS)
@@ -1353,7 +1353,7 @@ static void process_check(private_connect_manager_t *this, message_t *message)
                DBG1(DBG_IKE, "checklist with id '%#B' not found",
                                &check->connect_id);
                check_destroy(check);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return;
        }
        
@@ -1363,7 +1363,7 @@ static void process_check(private_connect_manager_t *this, message_t *message)
                DBG1(DBG_IKE, "connectivity check verification failed");
                check_destroy(check);
                chunk_free(&sig);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return;
        }
        chunk_free(&sig);
@@ -1377,7 +1377,7 @@ static void process_check(private_connect_manager_t *this, message_t *message)
                process_response(this, check, checklist);
        }
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        check_destroy(check);
 }
@@ -1392,7 +1392,7 @@ static bool check_and_register(private_connect_manager_t *this,
        initiated_t *initiated;
        bool already_there = TRUE;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        if (get_initiated_by_ids(this, id, peer_id, &initiated) != SUCCESS)
        {
@@ -1408,7 +1408,7 @@ static bool check_and_register(private_connect_manager_t *this,
                initiated->mediated->insert_last(initiated->mediated, mediated_sa->clone(mediated_sa));
        }
 
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
 
        return already_there;
 }
@@ -1421,12 +1421,12 @@ static void check_and_initiate(private_connect_manager_t *this, ike_sa_id_t *med
 {
        initiated_t *initiated;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        if (get_initiated_by_ids(this, id, peer_id, &initiated) != SUCCESS)
        {
                DBG2(DBG_IKE, "no waiting mediated connections with '%D'", peer_id);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return;
        }
        
@@ -1439,7 +1439,7 @@ static void check_and_initiate(private_connect_manager_t *this, ike_sa_id_t *med
        }
        iterator->destroy(iterator);
 
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -1451,20 +1451,20 @@ static status_t set_initiator_data(private_connect_manager_t *this,
 {
        check_list_t *checklist;
        
-       pthread_mutex_lock(&(this->mutex));     
+       this->mutex->lock(this->mutex); 
        
        if (get_checklist_by_id(this, connect_id, NULL) == SUCCESS)
        {
                DBG1(DBG_IKE, "checklist with id '%#B' already exists, aborting",
                                &connect_id);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return FAILED;
        }
        
        checklist = check_list_create(initiator, responder, connect_id, key, endpoints, is_initiator);
        this->checklists->insert_last(this->checklists, checklist);
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        return SUCCESS;
 }
@@ -1477,13 +1477,13 @@ static status_t set_responder_data(private_connect_manager_t *this,
 {
        check_list_t *checklist;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        if (get_checklist_by_id(this, connect_id, &checklist) != SUCCESS)
        {
                DBG1(DBG_IKE, "checklist with id '%#B' not found",
                                &connect_id);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return NOT_FOUND;
        }
        
@@ -1496,7 +1496,7 @@ static status_t set_responder_data(private_connect_manager_t *this,
        /* send the first check immediately */
        schedule_checks(this, checklist, 0);
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        return SUCCESS;
 }
@@ -1508,13 +1508,13 @@ static status_t stop_checks(private_connect_manager_t *this, chunk_t connect_id)
 {
        check_list_t *checklist;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        if (get_checklist_by_id(this, connect_id, &checklist) != SUCCESS)
        {
                DBG1(DBG_IKE, "checklist with id '%#B' not found",
                                &connect_id);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return NOT_FOUND;
        }
        
@@ -1523,7 +1523,7 @@ static status_t stop_checks(private_connect_manager_t *this, chunk_t connect_id)
        remove_checklist(this, checklist);
        check_list_destroy(checklist);
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        return SUCCESS;
 }
@@ -1533,14 +1533,14 @@ static status_t stop_checks(private_connect_manager_t *this, chunk_t connect_id)
  */
 static void destroy(private_connect_manager_t *this)
 {
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        this->hasher->destroy(this->hasher);
        this->checklists->destroy_function(this->checklists, (void*)check_list_destroy);
        this->initiated->destroy_function(this->initiated, (void*)initiated_destroy);
        
-       pthread_mutex_unlock(&(this->mutex));   
-       pthread_mutex_destroy(&(this->mutex));
+       this->mutex->unlock(this->mutex);       
+       this->mutex->destroy(this->mutex);
        free(this);
 }
 
@@ -1570,7 +1570,7 @@ connect_manager_t *connect_manager_create()
        this->checklists = linked_list_create();
        this->initiated = linked_list_create();
        
-       pthread_mutex_init(&(this->mutex), NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
        
        return (connect_manager_t*)this;
 }
index eaf4bed..dd214fe 100644 (file)
@@ -16,7 +16,6 @@
  * $Id$
  */
 
-#include <pthread.h>
 #include <string.h>
 
 #include "ike_sa_manager.h"
@@ -24,6 +23,7 @@
 #include <daemon.h>
 #include <sa/ike_sa_id.h>
 #include <bus/bus.h>
+#include <utils/mutex.h>
 #include <utils/linked_list.h>
 #include <crypto/hashers/hasher.h>
 
@@ -42,7 +42,7 @@ struct entry_t {
        /**
         * Condvar where threads can wait until ike_sa_t object is free for use again.
         */
-       pthread_cond_t condvar;
+       condvar_t *condvar;
        
        /**
         * Is this ike_sa currently checked out?
@@ -107,6 +107,7 @@ static status_t entry_destroy(entry_t *this)
        DESTROY_IF(this->other);
        DESTROY_IF(this->my_id);
        DESTROY_IF(this->other_id);
+       this->condvar->destroy(this->condvar);
        free(this);
        return SUCCESS;
 }
@@ -119,7 +120,7 @@ static entry_t *entry_create(ike_sa_id_t *ike_sa_id)
        entry_t *this = malloc_thing(entry_t);
        
        this->waiting_threads = 0;
-       pthread_cond_init(&this->condvar, NULL);
+       this->condvar = condvar_create(CONDVAR_DEFAULT);
        
        /* we set checkout flag when we really give it out */
        this->checked_out = FALSE;
@@ -155,7 +156,7 @@ struct private_ike_sa_manager_t {
         /**
          * Lock for exclusivly accessing the manager.
          */
-        pthread_mutex_t mutex;
+        mutex_t *mutex;
 
         /**
          * Linked list with entries for the ike_sa_t objects.
@@ -278,9 +279,9 @@ static status_t delete_entry(private_ike_sa_manager_t *this, entry_t *entry)
                        while (entry->waiting_threads)
                        {
                                /* wake up all */
-                               pthread_cond_broadcast(&(entry->condvar));
+                               entry->condvar->broadcast(entry->condvar);
                                /* they will wake us again when their work is done */
-                               pthread_cond_wait(&(entry->condvar), &(this->mutex));
+                               entry->condvar->wait(entry->condvar, this->mutex);
                        }
                        
                        DBG2(DBG_MGR,  "found entry by pointer, deleting it");
@@ -310,14 +311,14 @@ static bool wait_for_entry(private_ike_sa_manager_t *this, entry_t *entry)
                /* so wait until we can get it for us.
                 * we register us as waiting. */
                entry->waiting_threads++;
-               pthread_cond_wait(&(entry->condvar), &(this->mutex));
+               entry->condvar->wait(entry->condvar, this->mutex);
                entry->waiting_threads--;
        }
        /* hm, a deletion request forbids us to get this SA, get next one */
        if (entry->driveout_waiting_threads)
        {
                /* we must signal here, others may be waiting on it, too */
-               pthread_cond_signal(&(entry->condvar));
+               entry->condvar->signal(entry->condvar);
                return FALSE;
        }
        return TRUE;
@@ -345,7 +346,7 @@ static ike_sa_t* checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id
        DBG2(DBG_MGR, "checkout IKE_SA, %d IKE_SAs in manager",
                 this->ike_sa_list->get_count(this->ike_sa_list));
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        if (get_entry_by_id(this, ike_sa_id, &entry) == SUCCESS)
        {
                if (wait_for_entry(this, entry))
@@ -355,7 +356,7 @@ static ike_sa_t* checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id
                        ike_sa = entry->ike_sa;
                }
        }
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        charon->bus->set_sa(charon->bus, ike_sa);
        return ike_sa;
 }
@@ -378,10 +379,10 @@ static ike_sa_t *checkout_new(private_ike_sa_manager_t* this, bool initiator)
        }
        entry = entry_create(id);
        id->destroy(id);
-       pthread_mutex_lock(&this->mutex);       
+       this->mutex->lock(this->mutex); 
        this->ike_sa_list->insert_last(this->ike_sa_list, entry);
        entry->checked_out = TRUE;
-       pthread_mutex_unlock(&this->mutex);     
+       this->mutex->unlock(this->mutex);       
        DBG2(DBG_MGR, "created IKE_SA, %d IKE_SAs in manager",
                 this->ike_sa_list->get_count(this->ike_sa_list));
        return entry->ike_sa;
@@ -413,7 +414,7 @@ static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
                this->hasher->allocate_hash(this->hasher, data, &hash);
                chunk_free(&data);
                
-               pthread_mutex_lock(&this->mutex);
+               this->mutex->lock(this->mutex);
                enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
                while (enumerator->enumerate(enumerator, &entry))
                {
@@ -422,7 +423,7 @@ static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
                                if (entry->message_id == 0)
                                {
                                        enumerator->destroy(enumerator);
-                                       pthread_mutex_unlock(&this->mutex);
+                                       this->mutex->unlock(this->mutex);
                                        chunk_free(&hash);
                                        id->destroy(id);
                                        DBG1(DBG_MGR, "ignoring IKE_SA_INIT, already processing");
@@ -439,7 +440,7 @@ static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
                        }
                }
                enumerator->destroy(enumerator);
-               pthread_mutex_unlock(&this->mutex);
+               this->mutex->unlock(this->mutex);
                
                if (ike_sa == NULL)
                {
@@ -450,11 +451,11 @@ static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
                                id->set_responder_spi(id, get_next_spi(this));
                                entry = entry_create(id);
                                
-                               pthread_mutex_lock(&this->mutex);
+                               this->mutex->lock(this->mutex);
                                this->ike_sa_list->insert_last(this->ike_sa_list, entry);
                                entry->checked_out = TRUE;
                                entry->message_id = message->get_message_id(message);
-                               pthread_mutex_unlock(&this->mutex);
+                               this->mutex->unlock(this->mutex);
                                entry->init_hash = hash;
                                ike_sa = entry->ike_sa;
                        }
@@ -473,7 +474,7 @@ static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
                return ike_sa;
        }
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        if (get_entry_by_id(this, id, &entry) == SUCCESS)
        {
                /* only check out if we are not processing this request */
@@ -496,7 +497,7 @@ static ike_sa_t* checkout_by_message(private_ike_sa_manager_t* this,
                        ike_sa = entry->ike_sa;
                }
        }
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        id->destroy(id);
        charon->bus->set_sa(charon->bus, ike_sa);
        return ike_sa;
@@ -521,7 +522,7 @@ static ike_sa_t* checkout_by_config(private_ike_sa_manager_t *this,
        my_host = host_create_from_dns(ike_cfg->get_my_addr(ike_cfg), 0, 0);
        other_host = host_create_from_dns(ike_cfg->get_other_addr(ike_cfg), 0, 0);
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        if (my_host && other_host && this->reuse_ikesa)
        {
@@ -604,7 +605,7 @@ static ike_sa_t* checkout_by_config(private_ike_sa_manager_t *this,
                new_entry->checked_out = TRUE;
                ike_sa = new_entry->ike_sa;
        }
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        charon->bus->set_sa(charon->bus, ike_sa);
        return ike_sa;
 }
@@ -621,7 +622,7 @@ static ike_sa_t* checkout_by_id(private_ike_sa_manager_t *this, u_int32_t id,
        ike_sa_t *ike_sa = NULL;
        child_sa_t *child_sa;
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
        while (enumerator->enumerate(enumerator, &entry))
@@ -658,7 +659,7 @@ static ike_sa_t* checkout_by_id(private_ike_sa_manager_t *this, u_int32_t id,
                }
        }
        enumerator->destroy(enumerator);
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        charon->bus->set_sa(charon->bus, ike_sa);
        return ike_sa;
@@ -676,7 +677,7 @@ static ike_sa_t* checkout_by_name(private_ike_sa_manager_t *this, char *name,
        ike_sa_t *ike_sa = NULL;
        child_sa_t *child_sa;
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
        while (enumerator->enumerate(enumerator, &entry))
@@ -713,7 +714,7 @@ static ike_sa_t* checkout_by_name(private_ike_sa_manager_t *this, char *name,
                }
        }
        enumerator->destroy(enumerator);
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        charon->bus->set_sa(charon->bus, ike_sa);
        return ike_sa;
@@ -733,7 +734,7 @@ static ike_sa_t* checkout_duplicate(private_ike_sa_manager_t *this,
        me = ike_sa->get_my_id(ike_sa);
        other = ike_sa->get_other_id(ike_sa);
        
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
        while (enumerator->enumerate(enumerator, &entry))
        {
@@ -756,7 +757,7 @@ static ike_sa_t* checkout_duplicate(private_ike_sa_manager_t *this,
                }
        }
        enumerator->destroy(enumerator);
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
        return duplicate;
 }
 
@@ -765,7 +766,7 @@ static ike_sa_t* checkout_duplicate(private_ike_sa_manager_t *this,
  */
 static void enumerator_unlock(private_ike_sa_manager_t *this)
 {
-       pthread_mutex_unlock(&this->mutex);
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -787,7 +788,7 @@ static bool enumerator_filter(private_ike_sa_manager_t *this,
  */
 static enumerator_t *create_enumerator(private_ike_sa_manager_t* this)
 {
-       pthread_mutex_lock(&this->mutex);
+       this->mutex->lock(this->mutex);
        return enumerator_create_filter(
                                                this->ike_sa_list->create_enumerator(this->ike_sa_list),
                                                (void*)enumerator_filter, this, (void*)enumerator_unlock);
@@ -813,7 +814,7 @@ static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
        
        DBG2(DBG_MGR, "checkin IKE_SA");
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        /* look for the entry */
        if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
@@ -846,7 +847,7 @@ static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
                        entry->other_id = other_id->clone(other_id);
                }
                DBG2(DBG_MGR, "check-in of IKE_SA successful.");
-               pthread_cond_signal(&(entry->condvar));
+               entry->condvar->signal(entry->condvar);
                retval = SUCCESS;
        }
        else
@@ -858,7 +859,7 @@ static status_t checkin(private_ike_sa_manager_t *this, ike_sa_t *ike_sa)
        
        DBG2(DBG_MGR, "%d IKE_SAs in manager now",
                 this->ike_sa_list->get_count(this->ike_sa_list));
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        
        charon->bus->set_sa(charon->bus, NULL);
        return retval;
@@ -882,7 +883,7 @@ static status_t checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ik
        ike_sa_id = ike_sa->get_id(ike_sa);
        DBG2(DBG_MGR, "checkin and destroy IKE_SA");
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        if (get_entry_by_sa(this, ike_sa, &entry) == SUCCESS)
        {
@@ -901,7 +902,7 @@ static status_t checkin_and_destroy(private_ike_sa_manager_t *this, ike_sa_t *ik
        }
        charon->bus->set_sa(charon->bus, NULL);
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        return retval;
 }
 
@@ -914,7 +915,7 @@ static int get_half_open_count(private_ike_sa_manager_t *this, host_t *ip)
        entry_t *entry;
        int count = 0;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        enumerator = this->ike_sa_list->create_enumerator(this->ike_sa_list);
        while (enumerator->enumerate(enumerator, &entry))
        {
@@ -938,7 +939,7 @@ static int get_half_open_count(private_ike_sa_manager_t *this, host_t *ip)
        }
        enumerator->destroy(enumerator);
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
        return count;
 }
 
@@ -951,7 +952,7 @@ static void flush(private_ike_sa_manager_t *this)
        enumerator_t *enumerator;
        entry_t *entry;
        
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        DBG2(DBG_MGR, "going to destroy IKE_SA manager and all managed IKE_SA's");
        /* Step 1: drive out all waiting threads  */
        DBG2(DBG_MGR, "set driveout flags for all stored IKE_SA's");
@@ -971,9 +972,9 @@ static void flush(private_ike_sa_manager_t *this)
                while (entry->waiting_threads)
                {
                        /* wake up all */
-                       pthread_cond_broadcast(&(entry->condvar));
+                       entry->condvar->broadcast(entry->condvar);
                        /* go sleeping until they are gone */
-                       pthread_cond_wait(&(entry->condvar), &(this->mutex));
+                       entry->condvar->wait(entry->condvar, this->mutex);
                }
        }
        enumerator->destroy(enumerator);
@@ -996,7 +997,7 @@ static void flush(private_ike_sa_manager_t *this)
                entry_destroy(entry);
        }
        charon->bus->set_sa(charon->bus, NULL);
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -1007,7 +1008,7 @@ static void destroy(private_ike_sa_manager_t *this)
        this->ike_sa_list->destroy(this->ike_sa_list);
        this->rng->destroy(this->rng);
        this->hasher->destroy(this->hasher);
-       
+       this->mutex->destroy(this->mutex);
        free(this);
 }
 
@@ -1050,7 +1051,7 @@ ike_sa_manager_t *ike_sa_manager_create()
                return NULL;
        }
        this->ike_sa_list = linked_list_create();
-       pthread_mutex_init(&this->mutex, NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
        this->reuse_ikesa = lib->settings->get_bool(lib->settings,
                                                                                                "charon.reuse_ikesa", TRUE);
        return &this->public;
index 3bc6ede..fbdf3ad 100644 (file)
@@ -17,8 +17,8 @@
 
 #include "mediation_manager.h"
 
-#include <pthread.h>
 #include <daemon.h>
+#include <utils/mutex.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/mediation_job.h>
 
@@ -80,7 +80,7 @@ struct private_mediation_manager_t {
         /**
          * Lock for exclusivly accessing the manager.
          */
-        pthread_mutex_t mutex;
+        mutex_t *mutex;
 
         /**
          * Linked list with state entries.
@@ -182,7 +182,7 @@ static void remove_sa(private_mediation_manager_t *this, ike_sa_id_t *ike_sa_id)
        iterator_t *iterator;
        peer_t *peer;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        iterator = this->peers->create_iterator(this->peers, TRUE);
        while (iterator->iterate(iterator, (void**)&peer))
@@ -199,7 +199,7 @@ static void remove_sa(private_mediation_manager_t *this, ike_sa_id_t *ike_sa_id)
        }
        iterator->destroy(iterator);
 
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -211,7 +211,7 @@ static void update_sa_id(private_mediation_manager_t *this, identification_t *pe
        peer_t *peer;
        bool found = FALSE;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        iterator = this->peers->create_iterator(this->peers, TRUE);
        while (iterator->iterate(iterator, (void**)&peer))
@@ -244,7 +244,7 @@ static void update_sa_id(private_mediation_manager_t *this, identification_t *pe
                requester->destroy(requester);
        }
        
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
 }
 
 /**
@@ -256,17 +256,17 @@ static ike_sa_id_t *check(private_mediation_manager_t *this,
        peer_t *peer;
        ike_sa_id_t *ike_sa_id;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        if (get_peer_by_id(this, peer_id, &peer) != SUCCESS)
        {
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return NULL;
        }
 
        ike_sa_id = peer->ike_sa_id;
 
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
 
        return ike_sa_id;
 }
@@ -280,7 +280,7 @@ static ike_sa_id_t *check_and_register(private_mediation_manager_t *this,
        peer_t *peer;
        ike_sa_id_t *ike_sa_id;
 
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
 
        if (get_peer_by_id(this, peer_id, &peer) != SUCCESS)
        {
@@ -294,13 +294,13 @@ static ike_sa_id_t *check_and_register(private_mediation_manager_t *this,
                /* the peer is not online */
                DBG2(DBG_IKE, "requested peer '%D' is offline, registering peer '%D'", peer_id, requester);
                register_peer(peer, requester);
-               pthread_mutex_unlock(&(this->mutex));
+               this->mutex->unlock(this->mutex);
                return NULL;
        }
 
        ike_sa_id = peer->ike_sa_id;
 
-       pthread_mutex_unlock(&(this->mutex));
+       this->mutex->unlock(this->mutex);
 
        return ike_sa_id;
 }
@@ -310,12 +310,12 @@ static ike_sa_id_t *check_and_register(private_mediation_manager_t *this,
  */
 static void destroy(private_mediation_manager_t *this)
 {
-       pthread_mutex_lock(&(this->mutex));
+       this->mutex->lock(this->mutex);
        
        this->peers->destroy_function(this->peers, (void*)peer_destroy);
        
-       pthread_mutex_unlock(&(this->mutex));   
-       pthread_mutex_destroy(&(this->mutex));
+       this->mutex->unlock(this->mutex);
+       this->mutex->destroy(this->mutex);
        free(this);
 }
 
@@ -333,7 +333,7 @@ mediation_manager_t *mediation_manager_create()
        this->public.check_and_register = (ike_sa_id_t*(*)(mediation_manager_t*,identification_t*,identification_t*))check_and_register;
        
        this->peers = linked_list_create();
-       pthread_mutex_init(&(this->mutex), NULL);
+       this->mutex = mutex_create(MUTEX_DEFAULT);
        
        return (mediation_manager_t*)this;
 }
index 39c4af9..744b6e8 100644 (file)
@@ -24,6 +24,7 @@
 #include "openssl_plugin.h"
 
 #include <library.h>
+#include <utils/mutex.h>
 #include "openssl_crypter.h"
 #include "openssl_hasher.h"
 #include "openssl_diffie_hellman.h"
@@ -49,7 +50,7 @@ struct private_openssl_plugin_t {
 /**
  * Array of static mutexs, with CRYPTO_num_locks() mutex
  */
-static pthread_mutex_t *mutex;
+static mutex_t **mutex;
 
 /**
  * Locking callback for static locks
@@ -58,11 +59,11 @@ static void locking_function(int mode, int type, const char *file, int line)
 {
        if (mode & CRYPTO_LOCK)
        {
-               pthread_mutex_lock(&mutex[type]);
+               mutex[type]->lock(mutex[type]);
        }
        else
        {
-               pthread_mutex_unlock(&mutex[type]);
+               mutex[type]->unlock(mutex[type]);
        }
 }
 
@@ -70,7 +71,7 @@ static void locking_function(int mode, int type, const char *file, int line)
  * Implementation of dynlock
  */
 struct CRYPTO_dynlock_value {
-       pthread_mutex_t mutex;
+       mutex_t *mutex;
 };
 
 /**
@@ -81,7 +82,7 @@ static struct CRYPTO_dynlock_value *create_function(const char *file, int line)
        struct CRYPTO_dynlock_value *lock;
        
        lock = malloc_thing(struct CRYPTO_dynlock_value);
-       pthread_mutex_init(&lock->mutex, NULL);
+       lock->mutex = mutex_create(MUTEX_DEFAULT);
        return lock;
 }
 
@@ -93,11 +94,11 @@ static void lock_function(int mode, struct CRYPTO_dynlock_value *lock,
 {
        if (mode & CRYPTO_LOCK)
        {
-               pthread_mutex_lock(&lock->mutex);
+               lock->mutex->lock(lock->mutex);
        }
        else
        {
-               pthread_mutex_unlock(&lock->mutex);
+               lock->mutex->unlock(lock->mutex);
        }
 }
 
@@ -107,7 +108,7 @@ static void lock_function(int mode, struct CRYPTO_dynlock_value *lock,
 static void destroy_function(struct CRYPTO_dynlock_value *lock,
                                                         const char *file, int line)
 {
-       pthread_mutex_destroy(&lock->mutex);
+       lock->mutex->destroy(lock->mutex);
        free(lock);
 }
 
@@ -134,10 +135,10 @@ static void threading_init()
        CRYPTO_set_dynlock_destroy_callback(destroy_function);
        
        num_locks = CRYPTO_num_locks();
-       mutex = malloc(sizeof(pthread_mutex_t) * num_locks);
+       mutex = malloc(sizeof(mutex_t*) * num_locks);
        for (i = 0; i < num_locks; i++)
        {
-               pthread_mutex_init(&mutex[i], NULL);
+               mutex[i] = mutex_create(MUTEX_DEFAULT);
        }
 }
 
@@ -151,7 +152,7 @@ static void threading_cleanup()
        num_locks = CRYPTO_num_locks();
        for (i = 0; i < num_locks; i++)
        {
-               pthread_mutex_destroy(&mutex[i]);
+               mutex[i]->destroy(mutex[i]);
        }
        free(mutex);
 }
@@ -181,8 +182,6 @@ static void destroy(private_openssl_plugin_t *this)
        ENGINE_cleanup();
        EVP_cleanup();
        
-       threading_cleanup();
-       
        free(this);
 }