UnixOperatingSystem.st
changeset 8592 1c5216cd013e
parent 8564 48fd5e5b9d21
child 8599 9edfb6a871bf
equal deleted inserted replaced
8591:6ff3f2b33a0f 8592:1c5216cd013e
     7  inclusion of the above copyright notice.   This software may not
     7  inclusion of the above copyright notice.   This software may not
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
       
    12 
       
    13 'From Smalltalk/X, Version:5.2.4 on 22-09-2004 at 20:27:22'                     !
    12 
    14 
    13 "{ Package: 'stx:libbasic' }"
    15 "{ Package: 'stx:libbasic' }"
    14 
    16 
    15 AbstractOperatingSystem subclass:#UnixOperatingSystem
    17 AbstractOperatingSystem subclass:#UnixOperatingSystem
    16 	instanceVariableNames:''
    18 	instanceVariableNames:''
  8091     "This returns the microsecond timers value - if available.
  8093     "This returns the microsecond timers value - if available.
  8092      On some machines, times with this precision may not be available,
  8094      On some machines, times with this precision may not be available,
  8093      on those, the returned value may be rounded towards some internal
  8095      on those, the returned value may be rounded towards some internal
  8094      clock resolution value."
  8096      clock resolution value."
  8095 
  8097 
  8096     |seconds millis micros error|
  8098     |seconds micros error|
  8097 
  8099 
  8098 %{
  8100 %{
  8099 #if defined(HAS_GETTIMEOFDAY)
  8101 #if defined(_POSIX_MONOTONIC_CLOCK)
  8100     long __seconds = 0;
  8102     unsigned long __seconds = 0;
  8101     long __millis = 0;
  8103     unsigned long __micros = 0;
  8102     long __micros = 0;
  8104     struct timespec ts;
  8103 
  8105 
       
  8106     if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
       
  8107         error = @symbol(bad);
       
  8108 
       
  8109     seconds = __MKUINT(ts.tv_sec);
       
  8110     micros  = __MKUINT(ts.tv_nsec / 1000);
       
  8111 
       
  8112 #elif defined(HAS_GETTIMEOFDAY)
  8104     struct timeval tb;
  8113     struct timeval tb;
  8105 
  8114 
  8106     gettimeofday(&tb, NULL /* &tzb */);
  8115     gettimeofday(&tb, NULL /* &tzb */);
  8107     __seconds = tb.tv_sec;
  8116     if (tb.tv_usec >= (1000*1000)) {
  8108     __micros = tb.tv_usec;
       
  8109     if (__micros >= (1000*1000)) {
       
  8110         error = @symbol(bad);
  8117         error = @symbol(bad);
  8111     }
  8118     }
  8112 
  8119 
  8113     seconds = __MKINT(__seconds);
  8120     seconds = __MKUINT(tb.tv_sec);
  8114     millis  = __MKINT(__millis);
  8121     micros  = __MKUINT(tb.tv_usec);
  8115     micros  = __MKINT(__micros);
       
  8116 #endif
  8122 #endif
  8117 %}.
  8123 %}.
  8118 
  8124 
  8119     seconds notNil ifTrue:[
  8125     seconds notNil ifTrue:[
  8120         ^ (((seconds * 1000) + millis) * 1000) + micros
  8126         ^ (seconds * 1000000) + micros
  8121     ].
  8127     ].
  8122     error isNil ifTrue:[
  8128     error isNil ifTrue:[
  8123         ^ self getMillisecondTime * 1000
  8129         ^ self getMillisecondTime * 1000
  8124     ].
  8130     ].
  8125     self primitiveFailed:error.
  8131     self primitiveFailed:error.
  8148 
  8154 
  8149 %{  /* NOCONTEXT */
  8155 %{  /* NOCONTEXT */
  8150 
  8156 
  8151     long t = 0;
  8157     long t = 0;
  8152 
  8158 
       
  8159 
       
  8160 #if defined(_POSIX_MONOTONIC_CLOCK)
       
  8161     struct timespec ts;
       
  8162 
       
  8163     if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
       
  8164         goto err;
       
  8165 
       
  8166     t = ts.tv_sec*1000 + ts.tv_nsec/1000000;
       
  8167 
       
  8168 #elif defined(_SC_CLK_TCK)
  8153 /*
  8169 /*
  8154  * We prefer times here, since it is immune to clock changes!
  8170  * We prefer times here, since it is monotonic and immune to clock changes
       
  8171  *  but: it has less precision!
  8155  */
  8172  */
  8156 
       
  8157 #if defined(_SC_CLK_TCK)
       
  8158 # include <sys/times.h>
  8173 # include <sys/times.h>
  8159 
  8174 
  8160     static int millisecondsPerTick;
  8175     static int millisecondsPerTick;
  8161     clock_t ticks;
  8176     clock_t ticks;
  8162     struct tms tb;
  8177     struct tms tb;
  8166         if (ticksPerSecond <= 0)
  8181         if (ticksPerSecond <= 0)
  8167             goto err;
  8182             goto err;
  8168         if (ticksPerSecond > 1000)
  8183         if (ticksPerSecond > 1000)
  8169             goto err;
  8184             goto err;
  8170         millisecondsPerTick = 1000 / ticksPerSecond;
  8185         millisecondsPerTick = 1000 / ticksPerSecond;
       
  8186 /*
       
  8187 printf("milliSecondsPerTick: %d\n", millisecondsPerTick);
       
  8188 */
  8171     }
  8189     }
  8172 
  8190 
  8173     ticks = times(&tb);
  8191     ticks = times(&tb);
  8174     if (ticks < 0)
  8192     if (ticks < 0)
  8175         goto err;
  8193         goto err;
 12229 ! !
 12247 ! !
 12230 
 12248 
 12231 !UnixOperatingSystem class methodsFor:'documentation'!
 12249 !UnixOperatingSystem class methodsFor:'documentation'!
 12232 
 12250 
 12233 version
 12251 version
 12234     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.215 2004-09-21 17:52:44 stefan Exp $'
 12252     ^ '$Header: /cvs/stx/stx/libbasic/UnixOperatingSystem.st,v 1.216 2004-09-23 19:26:39 stefan Exp $'
 12235 ! !
 12253 ! !
 12236 
 12254 
 12237 UnixOperatingSystem initialize!
 12255 UnixOperatingSystem initialize!
 12238 UnixOperatingSystem::FileDescriptorHandle initialize!
 12256 UnixOperatingSystem::FileDescriptorHandle initialize!