Moved mutex.c to a separate folder in order to cleanly wrap other threading primitive...
authorTobias Brunner <tobias@strongswan.org>
Mon, 7 Dec 2009 14:56:04 +0000 (15:56 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 23 Dec 2009 16:00:58 +0000 (17:00 +0100)
48 files changed:
src/charon/bus/bus.c
src/charon/config/backend_manager.c
src/charon/config/peer_cfg.c
src/charon/credentials/credential_manager.c
src/charon/credentials/sets/cert_cache.c
src/charon/network/sender.c
src/charon/plugins/eap_radius/radius_client.c
src/charon/plugins/eap_sim_file/eap_sim_file_triplets.c
src/charon/plugins/kernel_klips/kernel_klips_ipsec.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/plugins/kernel_pfroute/kernel_pfroute_net.c
src/charon/plugins/load_tester/load_tester_plugin.c
src/charon/plugins/nm/nm_creds.c
src/charon/plugins/resolve/resolve_handler.c
src/charon/plugins/stroke/stroke_attribute.c
src/charon/plugins/stroke/stroke_ca.c
src/charon/plugins/stroke/stroke_config.c
src/charon/plugins/stroke/stroke_cred.c
src/charon/plugins/stroke/stroke_socket.c
src/charon/plugins/unit_tester/tests/test_mutex.c
src/charon/processing/jobs/callback_job.c
src/charon/processing/processor.c
src/charon/processing/scheduler.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/charon/sa/trap_manager.c
src/libfast/dispatcher.c
src/libstrongswan/Makefile.am
src/libstrongswan/attributes/attribute_manager.c
src/libstrongswan/credentials/credential_factory.c
src/libstrongswan/credentials/keys/key_encoding.c
src/libstrongswan/crypto/crypto_factory.c
src/libstrongswan/database/database_factory.c
src/libstrongswan/fetcher/fetcher_manager.c
src/libstrongswan/library.h
src/libstrongswan/plugins/gcrypt/gcrypt_plugin.c
src/libstrongswan/plugins/mysql/mysql_database.c
src/libstrongswan/plugins/openssl/openssl_plugin.c
src/libstrongswan/plugins/sqlite/sqlite_database.c
src/libstrongswan/threading.h [new file with mode: 0644]
src/libstrongswan/threading/mutex.c [new file with mode: 0644]
src/libstrongswan/utils/mutex.c [deleted file]
src/libstrongswan/utils/mutex.h [deleted file]

index 1dd3aee..c218423 100644 (file)
@@ -19,7 +19,7 @@
 #include <stdint.h>
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 ENUM(debug_names, DBG_DMN, DBG_LIB,
        "DMN",
index 0665d8e..dabae34 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <daemon.h>
 #include <utils/linked_list.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 
 typedef struct private_backend_manager_t private_backend_manager_t;
index df946b7..cf5d006 100644 (file)
@@ -21,7 +21,7 @@
 
 #include <daemon.h>
 
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <utils/identification.h>
 
index 1d20a1e..36d634c 100644 (file)
@@ -18,7 +18,7 @@
 #include "credential_manager.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <credentials/sets/cert_cache.h>
 #include <credentials/sets/auth_cfg_wrapper.h>
index de8994b..037f102 100644 (file)
@@ -19,7 +19,7 @@
 #include <sched.h>
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 
 /** cache size, a power of 2 for fast modulo */
index 402773f..dc7b062 100644 (file)
@@ -22,7 +22,7 @@
 #include <daemon.h>
 #include <network/socket.h>
 #include <processing/jobs/callback_job.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 
 typedef struct private_sender_t private_sender_t;
index 63c3b84..5d4779c 100644 (file)
@@ -21,7 +21,7 @@
 #include <daemon.h>
 #include <utils/host.h>
 #include <utils/linked_list.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 /**
  * Default RADIUS server port, when not configured
index 12d3a6f..cda0c0f 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <daemon.h>
 #include <utils/linked_list.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_eap_sim_file_triplets_t private_eap_sim_file_triplets_t;
 
index e0e4a76..dfa79f4 100644 (file)
@@ -30,7 +30,7 @@
 #include "kernel_klips_ipsec.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/acquire_job.h>
 #include <processing/jobs/rekey_child_sa_job.h>
index 9cebd46..691bab7 100644 (file)
@@ -36,7 +36,7 @@
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/hashtable.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/acquire_job.h>
index 609a5c3..67e7c82 100644 (file)
@@ -26,7 +26,7 @@
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/roam_job.h>
index 3d8ca88..e4bdbb1 100644 (file)
@@ -22,7 +22,7 @@
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_netlink_socket_t private_netlink_socket_t;
 
index b26a2bc..98f7a57 100644 (file)
@@ -56,7 +56,7 @@
 
 #include <daemon.h>
 #include <utils/host.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/acquire_job.h>
 #include <processing/jobs/migrate_job.h>
index 7688104..814fbad 100644 (file)
@@ -26,7 +26,7 @@
 
 #include <daemon.h>
 #include <utils/host.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/roam_job.h>
index 63c39b7..de721e0 100644 (file)
@@ -24,7 +24,7 @@
 
 #include <daemon.h>
 #include <processing/jobs/callback_job.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_load_tester_plugin_t private_load_tester_plugin_t;
 
index 4f90fd6..68ef61f 100644 (file)
@@ -20,7 +20,7 @@
 #include <unistd.h>
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <credentials/certificates/x509.h>
 
 typedef struct private_nm_creds_t private_nm_creds_t;
index 1d18335..7f7e024 100644 (file)
@@ -18,7 +18,7 @@
 #include <unistd.h>
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_resolve_handler_t private_resolve_handler_t;
 
index db8b53e..d013528 100644 (file)
@@ -18,7 +18,7 @@
 #include <daemon.h>
 #include <utils/linked_list.h>
 #include <utils/hashtable.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 #define POOL_LIMIT (sizeof(uintptr_t)*8)
 
index 152ef74..ebcd9ac 100644 (file)
@@ -17,7 +17,7 @@
 #include "stroke_ca.h"
 #include "stroke_cred.h"
 
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <crypto/hashers/hasher.h>
 
index 08dafd2..c03349a 100644 (file)
@@ -16,7 +16,7 @@
 #include "stroke_config.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/lexparser.h>
 
 typedef struct private_stroke_config_t private_stroke_config_t;
index 627418b..9183098 100644 (file)
@@ -27,7 +27,7 @@
 #include <credentials/certificates/ac.h>
 #include <utils/linked_list.h>
 #include <utils/lexparser.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <daemon.h>
 
 /* configuration directories and files */
index 2faa235..f834f8e 100644 (file)
@@ -27,7 +27,7 @@
 
 #include <processing/jobs/callback_job.h>
 #include <daemon.h>
-#include <utils/mutex.h> /* for Mac OS X compatible accept */
+#include <threading.h> /* for Mac OS X compatible accept */
 
 #include "stroke_config.h"
 #include "stroke_control.h"
index cfe00cc..900b652 100644 (file)
@@ -14,7 +14,7 @@
  */
 
 #include <library.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 #include <unistd.h>
 #include <sched.h>
index ad51dc5..e4299af 100644 (file)
@@ -18,7 +18,7 @@
 #include <pthread.h>
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_callback_job_t private_callback_job_t;
 
index a364017..248bba1 100644 (file)
@@ -22,7 +22,7 @@
 #include "processor.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 
 
index d09b384..36ccb14 100644 (file)
@@ -23,7 +23,7 @@
 #include <daemon.h>
 #include <processing/processor.h>
 #include <processing/jobs/callback_job.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 /* the initial size of the heap */
 #define HEAP_SIZE_DEFAULT 64
index eb4cb0c..3738cb9 100644 (file)
@@ -16,7 +16,7 @@
 #include "eap_manager.h"
 
 #include <utils/linked_list.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_eap_manager_t private_eap_manager_t;
 typedef struct eap_entry_t eap_entry_t;
index fe95c03..e26c6c6 100644 (file)
@@ -18,7 +18,7 @@
 #include <math.h>
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <crypto/hashers/hasher.h>
 
index 0ad39f3..bb8a4e0 100644 (file)
@@ -22,7 +22,7 @@
 #include <daemon.h>
 #include <sa/ike_sa_id.h>
 #include <bus/bus.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <crypto/hashers/hasher.h>
 
index 2b05df1..5161a71 100644 (file)
@@ -16,7 +16,7 @@
 #include "mediation_manager.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/mediation_job.h>
 
index ec68e94..edcba3a 100644 (file)
@@ -16,7 +16,7 @@
 #include "trap_manager.h"
 
 #include <daemon.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 
 
index 04fa339..a4d23bf 100644 (file)
@@ -24,7 +24,7 @@
 #include <unistd.h>
 
 #include <debug.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <utils/hashtable.h>
 
index 2ed9f7d..bf5a112 100644 (file)
@@ -43,6 +43,7 @@ credentials/ietf_attributes/ietf_attributes.c credentials/ietf_attributes/ietf_a
 database/database.h database/database_factory.h database/database_factory.c \
 fetcher/fetcher.h fetcher/fetcher_manager.h fetcher/fetcher_manager.c \
 selectors/traffic_selector.c selectors/traffic_selector.h \
+threading.h threading/mutex.c \
 utils.h utils.c \
 utils/host.c utils/host.h \
 utils/identification.c utils/identification.h \
@@ -52,7 +53,6 @@ utils/linked_list.c utils/linked_list.h \
 utils/hashtable.c utils/hashtable.h \
 utils/enumerator.c utils/enumerator.h \
 utils/optionsfrom.c utils/optionsfrom.h \
-utils/mutex.c utils/mutex.h \
 utils/backtrace.c utils/backtrace.h \
 plugins/plugin_loader.c plugins/plugin_loader.h plugins/plugin.h
 
index 66f1d27..4b0c509 100644 (file)
@@ -17,7 +17,7 @@
 
 #include <debug.h>
 #include <utils/linked_list.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_attribute_manager_t private_attribute_manager_t;
 
index 0de0946..7e94b9d 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <debug.h>
 #include <utils/linked_list.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <credentials/certificates/x509.h>
 
 ENUM(credential_type_names, CRED_PRIVATE_KEY, CRED_CERTIFICATE,
index 7c09b9f..b9bd89f 100644 (file)
@@ -19,7 +19,7 @@
 
 #include <utils/linked_list.h>
 #include <utils/hashtable.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_key_encoding_t private_key_encoding_t;
 
index 9bc986f..2181086 100644 (file)
@@ -16,7 +16,7 @@
 #include "crypto_factory.h"
 
 #include <debug.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 #include <crypto/crypto_tester.h>
 
index c0c6661..a0f50c8 100644 (file)
@@ -16,7 +16,7 @@
 #include "database_factory.h"
 
 #include <utils/linked_list.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_database_factory_t private_database_factory_t;
 
index 7a31f17..fd3b0ee 100644 (file)
@@ -16,7 +16,7 @@
 #include "fetcher_manager.h"
 
 #include <debug.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 
 typedef struct private_fetcher_manager_t private_fetcher_manager_t;
index 2673afa..78a6fe0 100644 (file)
@@ -40,6 +40,9 @@
  * @defgroup plugins plugins
  * @ingroup libstrongswan
  *
+ * @defgroup threading threading
+ * @ingroup libstrongswan
+ *
  * @defgroup utils utils
  * @ingroup libstrongswan
  */
index 083cab5..66481da 100644 (file)
@@ -24,7 +24,7 @@
 
 #include <library.h>
 #include <debug.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 #include <errno.h>
 #include <gcrypt.h>
index 9775a86..f01321f 100644 (file)
@@ -21,7 +21,7 @@
 #include "mysql_database.h"
 
 #include <debug.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include <utils/linked_list.h>
 
 /* Older mysql.h headers do not define it, but we need it. It is not returned
index 7d94134..0cb526d 100644 (file)
@@ -23,7 +23,7 @@
 #include "openssl_plugin.h"
 
 #include <library.h>
-#include <utils/mutex.h>
+#include <threading.h>
 #include "openssl_util.h"
 #include "openssl_crypter.h"
 #include "openssl_hasher.h"
index 4e18de1..afe67f6 100644 (file)
@@ -19,7 +19,7 @@
 #include <unistd.h>
 #include <library.h>
 #include <debug.h>
-#include <utils/mutex.h>
+#include <threading.h>
 
 typedef struct private_sqlite_database_t private_sqlite_database_t;
 
diff --git a/src/libstrongswan/threading.h b/src/libstrongswan/threading.h
new file mode 100644 (file)
index 0000000..8b9f37f
--- /dev/null
@@ -0,0 +1,216 @@
+/*
+ * Copyright (C) 2008-2009 Tobias Brunner
+ * Copyright (C) 2008 Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+/**
+ * @defgroup threading threading
+ * @{ @ingroup libstrongswan
+ */
+
+#ifndef THREADING_H_
+#define THREADING_H_
+
+typedef struct mutex_t mutex_t;
+typedef struct condvar_t condvar_t;
+typedef struct rwlock_t rwlock_t;
+typedef enum mutex_type_t mutex_type_t;
+typedef enum condvar_type_t condvar_type_t;
+typedef enum rwlock_type_t rwlock_type_t;
+
+#include <library.h>
+
+#ifdef __APPLE__
+/* on Mac OS X 10.5 several system calls we use are no cancellation points.
+ * fortunately, select isn't one of them, so we wrap some of the others with
+ * calls to select(2).
+ */
+#include <sys/socket.h>
+#include <sys/select.h>
+
+#define WRAP_WITH_SELECT(func, socket, ...)\
+       fd_set rfds; FD_ZERO(&rfds); FD_SET(socket, &rfds);\
+       if (select(socket + 1, &rfds, NULL, NULL, NULL) <= 0) { return -1; }\
+       return func(socket, __VA_ARGS__)
+
+static inline int cancellable_accept(int socket, struct sockaddr *address,
+                                                                        socklen_t *address_len)
+{
+       WRAP_WITH_SELECT(accept, socket, address, address_len);
+}
+#define accept cancellable_accept
+static inline int cancellable_recvfrom(int socket, void *buffer, size_t length,
+                               int flags, struct sockaddr *address, socklen_t *address_len)
+{
+       WRAP_WITH_SELECT(recvfrom, socket, buffer, length, flags, address, address_len);
+}
+#define recvfrom cancellable_recvfrom
+#endif /* __APPLE__ */
+
+/**
+ * Type of mutex.
+ */
+enum mutex_type_t {
+       /** default mutex */
+       MUTEX_TYPE_DEFAULT      = 0,
+       /** allow recursive locking of the mutex */
+       MUTEX_TYPE_RECURSIVE    = 1,
+};
+
+/**
+ * Type of condvar.
+ */
+enum condvar_type_t {
+       /** default condvar */
+       CONDVAR_TYPE_DEFAULT    = 0,
+};
+
+/**
+ * Type of read-write lock.
+ */
+enum rwlock_type_t {
+       /** default condvar */
+       RWLOCK_TYPE_DEFAULT     = 0,
+};
+
+/**
+ * Mutex wrapper implements simple, portable and advanced mutex functions.
+ */
+struct mutex_t {
+
+       /**
+        * Acquire the lock to the mutex.
+        */
+       void (*lock)(mutex_t *this);
+
+       /**
+        * Release the lock on the mutex.
+        */
+       void (*unlock)(mutex_t *this);
+
+       /**
+        * Destroy a mutex instance.
+        */
+       void (*destroy)(mutex_t *this);
+};
+
+/**
+ * Condvar wrapper to use in conjunction with mutex_t.
+ */
+struct condvar_t {
+
+       /**
+        * Wait on a condvar until it gets signalized.
+        *
+        * @param mutex                 mutex to release while waiting
+        */
+       void (*wait)(condvar_t *this, mutex_t *mutex);
+
+       /**
+        * Wait on a condvar until it gets signalized, or times out.
+        *
+        * @param mutex                 mutex to release while waiting
+        * @param timeout               timeout im ms
+        * @return                              TRUE if timed out, FALSE otherwise
+        */
+       bool (*timed_wait)(condvar_t *this, mutex_t *mutex, u_int timeout);
+
+       /**
+        * Wait on a condvar until it gets signalized, or times out.
+        *
+        * The passed timeval should be calculated based on the time_monotonic()
+        * function.
+        *
+        * @param mutex                 mutex to release while waiting
+        * @param tv                    absolute time until timeout
+        * @return                              TRUE if timed out, FALSE otherwise
+        */
+       bool (*timed_wait_abs)(condvar_t *this, mutex_t *mutex, timeval_t tv);
+
+       /**
+        * Wake up a single thread in a condvar.
+        */
+       void (*signal)(condvar_t *this);
+
+       /**
+        * Wake up all threads in a condvar.
+        */
+       void (*broadcast)(condvar_t *this);
+
+       /**
+        * Destroy a condvar and free its resources.
+        */
+       void (*destroy)(condvar_t *this);
+};
+
+/**
+ * Read-Write lock wrapper.
+ */
+struct rwlock_t {
+
+       /**
+        * Acquire the read lock.
+        */
+       void (*read_lock)(rwlock_t *this);
+
+       /**
+        * Acquire the write lock.
+        */
+       void (*write_lock)(rwlock_t *this);
+
+       /**
+        * Try to acquire the write lock.
+        *
+        * Never blocks, but returns FALSE if the lock was already occupied.
+        *
+        * @return              TRUE if lock acquired
+        */
+       bool (*try_write_lock)(rwlock_t *this);
+
+       /**
+        * Release any acquired lock.
+        */
+       void (*unlock)(rwlock_t *this);
+
+       /**
+        * Destroy the read-write lock.
+        */
+       void (*destroy)(rwlock_t *this);
+};
+
+/**
+ * Create a mutex instance.
+ *
+ * @param type         type of mutex to create
+ * @return                     unlocked mutex instance
+ */
+mutex_t *mutex_create(mutex_type_t type);
+
+/**
+ * Create a condvar instance.
+ *
+ * @param type         type of condvar to create
+ * @return                     condvar instance
+ */
+condvar_t *condvar_create(condvar_type_t type);
+
+/**
+ * Create a read-write lock instance.
+ *
+ * @param type         type of rwlock to create
+ * @return                     unlocked rwlock instance
+ */
+rwlock_t *rwlock_create(rwlock_type_t type);
+
+#endif /** THREADING_H_ @}*/
diff --git a/src/libstrongswan/threading/mutex.c b/src/libstrongswan/threading/mutex.c
new file mode 100644 (file)
index 0000000..3363402
--- /dev/null
@@ -0,0 +1,551 @@
+/*
+ * Copyright (C) 2008-2009 Tobias Brunner
+ * Copyright (C) 2008 Martin Willi
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#define _GNU_SOURCE
+#include <pthread.h>
+#include <stdint.h>
+#include <time.h>
+#include <errno.h>
+
+#include <threading.h>
+#include <library.h>
+#include <debug.h>
+
+typedef struct private_mutex_t private_mutex_t;
+typedef struct private_r_mutex_t private_r_mutex_t;
+typedef struct private_condvar_t private_condvar_t;
+typedef struct private_rwlock_t private_rwlock_t;
+
+#ifdef LOCK_PROFILER
+
+/**
+ * Do not report mutexes with an overall waiting time smaller than this (in us)
+ */
+#define PROFILE_WAIT_TRESHHOLD 10000
+
+/**
+ * Do not report mutexes with an overall lock count smaller than this
+ */
+#define PROFILE_LOCK_TRESHHOLD 1000
+
+
+#include <utils/backtrace.h>
+
+typedef struct lock_profile_t lock_profile_t;
+
+struct lock_profile_t {
+
+       /**
+        * how long threads have waited for the lock in this mutex so far
+        */
+       timeval_t waited;
+
+       /**
+        * How many times the lock has been invoked
+        */
+       u_int locked;
+
+       /**
+        * backtrace where mutex has been created
+        */
+       backtrace_t *backtrace;
+};
+
+/**
+ * Print and cleanup mutex profiler
+ */
+static void profiler_cleanup(lock_profile_t *profile)
+{
+       if (profile->waited.tv_sec > 0 ||
+               profile->waited.tv_usec > PROFILE_WAIT_TRESHHOLD ||
+               profile->locked > PROFILE_LOCK_TRESHHOLD)
+       {
+               fprintf(stderr, "%d.%03ds / %d times in lock created at:",
+                       profile->waited.tv_sec, profile->waited.tv_usec, profile->locked);
+               profile->backtrace->log(profile->backtrace, stderr);
+       }
+       profile->backtrace->destroy(profile->backtrace);
+}
+
+/**
+ * Initialize mutex profiler
+ */
+static void profiler_init(lock_profile_t *profile)
+{
+       profile->backtrace = backtrace_create(2);
+       timerclear(&profile->waited);
+       profile->locked = 0;
+}
+
+#define profiler_start(profile) { \
+       struct timeval _start, _end, _diff; \
+       (profile)->locked++; \
+       time_monotonic(&_start);
+
+#define profiler_end(profile) \
+       time_monotonic(&_end); \
+       timersub(&_end, &_start, &_diff); \
+       timeradd(&(profile)->waited, &_diff, &(profile)->waited); }
+
+#else /* !LOCK_PROFILER */
+
+#define lock_profile_t struct {}
+#define profiler_cleanup(...) {}
+#define profiler_init(...) {}
+#define profiler_start(...) {}
+#define profiler_end(...) {}
+
+#endif /* LOCK_PROFILER */
+
+/**
+ * private data of mutex
+ */
+struct private_mutex_t {
+
+       /**
+        * public functions
+        */
+       mutex_t public;
+
+       /**
+        * wrapped pthread mutex
+        */
+       pthread_mutex_t mutex;
+
+       /**
+        * is this a recursiv emutex, implementing private_r_mutex_t?
+        */
+       bool recursive;
+
+       /**
+        * profiling info, if enabled
+        */
+       lock_profile_t profile;
+};
+
+/**
+ * private data of mutex, extended by recursive locking information
+ */
+struct private_r_mutex_t {
+
+       /**
+        * Extends private_mutex_t
+        */
+       private_mutex_t generic;
+
+       /**
+        * thread which currently owns mutex
+        */
+       pthread_t thread;
+
+       /**
+        * times we have locked the lock, stored per thread
+        */
+       pthread_key_t times;
+};
+
+/**
+ * private data of condvar
+ */
+struct private_condvar_t {
+
+       /**
+        * public functions
+        */
+       condvar_t public;
+
+       /**
+        * wrapped pthread condvar
+        */
+       pthread_cond_t condvar;
+};
+
+/**
+ * private data of rwlock
+ */
+struct private_rwlock_t {
+
+       /**
+        * public functions
+        */
+       rwlock_t public;
+
+       /**
+        * wrapped pthread rwlock
+        */
+       pthread_rwlock_t rwlock;
+
+       /**
+        * profiling info, if enabled
+        */
+       lock_profile_t profile;
+};
+
+/**
+ * Implementation of mutex_t.lock.
+ */
+static void lock(private_mutex_t *this)
+{
+       int err;
+
+       profiler_start(&this->profile);
+       err = pthread_mutex_lock(&this->mutex);
+       if (err)
+       {
+               DBG1("!!! MUTEX LOCK ERROR: %s !!!", strerror(err));
+       }
+       profiler_end(&this->profile);
+}
+
+/**
+ * Implementation of mutex_t.unlock.
+ */
+static void unlock(private_mutex_t *this)
+{
+       int err;
+
+       err = pthread_mutex_unlock(&this->mutex);
+       if (err)
+       {
+               DBG1("!!! MUTEX UNLOCK ERROR: %s !!!", strerror(err));
+       }
+}
+
+/**
+ * Implementation of mutex_t.lock.
+ */
+static void lock_r(private_r_mutex_t *this)
+{
+       pthread_t self = pthread_self();
+
+       if (this->thread == self)
+       {
+               uintptr_t times;
+
+               /* times++ */
+               times = (uintptr_t)pthread_getspecific(this->times);
+               pthread_setspecific(this->times, (void*)times + 1);
+       }
+       else
+       {
+               lock(&this->generic);
+               this->thread = self;
+               /* times = 1 */
+               pthread_setspecific(this->times, (void*)1);
+       }
+}
+
+/**
+ * Implementation of mutex_t.unlock.
+ */
+static void unlock_r(private_r_mutex_t *this)
+{
+       uintptr_t times;
+
+       /* times-- */
+       times = (uintptr_t)pthread_getspecific(this->times);
+       pthread_setspecific(this->times, (void*)--times);
+
+       if (times == 0)
+       {
+               this->thread = 0;
+               unlock(&this->generic);
+       }
+}
+
+/**
+ * Implementation of mutex_t.destroy
+ */
+static void mutex_destroy(private_mutex_t *this)
+{
+       profiler_cleanup(&this->profile);
+       pthread_mutex_destroy(&this->mutex);
+       free(this);
+}
+
+/**
+ * Implementation of mutex_t.destroy for recursive mutex'
+ */
+static void mutex_destroy_r(private_r_mutex_t *this)
+{
+       profiler_cleanup(&this->generic.profile);
+       pthread_mutex_destroy(&this->generic.mutex);
+       pthread_key_delete(this->times);
+       free(this);
+}
+
+/*
+ * see header file
+ */
+mutex_t *mutex_create(mutex_type_t type)
+{
+       switch (type)
+       {
+               case MUTEX_TYPE_RECURSIVE:
+               {
+                       private_r_mutex_t *this = malloc_thing(private_r_mutex_t);
+
+                       this->generic.public.lock = (void(*)(mutex_t*))lock_r;
+                       this->generic.public.unlock = (void(*)(mutex_t*))unlock_r;
+                       this->generic.public.destroy = (void(*)(mutex_t*))mutex_destroy_r;
+
+                       pthread_mutex_init(&this->generic.mutex, NULL);
+                       pthread_key_create(&this->times, NULL);
+                       this->generic.recursive = TRUE;
+                       profiler_init(&this->generic.profile);
+                       this->thread = 0;
+
+                       return &this->generic.public;
+               }
+               case MUTEX_TYPE_DEFAULT:
+               default:
+               {
+                       private_mutex_t *this = malloc_thing(private_mutex_t);
+
+                       this->public.lock = (void(*)(mutex_t*))lock;
+                       this->public.unlock = (void(*)(mutex_t*))unlock;
+                       this->public.destroy = (void(*)(mutex_t*))mutex_destroy;
+
+                       pthread_mutex_init(&this->mutex, NULL);
+                       this->recursive = FALSE;
+                       profiler_init(&this->profile);
+
+                       return &this->public;
+               }
+       }
+}
+
+/**
+ * Implementation of condvar_t.wait.
+ */
+static void _wait(private_condvar_t *this, private_mutex_t *mutex)
+{
+       if (mutex->recursive)
+       {
+               private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
+
+               /* mutex owner gets cleared during condvar wait */
+               recursive->thread = 0;
+               pthread_cond_wait(&this->condvar, &mutex->mutex);
+               recursive->thread = pthread_self();
+       }
+       else
+       {
+               pthread_cond_wait(&this->condvar, &mutex->mutex);
+       }
+}
+
+/**
+ * Implementation of condvar_t.timed_wait_abs.
+ */
+static bool timed_wait_abs(private_condvar_t *this, private_mutex_t *mutex,
+                                                  timeval_t time)
+{
+       struct timespec ts;
+       bool timed_out;
+
+       ts.tv_sec = time.tv_sec;
+       ts.tv_nsec = time.tv_usec * 1000;
+
+       if (mutex->recursive)
+       {
+               private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
+
+               recursive->thread = 0;
+               timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
+                                                                                  &ts) == ETIMEDOUT;
+               recursive->thread = pthread_self();
+       }
+       else
+       {
+               timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
+                                                                                  &ts) == ETIMEDOUT;
+       }
+       return timed_out;
+}
+
+/**
+ * Implementation of condvar_t.timed_wait.
+ */
+static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
+                                          u_int timeout)
+{
+       timeval_t tv;
+       u_int s, ms;
+
+       time_monotonic(&tv);
+
+       s = timeout / 1000;
+       ms = timeout % 1000;
+
+       tv.tv_sec += s;
+       tv.tv_usec += ms * 1000;
+
+       if (tv.tv_usec > 1000000 /* 1s */)
+       {
+               tv.tv_usec -= 1000000;
+               tv.tv_sec++;
+       }
+       return timed_wait_abs(this, mutex, tv);
+}
+
+/**
+ * Implementation of condvar_t.signal.
+ */
+static void _signal(private_condvar_t *this)
+{
+       pthread_cond_signal(&this->condvar);
+}
+
+/**
+ * Implementation of condvar_t.broadcast.
+ */
+static void broadcast(private_condvar_t *this)
+{
+       pthread_cond_broadcast(&this->condvar);
+}
+
+/**
+ * Implementation of condvar_t.destroy
+ */
+static void condvar_destroy(private_condvar_t *this)
+{
+       pthread_cond_destroy(&this->condvar);
+       free(this);
+}
+
+/*
+ * see header file
+ */
+condvar_t *condvar_create(condvar_type_t type)
+{
+       switch (type)
+       {
+               case CONDVAR_TYPE_DEFAULT:
+               default:
+               {
+                       pthread_condattr_t condattr;
+                       private_condvar_t *this = malloc_thing(private_condvar_t);
+
+                       this->public.wait = (void(*)(condvar_t*, mutex_t *mutex))_wait;
+                       this->public.timed_wait = (bool(*)(condvar_t*, mutex_t *mutex, u_int timeout))timed_wait;
+                       this->public.timed_wait_abs = (bool(*)(condvar_t*, mutex_t *mutex, timeval_t time))timed_wait_abs;
+                       this->public.signal = (void(*)(condvar_t*))_signal;
+                       this->public.broadcast = (void(*)(condvar_t*))broadcast;
+                       this->public.destroy = (void(*)(condvar_t*))condvar_destroy;
+
+                       pthread_condattr_init(&condattr);
+#ifdef HAVE_CONDATTR_CLOCK_MONOTONIC
+                       pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
+#endif
+                       pthread_cond_init(&this->condvar, &condattr);
+                       pthread_condattr_destroy(&condattr);
+
+                       return &this->public;
+               }
+       }
+}
+
+/**
+ * Implementation of rwlock_t.read_lock
+ */
+static void read_lock(private_rwlock_t *this)
+{
+       int err;
+
+       profiler_start(&this->profile);
+       err = pthread_rwlock_rdlock(&this->rwlock);
+       if (err != 0)
+       {
+               DBG1("!!! RWLOCK READ LOCK ERROR: %s !!!", strerror(err));
+       }
+       profiler_end(&this->profile);
+}
+
+/**
+ * Implementation of rwlock_t.write_lock
+ */
+static void write_lock(private_rwlock_t *this)
+{
+       int err;
+
+       profiler_start(&this->profile);
+       err = pthread_rwlock_wrlock(&this->rwlock);
+       if (err != 0)
+       {
+               DBG1("!!! RWLOCK WRITE LOCK ERROR: %s !!!", strerror(err));
+       }
+       profiler_end(&this->profile);
+}
+
+/**
+ * Implementation of rwlock_t.try_write_lock
+ */
+static bool try_write_lock(private_rwlock_t *this)
+{
+       return pthread_rwlock_trywrlock(&this->rwlock) == 0;
+}
+
+/**
+ * Implementation of rwlock_t.unlock
+ */
+static void rw_unlock(private_rwlock_t *this)
+{
+       int err;
+
+       err = pthread_rwlock_unlock(&this->rwlock);
+       if (err != 0)
+       {
+               DBG1("!!! RWLOCK UNLOCK ERROR: %s !!!", strerror(err));
+       }
+}
+
+/**
+ * Implementation of rwlock_t.destroy
+ */
+static void rw_destroy(private_rwlock_t *this)
+{
+       pthread_rwlock_destroy(&this->rwlock);
+       profiler_cleanup(&this->profile);
+       free(this);
+}
+
+/*
+ * see header file
+ */
+rwlock_t *rwlock_create(rwlock_type_t type)
+{
+       switch (type)
+       {
+               case RWLOCK_TYPE_DEFAULT:
+               default:
+               {
+                       private_rwlock_t *this = malloc_thing(private_rwlock_t);
+
+                       this->public.read_lock = (void(*)(rwlock_t*))read_lock;
+                       this->public.write_lock = (void(*)(rwlock_t*))write_lock;
+                       this->public.try_write_lock = (bool(*)(rwlock_t*))try_write_lock;
+                       this->public.unlock = (void(*)(rwlock_t*))rw_unlock;
+                       this->public.destroy = (void(*)(rwlock_t*))rw_destroy;
+
+                       pthread_rwlock_init(&this->rwlock, NULL);
+                       profiler_init(&this->profile);
+
+                       return &this->public;
+               }
+       }
+}
+
diff --git a/src/libstrongswan/utils/mutex.c b/src/libstrongswan/utils/mutex.c
deleted file mode 100644 (file)
index 3e1885a..0000000
+++ /dev/null
@@ -1,552 +0,0 @@
-/*
- * Copyright (C) 2008 Tobias Brunner
- * Copyright (C) 2008 Martin Willi
- * Hochschule fuer Technik Rapperswil
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
- */
-
-#define _GNU_SOURCE
-#include <pthread.h>
-#include <stdint.h>
-#include <time.h>
-#include <errno.h>
-
-#include "mutex.h"
-
-#include <library.h>
-#include <debug.h>
-
-typedef struct private_mutex_t private_mutex_t;
-typedef struct private_r_mutex_t private_r_mutex_t;
-typedef struct private_condvar_t private_condvar_t;
-typedef struct private_rwlock_t private_rwlock_t;
-
-#ifdef LOCK_PROFILER
-
-/**
- * Do not report mutexes with an overall waiting time smaller than this (in us)
- */
-#define PROFILE_WAIT_TRESHHOLD 10000
-
-/**
- * Do not report mutexes with an overall lock count smaller than this
- */
-#define PROFILE_LOCK_TRESHHOLD 1000
-
-
-#include <utils/backtrace.h>
-
-typedef struct lock_profile_t lock_profile_t;
-
-struct lock_profile_t {
-
-       /**
-        * how long threads have waited for the lock in this mutex so far
-        */
-       timeval_t waited;
-
-       /**
-        * How many times the lock has been invoked
-        */
-       u_int locked;
-
-       /**
-        * backtrace where mutex has been created
-        */
-       backtrace_t *backtrace;
-};
-
-/**
- * Print and cleanup mutex profiler
- */
-static void profiler_cleanup(lock_profile_t *profile)
-{
-       if (profile->waited.tv_sec > 0 ||
-               profile->waited.tv_usec > PROFILE_WAIT_TRESHHOLD ||
-               profile->locked > PROFILE_LOCK_TRESHHOLD)
-       {
-               fprintf(stderr, "%d.%03ds / %d times in lock created at:",
-                       profile->waited.tv_sec, profile->waited.tv_usec, profile->locked);
-               profile->backtrace->log(profile->backtrace, stderr);
-       }
-       profile->backtrace->destroy(profile->backtrace);
-}
-
-/**
- * Initialize mutex profiler
- */
-static void profiler_init(lock_profile_t *profile)
-{
-       profile->backtrace = backtrace_create(2);
-       timerclear(&profile->waited);
-       profile->locked = 0;
-}
-
-#define profiler_start(profile) { \
-       struct timeval _start, _end, _diff; \
-       (profile)->locked++; \
-       time_monotonic(&_start);
-
-#define profiler_end(profile) \
-       time_monotonic(&_end); \
-       timersub(&_end, &_start, &_diff); \
-       timeradd(&(profile)->waited, &_diff, &(profile)->waited); }
-
-#else /* !LOCK_PROFILER */
-
-#define lock_profile_t struct {}
-#define profiler_cleanup(...) {}
-#define profiler_init(...) {}
-#define profiler_start(...) {}
-#define profiler_end(...) {}
-
-#endif /* LOCK_PROFILER */
-
-/**
- * private data of mutex
- */
-struct private_mutex_t {
-
-       /**
-        * public functions
-        */
-       mutex_t public;
-
-       /**
-        * wrapped pthread mutex
-        */
-       pthread_mutex_t mutex;
-
-       /**
-        * is this a recursiv emutex, implementing private_r_mutex_t?
-        */
-       bool recursive;
-
-       /**
-        * profiling info, if enabled
-        */
-       lock_profile_t profile;
-};
-
-/**
- * private data of mutex, extended by recursive locking information
- */
-struct private_r_mutex_t {
-
-       /**
-        * Extends private_mutex_t
-        */
-       private_mutex_t generic;
-
-       /**
-        * thread which currently owns mutex
-        */
-       pthread_t thread;
-
-       /**
-        * times we have locked the lock, stored per thread
-        */
-       pthread_key_t times;
-};
-
-/**
- * private data of condvar
- */
-struct private_condvar_t {
-
-       /**
-        * public functions
-        */
-       condvar_t public;
-
-       /**
-        * wrapped pthread condvar
-        */
-       pthread_cond_t condvar;
-};
-
-/**
- * private data of rwlock
- */
-struct private_rwlock_t {
-
-       /**
-        * public functions
-        */
-       rwlock_t public;
-
-       /**
-        * wrapped pthread rwlock
-        */
-       pthread_rwlock_t rwlock;
-
-       /**
-        * profiling info, if enabled
-        */
-       lock_profile_t profile;
-};
-
-/**
- * Implementation of mutex_t.lock.
- */
-static void lock(private_mutex_t *this)
-{
-       int err;
-
-       profiler_start(&this->profile);
-       err = pthread_mutex_lock(&this->mutex);
-       if (err)
-       {
-               DBG1("!!! MUTEX LOCK ERROR: %s !!!", strerror(err));
-       }
-       profiler_end(&this->profile);
-}
-
-/**
- * Implementation of mutex_t.unlock.
- */
-static void unlock(private_mutex_t *this)
-{
-       int err;
-
-       err = pthread_mutex_unlock(&this->mutex);
-       if (err)
-       {
-               DBG1("!!! MUTEX UNLOCK ERROR: %s !!!", strerror(err));
-       }
-}
-
-/**
- * Implementation of mutex_t.lock.
- */
-static void lock_r(private_r_mutex_t *this)
-{
-       pthread_t self = pthread_self();
-
-       if (this->thread == self)
-       {
-               uintptr_t times;
-
-               /* times++ */
-               times = (uintptr_t)pthread_getspecific(this->times);
-               pthread_setspecific(this->times, (void*)times + 1);
-       }
-       else
-       {
-               lock(&this->generic);
-               this->thread = self;
-               /* times = 1 */
-               pthread_setspecific(this->times, (void*)1);
-       }
-}
-
-/**
- * Implementation of mutex_t.unlock.
- */
-static void unlock_r(private_r_mutex_t *this)
-{
-       uintptr_t times;
-
-       /* times-- */
-       times = (uintptr_t)pthread_getspecific(this->times);
-       pthread_setspecific(this->times, (void*)--times);
-
-       if (times == 0)
-       {
-               this->thread = 0;
-               unlock(&this->generic);
-       }
-}
-
-/**
- * Implementation of mutex_t.destroy
- */
-static void mutex_destroy(private_mutex_t *this)
-{
-       profiler_cleanup(&this->profile);
-       pthread_mutex_destroy(&this->mutex);
-       free(this);
-}
-
-/**
- * Implementation of mutex_t.destroy for recursive mutex'
- */
-static void mutex_destroy_r(private_r_mutex_t *this)
-{
-       profiler_cleanup(&this->generic.profile);
-       pthread_mutex_destroy(&this->generic.mutex);
-       pthread_key_delete(this->times);
-       free(this);
-}
-
-/*
- * see header file
- */
-mutex_t *mutex_create(mutex_type_t type)
-{
-       switch (type)
-       {
-               case MUTEX_TYPE_RECURSIVE:
-               {
-                       private_r_mutex_t *this = malloc_thing(private_r_mutex_t);
-
-                       this->generic.public.lock = (void(*)(mutex_t*))lock_r;
-                       this->generic.public.unlock = (void(*)(mutex_t*))unlock_r;
-                       this->generic.public.destroy = (void(*)(mutex_t*))mutex_destroy_r;
-
-                       pthread_mutex_init(&this->generic.mutex, NULL);
-                       pthread_key_create(&this->times, NULL);
-                       this->generic.recursive = TRUE;
-                       profiler_init(&this->generic.profile);
-                       this->thread = 0;
-
-                       return &this->generic.public;
-               }
-               case MUTEX_TYPE_DEFAULT:
-               default:
-               {
-                       private_mutex_t *this = malloc_thing(private_mutex_t);
-
-                       this->public.lock = (void(*)(mutex_t*))lock;
-                       this->public.unlock = (void(*)(mutex_t*))unlock;
-                       this->public.destroy = (void(*)(mutex_t*))mutex_destroy;
-
-                       pthread_mutex_init(&this->mutex, NULL);
-                       this->recursive = FALSE;
-                       profiler_init(&this->profile);
-
-                       return &this->public;
-               }
-       }
-}
-
-/**
- * Implementation of condvar_t.wait.
- */
-static void _wait(private_condvar_t *this, private_mutex_t *mutex)
-{
-       if (mutex->recursive)
-       {
-               private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
-
-               /* mutex owner gets cleared during condvar wait */
-               recursive->thread = 0;
-               pthread_cond_wait(&this->condvar, &mutex->mutex);
-               recursive->thread = pthread_self();
-       }
-       else
-       {
-               pthread_cond_wait(&this->condvar, &mutex->mutex);
-       }
-}
-
-/**
- * Implementation of condvar_t.timed_wait_abs.
- */
-static bool timed_wait_abs(private_condvar_t *this, private_mutex_t *mutex,
-                                                  timeval_t time)
-{
-       struct timespec ts;
-       bool timed_out;
-
-       ts.tv_sec = time.tv_sec;
-       ts.tv_nsec = time.tv_usec * 1000;
-
-       if (mutex->recursive)
-       {
-               private_r_mutex_t* recursive = (private_r_mutex_t*)mutex;
-
-               recursive->thread = 0;
-               timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
-                                                                                  &ts) == ETIMEDOUT;
-               recursive->thread = pthread_self();
-       }
-       else
-       {
-               timed_out = pthread_cond_timedwait(&this->condvar, &mutex->mutex,
-                                                                                  &ts) == ETIMEDOUT;
-       }
-       return timed_out;
-}
-
-/**
- * Implementation of condvar_t.timed_wait.
- */
-static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
-                                          u_int timeout)
-{
-       timeval_t tv;
-       u_int s, ms;
-
-       time_monotonic(&tv);
-
-       s = timeout / 1000;
-       ms = timeout % 1000;
-
-       tv.tv_sec += s;
-       tv.tv_usec += ms * 1000;
-
-       if (tv.tv_usec > 1000000 /* 1s */)
-       {
-               tv.tv_usec -= 1000000;
-               tv.tv_sec++;
-       }
-       return timed_wait_abs(this, mutex, tv);
-}
-
-/**
- * Implementation of condvar_t.signal.
- */
-static void _signal(private_condvar_t *this)
-{
-       pthread_cond_signal(&this->condvar);
-}
-
-/**
- * Implementation of condvar_t.broadcast.
- */
-static void broadcast(private_condvar_t *this)
-{
-       pthread_cond_broadcast(&this->condvar);
-}
-
-/**
- * Implementation of condvar_t.destroy
- */
-static void condvar_destroy(private_condvar_t *this)
-{
-       pthread_cond_destroy(&this->condvar);
-       free(this);
-}
-
-/*
- * see header file
- */
-condvar_t *condvar_create(condvar_type_t type)
-{
-       switch (type)
-       {
-               case CONDVAR_TYPE_DEFAULT:
-               default:
-               {
-                       pthread_condattr_t condattr;
-                       private_condvar_t *this = malloc_thing(private_condvar_t);
-
-                       this->public.wait = (void(*)(condvar_t*, mutex_t *mutex))_wait;
-                       this->public.timed_wait = (bool(*)(condvar_t*, mutex_t *mutex, u_int timeout))timed_wait;
-                       this->public.timed_wait_abs = (bool(*)(condvar_t*, mutex_t *mutex, timeval_t time))timed_wait_abs;
-                       this->public.signal = (void(*)(condvar_t*))_signal;
-                       this->public.broadcast = (void(*)(condvar_t*))broadcast;
-                       this->public.destroy = (void(*)(condvar_t*))condvar_destroy;
-
-                       pthread_condattr_init(&condattr);
-#ifdef HAVE_CONDATTR_CLOCK_MONOTONIC
-                       pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC);
-#endif
-                       pthread_cond_init(&this->condvar, &condattr);
-                       pthread_condattr_destroy(&condattr);
-
-                       return &this->public;
-               }
-       }
-}
-
-/**
- * Implementation of rwlock_t.read_lock
- */
-static void read_lock(private_rwlock_t *this)
-{
-       int err;
-
-       profiler_start(&this->profile);
-       err = pthread_rwlock_rdlock(&this->rwlock);
-       if (err != 0)
-       {
-               DBG1("!!! RWLOCK READ LOCK ERROR: %s !!!", strerror(err));
-       }
-       profiler_end(&this->profile);
-}
-
-/**
- * Implementation of rwlock_t.write_lock
- */
-static void write_lock(private_rwlock_t *this)
-{
-       int err;
-
-       profiler_start(&this->profile);
-       err = pthread_rwlock_wrlock(&this->rwlock);
-       if (err != 0)
-       {
-               DBG1("!!! RWLOCK WRITE LOCK ERROR: %s !!!", strerror(err));
-       }
-       profiler_end(&this->profile);
-}
-
-/**
- * Implementation of rwlock_t.try_write_lock
- */
-static bool try_write_lock(private_rwlock_t *this)
-{
-       return pthread_rwlock_trywrlock(&this->rwlock) == 0;
-}
-
-/**
- * Implementation of rwlock_t.unlock
- */
-static void rw_unlock(private_rwlock_t *this)
-{
-       int err;
-
-       err = pthread_rwlock_unlock(&this->rwlock);
-       if (err != 0)
-       {
-               DBG1("!!! RWLOCK UNLOCK ERROR: %s !!!", strerror(err));
-       }
-}
-
-/**
- * Implementation of rwlock_t.destroy
- */
-static void rw_destroy(private_rwlock_t *this)
-{
-       pthread_rwlock_destroy(&this->rwlock);
-       profiler_cleanup(&this->profile);
-       free(this);
-}
-
-/*
- * see header file
- */
-rwlock_t *rwlock_create(rwlock_type_t type)
-{
-       switch (type)
-       {
-               case RWLOCK_TYPE_DEFAULT:
-               default:
-               {
-                       private_rwlock_t *this = malloc_thing(private_rwlock_t);
-
-                       this->public.read_lock = (void(*)(rwlock_t*))read_lock;
-                       this->public.write_lock = (void(*)(rwlock_t*))write_lock;
-                       this->public.try_write_lock = (bool(*)(rwlock_t*))try_write_lock;
-                       this->public.unlock = (void(*)(rwlock_t*))rw_unlock;
-                       this->public.destroy = (void(*)(rwlock_t*))rw_destroy;
-
-                       pthread_rwlock_init(&this->rwlock, NULL);
-                       profiler_init(&this->profile);
-
-                       return &this->public;
-               }
-       }
-}
-
diff --git a/src/libstrongswan/utils/mutex.h b/src/libstrongswan/utils/mutex.h
deleted file mode 100644 (file)
index 8e53c82..0000000
+++ /dev/null
@@ -1,216 +0,0 @@
-/*
- * Copyright (C) 2008 Tobias Brunner
- * Copyright (C) 2008 Martin Willi
- * Hochschule fuer Technik Rapperswil
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * for more details.
- */
-
-/**
- * @defgroup mutex mutex
- * @{ @ingroup utils
- */
-
-#ifndef MUTEX_H_
-#define MUTEX_H_
-
-typedef struct mutex_t mutex_t;
-typedef struct condvar_t condvar_t;
-typedef struct rwlock_t rwlock_t;
-typedef enum mutex_type_t mutex_type_t;
-typedef enum condvar_type_t condvar_type_t;
-typedef enum rwlock_type_t rwlock_type_t;
-
-#include <library.h>
-
-#ifdef __APPLE__
-/* on Mac OS X 10.5 several system calls we use are no cancellation points.
- * fortunately, select isn't one of them, so we wrap some of the others with
- * calls to select(2).
- */
-#include <sys/socket.h>
-#include <sys/select.h>
-
-#define WRAP_WITH_SELECT(func, socket, ...)\
-       fd_set rfds; FD_ZERO(&rfds); FD_SET(socket, &rfds);\
-       if (select(socket + 1, &rfds, NULL, NULL, NULL) <= 0) { return -1; }\
-       return func(socket, __VA_ARGS__)
-
-static inline int cancellable_accept(int socket, struct sockaddr *address,
-                                                                        socklen_t *address_len)
-{
-       WRAP_WITH_SELECT(accept, socket, address, address_len);
-}
-#define accept cancellable_accept
-static inline int cancellable_recvfrom(int socket, void *buffer, size_t length,
-                               int flags, struct sockaddr *address, socklen_t *address_len)
-{
-       WRAP_WITH_SELECT(recvfrom, socket, buffer, length, flags, address, address_len);
-}
-#define recvfrom cancellable_recvfrom
-#endif /* __APPLE__ */
-
-/**
- * Type of mutex.
- */
-enum mutex_type_t {
-       /** default mutex */
-       MUTEX_TYPE_DEFAULT      = 0,
-       /** allow recursive locking of the mutex */
-       MUTEX_TYPE_RECURSIVE    = 1,
-};
-
-/**
- * Type of condvar.
- */
-enum condvar_type_t {
-       /** default condvar */
-       CONDVAR_TYPE_DEFAULT    = 0,
-};
-
-/**
- * Type of read-write lock.
- */
-enum rwlock_type_t {
-       /** default condvar */
-       RWLOCK_TYPE_DEFAULT     = 0,
-};
-
-/**
- * Mutex wrapper implements simple, portable and advanced mutex functions.
- */
-struct mutex_t {
-
-       /**
-        * Acquire the lock to the mutex.
-        */
-       void (*lock)(mutex_t *this);
-
-       /**
-        * Release the lock on the mutex.
-        */
-       void (*unlock)(mutex_t *this);
-
-       /**
-        * Destroy a mutex instance.
-        */
-       void (*destroy)(mutex_t *this);
-};
-
-/**
- * Condvar wrapper to use in conjunction with mutex_t.
- */
-struct condvar_t {
-
-       /**
-        * Wait on a condvar until it gets signalized.
-        *
-        * @param mutex                 mutex to release while waiting
-        */
-       void (*wait)(condvar_t *this, mutex_t *mutex);
-
-       /**
-        * Wait on a condvar until it gets signalized, or times out.
-        *
-        * @param mutex                 mutex to release while waiting
-        * @param timeout               timeout im ms
-        * @return                              TRUE if timed out, FALSE otherwise
-        */
-       bool (*timed_wait)(condvar_t *this, mutex_t *mutex, u_int timeout);
-
-       /**
-        * Wait on a condvar until it gets signalized, or times out.
-        *
-        * The passed timeval should be calculated based on the time_monotonic()
-        * function.
-        *
-        * @param mutex                 mutex to release while waiting
-        * @param tv                    absolute time until timeout
-        * @return                              TRUE if timed out, FALSE otherwise
-        */
-       bool (*timed_wait_abs)(condvar_t *this, mutex_t *mutex, timeval_t tv);
-
-       /**
-        * Wake up a single thread in a condvar.
-        */
-       void (*signal)(condvar_t *this);
-
-       /**
-        * Wake up all threads in a condvar.
-        */
-       void (*broadcast)(condvar_t *this);
-
-       /**
-        * Destroy a condvar and free its resources.
-        */
-       void (*destroy)(condvar_t *this);
-};
-
-/**
- * Read-Write lock wrapper.
- */
-struct rwlock_t {
-
-       /**
-        * Acquire the read lock.
-        */
-       void (*read_lock)(rwlock_t *this);
-
-       /**
-        * Acquire the write lock.
-        */
-       void (*write_lock)(rwlock_t *this);
-
-       /**
-        * Try to acquire the write lock.
-        *
-        * Never blocks, but returns FALSE if the lock was already occupied.
-        *
-        * @return              TRUE if lock acquired
-        */
-       bool (*try_write_lock)(rwlock_t *this);
-
-       /**
-        * Release any acquired lock.
-        */
-       void (*unlock)(rwlock_t *this);
-
-       /**
-        * Destroy the read-write lock.
-        */
-       void (*destroy)(rwlock_t *this);
-};
-
-/**
- * Create a mutex instance.
- *
- * @param type         type of mutex to create
- * @return                     unlocked mutex instance
- */
-mutex_t *mutex_create(mutex_type_t type);
-
-/**
- * Create a condvar instance.
- *
- * @param type         type of condvar to create
- * @return                     condvar instance
- */
-condvar_t *condvar_create(condvar_type_t type);
-
-/**
- * Create a read-write lock instance.
- *
- * @param type         type of rwlock to create
- * @return                     unlocked rwlock instance
- */
-rwlock_t *rwlock_create(rwlock_type_t type);
-
-#endif /** MUTEX_H_ @}*/