Hi, I was inspired by Time::HiRes to create 2 new simple ops for parrot: usleep(int), and sleep(num), to behave a bit more like the float version of the time op.
I've attached a patch made off of the 0.0.6 source tree that works *for Linux* as a proof of concept to try and spark some discussion. Regards, +-- Steve Purkis <[EMAIL PROTECTED]>
diff -rc parrot_0.0.6/ChangeLog parrot_0.0.6-usleep/ChangeLog *** parrot_0.0.6/ChangeLog Tue Mar 19 23:03:05 2002 --- parrot_0.0.6-usleep/ChangeLog Sun Jul 14 02:10:42 2002 *************** *** 1,3 **** --- 1,6 ---- + 2002-07-14 02:05 spurkis + * added usleep(INT) and sleep(NUM) for linux + 2002-03-19 22:54 nicholas * docs/running.pod: Patch from Simon Glover <[EMAIL PROTECTED]>: diff -rc parrot_0.0.6/config/gen/platform/generic.c parrot_0.0.6-usleep/config/gen/platform/generic.c *** parrot_0.0.6/config/gen/platform/generic.c Fri May 24 07:32:06 2002 --- parrot_0.0.6-usleep/config/gen/platform/generic.c Sat Jul 13 23:48:53 2002 *************** *** 47,52 **** --- 47,63 ---- /* + ** Parrot_usleep() + */ + + void + Parrot_usleep(unsigned int microseconds) + { + usleep(microseconds); + } + + + /* ** Parrot_setenv() */ diff -rc parrot_0.0.6/config/gen/platform/generic.h parrot_0.0.6-usleep/config/gen/platform/generic.h *** parrot_0.0.6/config/gen/platform/generic.h Fri May 24 07:32:06 2002 --- parrot_0.0.6-usleep/config/gen/platform/generic.h Sat Jul 13 23:48:53 2002 *************** *** 17,22 **** --- 17,23 ---- */ void Parrot_sleep(unsigned int seconds); + void Parrot_usleep(unsigned int microseconds); INTVAL Parrot_intval_time(void); FLOATVAL Parrot_floatval_time(void); void Parrot_setenv(const char *name, const char *value); diff -rc parrot_0.0.6/core.ops parrot_0.0.6-usleep/core.ops *** parrot_0.0.6/core.ops Wed Jun 5 02:56:08 2002 --- parrot_0.0.6-usleep/core.ops Sun Jul 14 01:58:19 2002 *************** *** 3313,3328 **** Sleep for $1 seconds =cut inline op sleep(in INT) { if ($1 < 0) { ! internal_exception(NEG_SLEEP, "Cannot go back in time"); } Parrot_sleep((UINTVAL)$1); goto NEXT(); } ####################################### --- 3313,3352 ---- Sleep for $1 seconds + =item B<sleep>(in NUM) + + Sleep for $1 fractional seconds + + =item B<usleep>(in INT) + + Sleep for $1 microseconds + =cut inline op sleep(in INT) { if ($1 < 0) { ! internal_exception(NEG_SLEEP, "Cannot sleep back in time"); } Parrot_sleep((UINTVAL)$1); goto NEXT(); } + inline op sleep(in NUM) { + if ((FLOATVAL) $1 < (FLOATVAL) 0.0) { + internal_exception(NEG_SLEEP, "Cannot sleep back in time"); + } + Parrot_usleep((UINTVAL) ($1 * (FLOATVAL) 1000000.0)); + goto NEXT(); + } + + inline op usleep(in INT) { + if ($1 < 0) { + internal_exception(NEG_SLEEP, "Cannot sleep back in time"); + } + Parrot_usleep((UINTVAL)$1); + goto NEXT(); + } + ####################################### Only in parrot_0.0.6-usleep/examples/assembly: sleep.pasm diff -rc parrot_0.0.6/platforms/generic.c parrot_0.0.6-usleep/platforms/generic.c *** parrot_0.0.6/platforms/generic.c Sun Jun 2 05:10:13 2002 --- parrot_0.0.6-usleep/platforms/generic.c Sat Jul 13 23:48:53 2002 *************** *** 45,50 **** --- 45,57 ---- } + void + Parrot_usleep(unsigned int microseconds) + { + usleep(seconds); + } + + /* ** Parrot_setenv() */ diff -rc parrot_0.0.6/platforms/generic.h parrot_0.0.6-usleep/platforms/generic.h *** parrot_0.0.6/platforms/generic.h Fri Mar 8 04:37:25 2002 --- parrot_0.0.6-usleep/platforms/generic.h Sat Jul 13 23:48:53 2002 *************** *** 17,22 **** --- 17,23 ---- */ void Parrot_sleep(unsigned int seconds); + void Parrot_usleep(unsigned int microseconds); INTVAL Parrot_intval_time(void); FLOATVAL Parrot_floatval_time(void); void Parrot_setenv(const char *name, const char *value); diff -rc parrot_0.0.6/t/op/time.t parrot_0.0.6-usleep/t/op/time.t *** parrot_0.0.6/t/op/time.t Sat Jun 1 04:54:19 2002 --- parrot_0.0.6-usleep/t/op/time.t Sun Jul 14 01:58:38 2002 *************** *** 1,6 **** #! perl -w ! use Parrot::Test tests => 4; output_is(<<'CODE', <<'OUTPUT', "time_i"); time I0 --- 1,6 ---- #! perl -w ! use Parrot::Test tests => 8; output_is(<<'CODE', <<'OUTPUT', "time_i"); time I0 *************** *** 65,73 **** done OUTPUT ! output_is(<<CODE, 'Cannot go back in time', "sleep"); sleep -1 end CODE 1; --- 65,127 ---- done OUTPUT ! output_is(<<CODE, <<OUTPUT, "usleep"); ! print "start\\n" ! ! time N1 ! ! usleep 100000 ! set I0, 100000 ! usleep I0 ! ! time N0 ! ! gt N0, N1, ALLOK ! print "no, usleeping made time go the wrong way " ! ! ALLOK: ! print "done\\n" ! end ! CODE ! start ! done ! OUTPUT ! ! output_is(<<CODE, <<OUTPUT, "sleep"); ! print "start\\n" ! ! time N1 ! ! sleep 0.1 ! set N0, 0.1 ! sleep N0 ! ! time N0 ! ! gt N0, N1, ALLOK ! print "no, sleeping (fractional time) made time go the wrong way " ! ! ALLOK: ! print "done\\n" ! end ! CODE ! start ! done ! OUTPUT ! ! output_is(<<CODE, 'Cannot sleep back in time', "sleep"); sleep -1 end CODE + output_is(<<CODE, 'Cannot sleep back in time', "sleep"); + sleep -1.0 + end + CODE + + output_is(<<CODE, 'Cannot sleep back in time', "usleep"); + usleep -1 + end + CODE + 1;