TWin32Thread.cxx

Go to the documentation of this file.
00001 // @(#)root/thread:$Id: TWin32Thread.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Bertrand Bellenot  20/10/2004
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TWin32Thread                                                         //
00015 //                                                                      //
00016 // This class provides an interface to the win32 thread routines.       //
00017 //                                                                      //
00018 //////////////////////////////////////////////////////////////////////////
00019 
00020 #include "TWin32Thread.h"
00021 #include <process.h>
00022 #include <errno.h>
00023 
00024 ClassImp(TWin32Thread)
00025 
00026 //______________________________________________________________________________
00027 Int_t TWin32Thread::Run(TThread *th)
00028 {
00029    // Win32 threads -- spawn new thread (like pthread_create).
00030    // Win32 has a thread handle in addition to the thread ID.
00031 
00032    DWORD  dwThreadId;
00033    HANDLE hHandle = CreateThread(0, 0,
00034                                  (LPTHREAD_START_ROUTINE)&TThread::Function,
00035                                  th, 0, (DWORD*)&dwThreadId);
00036    if (th->fDetached) {
00037       ::CloseHandle(hHandle);
00038       th->fHandle = 0L;
00039    } else
00040       th->fHandle = (Long_t)hHandle;
00041 
00042    th->fId = dwThreadId;
00043 
00044    return hHandle ? 0 : EINVAL;
00045 }
00046 
00047 //______________________________________________________________________________
00048 Int_t TWin32Thread::Join(TThread *th, void **ret)
00049 {
00050    // Wait for specified thread execution (if any) to complete
00051    // (like pthread_join).
00052 
00053    DWORD R = WaitForSingleObject((HANDLE)th->fHandle, INFINITE);
00054 
00055    if ( (R == WAIT_OBJECT_0) || (R == WAIT_ABANDONED) ) {
00056       //::CloseHandle((HANDLE)th->fHandle);
00057       return 0;
00058    }
00059    if ( R == WAIT_TIMEOUT )
00060       return EAGAIN;
00061    return EINVAL;
00062 }
00063 
00064 //______________________________________________________________________________
00065 Int_t TWin32Thread::Exit(void *ret)
00066 {
00067    // Exit the thread.
00068 
00069    ExitThread(0);
00070    return 0;
00071 }
00072 
00073 //______________________________________________________________________________
00074 Int_t TWin32Thread::Kill(TThread *th)
00075 {
00076    // This is a somewhat dangerous function; it's not
00077    // suggested to Stop() threads a lot.
00078 
00079    if (TerminateThread((HANDLE)th->fHandle,0)) {
00080       th->fState = TThread::kCanceledState;
00081       return 0;
00082    }
00083    return EINVAL;
00084 }
00085 
00086 //______________________________________________________________________________
00087 Int_t TWin32Thread::CleanUpPush(void **main, void *free,void *arg)
00088 {
00089    if (!free) fprintf(stderr, "CleanUpPush ***ERROR*** Routine=0\n");
00090    new TWin32ThreadCleanUp(main,free,arg);
00091    return 0;
00092 }
00093 
00094 //______________________________________________________________________________
00095 Int_t TWin32Thread::CleanUpPop(void **main,Int_t exe)
00096 {
00097    if (!*main) return 1;
00098    TWin32ThreadCleanUp *l = (TWin32ThreadCleanUp*)(*main);
00099    if (!l->fRoutine) fprintf(stderr,"CleanUpPop ***ERROR*** Routine=0\n");
00100    if (exe && l->fRoutine) ((void (*)(void*))(l->fRoutine))(l->fArgument);
00101    *main = l->fNext;  delete l;
00102    return 0;
00103 }
00104 
00105 //______________________________________________________________________________
00106 Int_t TWin32Thread::CleanUp(void **main)
00107 {
00108    fprintf(stderr," CleanUp %lx\n",(ULong_t)*main);
00109    while(!CleanUpPop(main,1)) { }
00110    return 0;
00111 }
00112 
00113 //______________________________________________________________________________
00114 Long_t TWin32Thread::SelfId()
00115 {
00116    // Return the current thread's ID.
00117 
00118    return (Long_t)::GetCurrentThreadId();
00119 }
00120 
00121 //______________________________________________________________________________
00122 Int_t TWin32Thread::SetCancelOff()
00123 {
00124    if (gDebug)
00125       Warning("SetCancelOff", "Not implemented on Win32");
00126    return 0;
00127 }
00128 
00129 //______________________________________________________________________________
00130 Int_t TWin32Thread::SetCancelOn()
00131 {
00132    if (gDebug)
00133       Warning("SetCancelOn", "Not implemented on Win32");
00134    return 0;
00135 }
00136 
00137 //______________________________________________________________________________
00138 Int_t TWin32Thread::SetCancelAsynchronous()
00139 {
00140    if (gDebug)
00141       Warning("SetCancelAsynchronous", "Not implemented on Win32");
00142    return 0;
00143 }
00144 
00145 //______________________________________________________________________________
00146 Int_t TWin32Thread::SetCancelDeferred()
00147 {
00148    if (gDebug)
00149       Warning("SetCancelDeferred", "Not implemented on Win32");
00150    return 0;
00151 }
00152 
00153 //______________________________________________________________________________
00154 Int_t TWin32Thread::CancelPoint()
00155 {
00156    if (gDebug)
00157       Warning("CancelPoint", "Not implemented on Win32");
00158    return 0;
00159 }
00160 
00161 //   Clean Up section. PTHREAD implementations of cleanup after cancel are
00162 //   too different and often too bad. Temporary I invent my own bicycle.
00163 //                                                              V.Perev.
00164 
00165 //______________________________________________________________________________
00166 TWin32ThreadCleanUp::TWin32ThreadCleanUp(void **main, void *routine, void *arg)
00167 {
00168    fNext = (TWin32ThreadCleanUp*)*main;
00169    fRoutine = routine; fArgument = arg;
00170    *main  = this;
00171 }

Generated on Tue Jul 5 14:12:02 2011 for ROOT_528-00b_version by  doxygen 1.5.1