Thread_POSIX.h

Go to the documentation of this file.
00001 /* ES40 emulator.
00002  * Copyright (C) 2007-2008 by the ES40 Emulator Project
00003  *
00004  * WWW    : http://sourceforge.net/projects/es40
00005  * E-mail : camiel@camicom.com
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  * 
00021  * Although this is not required, the author would appreciate being notified of, 
00022  * and receiving any modifications you may make to the source code that might serve
00023  * the general public.
00024  */
00025 
00037 //
00038 // Thread_POSIX.h
00039 //
00040 // $Id: Thread_POSIX.h,v 1.1 2008/03/31 19:13:34 iamcamiel Exp $
00041 //
00042 // Library: Foundation
00043 // Package: Threading
00044 // Module:  Thread
00045 //
00046 // Definition of the ThreadImpl class for POSIX Threads.
00047 //
00048 // Copyright (c) 2004-2007, Applied Informatics Software Engineering GmbH.
00049 // and Contributors.
00050 //
00051 // Permission is hereby granted, free of charge, to any person or organization
00052 // obtaining a copy of the software and accompanying documentation covered by
00053 // this license (the "Software") to use, reproduce, display, distribute,
00054 // execute, and transmit the Software, and to prepare derivative works of the
00055 // Software, and to permit third-parties to whom the Software is furnished to
00056 // do so, all subject to the following:
00057 // 
00058 // The copyright notices in the Software and this entire statement, including
00059 // the above license grant, this restriction and the following disclaimer,
00060 // must be included in all copies of the Software, in whole or in part, and
00061 // all derivative works of the Software, unless such copies or derivative
00062 // works are solely in the form of machine-executable object code generated by
00063 // a source language processor.
00064 // 
00065 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00066 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00067 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00068 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00069 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00070 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00071 // DEALINGS IN THE SOFTWARE.
00072 //
00073 
00074 
00075 #ifndef Foundation_Thread_POSIX_INCLUDED
00076 #define Foundation_Thread_POSIX_INCLUDED
00077 
00078 
00079 #include "VMS/Foundation.h"
00080 #include "VMS/Runnable.h"
00081 #include "VMS/Event.h"
00082 #include "VMS/RefCountedObject.h"
00083 #include "VMS/AutoPtr.h"
00084 #include <pthread.h>
00085 #include <errno.h>
00086 
00087 
00088 namespace Poco {
00089 
00090 
00091 class Foundation_API ThreadImpl
00092 {
00093 public: 
00094         enum Priority
00095         {
00096                 PRIO_LOWEST_IMPL,
00097                 PRIO_LOW_IMPL,
00098                 PRIO_NORMAL_IMPL,
00099                 PRIO_HIGH_IMPL,
00100                 PRIO_HIGHEST_IMPL
00101         };
00102 
00103         ThreadImpl();                           
00104         ~ThreadImpl();
00105 
00106         Runnable& targetImpl() const;
00107         void setPriorityImpl(int prio);
00108         int getPriorityImpl() const;
00109         void startImpl(Runnable& target);
00110 
00111         void joinImpl();
00112         bool joinImpl(long milliseconds);
00113         bool isRunningImpl() const;
00114         static void sleepImpl(long milliseconds);
00115         static void yieldImpl();
00116         static ThreadImpl* currentImpl();
00117 
00118 protected:
00119         static void* entry(void* pThread);
00120         static int mapPrio(int prio);
00121 
00122 private:
00123         struct ThreadData: public RefCountedObject
00124         {
00125                 ThreadData():
00126                         pTarget(0),
00127                         thread(0),
00128                         prio(PRIO_NORMAL_IMPL),
00129                         done(false)
00130                 {
00131                 }
00132 
00133                 Runnable* pTarget;
00134                 pthread_t thread;
00135                 int       prio;
00136                 Event     done;
00137         };
00138         
00139         AutoPtr<ThreadData> _pData;
00140         
00141         static pthread_key_t _currentKey;
00142         static bool          _haveCurrentKey;
00143         
00144 #if defined(POCO_OS_FAMILY_UNIX)
00145         SignalHandler::JumpBufferVec _jumpBufferVec;
00146         friend class SignalHandler;
00147 #endif
00148 };
00149 
00150 
00151 //
00152 // inlines
00153 //
00154 inline int ThreadImpl::getPriorityImpl() const
00155 {
00156         return _pData->prio;
00157 }
00158 
00159 
00160 inline void ThreadImpl::sleepImpl(long milliseconds)
00161 {
00162 #if defined(__VMS) || defined(__digital__)
00163                 // This is specific to DECThreads
00164                 struct timespec interval;
00165                 interval.tv_sec  = milliseconds / 1000;
00166                 interval.tv_nsec = (milliseconds % 1000)*1000000; 
00167                 pthread_delay_np(&interval);
00168 #else 
00169                 struct timeval tv;
00170                 tv.tv_sec  = milliseconds / 1000;
00171                 tv.tv_usec = (milliseconds % 1000) * 1000;
00172                 select(0, NULL, NULL, NULL, &tv);       
00173 #endif
00174 }
00175 
00176 
00177 inline void ThreadImpl::yieldImpl()
00178 {
00179         sched_yield();
00180 }
00181 
00182 
00183 } // namespace Poco
00184 
00185 
00186 #endif // Foundation_Thread_POSIX_INCLUDED

SourceForge.net Logo
Project space on SourceForge.net