mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
new harbor
This commit is contained in:
@@ -13,6 +13,7 @@ struct message_queue {
|
||||
int head;
|
||||
int tail;
|
||||
int lock;
|
||||
int in_global;
|
||||
struct skynet_message *queue;
|
||||
};
|
||||
|
||||
@@ -24,14 +25,6 @@ struct global_queue {
|
||||
struct message_queue ** queue;
|
||||
};
|
||||
|
||||
struct message_remote_queue {
|
||||
int cap;
|
||||
int head;
|
||||
int tail;
|
||||
int lock;
|
||||
struct skynet_remote_message *queue;
|
||||
};
|
||||
|
||||
static struct global_queue *Q = NULL;
|
||||
|
||||
static inline void
|
||||
@@ -42,7 +35,7 @@ _lock_global_queue() {
|
||||
#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {}
|
||||
#define UNLOCK(q) __sync_lock_release(&(q)->lock);
|
||||
|
||||
void
|
||||
static void
|
||||
skynet_globalmq_push(struct message_queue * queue) {
|
||||
struct global_queue *q= Q;
|
||||
LOCK(q)
|
||||
@@ -95,6 +88,7 @@ skynet_mq_create(uint32_t handle) {
|
||||
q->head = 0;
|
||||
q->tail = 0;
|
||||
q->lock = 0;
|
||||
q->in_global = 1;
|
||||
q->queue = malloc(sizeof(struct skynet_message) * q->cap);
|
||||
|
||||
return q;
|
||||
@@ -114,7 +108,7 @@ skynet_mq_handle(struct message_queue *q) {
|
||||
|
||||
int
|
||||
skynet_mq_pop(struct message_queue *q, struct skynet_message *message) {
|
||||
int ret = -1;
|
||||
int ret = 1;
|
||||
LOCK(q)
|
||||
|
||||
if (q->head != q->tail) {
|
||||
@@ -124,6 +118,10 @@ skynet_mq_pop(struct message_queue *q, struct skynet_message *message) {
|
||||
q->head = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
q->in_global = 0;
|
||||
}
|
||||
|
||||
UNLOCK(q)
|
||||
|
||||
@@ -134,23 +132,30 @@ void
|
||||
skynet_mq_push(struct message_queue *q, struct skynet_message *message) {
|
||||
LOCK(q)
|
||||
|
||||
q->queue[q->tail] = *message;
|
||||
if (++ q->tail >= q->cap) {
|
||||
q->tail = 0;
|
||||
if (message) {
|
||||
q->queue[q->tail] = *message;
|
||||
if (++ q->tail >= q->cap) {
|
||||
q->tail = 0;
|
||||
}
|
||||
|
||||
if (q->head == q->tail) {
|
||||
struct skynet_message *new_queue = malloc(sizeof(struct skynet_message) * q->cap * 2);
|
||||
int i;
|
||||
for (i=0;i<q->cap;i++) {
|
||||
new_queue[i] = q->queue[(q->head + i) % q->cap];
|
||||
}
|
||||
q->head = 0;
|
||||
q->tail = q->cap;
|
||||
q->cap *= 2;
|
||||
|
||||
free(q->queue);
|
||||
q->queue = new_queue;
|
||||
}
|
||||
}
|
||||
|
||||
if (q->head == q->tail) {
|
||||
struct skynet_message *new_queue = malloc(sizeof(struct skynet_message) * q->cap * 2);
|
||||
int i;
|
||||
for (i=0;i<q->cap;i++) {
|
||||
new_queue[i] = q->queue[(q->head + i) % q->cap];
|
||||
}
|
||||
q->head = 0;
|
||||
q->tail = q->cap;
|
||||
q->cap *= 2;
|
||||
|
||||
free(q->queue);
|
||||
q->queue = new_queue;
|
||||
if (q->in_global == 0) {
|
||||
skynet_globalmq_push(q);
|
||||
q->in_global = 1;
|
||||
}
|
||||
|
||||
UNLOCK(q)
|
||||
@@ -170,68 +175,8 @@ skynet_mq_init(int n) {
|
||||
Q=q;
|
||||
}
|
||||
|
||||
// remote message queue
|
||||
|
||||
struct message_remote_queue *
|
||||
skynet_remotemq_create(void) {
|
||||
struct message_remote_queue *q = malloc(sizeof(*q));
|
||||
q->cap = DEFAULT_QUEUE_SIZE;
|
||||
q->head = 0;
|
||||
q->tail = 0;
|
||||
q->lock = 0;
|
||||
q->queue = malloc(sizeof(struct skynet_remote_message) * q->cap);
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
void
|
||||
skynet_remotemq_release(struct message_remote_queue *q) {
|
||||
free(q->queue);
|
||||
free(q);
|
||||
}
|
||||
|
||||
int
|
||||
skynet_remotemq_pop(struct message_remote_queue *q, struct skynet_remote_message *message) {
|
||||
int ret = -1;
|
||||
LOCK(q)
|
||||
|
||||
if (q->head != q->tail) {
|
||||
*message = q->queue[q->head];
|
||||
ret = 0;
|
||||
if ( ++ q->head >= q->cap) {
|
||||
q->head = 0;
|
||||
}
|
||||
}
|
||||
|
||||
UNLOCK(q)
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
skynet_remotemq_push(struct message_remote_queue *q, struct skynet_remote_message *message) {
|
||||
assert(message->destination != 0);
|
||||
LOCK(q)
|
||||
|
||||
q->queue[q->tail] = *message;
|
||||
|
||||
if (++ q->tail >= q->cap) {
|
||||
q->tail = 0;
|
||||
}
|
||||
|
||||
if (q->head == q->tail) {
|
||||
struct skynet_remote_message *new_queue = malloc(sizeof(struct skynet_remote_message) * q->cap * 2);
|
||||
int i;
|
||||
for (i=0;i<q->cap;i++) {
|
||||
new_queue[i] = q->queue[(q->head + i) % q->cap];
|
||||
}
|
||||
q->head = 0;
|
||||
q->tail = q->cap;
|
||||
q->cap *= 2;
|
||||
|
||||
free(q->queue);
|
||||
q->queue = new_queue;
|
||||
}
|
||||
|
||||
UNLOCK(q)
|
||||
skynet_mq_force_push(struct message_queue * queue) {
|
||||
assert(queue->in_global);
|
||||
skynet_globalmq_push(queue);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user