Branch: Tag:

2018-12-04

2018-12-04 17:11:38 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Protocols.HTTP.Session: Improved thread-safety.

Fixes race-condition when multiple threads call give_me_connection()
with the same url at the same time.

Fixes [PIKE-148].

1:   #pike __REAL_VERSION__    - // $Id: Session.pike,v 1.14 2004/01/11 00:49:02 nilsson Exp $ + // $Id$      import Protocols.HTTP;   
601:      // internal (but readable for debug purposes)   mapping(string:array(KeptConnection)) connection_cache=([]); + Thread.Mutex connection_cache_mux = Thread.Mutex();   int connections_kept_n=0;   int connections_inuse_n=0;   mapping(string:int) connections_host_n=([]);
612:       void create(string _lookup,Query _q)    { +  Thread.MutexKey key = connection_cache_mux->lock(2);    lookup=_lookup;    q=_q;   
623:       void disconnect()    { +  Thread.MutexKey key = connection_cache_mux->lock(2);    connection_cache[lookup]-=({this});    if (!sizeof(connection_cache[lookup]))    m_delete(connection_cache,lookup);
638:       Query use()    { +  Thread.MutexKey key = connection_cache_mux->lock(2);    connection_cache[lookup]-=({this});    if (!sizeof(connection_cache[lookup]))    m_delete(connection_cache,lookup);
660:   {    Query q;    +  Thread.MutexKey key = connection_cache_mux->lock(); +     if (array(KeptConnection) v =    connection_cache[connection_lookup(url)])    {