| 1 | /** \file Thread.cpp |
|---|
| 2 | ** \date 2004-10-30 |
|---|
| 3 | ** \author grymse@alhem.net |
|---|
| 4 | **/ |
|---|
| 5 | /* |
|---|
| 6 | Copyright (C) 2004-2007 Anders Hedstrom |
|---|
| 7 | |
|---|
| 8 | This library is made available under the terms of the GNU GPL. |
|---|
| 9 | |
|---|
| 10 | If you would like to use this library in a closed-source application, |
|---|
| 11 | a separate license agreement is available. For information about |
|---|
| 12 | the closed-source license agreement for the C++ sockets library, |
|---|
| 13 | please visit http://www.alhem.net/Sockets/license.html and/or |
|---|
| 14 | email license@alhem.net. |
|---|
| 15 | |
|---|
| 16 | This program is free software; you can redistribute it and/or |
|---|
| 17 | modify it under the terms of the GNU General Public License |
|---|
| 18 | as published by the Free Software Foundation; either version 2 |
|---|
| 19 | of the License, or (at your option) any later version. |
|---|
| 20 | |
|---|
| 21 | This program is distributed in the hope that it will be useful, |
|---|
| 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 24 | GNU General Public License for more details. |
|---|
| 25 | |
|---|
| 26 | You should have received a copy of the GNU General Public License |
|---|
| 27 | along with this program; if not, write to the Free Software |
|---|
| 28 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 29 | */ |
|---|
| 30 | #include <stdio.h> |
|---|
| 31 | #ifdef _WIN32 |
|---|
| 32 | #include <process.h> |
|---|
| 33 | #include "socket_include.h" |
|---|
| 34 | #else |
|---|
| 35 | #include <unistd.h> |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | #include "Thread.h" |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | #ifdef SOCKETS_NAMESPACE |
|---|
| 42 | namespace SOCKETS_NAMESPACE { |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | Thread::Thread(bool release) |
|---|
| 47 | :m_thread(0) |
|---|
| 48 | ,m_running(true) |
|---|
| 49 | ,m_release(false) |
|---|
| 50 | ,m_b_delete_on_exit(false) |
|---|
| 51 | ,m_b_destructor(false) |
|---|
| 52 | { |
|---|
| 53 | #ifdef _WIN32 |
|---|
| 54 | // m_thread = ::CreateThread(NULL, 0, StartThread, this, 0, &m_dwThreadId); |
|---|
| 55 | m_thread = (HANDLE)_beginthreadex(NULL, 0, &StartThread, this, 0, &m_dwThreadId); |
|---|
| 56 | #else |
|---|
| 57 | pthread_attr_t attr; |
|---|
| 58 | |
|---|
| 59 | pthread_attr_init(&attr); |
|---|
| 60 | pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); |
|---|
| 61 | if (pthread_create(&m_thread,&attr, StartThread,this) == -1) |
|---|
| 62 | { |
|---|
| 63 | perror("Thread: create failed"); |
|---|
| 64 | SetRunning(false); |
|---|
| 65 | } |
|---|
| 66 | // pthread_attr_destroy(&attr); |
|---|
| 67 | #endif |
|---|
| 68 | m_release = release; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | Thread::~Thread() |
|---|
| 73 | { |
|---|
| 74 | m_b_destructor = true; |
|---|
| 75 | if (m_running) |
|---|
| 76 | { |
|---|
| 77 | SetRelease(true); |
|---|
| 78 | SetRunning(false); |
|---|
| 79 | #ifdef _WIN32 |
|---|
| 80 | Sleep(1000); |
|---|
| 81 | #else |
|---|
| 82 | sleep(1); |
|---|
| 83 | #endif |
|---|
| 84 | } |
|---|
| 85 | #ifdef _WIN32 |
|---|
| 86 | if (m_thread) |
|---|
| 87 | ::CloseHandle(m_thread); |
|---|
| 88 | #endif |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | threadfunc_t STDPREFIX Thread::StartThread(threadparam_t zz) |
|---|
| 93 | { |
|---|
| 94 | Thread *p = (Thread *)zz; |
|---|
| 95 | |
|---|
| 96 | while (p -> m_running && !p -> m_release) |
|---|
| 97 | { |
|---|
| 98 | #ifdef _WIN32 |
|---|
| 99 | Sleep(1000); |
|---|
| 100 | #else |
|---|
| 101 | sleep(1); |
|---|
| 102 | #endif |
|---|
| 103 | } |
|---|
| 104 | if (p -> m_running) |
|---|
| 105 | { |
|---|
| 106 | p -> Run(); |
|---|
| 107 | } |
|---|
| 108 | p -> SetRunning(false); // if return |
|---|
| 109 | if (p -> DeleteOnExit() && !p -> IsDestructor()) |
|---|
| 110 | { |
|---|
| 111 | delete p; |
|---|
| 112 | } |
|---|
| 113 | #ifdef _WIN32 |
|---|
| 114 | _endthreadex(0); |
|---|
| 115 | #endif |
|---|
| 116 | return (threadfunc_t)NULL; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | bool Thread::IsRunning() |
|---|
| 121 | { |
|---|
| 122 | return m_running; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | void Thread::SetRunning(bool x) |
|---|
| 127 | { |
|---|
| 128 | m_running = x; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | bool Thread::IsReleased() |
|---|
| 133 | { |
|---|
| 134 | return m_release; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | void Thread::SetRelease(bool x) |
|---|
| 139 | { |
|---|
| 140 | m_release = x; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | bool Thread::DeleteOnExit() |
|---|
| 145 | { |
|---|
| 146 | return m_b_delete_on_exit; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | void Thread::SetDeleteOnExit(bool x) |
|---|
| 151 | { |
|---|
| 152 | m_b_delete_on_exit = x; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | bool Thread::IsDestructor() |
|---|
| 157 | { |
|---|
| 158 | return m_b_destructor; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | #ifdef SOCKETS_NAMESPACE |
|---|
| 163 | } |
|---|
| 164 | #endif |
|---|
| 165 | |
|---|
| 166 | |
|---|