make accessing tr_list's recycle nodes threadsafe. patch by mike.did, fixes #5352

This commit is contained in:
Jordan Lee 2013-08-25 16:27:19 +00:00
parent 5661aaad5e
commit d112a813e9
1 changed files with 16 additions and 0 deletions

View File

@ -12,12 +12,24 @@
#include "transmission.h" #include "transmission.h"
#include "list.h" #include "list.h"
#include "platform.h"
#include "utils.h" #include "utils.h"
static const tr_list TR_LIST_CLEAR = { NULL, NULL, NULL }; static const tr_list TR_LIST_CLEAR = { NULL, NULL, NULL };
static tr_list * recycled_nodes = NULL; static tr_list * recycled_nodes = NULL;
static tr_lock*
getRecycledNodesLock (void)
{
static tr_lock * l = NULL;
if (!l)
l = tr_lockNew ();
return l;
}
static tr_list* static tr_list*
node_alloc (void) node_alloc (void)
{ {
@ -29,8 +41,10 @@ node_alloc (void)
} }
else else
{ {
tr_lockLock (getRecycledNodesLock ());
ret = recycled_nodes; ret = recycled_nodes;
recycled_nodes = recycled_nodes->next; recycled_nodes = recycled_nodes->next;
tr_lockUnlock (getRecycledNodesLock ());
} }
*ret = TR_LIST_CLEAR; *ret = TR_LIST_CLEAR;
@ -43,8 +57,10 @@ node_free (tr_list* node)
if (node != NULL) if (node != NULL)
{ {
*node = TR_LIST_CLEAR; *node = TR_LIST_CLEAR;
tr_lockLock (getRecycledNodesLock ());
node->next = recycled_nodes; node->next = recycled_nodes;
recycled_nodes = node; recycled_nodes = node;
tr_lockUnlock (getRecycledNodesLock ());
} }
} }