diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-08-25 20:50:10 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-08-25 20:51:42 +0200 |
commit | 3c29ceffe961b1b701e8dc91ca88410ea9b3f1e5 (patch) | |
tree | 49d5b38fd1e478e9758c3abf3928adf8f8137777 | |
parent | m + add header composing macros (diff) | |
download | mds-3c29ceffe961b1b701e8dc91ca88410ea9b3f1e5.tar.gz mds-3c29ceffe961b1b701e8dc91ca88410ea9b3f1e5.tar.bz2 mds-3c29ceffe961b1b701e8dc91ca88410ea9b3f1e5.tar.xz |
libmdsclient: comm: init and destroy
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | src/libmdsclient/comm.c | 33 | ||||
-rw-r--r-- | src/libmdsclient/comm.h | 16 |
2 files changed, 43 insertions, 6 deletions
diff --git a/src/libmdsclient/comm.c b/src/libmdsclient/comm.c index b3091fb..6c1f34f 100644 --- a/src/libmdsclient/comm.c +++ b/src/libmdsclient/comm.c @@ -18,19 +18,28 @@ #include "comm.h" #include <stdlib.h> +#include <unistd.h> /** - * Initialise a connection descriptor with the the default values + * Initialise a connection descriptor * * @param this The connection descriptor + * @return Zero on success, -1 on error, `ernno` + * will have been set accordingly on error */ -void libmds_connection_initialise(libmds_connection_t* restrict this) +int libmds_connection_initialise(libmds_connection_t* restrict this) { this->socket_fd = -1; this->message_id = UINT32_MAX; this->client_id = NULL; + this->mutex_initialised = 0; + errno = pthread_mutex_init(&(this->mutex), NULL); + if (errno) + return -1; + this->mutex_initialised = 1; + return 0; } @@ -63,7 +72,25 @@ void libmds_connection_destroy(libmds_connection_t* restrict this) if (this == NULL) return; - /* TODO */ + if (this->socket_fd >= 0) + { + while (close(this->socket_fd)) + { + if (errno == EINTR) + continue; + break; /* errno may be EBADF or EIO. */ + } + this->socket_fd = -1; + } + + free(this->client_id); + this->client_id = NULL; + + if (this->mutex_initialised) + { + this->mutex_initialised = 0; + pthread_mutex_destroy(&(this->mutex)); /* Can return EBUSY. */ + } } diff --git a/src/libmdsclient/comm.h b/src/libmdsclient/comm.h index 46f9ed1..833fa97 100644 --- a/src/libmdsclient/comm.h +++ b/src/libmdsclient/comm.h @@ -53,19 +53,29 @@ typedef struct libmds_connection /** * Mutex used to hinder concurrent modification * and concurrent message passing + * + * This mutex is a fast mutex, a thread may not + * lock it more than once */ pthread_mutex_t mutex; + /** + * Whether `mutex` is initialised + */ + int mutex_initialised; + } libmds_connection_t; /** - * Initialise a connection descriptor with the the default values + * Initialise a connection descriptor * - * @param this The connection descriptor + * @param this The connection descriptor + * @return Zero on success, -1 on error, `ernno` + * will have been set accordingly on error */ __attribute__((nonnull)) -void libmds_connection_initialise(libmds_connection_t* restrict this); +int libmds_connection_initialise(libmds_connection_t* restrict this); /** * Allocate and initialise a connection descriptor |