summaryrefslogtreecommitdiff
path: root/src/lib/cdap_req.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/cdap_req.c')
-rw-r--r--src/lib/cdap_req.c45
1 files changed, 27 insertions, 18 deletions
diff --git a/src/lib/cdap_req.c b/src/lib/cdap_req.c
index 2f55b107..b60e73ad 100644
--- a/src/lib/cdap_req.c
+++ b/src/lib/cdap_req.c
@@ -3,8 +3,8 @@
*
* CDAP - CDAP request management
*
- * Sander Vrijders <[email protected]>
- * Dimitri Staessens <[email protected]>
+ * Dimitri Staessens <[email protected]>
+ * Sander Vrijders <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@@ -30,7 +30,8 @@
#include <stdlib.h>
#include <assert.h>
-struct cdap_req * cdap_req_create(cdap_key_t key)
+struct cdap_req * cdap_req_create(int fd,
+ cdap_key_t key)
{
struct cdap_req * creq = malloc(sizeof(*creq));
pthread_condattr_t cattr;
@@ -38,10 +39,10 @@ struct cdap_req * cdap_req_create(cdap_key_t key)
if (creq == NULL)
return NULL;
- creq->key = key;
+ creq->fd = fd;
+ creq->key = key;
creq->state = REQ_INIT;
-
- creq->response = -1;
+ creq->response = -1;
creq->data.data = NULL;
creq->data.len = 0;
@@ -65,17 +66,21 @@ void cdap_req_destroy(struct cdap_req * creq)
pthread_mutex_lock(&creq->lock);
- if (creq->state == REQ_DESTROY) {
+ switch(creq->state) {
+ case REQ_DESTROY:
pthread_mutex_unlock(&creq->lock);
return;
- }
-
- if (creq->state == REQ_INIT)
+ case REQ_INIT:
creq->state = REQ_NULL;
-
- if (creq->state == REQ_PENDING) {
+ pthread_cond_broadcast(&creq->cond);
+ break;
+ case REQ_PENDING:
+ case REQ_RESPONSE:
creq->state = REQ_DESTROY;
pthread_cond_broadcast(&creq->cond);
+ break;
+ default:
+ break;
}
while (creq->state != REQ_NULL)
@@ -110,21 +115,25 @@ int cdap_req_wait(struct cdap_req * creq)
creq->state = REQ_PENDING;
pthread_cond_broadcast(&creq->cond);
- while (creq->state == REQ_PENDING) {
+ while (creq->state == REQ_PENDING && ret != -ETIMEDOUT)
ret = -pthread_cond_timedwait(&creq->cond,
&creq->lock,
&abstime);
- if (ret == -ETIMEDOUT)
- break;
- }
- if (creq->state == REQ_DESTROY) {
+ switch(creq->state) {
+ case REQ_DESTROY:
ret = -1;
+ case REQ_PENDING:
creq->state = REQ_NULL;
pthread_cond_broadcast(&creq->cond);
- } else {
+ break;
+ case REQ_RESPONSE:
creq->state = REQ_DONE;
pthread_cond_broadcast(&creq->cond);
+ break;
+ default:
+ assert(false);
+ break;
}
pthread_mutex_unlock(&creq->lock);