Revert "add nested spinlock, see issue #646"

This reverts commit ba59ed2e6c.
This commit is contained in:
Cloud Wu
2017-06-27 22:08:33 +08:00
parent ba59ed2e6c
commit 9320d8b28d
2 changed files with 16 additions and 82 deletions

View File

@@ -37,9 +37,6 @@
#define PRIORITY_HIGH 0
#define PRIORITY_LOW 1
#define DIRECTWRITE_SOCKETTHREAD 1
#define DIRECTWRITE_WORKERTHREAD 2
#define HASH_ID(id) (((unsigned)id) % MAX_SOCKET)
#define PROTOCOL_TCP 0
@@ -91,7 +88,7 @@ struct socket {
int size;
uint8_t udp_address[UDP_ADDRESS_SIZE];
} p;
struct spinlock_nested dw_lock;
struct spinlock dw_lock;
int dw_offset;
const void * dw_buffer;
size_t dw_size;
@@ -365,7 +362,7 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_message *r
if (s->type != SOCKET_TYPE_PACCEPT && s->type != SOCKET_TYPE_PLISTEN) {
sp_del(ss->event_fd, s->fd);
}
spinlock_nestedlock(&s->dw_lock, DIRECTWRITE_SOCKETTHREAD);
spinlock_lock(&s->dw_lock);
if (s->type != SOCKET_TYPE_BIND) {
if (close(s->fd) < 0) {
perror("close socket:");
@@ -376,7 +373,7 @@ force_close(struct socket_server *ss, struct socket *s, struct socket_message *r
free_buffer(ss, s->dw_buffer, s->dw_size);
s->dw_buffer = NULL;
}
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_SOCKETTHREAD);
spinlock_unlock(&s->dw_lock);
}
void
@@ -422,7 +419,7 @@ new_fd(struct socket_server *ss, int id, int fd, int protocol, uintptr_t opaque,
s->warn_size = 0;
check_wb_list(&s->high);
check_wb_list(&s->low);
spinlock_nestedinit(&s->dw_lock);
spinlock_init(&s->dw_lock);
s->dw_buffer = NULL;
s->dw_size = 0;
return s;
@@ -685,7 +682,7 @@ send_buffer_(struct socket_server *ss, struct socket *s, struct socket_message *
static int
send_buffer(struct socket_server *ss, struct socket *s, struct socket_message *result) {
if (!spinlock_nestedtrylock(&s->dw_lock, DIRECTWRITE_SOCKETTHREAD))
if (!spinlock_trylock(&s->dw_lock))
return -1; // blocked by direct write, send later.
if (s->dw_buffer) {
// add direct write buffer before high.head
@@ -706,7 +703,7 @@ send_buffer(struct socket_server *ss, struct socket *s, struct socket_message *r
s->dw_buffer = NULL;
}
int r = send_buffer_(ss,s,result);
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_SOCKETTHREAD);
spinlock_unlock(&s->dw_lock);
return r;
}
@@ -1421,7 +1418,7 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz
return -1;
}
if (can_direct_write(s,id) && spinlock_nestedtrylock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD)) {
if (can_direct_write(s,id) && spinlock_trylock(&s->dw_lock)) {
// may be we can send directly, double check
if (can_direct_write(s,id)) {
// send directly
@@ -1441,7 +1438,7 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz
}
if (n == so.sz) {
// write done
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD);
spinlock_unlock(&s->dw_lock);
so.free_func((void *)buffer);
return 0;
}
@@ -1449,14 +1446,12 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz
s->dw_buffer = buffer;
s->dw_size = sz;
s->dw_offset = n;
spinlock_unlock(&s->dw_lock);
sp_write(ss->event_fd, s->fd, s, true);
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD);
return 0;
}
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD);
spinlock_unlock(&s->dw_lock);
}
struct request_package request;
@@ -1686,7 +1681,7 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp
return -1;
}
if (can_direct_write(s,id) && spinlock_nestedtrylock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD)) {
if (can_direct_write(s,id) && spinlock_trylock(&s->dw_lock)) {
// may be we can send directly, double check
if (can_direct_write(s,id)) {
// send directly
@@ -1697,12 +1692,12 @@ socket_server_udp_send(struct socket_server *ss, int id, const struct socket_udp
int n = sendto(s->fd, so.buffer, so.sz, 0, &sa.s, sasz);
if (n >= 0) {
// sendto succ
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD);
spinlock_unlock(&s->dw_lock);
so.free_func((void *)buffer);
return 0;
}
}
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD);
spinlock_unlock(&s->dw_lock);
// let socket thread try again, udp doesn't care the order
}
@@ -1723,13 +1718,13 @@ socket_server_udp_connect(struct socket_server *ss, int id, const char * addr, i
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
return -1;
}
spinlock_nestedlock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD);
spinlock_lock(&s->dw_lock);
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD);
spinlock_unlock(&s->dw_lock);
return -1;
}
ATOM_INC(&s->udpconnecting);
spinlock_nestedunlock(&s->dw_lock, DIRECTWRITE_WORKERTHREAD);
spinlock_unlock(&s->dw_lock);
int status;
struct addrinfo ai_hints;

View File

@@ -1,8 +1,6 @@
#ifndef SKYNET_SPINLOCK_H
#define SKYNET_SPINLOCK_H
#include <assert.h>
#define SPIN_INIT(q) spinlock_init(&(q)->lock);
#define SPIN_LOCK(q) spinlock_lock(&(q)->lock);
#define SPIN_UNLOCK(q) spinlock_unlock(&(q)->lock);
@@ -77,63 +75,4 @@ spinlock_destroy(struct spinlock *lock) {
#endif
struct spinlock_nested {
struct spinlock lock;
int thread;
int count;
};
static inline void
spinlock_nestedinit(struct spinlock_nested *ln) {
spinlock_init(&ln->lock);
ln->thread = 0;
ln->count = 0;
}
static inline void
spinlock_nestedlock(struct spinlock_nested *ln, int thread) {
if (thread != ln->thread) {
spinlock_lock(&ln->lock);
assert(ln->count == 0);
ln->thread = thread;
}
++ln->count;
}
static inline int
spinlock_nestedtrylock(struct spinlock_nested *ln, int thread) {
if (thread != ln->thread) {
if (spinlock_trylock(&ln->lock)) {
assert(ln->count == 0);
ln->thread = thread;
++ln->count;
return 1; // lock succ
} else {
return 0;
}
} else {
// in the same thread
++ln->count;
return 1;
}
}
static inline void
spinlock_nestedunlock(struct spinlock_nested *ln, int thread) {
assert(thread == ln->thread);
--ln->count;
if (ln->count > 0) {
return;
}
assert(ln->count == 0);
ln->thread = 0;
spinlock_unlock(&ln->lock);
}
static inline void
spinlock_nesteddestroy(struct spinlock_nested *ln) {
assert(ln->thread == 0 && ln->count == 0);
spinlock_destroy(&ln->lock);
}
#endif