poco/Foundation/include/Poco/Thread_WIN32.h

184 lines
4.1 KiB
C
Raw Normal View History

2006-10-23 17:48:43 +02:00
//
// Thread_WIN32.h
//
2008-01-28 18:23:19 +01:00
// $Id: //poco/svn/Foundation/include/Poco/Thread_WIN32.h#2 $
2006-10-23 17:48:43 +02:00
//
// Library: Foundation
// Package: Threading
// Module: Thread
//
// Definition of the ThreadImpl class for WIN32.
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Foundation_Thread_WIN32_INCLUDED
#define Foundation_Thread_WIN32_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Runnable.h"
2007-05-05 15:15:55 +02:00
#include "Poco/UnWindows.h"
2006-10-23 17:48:43 +02:00
namespace Poco {
class Foundation_API ThreadImpl
{
public:
2008-04-11 03:37:49 +02:00
typedef void (*Callback)(void*);
#if defined(_DLL)
typedef DWORD (WINAPI *Entry)(LPVOID);
#else
typedef unsigned (__stdcall *Entry)(void*);
#endif
struct CallbackData
{
CallbackData(): callback(0), pData(0)
{
}
Callback callback;
void* pData;
};
2006-10-23 17:48:43 +02:00
enum Priority
{
PRIO_LOWEST_IMPL = THREAD_PRIORITY_LOWEST,
PRIO_LOW_IMPL = THREAD_PRIORITY_BELOW_NORMAL,
PRIO_NORMAL_IMPL = THREAD_PRIORITY_NORMAL,
PRIO_HIGH_IMPL = THREAD_PRIORITY_ABOVE_NORMAL,
PRIO_HIGHEST_IMPL = THREAD_PRIORITY_HIGHEST
};
ThreadImpl();
~ThreadImpl();
void setPriorityImpl(int prio);
int getPriorityImpl() const;
2008-04-16 01:25:19 +02:00
void setOSPriorityImpl(int prio);
int getOSPriorityImpl() const;
static int getMinOSPriorityImpl();
static int getMaxOSPriorityImpl();
void setStackSizeImpl(int size);
int getStackSizeImpl() const;
2006-10-23 17:48:43 +02:00
void startImpl(Runnable& target);
2008-04-11 03:37:49 +02:00
void startImpl(Callback target, void* pData = 0);
2006-10-23 17:48:43 +02:00
void joinImpl();
2007-04-25 10:39:02 +02:00
bool joinImpl(long milliseconds);
2006-10-23 17:48:43 +02:00
bool isRunningImpl() const;
static void sleepImpl(long milliseconds);
static void yieldImpl();
static ThreadImpl* currentImpl();
protected:
#if defined(_DLL)
2008-04-11 03:37:49 +02:00
static DWORD WINAPI runnableEntry(LPVOID pThread);
#else
static unsigned __stdcall runnableEntry(void* pThread);
#endif
#if defined(_DLL)
static DWORD WINAPI functionEntry(LPVOID pThread);
2006-10-23 17:48:43 +02:00
#else
2008-04-11 03:37:49 +02:00
static unsigned __stdcall functionEntry(void* pThread);
2006-10-23 17:48:43 +02:00
#endif
2008-04-11 03:37:49 +02:00
void createImpl(Entry ent, void* pData);
void threadCleanup();
2006-10-23 17:48:43 +02:00
private:
2008-04-11 03:37:49 +02:00
Runnable* _pRunnableTarget;
CallbackData _callbackTarget;
HANDLE _thread;
int _prio;
int _stackSize;
2006-10-23 17:48:43 +02:00
static DWORD _currentKey;
};
//
// inlines
//
inline int ThreadImpl::getPriorityImpl() const
{
return _prio;
}
2008-04-16 01:25:19 +02:00
inline int ThreadImpl::getOSPriorityImpl() const
{
return _prio;
}
inline int ThreadImpl::getMinOSPriorityImpl()
{
return PRIO_LOWEST_IMPL;
}
inline int ThreadImpl::getMaxOSPriorityImpl()
{
return PRIO_HIGHEST_IMPL;
}
2006-10-23 17:48:43 +02:00
inline void ThreadImpl::sleepImpl(long milliseconds)
{
2007-08-10 15:57:07 +02:00
Sleep(DWORD(milliseconds));
2006-10-23 17:48:43 +02:00
}
inline void ThreadImpl::yieldImpl()
{
Sleep(0);
}
inline void ThreadImpl::setStackSizeImpl(int size)
2008-04-11 03:37:49 +02:00
{
_stackSize = size;
}
inline int ThreadImpl::getStackSizeImpl() const
2008-04-11 03:37:49 +02:00
{
return _stackSize;
}
2006-10-23 17:48:43 +02:00
} // namespace Poco
#endif // Foundation_Thread_WIN32_INCLUDED