summaryrefslogtreecommitdiff
path: root/code/core/msgq.c
blob: 53c55ea819ccbeffae13f1b2f42374a95adc6545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "core.h"

#ifdef ECP_WITH_PTHREAD

#include <sys/time.h>

#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))

#define MSG_NEXT(msgi, max_msgs)    ((msgi + 1) % max_msgs)

static struct timespec *abstime_ts(struct timespec *ts, unsigned int msec) {
    struct timeval tv;
    uint64_t us_start;
    
    gettimeofday(&tv, NULL);
    us_start = tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec;
    us_start += msec * 1000;
    ts->tv_sec = us_start / 1000000;
    ts->tv_nsec = (us_start % 1000000) * 1000;
    return ts;
}

int ecp_conn_msgq_create(ECPConnection *conn) {
    int i;
    
    for (i=0; i<ECP_MAX_MTYPE; i++) {
        int rv = pthread_cond_init(&conn->msgq.cond[i], NULL);
        if (rv) {
            int j;

            for (j=0; j<i; j++) {
                pthread_cond_destroy(&conn->msgq.cond[j]);
            }
            return ECP_ERR;
        }
    }

    return ECP_OK;
}

void ecp_conn_msgq_destroy(ECPConnection *conn) {
    int i;
    
    for (i=0; i<ECP_MAX_MTYPE; i++) {
        pthread_cond_destroy(&conn->msgq.cond[i]);
    }
}

ssize_t ecp_conn_msgq_push(ECPConnection *conn, unsigned char *msg, size_t msg_size) {
    ECPConnMsgQ *msgq = &conn->msgq;
    unsigned short msg_idx = msgq->empty_idx;
    unsigned short i;
    unsigned short done = 0;
    unsigned char mtype;
    
    if (msg_size == 0) return ECP_OK;

    mtype = *msg;
    msg++;
    msg_size--;

    if (mtype >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE;
    if (msg_size >= ECP_MAX_MSG) return ECP_ERR_MAX_MSG;
    if (msg_size < ECP_MIN_MSG) return ECP_ERR_MIN_MSG;
    
    for (i=0; i<ECP_MAX_CONN_MSG; i++) {
        if (!msgq->occupied[msg_idx]) {
            ECPMessage *message = &msgq->msg[msg_idx];
            if (msg_size > 0) memcpy(message->msg, msg, msg_size);
            message->size = msg_size;
            if (msgq->r_idx[mtype] == msgq->w_idx[mtype]) pthread_cond_signal(&msgq->cond[mtype]);
            msgq->msg_idx[mtype][msgq->w_idx[mtype]] = msg_idx;
            msgq->w_idx[mtype] = MSG_NEXT(msgq->w_idx[mtype], ECP_MAX_CONN_MSG+1);

            msgq->empty_idx = MSG_NEXT(msg_idx, ECP_MAX_CONN_MSG);
            msgq->occupied[msg_idx] = 1;
            done = 1;
            break;
        }
        msg_idx = MSG_NEXT(msg_idx, ECP_MAX_CONN_MSG);
    }
    if (done) {
        return msg_size+1;
    } else {
        return ECP_ERR_MAX_CONN_MSG;
    }
}

ssize_t ecp_conn_msgq_pop(ECPConnection *conn, unsigned char mtype, unsigned char *msg, size_t msg_size, unsigned int timeout) {
    ECPConnMsgQ *msgq = &conn->msgq;
    ECPMessage *message;
    ssize_t rv = ECP_OK;
    unsigned short msg_idx;

    if (mtype >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE;

    if (msgq->r_idx[mtype] == msgq->w_idx[mtype]) {
        if (timeout == -1) {
            pthread_cond_wait(&msgq->cond[mtype], &conn->mutex);
        } else {
            struct timespec ts;
            int _rv = pthread_cond_timedwait(&msgq->cond[mtype], &conn->mutex, abstime_ts(&ts, timeout));
            if (_rv) rv = ECP_ERR_TIMEOUT;
        }
    }
    if (!rv) {
        msg_idx = msgq->msg_idx[mtype][msgq->r_idx[mtype]];
        msgq->r_idx[mtype] = MSG_NEXT(msgq->r_idx[mtype], ECP_MAX_CONN_MSG+1);
        msgq->occupied[msg_idx] = 0;
        message = &msgq->msg[msg_idx];
        rv = message->size;
        if (rv >= 0) {
            rv = MIN(msg_size, rv);
            memcpy(msg, message->msg, rv);
        }
    }
    return rv;
}

#endif  /* ECP_WITH_PTHREAD */