Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phStandard.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 #ifdef HAVE_CONFIG_H
00027     #include <phissionconfig.h>
00028 #endif
00029 
00030 #include <phStandard.h>
00031 
00032 #if defined(HAVE_SYS_TYPES_H)
00033     #include <sys/types.h>
00034 #endif
00035 #if defined(HAVE_UNISTD_H)
00036     #include <unistd.h>
00037 #endif
00038 
00039 #include <phError.h>
00040 #include <phMemory.h>
00041 #include <phPrint.h>
00042 
00043 #ifdef __ADSPBLACKFIN__
00044     #include <PhissionVDK.h>
00045 #endif
00046 
00047 #ifdef __cplusplus
00048 extern "C" {
00049 #endif
00050 
00051 /* ------------------------------------------------------------------------ */
00052 #if defined(WIN32)
00053 #include <windows.h>
00054 uintmax_t  phGetCurrentThreadId( void ) 
00055                     { return (uintmax_t)GetCurrentThreadId(); }
00056 void       phYield( void ) 
00057                     { SwitchToThread(); }
00058 uintmax_t  phProcessId( void ) 
00059                     { return (uintmax_t)GetCurrentProcessId(); }
00060 #endif
00061 
00062 /* ------------------------------------------------------------------------ */
00063 #if defined(HAVE_PTHREAD) && !defined(WIN32)
00064 #include <pthread.h>
00065 uintmax_t  phGetCurrentThreadId( void ) 
00066                     { return (uintmax_t)pthread_self(); }
00067 
00068 #include <sched.h>
00069 void       phYield( void ) 
00070                     { sched_yield(); }
00071 uintmax_t  phProcessId( void ) 
00072                     { return (uintmax_t)getpid(); }
00073 
00074 #endif
00075 
00076 /* ------------------------------------------------------------------------ */
00077 #ifdef __ADSPBLACKFIN__
00078 uintmax_t  phGetCurrentThreadId( void )
00079                     { return (uintmax_t)VDK::GetThreadID(); }
00080 void       phYield( void )
00081                     { /*VDK::Yield();*/ VDK::Sleep(1); }
00082 uintmax_t  phProcessId( void ) /* VDK has no processes, only threads */
00083                     { return (uintmax_t)VDK::GetThreadID(); }
00084 #endif
00085 
00086 /* ------------------------------------------------------------------------- *
00087  * phSleep:
00088  * ------------------------------------------------------------------------- */
00089 void phSleep(uint32_t seconds)
00090 {
00091 #if defined(__ADSPBLACKFIN__)
00092     float ticks = seconds / (VDK::GetTickPeriod() * 1000000);
00093     //phPRINT("VDK::Sleep(%f)\n",ticks);
00094     VDK::Sleep(ticks >= 1.0 ? (int)ticks : 1);
00095 #else
00096     #if !defined(_MSC_VER)
00097     ::sleep(seconds);
00098     #else
00099     Sleep(seconds*1000);
00100     #endif
00101 #endif
00102 }
00103 
00104 
00105 /* ------------------------------------------------------------------------- *
00106  * phMSleep:
00107  * ------------------------------------------------------------------------- */
00108 void phMSleep( uint32_t milliseconds)
00109 {
00110 #if defined(__ADSPBLACKFIN__)
00111     float ticks = milliseconds / VDK::GetTickPeriod();
00112     //phPRINT("VDK::Sleep(%f)\n",ticks);
00113     VDK::Sleep(ticks >= 1.0 ? (int)ticks : 1);
00114 #else
00115     #if !defined(WIN32)
00116     ::usleep(milliseconds*1000);
00117     #endif
00118     #if defined(WIN32)
00119     Sleep(milliseconds);
00120     #endif
00121 #endif
00122 }
00123 
00124 /* ------------------------------------------------------------------------- *
00125  * phUSleep:
00126  * ------------------------------------------------------------------------- */
00127 void phUSleep(uint32_t microseconds)
00128 {
00129 #if defined(__ADSPBLACKFIN__)
00130     float ticks = microseconds / (VDK::GetTickPeriod() * 1000);
00131     //phPRINT("VDK::Sleep(%f)\n",ticks);
00132     VDK::Sleep(ticks >= 1.0 ? (int)ticks : 1);
00133 #else
00134     #if !defined(_MSC_VER)
00135     ::usleep(microseconds);
00136     #else
00137     Sleep(microseconds/1000);
00138     #endif
00139 #endif
00140 }
00141 
00142 /* ------------------------------------------------------------------------ */
00143 int phStringCopy( const char        *str, 
00144                   const uint32_t    maxlen,
00145                   char              **copy,
00146                   uint32_t          *copy_length,
00147                   char              *default_str )
00148 {
00149     phFUNCTION("phStringCopy")
00150     unsigned long length = 0;
00151     
00152     if ((copy == NULL) || (str == NULL))
00153     {
00154         return phFAIL;
00155     }
00156     
00157     length = ((str != NULL) ? strlen(str) : 0);
00158     if (maxlen > 0)
00159         length = (length < maxlen) ? length : maxlen;
00160     if (default_str == NULL)
00161         default_str = "";
00162     
00163     if (length > 0)
00164     {
00165         if ((*copy) == NULL)
00166         {
00167             (*copy) = (char *)phCalloc((length+1),sizeof(char));
00168             phCHECK_NULLPTR((*copy),"phCalloc","phCalloc failed");
00169             
00170             if (copy_length != NULL)
00171                 (*copy_length) = length;
00172         }
00173         else if ((length > (*copy_length)) && (length > 0))
00174         {
00175             (*copy) = (char *)phRealloc((*copy),(length+1) * sizeof(char));
00176             phCHECK_NULLPTR((*copy),"phRealloc","phRealloc failed");
00177             
00178             if (copy_length != NULL)
00179                 (*copy_length) = length;
00180         }
00181         
00182         sprintf((*copy),"%s",((str != NULL)? str : default_str));    
00183     }
00184     /* Free up the name buffer and reset the size */
00185     else
00186     {
00187         phFree((*copy));
00188         if (copy_length != NULL)
00189             (*copy_length) = 0;
00190     }
00191 
00192     return phSUCCESS;
00193 error:
00194     return phFAIL;
00195 }
00196 
00197 #ifdef __cplusplus
00198 } /* extern "C" { */
00199 #endif
00200 
00201 
00202 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:03 2007 for phission by  doxygen 1.4.4