Back to index

im-sdk  12.3.91
IMThreadP.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #include <windows.h>
00043 #include "IMThread.hh"
00044 
00045 struct IMThreadP {
00046   void *stack_base;
00047   size_t stack_size;
00048   ThreadStartProc start_routine;
00049   void *arg;
00050   long flags;
00051   HANDLE tid;
00052 };
00053 
00054 IMThread::IMThread(ThreadStartProc start_routine, void *arg) {
00055   p = new IMThreadP;
00056 
00057   p->stack_base = NULL;
00058   p->stack_size = 0;
00059   p->start_routine = start_routine;
00060   p->arg = arg;
00061   p->flags = 0;
00062   Manage();
00063 }
00064 
00065 IMThread::IMThread(void *stack_base, size_t stack_size,
00066                  ThreadStartProc start_routine,
00067                  void *arg, long flags) {
00068 
00069   size_t min_size = 0;
00070 
00071   p = new IMThreadP;
00072 
00073   p->stack_base = stack_base;
00074 
00075   p->stack_size = (stack_size < min_size)? min_size : stack_size;
00076   p->start_routine = start_routine;
00077   p->arg = arg;
00078   p->flags = flags;
00079   Manage();
00080 }
00081 
00082 IMThread::~IMThread() {
00083   Unmanage();
00084   delete p;
00085 }
00086 
00087 int
00088 IMThread::Manage() {
00089   DWORD threadID; 
00090   int ret = 0;
00091   p->tid = CreateThread(0, p->stack_size,
00092                             (LPTHREAD_START_ROUTINE) p->start_routine, p->arg, p->flags, &threadID); 
00093   if (p->tid == NULL)
00094          ret = -1;
00095   return ret;
00096 }
00097 
00098 int
00099 IMThread::Unmanage() {
00100   int status = 0;
00101   if (p->tid != 0) {
00102      BOOL bstatus;
00103      DWORD threadStatus;
00104   
00105      bstatus = GetExitCodeThread(p->tid, &threadStatus);
00106      if (threadStatus == STILL_ACTIVE) {
00107         // Stop the thread
00108         TerminateThread(p->tid, 0);
00109            CloseHandle(p->tid);
00110      }
00111      delete p;
00112    } 
00113   return status;
00114 }
00115 
00116 // thr_join(3T)
00117 int
00118 IMThread::Join(IMThread * wait_for, IMThread * departed, void **status) {
00119   return 0;
00120 }
00121 
00122 int
00123 IMThread::Join(IMThread *departed, void **status) {
00124   return 0;
00125 }
00126 
00127 int
00128 IMThread::Join(void **status) {
00129   return 0;
00130 }
00131 
00132 // thr_suspend(3T), thr_continue(3T)
00133 int
00134 IMThread::Suspend() {
00135   DWORD suspend_status;
00136 
00137   suspend_status = SuspendThread(p->tid);
00138   if (suspend_status & 0xFFFFFFFF)
00139      return -1;
00140   else
00141      return 0; 
00142 }
00143 
00144 int
00145 IMThread::Continue() {
00146   DWORD resume_status;
00147 
00148   resume_status = ResumeThread(p->tid);
00149   if (resume_status & 0xFFFFFFFF)
00150      return -1;
00151   else
00152      return 0; 
00153 }
00154 
00155 #if 0
00156 unsigned int
00157 IMThread::get_thread() {
00158   DWORD id;
00159   id = GetCurrentThreadId();
00160   return id;
00161 }
00162 
00163 #endif