Changeset ed4dc33


Ignore:
Timestamp:
Dec 10, 2017 1:06:49 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
b04924d
Parents:
8e51ca7
Message:

Router: Change RI locking to a read/write lock (ticket #2096)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • router/java/src/net/i2p/router/Router.java

    r8e51ca7 red4dc33  
    2121import java.util.TimeZone;
    2222import java.util.concurrent.ConcurrentHashMap;
     23import java.util.concurrent.locks.ReentrantReadWriteLock;
    2324
    2425import gnu.getopt.Getopt;
     
    7778    private String _configFilename;
    7879    private RouterInfo _routerInfo;
    79     private final Object _routerInfoLock = new Object();
     80    private final ReentrantReadWriteLock _routerInfoLock = new ReentrantReadWriteLock(false);
    8081    /** not for external use */
    8182    public final Object routerInfoFileLock = new Object();
     
    518519     *  Warning - risk of deadlock - do not call while holding locks
    519520     *
     521     *  Note: Due to lock contention, especially during a
     522     *  rebuild of the router info, this may take a long time.
     523     *  For determining the current status of the router, use
     524     *  RouterContext.commSystem().getStatus().
    520525     */
    521526    public RouterInfo getRouterInfo() {
    522         synchronized (_routerInfoLock) {
     527        _routerInfoLock.readLock().lock();
     528        try {
    523529            return _routerInfo;
     530        } finally {
     531            _routerInfoLock.readLock().unlock();
    524532        }
    525533    }
     
    533541     */
    534542    public void setRouterInfo(RouterInfo info) {
    535         synchronized (_routerInfoLock) {
     543        _routerInfoLock.writeLock().lock();
     544        try {
    536545            _routerInfo = info;
     546        } finally {
     547            _routerInfoLock.writeLock().unlock();
    537548        }
    538549        if (_log.shouldLog(Log.INFO))
     
    846857     * Rebuild and republish our routerInfo since something significant
    847858     * has changed.
     859     * This is a non-blocking rebuild.
     860     *
    848861     * Not for external use.
     862     *
     863     * Warning - risk of deadlock - do not call while holding locks
     864     *
    849865     */
    850866    public void rebuildRouterInfo() { rebuildRouterInfo(false); }
     
    857873     *  Warning - risk of deadlock - do not call while holding locks
    858874     *
     875     * @param blockingRebuild If true, netdb publish will happen in-line.
     876     *                        This may take a long time.
    859877     */
    860878    public void rebuildRouterInfo(boolean blockingRebuild) {
    861879        if (_log.shouldLog(Log.INFO))
    862880            _log.info("Rebuilding new routerInfo");
    863         synchronized (_routerInfoLock) {
     881        _routerInfoLock.writeLock().lock();
     882        try {
    864883            locked_rebuildRouterInfo(blockingRebuild);
     884        } finally {
     885            _routerInfoLock.writeLock().unlock();
    865886        }
    866887    }
Note: See TracChangeset for help on using the changeset viewer.