xrootd
XrdClientThread.hh
Go to the documentation of this file.
00001 #ifndef XRC_THREAD_H
00002 #define XRC_THREAD_H
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                  X r d C l i e n t T h r e a d . h h                       */
00006 /*                                                                            */
00007 /* Author: F.Furano (INFN, 2005)                                              */
00008 /*                                                                            */
00009 /* This file is part of the XRootD software suite.                            */
00010 /*                                                                            */
00011 /* XRootD is free software: you can redistribute it and/or modify it under    */
00012 /* the terms of the GNU Lesser General Public License as published by the     */
00013 /* Free Software Foundation, either version 3 of the License, or (at your     */
00014 /* option) any later version.                                                 */
00015 /*                                                                            */
00016 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00017 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00018 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00019 /* License for more details.                                                  */
00020 /*                                                                            */
00021 /* You should have received a copy of the GNU Lesser General Public License   */
00022 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00023 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00024 /*                                                                            */
00025 /* The copyright holder's institutional names and contributor's names may not */
00026 /* be used to endorse or promote products derived from this software without  */
00027 /* specific prior written permission of the institution or contributor.       */
00028 /******************************************************************************/
00029 
00031 //                                                                      //
00032 // An user friendly thread wrapper                                      //
00033 //                                                                      //
00035 
00036 #include "XrdSys/XrdSysPthread.hh"
00037 
00038 void * XrdClientThreadDispatcher(void * arg);
00039 
00040 class XrdClientThread {
00041 private:
00042    pthread_t fThr;
00043 
00044    typedef void *(*VoidRtnFunc_t)(void *, XrdClientThread *);
00045    VoidRtnFunc_t ThreadFunc;
00046    friend void *XrdClientThreadDispatcher(void *);
00047 
00048  public:
00049    struct XrdClientThreadArgs {
00050       void *arg;
00051       XrdClientThread *threadobj;
00052    } fArg;
00053    
00054    
00055    XrdClientThread(VoidRtnFunc_t fn) {
00056 #ifndef WIN32
00057       fThr = 0;
00058 #endif
00059       ThreadFunc = fn;
00060    };
00061 
00062    virtual ~XrdClientThread() {
00063 
00064 //      Cancel();
00065    };
00066 
00067    int Cancel() {
00068       return XrdSysThread::Cancel(fThr);
00069    };
00070 
00071    int Run(void *arg = 0) {
00072       fArg.arg = arg;
00073       fArg.threadobj = this;
00074       return XrdSysThread::Run(&fThr, XrdClientThreadDispatcher, (void *)&fArg,
00075                                XRDSYSTHREAD_HOLD, "");
00076    };
00077 
00078    int Detach() {
00079       return XrdSysThread::Detach(fThr);
00080    };
00081 
00082    int Join(void **ret = 0) {
00083       return XrdSysThread::Join(fThr, ret);
00084    };
00085 
00086    // these funcs are to be called only from INSIDE the thread loop
00087    int     SetCancelOn() {
00088       return XrdSysThread::SetCancelOn();
00089    };
00090    int     SetCancelOff() {
00091       return XrdSysThread::SetCancelOff();
00092    };
00093    int     SetCancelAsynchronous() {
00094       return XrdSysThread::SetCancelAsynchronous();
00095    };
00096    int     SetCancelDeferred() {
00097       return XrdSysThread::SetCancelDeferred();
00098    };
00099    void     CancelPoint() {
00100       XrdSysThread::CancelPoint();
00101    };
00102 
00103    int MaskSignal(int snum = 0, bool block = 1);
00104 };
00105 #endif