Separated the public interfaces of the threading primitives.
authorTobias Brunner <tobias@strongswan.org>
Tue, 8 Dec 2009 15:53:01 +0000 (16:53 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 23 Dec 2009 16:01:53 +0000 (17:01 +0100)
50 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/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 [deleted file]
src/libstrongswan/threading/condvar.c
src/libstrongswan/threading/condvar.h
src/libstrongswan/threading/mutex.c
src/libstrongswan/threading/mutex.h
src/libstrongswan/threading/rwlock.c
src/libstrongswan/threading/rwlock.h

index c218423..d77797e 100644 (file)
@@ -19,7 +19,7 @@
 #include <stdint.h>
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 ENUM(debug_names, DBG_DMN, DBG_LIB,
        "DMN",
index dabae34..938927c 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <daemon.h>
 #include <utils/linked_list.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 
 
 typedef struct private_backend_manager_t private_backend_manager_t;
index cf5d006..9df14c9 100644 (file)
@@ -21,7 +21,7 @@
 
 #include <daemon.h>
 
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/linked_list.h>
 #include <utils/identification.h>
 
index 36d634c..1e85177 100644 (file)
@@ -18,7 +18,7 @@
 #include "credential_manager.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <utils/linked_list.h>
 #include <credentials/sets/cert_cache.h>
 #include <credentials/sets/auth_cfg_wrapper.h>
@@ -1140,7 +1140,7 @@ static bool verify_trust_chain(private_credential_manager_t *this,
                        {
                                auth->add(auth, AUTH_RULE_CA_CERT, issuer->get_ref(issuer));
                                DBG1(DBG_CFG, "  using trusted ca certificate \"%Y\"",
-                                                         issuer->get_subject(issuer));
+                                                         issuer->get_subject(issuer));
                                trusted = TRUE;
                        }
                        else
index 037f102..176accc 100644 (file)
@@ -19,7 +19,7 @@
 #include <sched.h>
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <utils/linked_list.h>
 
 /** cache size, a power of 2 for fast modulo */
index dc7b062..c644f1d 100644 (file)
@@ -22,7 +22,7 @@
 #include <daemon.h>
 #include <network/socket.h>
 #include <processing/jobs/callback_job.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 
 typedef struct private_sender_t private_sender_t;
index 5d4779c..e6d68c9 100644 (file)
@@ -21,7 +21,7 @@
 #include <daemon.h>
 #include <utils/host.h>
 #include <utils/linked_list.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 /**
  * Default RADIUS server port, when not configured
index cda0c0f..6b7d99f 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <daemon.h>
 #include <utils/linked_list.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 typedef struct private_eap_sim_file_triplets_t private_eap_sim_file_triplets_t;
 
index dfa79f4..29a77de 100644 (file)
@@ -30,7 +30,7 @@
 #include "kernel_klips_ipsec.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/acquire_job.h>
 #include <processing/jobs/rekey_child_sa_job.h>
index 691bab7..c7f005a 100644 (file)
@@ -36,7 +36,7 @@
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/hashtable.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/acquire_job.h>
index 67e7c82..4447dc0 100644 (file)
@@ -26,7 +26,7 @@
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/roam_job.h>
index e4bdbb1..b96186a 100644 (file)
@@ -22,7 +22,7 @@
 #include "kernel_netlink_shared.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 typedef struct private_netlink_socket_t private_netlink_socket_t;
 
index 98f7a57..8dc03b8 100644 (file)
@@ -56,7 +56,7 @@
 
 #include <daemon.h>
 #include <utils/host.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/acquire_job.h>
 #include <processing/jobs/migrate_job.h>
index 814fbad..9aee539 100644 (file)
@@ -26,7 +26,7 @@
 
 #include <daemon.h>
 #include <utils/host.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/callback_job.h>
 #include <processing/jobs/roam_job.h>
index de721e0..9c6cc99 100644 (file)
@@ -24,7 +24,7 @@
 
 #include <daemon.h>
 #include <processing/jobs/callback_job.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 typedef struct private_load_tester_plugin_t private_load_tester_plugin_t;
 
index 68ef61f..193838e 100644 (file)
@@ -20,7 +20,7 @@
 #include <unistd.h>
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <credentials/certificates/x509.h>
 
 typedef struct private_nm_creds_t private_nm_creds_t;
index 7f7e024..714c751 100644 (file)
@@ -18,7 +18,7 @@
 #include <unistd.h>
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 typedef struct private_resolve_handler_t private_resolve_handler_t;
 
index d013528..7a5ce68 100644 (file)
@@ -18,7 +18,7 @@
 #include <daemon.h>
 #include <utils/linked_list.h>
 #include <utils/hashtable.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 #define POOL_LIMIT (sizeof(uintptr_t)*8)
 
index ebcd9ac..49146f1 100644 (file)
@@ -17,7 +17,7 @@
 #include "stroke_ca.h"
 #include "stroke_cred.h"
 
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <utils/linked_list.h>
 #include <crypto/hashers/hasher.h>
 
index c03349a..1f585f7 100644 (file)
@@ -16,7 +16,7 @@
 #include "stroke_config.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/lexparser.h>
 
 typedef struct private_stroke_config_t private_stroke_config_t;
index 9183098..bc0b2f6 100644 (file)
@@ -27,7 +27,7 @@
 #include <credentials/certificates/ac.h>
 #include <utils/linked_list.h>
 #include <utils/lexparser.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <daemon.h>
 
 /* configuration directories and files */
index f834f8e..6a59999 100644 (file)
@@ -27,7 +27,7 @@
 
 #include <processing/jobs/callback_job.h>
 #include <daemon.h>
-#include <threading.h> /* for Mac OS X compatible accept */
+#include <threading/mutex.h> /* for Mac OS X compatible accept */
 
 #include "stroke_config.h"
 #include "stroke_control.h"
index 900b652..77085cb 100644 (file)
@@ -14,7 +14,7 @@
  */
 
 #include <library.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 #include <unistd.h>
 #include <sched.h>
index e4299af..145481c 100644 (file)
@@ -18,7 +18,7 @@
 #include <pthread.h>
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 typedef struct private_callback_job_t private_callback_job_t;
 
index 248bba1..620c01c 100644 (file)
@@ -22,7 +22,7 @@
 #include "processor.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/linked_list.h>
 
 
index 36ccb14..69c5490 100644 (file)
@@ -23,7 +23,7 @@
 #include <daemon.h>
 #include <processing/processor.h>
 #include <processing/jobs/callback_job.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 /* the initial size of the heap */
 #define HEAP_SIZE_DEFAULT 64
index 3738cb9..f795183 100644 (file)
@@ -16,7 +16,7 @@
 #include "eap_manager.h"
 
 #include <utils/linked_list.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 
 typedef struct private_eap_manager_t private_eap_manager_t;
 typedef struct eap_entry_t eap_entry_t;
index e26c6c6..b78ba07 100644 (file)
@@ -18,7 +18,7 @@
 #include <math.h>
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/linked_list.h>
 #include <crypto/hashers/hasher.h>
 
index bb8a4e0..6eb9d14 100644 (file)
@@ -22,7 +22,8 @@
 #include <daemon.h>
 #include <sa/ike_sa_id.h>
 #include <bus/bus.h>
-#include <threading.h>
+#include <threading/mutex.h>
+#include <threading/rwlock.h>
 #include <utils/linked_list.h>
 #include <crypto/hashers/hasher.h>
 
index 5161a71..035f490 100644 (file)
@@ -16,7 +16,7 @@
 #include "mediation_manager.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/linked_list.h>
 #include <processing/jobs/mediation_job.h>
 
index edcba3a..ed75899 100644 (file)
@@ -16,7 +16,7 @@
 #include "trap_manager.h"
 
 #include <daemon.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <utils/linked_list.h>
 
 
index a4d23bf..05c1122 100644 (file)
@@ -24,7 +24,7 @@
 #include <unistd.h>
 
 #include <debug.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/linked_list.h>
 #include <utils/hashtable.h>
 
index bdb6cda..d708eb9 100644 (file)
@@ -43,7 +43,6 @@ 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.h threading/mutex.c \
 threading/condvar.h threading/condvar.c \
 threading/rwlock.h threading/rwlock.c \
index 4b0c509..91fa1eb 100644 (file)
@@ -17,7 +17,7 @@
 
 #include <debug.h>
 #include <utils/linked_list.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 
 typedef struct private_attribute_manager_t private_attribute_manager_t;
 
index 7e94b9d..ff2393f 100644 (file)
@@ -20,7 +20,7 @@
 
 #include <debug.h>
 #include <utils/linked_list.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <credentials/certificates/x509.h>
 
 ENUM(credential_type_names, CRED_PRIVATE_KEY, CRED_CERTIFICATE,
index b9bd89f..89b2522 100644 (file)
@@ -19,7 +19,7 @@
 
 #include <utils/linked_list.h>
 #include <utils/hashtable.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 
 typedef struct private_key_encoding_t private_key_encoding_t;
 
index 2181086..46b5032 100644 (file)
@@ -16,7 +16,7 @@
 #include "crypto_factory.h"
 
 #include <debug.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <utils/linked_list.h>
 #include <crypto/crypto_tester.h>
 
index a0f50c8..3936565 100644 (file)
@@ -16,7 +16,7 @@
 #include "database_factory.h"
 
 #include <utils/linked_list.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 typedef struct private_database_factory_t private_database_factory_t;
 
index fd3b0ee..b73bfac 100644 (file)
@@ -16,7 +16,7 @@
 #include "fetcher_manager.h"
 
 #include <debug.h>
-#include <threading.h>
+#include <threading/rwlock.h>
 #include <utils/linked_list.h>
 
 typedef struct private_fetcher_manager_t private_fetcher_manager_t;
index 66481da..8c9ea89 100644 (file)
@@ -24,7 +24,7 @@
 
 #include <library.h>
 #include <debug.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 #include <errno.h>
 #include <gcrypt.h>
index f01321f..6c2a25b 100644 (file)
@@ -21,7 +21,7 @@
 #include "mysql_database.h"
 
 #include <debug.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include <utils/linked_list.h>
 
 /* Older mysql.h headers do not define it, but we need it. It is not returned
index 0cb526d..46446fc 100644 (file)
@@ -23,7 +23,7 @@
 #include "openssl_plugin.h"
 
 #include <library.h>
-#include <threading.h>
+#include <threading/mutex.h>
 #include "openssl_util.h"
 #include "openssl_crypter.h"
 #include "openssl_hasher.h"
index afe67f6..721f1a1 100644 (file)
@@ -19,7 +19,7 @@
 #include <unistd.h>
 #include <library.h>
 #include <debug.h>
-#include <threading.h>
+#include <threading/mutex.h>
 
 typedef struct private_sqlite_database_t private_sqlite_database_t;
 
diff --git a/src/libstrongswan/threading.h b/src/libstrongswan/threading.h
deleted file mode 100644 (file)
index 8b9f37f..0000000
+++ /dev/null
@@ -1,216 +0,0 @@
-/*
- * 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_ @}*/
index 7ba1c07..4c95890 100644 (file)
 #include <time.h>
 #include <errno.h>
 
-#include <threading.h>
 #include <library.h>
 #include <debug.h>
 
 #include "condvar.h"
 #include "mutex.h"
 
+typedef struct private_condvar_t private_condvar_t;
+
+/**
+ * private data of condvar
+ */
+struct private_condvar_t {
+
+       /**
+        * public functions
+        */
+       condvar_t public;
+
+       /**
+        * wrapped pthread condvar
+        */
+       pthread_cond_t condvar;
+};
+
 /**
  * Implementation of condvar_t.wait.
  */
index 9b29662..48c949c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Tobias Brunner
+ * Copyright (C) 2008-2009 Tobias Brunner
  * Copyright (C) 2008 Martin Willi
  * Hochschule fuer Technik Rapperswil
  *
  * for more details.
  */
 
+/**
+ * @defgroup condvar condvar
+ * @{ @ingroup threading
+ */
+
 #ifndef THREADING_CONDVAR_H_
 #define THREADING_CONDVAR_H_
 
-typedef struct private_condvar_t private_condvar_t;
+typedef struct condvar_t condvar_t;
+typedef enum condvar_type_t condvar_type_t;
+
+#include "mutex.h"
+
+/**
+ * Type of condvar.
+ */
+enum condvar_type_t {
+       /** default condvar */
+       CONDVAR_TYPE_DEFAULT = 0,
+};
 
 /**
- * private data of condvar
+ * Condvar wrapper to use in conjunction with mutex_t.
  */
-struct private_condvar_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);
 
        /**
-        * public functions
+        * Wake up all threads in a condvar.
         */
-       condvar_t public;
+       void (*broadcast)(condvar_t *this);
 
        /**
-        * wrapped pthread condvar
+        * Destroy a condvar and free its resources.
         */
-       pthread_cond_t condvar;
+       void (*destroy)(condvar_t *this);
 };
 
-#endif /* THREADING_CONDVAR_H_ */
+/**
+ * Create a condvar instance.
+ *
+ * @param type         type of condvar to create
+ * @return                     condvar instance
+ */
+condvar_t *condvar_create(condvar_type_t type);
+
+#endif /** THREADING_CONDVAR_H_ @} */
 
index a3d131a..6133822 100644 (file)
 #define _GNU_SOURCE
 #include <pthread.h>
 
-#include <threading.h>
 #include <library.h>
 #include <debug.h>
 
 #include "mutex.h"
 #include "lock_profiler.h"
 
+typedef struct private_mutex_t private_mutex_t;
+typedef struct private_r_mutex_t private_r_mutex_t;
+
+/**
+ * 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;
+};
+
 /**
  * Implementation of mutex_t.lock.
  */
index 50c0660..ca242f6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Tobias Brunner
+ * Copyright (C) 2008-2009 Tobias Brunner
  * Copyright (C) 2008 Martin Willi
  * Hochschule fuer Technik Rapperswil
  *
  * for more details.
  */
 
+/**
+ * @defgroup mutex mutex
+ * @{ @ingroup threading
+ */
+
 #ifndef THREADING_MUTEX_H_
 #define THREADING_MUTEX_H_
 
-#include "lock_profiler.h"
+typedef struct mutex_t mutex_t;
+typedef enum mutex_type_t mutex_type_t;
 
-typedef struct private_mutex_t private_mutex_t;
-typedef struct private_r_mutex_t private_r_mutex_t;
+#include "condvar.h"
 
-/**
- * private data of mutex
+#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).
  */
-struct private_mutex_t {
+#include <sys/socket.h>
+#include <sys/select.h>
 
-       /**
-        * public functions
-        */
-       mutex_t public;
+#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__)
 
-       /**
-        * wrapped pthread mutex
-        */
-       pthread_mutex_t mutex;
+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__ */
 
-       /**
-        * is this a recursiv emutex, implementing private_r_mutex_t?
-        */
-       bool recursive;
-
-       /**
-        * profiling info, if enabled
-        */
-       lock_profile_t profile;
+/**
+ * Type of mutex.
+ */
+enum mutex_type_t {
+       /** default mutex */
+       MUTEX_TYPE_DEFAULT      = 0,
+       /** allow recursive locking of the mutex */
+       MUTEX_TYPE_RECURSIVE    = 1,
 };
 
 /**
- * private data of mutex, extended by recursive locking information
+ * Mutex wrapper implements simple, portable and advanced mutex functions.
  */
-struct private_r_mutex_t {
+struct mutex_t {
 
        /**
-        * Extends private_mutex_t
+        * Acquire the lock to the mutex.
         */
-       private_mutex_t generic;
+       void (*lock)(mutex_t *this);
 
        /**
-        * thread which currently owns mutex
+        * Release the lock on the mutex.
         */
-       pthread_t thread;
+       void (*unlock)(mutex_t *this);
 
        /**
-        * times we have locked the lock, stored per thread
+        * Destroy a mutex instance.
         */
-       pthread_key_t times;
+       void (*destroy)(mutex_t *this);
 };
 
-#endif /* THREADING_MUTEX_H_ */
+/**
+ * Create a mutex instance.
+ *
+ * @param type         type of mutex to create
+ * @return                     unlocked mutex instance
+ */
+mutex_t *mutex_create(mutex_type_t type);
+
+#endif /** THREADING_MUTEX_H_ @} */
 
index bf59f50..ee9fb10 100644 (file)
 #define _GNU_SOURCE
 #include <pthread.h>
 
-#include <threading.h>
 #include <library.h>
 #include <debug.h>
 
 #include "rwlock.h"
+#include "condvar.h"
+#include "mutex.h"
 #include "lock_profiler.h"
 
+typedef struct private_rwlock_t private_rwlock_t;
+
+/**
+ * private data of rwlock
+ */
+struct private_rwlock_t {
+
+       /**
+        * public functions
+        */
+       rwlock_t public;
+
+#ifdef HAVE_PTHREAD_RWLOCK_INIT
+
+       /**
+        * wrapped pthread rwlock
+        */
+       pthread_rwlock_t rwlock;
+
+#else
+
+       /**
+        * mutex to emulate a native rwlock
+        */
+       mutex_t *mutex;
+
+       /**
+        * condvar to handle writers
+        */
+       condvar_t *writers;
+
+       /**
+        * condvar to handle readers
+        */
+       condvar_t *readers;
+
+       /**
+        * number of waiting writers
+        */
+       u_int waiting_writers;
+
+       /**
+        * number of readers holding the lock
+        */
+       u_int reader_count;
+
+       /**
+        * current writer thread, if any
+        */
+       pthread_t writer;
+
+#endif /* HAVE_PTHREAD_RWLOCK_INIT */
+
+       /**
+        * profiling info, if enabled
+        */
+       lock_profile_t profile;
+};
+
+
 #ifdef HAVE_PTHREAD_RWLOCK_INIT
 
 /**
index 2f4330f..a86a241 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Tobias Brunner
+ * Copyright (C) 2008-2009 Tobias Brunner
  * Copyright (C) 2008 Martin Willi
  * Hochschule fuer Technik Rapperswil
  *
  * for more details.
  */
 
+/**
+ * @defgroup rwlock rwlock
+ * @{ @ingroup threading
+ */
+
 #ifndef THREADING_RWLOCK_H_
 #define THREADING_RWLOCK_H_
 
-#include "lock_profiler.h"
-
-typedef struct private_rwlock_t private_rwlock_t;
+typedef struct rwlock_t rwlock_t;
+typedef enum rwlock_type_t rwlock_type_t;
 
 /**
- * private data of rwlock
+ * Type of read-write lock.
  */
-struct private_rwlock_t {
-
-       /**
-        * public functions
-        */
-       rwlock_t public;
-
-#ifdef HAVE_PTHREAD_RWLOCK_INIT
-
-       /**
-        * wrapped pthread rwlock
-        */
-       pthread_rwlock_t rwlock;
-
-#else
-
-       /**
-        * mutex to emulate a native rwlock
-        */
-       mutex_t *mutex;
+enum rwlock_type_t {
+       /** default condvar */
+       RWLOCK_TYPE_DEFAULT = 0,
+};
 
-       /**
-        * condvar to handle writers
       */
-       condvar_t *writers;
+/**
+ * Read-Write lock wrapper.
+ */
+struct rwlock_t {
 
        /**
-        * condvar to handle readers
+        * Acquire the read lock.
         */
-       condvar_t *readers;
+       void (*read_lock)(rwlock_t *this);
 
        /**
-        * number of waiting writers
+        * Acquire the write lock.
         */
-       u_int waiting_writers;
+       void (*write_lock)(rwlock_t *this);
 
        /**
-        * number of readers holding the lock
+        * Try to acquire the write lock.
+        *
+        * Never blocks, but returns FALSE if the lock was already occupied.
+        *
+        * @return              TRUE if lock acquired
         */
-       u_int reader_count;
+       bool (*try_write_lock)(rwlock_t *this);
 
        /**
-        * current writer thread, if any
+        * Release any acquired lock.
         */
-       pthread_t writer;
-
-#endif /* HAVE_PTHREAD_RWLOCK_INIT */
+       void (*unlock)(rwlock_t *this);
 
        /**
-        * profiling info, if enabled
+        * Destroy the read-write lock.
         */
-       lock_profile_t profile;
+       void (*destroy)(rwlock_t *this);
 };
 
-#endif /* THREADING_THREADING_H_ */
+/**
+ * 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_RWLOCK_H_ @} */