]> granicus.if.org Git - transmission/commitdiff
(trunk libT) make the evbuffer pool threadsafe
authorCharles Kerr <charles@transmissionbt.com>
Wed, 31 Dec 2008 14:29:28 +0000 (14:29 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Wed, 31 Dec 2008 14:29:28 +0000 (14:29 +0000)
libtransmission/utils.c

index bdd1dc7026934a4e7665de2494440fd5b077b5ee..303b97d3cda3d124f16044c8744427b5abc23631 100644 (file)
@@ -1301,19 +1301,39 @@ tr_int2ptr( int i )
 
 static tr_list * _bufferList = NULL;
 
+static tr_lock *
+getBufferLock( void )
+{
+    static tr_lock * lock = NULL;
+    if( lock == NULL )
+        lock = tr_lockNew( );
+    return lock;
+}
+
 struct evbuffer*
 tr_getBuffer( void )
 {
-    struct evbuffer * buf = tr_list_pop_front( &_bufferList );
+    struct evbuffer * buf;
+    tr_lock * l = getBufferLock( );
+    tr_lockLock( l );
+
+    buf = tr_list_pop_front( &_bufferList );
     if( buf == NULL )
         buf = evbuffer_new( );
-    assert( !EVBUFFER_LENGTH( buf ) );
+
+    tr_lockUnlock( l );
     return buf;
 }
 
 void
 tr_releaseBuffer( struct evbuffer * buf )
 {
+    tr_lock * l = getBufferLock( );
+    tr_lockLock( l );
+
     evbuffer_drain( buf, EVBUFFER_LENGTH( buf ) );
+    assert( EVBUFFER_LENGTH( buf ) == 0 );
     tr_list_prepend( &_bufferList, buf );
+
+    tr_lockUnlock( l );
 }