Changeset 1539

Show
Ignore:
Timestamp:
07/20/05 05:48:14 (3 years ago)
Author:
madcat
Message:

Added WorkThread::Pauser object, which is an exception-safe wrapper around
WorkThread::pause()/WorkThread::resume methods.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • hydranode/hncore/plugins/ed2k/src/downloadlist.cpp

    r1501 r1539  
    304304        using namespace boost::filesystem; 
    305305 
    306         WorkThread::instance().pause()
     306        WorkThread::Pauser pauser
    307307 
    308308        for (directory_iterator it(p); it != directory_iterator(); ++it) { 
     
    319319                } 
    320320        } 
    321  
    322         WorkThread::instance().resume(); 
    323321} 
    324322 
  • hydranode/hncore/src/fileslist.cpp

    r1501 r1539  
    9494        CHECK_THROW(!path.empty()); 
    9595        verifyPath(path); 
     96 
     97        WorkThread::Pauser pauser; // pause WorkThread while scanning 
     98 
    9699        scanSharedDir(path, recurse); 
     100 
    97101} catch (std::exception &e) { 
    98102        logError(boost::format("Error scanning shared directory: %s")%e.what()); 
     
    106110 
    107111        if (scan) { 
     112                WorkThread::Pauser pauser; // pause while scanning 
    108113                scanTempDir(p.native_directory_string()); 
    109114        } 
     
    191196 
    192197        if (recursion == 1) { 
    193                 WorkThread::instance().pause(); 
    194198                logMsg( 
    195199                        boost::format("Scanning shared directory %s %s") 
     
    236240        } 
    237241        recursion--; 
    238         if (recursion == 0) { 
    239                 WorkThread::instance().resume(); 
    240         } 
    241242} catch (std::exception &er) { 
    242243        logError(boost::format("Scanning shared directories: %s") % er.what()); 
    243244        recursion--; 
    244         if (recursion == 0) { 
    245                 WorkThread::instance().resume(); 
    246         } 
    247245} 
    248246MSVC_ONLY(;) 
  • hydranode/hncore/src/hydranode.cpp

    r1532 r1539  
    9090        initSockets(); 
    9191        Log::instance().remPreStr("     "); 
     92 
     93        // starting from here, we get to resource-intensive parts, so make sure 
     94        // workthread doesn't start IO things before we'r finished with startup 
     95        WorkThread::Pauser p; 
    9296 
    9397        logMsg(" * Initializing Shared Files List..."); 
     
    475479                return false; 
    476480        } 
    477 #ifndef WIN32 
    478         if (vm.count("background")) { 
    479                 init(s_argc, s_argv); 
    480                 logMsg("Backgrounding..."); 
    481  
    482                 // redirect stdout/stderr etc to /dev/null 
    483                 close(0); close(1); close(2); 
    484                 SchedBase::instance().disableStatus(); 
    485                 Log::instance().disableOutput(); 
    486                 int fd = open("/dev/null", O_RDWR); 
    487                 dup(fd); dup(fd); dup(fd); 
    488  
    489                 int pid = fork(); 
    490                 if (pid < 0) { 
    491                         perror("fork() failed: "); 
    492                         ::exit(1); 
    493                 } else if (pid == 0) { 
    494                         setsid(); 
    495                         mainLoop(); 
    496                         cleanUp(); 
    497                 } 
    498  
    499                 return false; 
    500         } 
    501 #endif 
    502481        if (vm.count("disable-status")) { 
    503482                SchedBase::instance().disableStatus(); 
     
    560539#endif 
    561540 
     541        // do backgrounding stuff after all other args have been processed 
     542#ifndef WIN32 
     543        if (vm.count("background")) { 
     544 
     545                int pid = fork(); 
     546                if (pid < 0) { 
     547                        perror("fork() failed: "); 
     548                        ::exit(1); 
     549                } else if (pid == 0) { 
     550                        setsid(); 
     551                        init(s_argc, s_argv); 
     552 
     553                        logMsg("Backgrounding..."); 
     554 
     555                        // redirect stdout/stderr etc to /dev/null 
     556                        close(0); close(1); close(2); 
     557                        SchedBase::instance().disableStatus(); 
     558                        Log::instance().disableOutput(); 
     559                        int fd = open("/dev/null", O_RDWR); 
     560                        dup(fd); dup(fd); dup(fd); 
     561 
     562                        mainLoop(); 
     563                        cleanUp(); 
     564                } 
     565 
     566                return false; 
     567        } 
     568#endif 
     569 
    562570        return true; 
    563571} catch (std::exception &e) { 
  • hydranode/include/hncore/workthread.h

    r1501 r1539  
    9494 
    9595        /** 
    96          * Pause the thread execution 
    97          */ 
    98         void pause(); 
    99  
    100         /** 
    101          * Resume the thread execution 
    102          */ 
    103         void resume(); 
    104  
    105         /** 
    10696         * Check whether the thread is running or paused 
    10797         * 
     
    10999         */ 
    110100        bool isRunning(); 
     101 
     102        /** 
     103         * Exception-safe wrapper object for pausing/resuming WorkThread 
     104         */ 
     105        struct Pauser { 
     106                Pauser() { 
     107                        instance().pause(); 
     108                } 
     109                ~Pauser() { 
     110                        instance().resume(); 
     111                } 
     112        }; 
     113 
    111114private: 
    112115        /** 
     
    122125        //! Main work thread loop function 
    123126        void threadLoop(); 
     127 
    124128        //! Used by secondary thread, this is used to check whether the thread 
    125129        //! should exit 
    126130        bool checkExit(); 
     131 
    127132        //! Used by main thread, this signals work thread to exit ASAP 
    128133        void exit(); 
     134 
     135        //! Pause the thread execution 
     136        void pause(); 
     137 
     138        //! Resume the thread execution 
     139        void resume(); 
     140 
    129141 
    130142        //! Pending jobs queue, filled by postWork() and processed by