9front - general discussion about 9front
 help / color / mirror / Atom feed
* etimer() assumes seconds together with APE
@ 2020-05-12 15:09 jamos
  2020-05-12 15:34 ` [9front] " Steve Simon
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: jamos @ 2020-05-12 15:09 UTC (permalink / raw)
  To: 9front

Dear list,

The function etimer(ulong key, int n) is used in libevent to get a timer 
event every 'n' milliseconds. However, if APE is used, etimer() delivers 
an event every 'n' second instead. The reason is that sleep() assumes 
seconds in APE and milliseconds in Plan 9.

I am aware that combining Plan 9 libraries with APE is a bit risky. When 
I earlier wanted to use etimer() in the netsurf framebuffer driver, I 
assumed it didn't work under APE, but I was not patient enough to wait 
1000 seconds, when I thought it would fire after one second.

Would this be considered a bug? It would be more consistent if etimer() 
would take milliseconds as well under APE, and it would also be more 
handy if one would like to sleep less than a second. I tried to find APE 
programs that might depend on the current behaviour, but I don't think 
there are many.

The following test program prints a string every 10 seconds (in APE) and 
every 10ms (if it can manage) under native Plan9. Stop by killing the 
processes.

/* Compile under native plan9:
  * 	8c ev.c; 8l -o native ev.8
  *
  * Compile under APE:
  * 	pcc  -D_POSIX_SOURCE -D_PLAN9_SOURCE -o posix ev.c
  */

#include <u.h>
#ifdef _POSIX_SOURCE
	#include <stdlib.h>
#else
	#include <libc.h>
#endif
#include <draw.h>
#include <event.h>

void
eresized(int) {}

void
main()
{
	int tim;	/* timer ticket */
	int e;		/* type of event */
	Event ev;	/* event struct */

	initdraw(0, 0, "timer test");
	einit(Ekeyboard|Emouse);
	tim = etimer(0, 10);	/* 10ms or 10s?*/
	for(;;){
		e = event(&ev);
		if(e == tim)
			print("Timer!\n");

	}
}


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] etimer() assumes seconds together with APE
  2020-05-12 15:09 etimer() assumes seconds together with APE jamos
@ 2020-05-12 15:34 ` Steve Simon
  2020-05-12 15:39 ` ori
  2020-05-12 22:16 ` cinap_lenrek
  2 siblings, 0 replies; 6+ messages in thread
From: Steve Simon @ 2020-05-12 15:34 UTC (permalink / raw)
  To: 9front

this brings back memories of the libplot on v7 unix, which had a close() entrypoint.

you had to link your app in two stages. the graphics stuff got bound to libplot with -r to keep the relocation data, then the result linked with the remainder of the app.

history just keeps repeating.

-Steve


> On 12 May 2020, at 5:10 pm, jamos@oboj.net wrote:
> 
> Dear list,
> 
> The function etimer(ulong key, int n) is used in libevent to get a timer event every 'n' milliseconds. However, if APE is used, etimer() delivers an event every 'n' second instead. The reason is that sleep() assumes seconds in APE and milliseconds in Plan 9.
> 
> I am aware that combining Plan 9 libraries with APE is a bit risky. When I earlier wanted to use etimer() in the netsurf framebuffer driver, I assumed it didn't work under APE, but I was not patient enough to wait 1000 seconds, when I thought it would fire after one second.
> 
> Would this be considered a bug? It would be more consistent if etimer() would take milliseconds as well under APE, and it would also be more handy if one would like to sleep less than a second. I tried to find APE programs that might depend on the current behaviour, but I don't think there are many.
> 
> The following test program prints a string every 10 seconds (in APE) and every 10ms (if it can manage) under native Plan9. Stop by killing the processes.
> 
> /* Compile under native plan9:
> *    8c ev.c; 8l -o native ev.8
> *
> * Compile under APE:
> *    pcc  -D_POSIX_SOURCE -D_PLAN9_SOURCE -o posix ev.c
> */
> 
> #include <u.h>
> #ifdef _POSIX_SOURCE
>    #include <stdlib.h>
> #else
>    #include <libc.h>
> #endif
> #include <draw.h>
> #include <event.h>
> 
> void
> eresized(int) {}
> 
> void
> main()
> {
>    int tim;    /* timer ticket */
>    int e;        /* type of event */
>    Event ev;    /* event struct */
> 
>    initdraw(0, 0, "timer test");
>    einit(Ekeyboard|Emouse);
>    tim = etimer(0, 10);    /* 10ms or 10s?*/
>    for(;;){
>        e = event(&ev);
>        if(e == tim)
>            print("Timer!\n");
> 
>    }
> }



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] etimer() assumes seconds together with APE
  2020-05-12 15:09 etimer() assumes seconds together with APE jamos
  2020-05-12 15:34 ` [9front] " Steve Simon
@ 2020-05-12 15:39 ` ori
  2020-05-12 22:05   ` cinap_lenrek
  2020-05-12 22:16 ` cinap_lenrek
  2 siblings, 1 reply; 6+ messages in thread
From: ori @ 2020-05-12 15:39 UTC (permalink / raw)
  To: jamos, 9front

> Dear list,
> 
> The function etimer(ulong key, int n) is used in libevent to get a timer 
> event every 'n' milliseconds. However, if APE is used, etimer() delivers 
> an event every 'n' second instead. The reason is that sleep() assumes 
> seconds in APE and milliseconds in Plan 9.
> 
> I am aware that combining Plan 9 libraries with APE is a bit risky.

Note, we've *designed* it to work: /sys/src/ape/lib/draw. This should
be fine.

> When I earlier wanted to use etimer() in the netsurf framebuffer driver, I 
> assumed it didn't work under APE, but I was not patient enough to wait 
> 1000 seconds, when I thought it would fire after one second.
> 
> Would this be considered a bug?

I'd call it a bug, yeah. It's a bit tricky to fix, since we share code
between native and ape-compatible draw -- it can be done with an ifdef,
but that's gross. Other approaches could be to add a compat.c file that
we link against in ape/libdraw.

I've got both approaches below:

diff -r 965e0f59464d sys/src/ape/lib/draw/ape.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/src/ape/lib/draw/ape.c	Tue May 12 08:37:35 2020 -0700
@@ -0,0 +1,17 @@
+#include <time.h>
+
+void
+_drawsleep(int ms)
+{
+	/*
+	 * This is gross. We need draw to compile under APE,
+	 * which defines sleep differently from plan 9.
+	 * Native sleep is in milliseconds, APE sleep is in
+	 * seconds. So we need to call different functions.
+	 */
+	struct timespec ts;
+	ts.tv_sec = ms/1000;
+	ts.tv_nsec = (ms%1000)*1000*1000;
+	while(ts.tv_sec != 0 && ts.tv_nsec != 0)
+		nanosleep(&ts, &ts);
+}
diff -r 965e0f59464d sys/src/ape/lib/draw/mkfile
--- a/sys/src/ape/lib/draw/mkfile	Sun May 10 22:51:40 2020 +0200
+++ b/sys/src/ape/lib/draw/mkfile	Tue May 12 08:37:35 2020 -0700
@@ -14,6 +14,7 @@
 	bytesperline.$O\
 	chan.$O\
 	cloadimage.$O\
+	ape.$O\
 	computil.$O\
 	creadimage.$O\
 	debug.$O\
diff -r 965e0f59464d sys/src/libdraw/event.c
--- a/sys/src/libdraw/event.c	Sun May 10 22:51:40 2020 +0200
+++ b/sys/src/libdraw/event.c	Tue May 12 08:37:35 2020 -0700
@@ -7,6 +7,8 @@
 typedef struct	Slave Slave;
 typedef struct	Ebuf Ebuf;
 
+extern void _drawsleep(int);
+
 struct Slave
 {
 	int	pid;
@@ -176,7 +178,7 @@
 		n = 1000;
 	t[0] = t[1] = Stimer - MAXSLAVE;
 	do
-		sleep(n);
+		_drawsleep(n);
 	while(write(epipe[1], t, 2) == 2);
 	t[0] = MAXSLAVE;
 	write(epipe[1], t, 1);
diff -r 965e0f59464d sys/src/libdraw/mkfile
--- a/sys/src/libdraw/mkfile	Sun May 10 22:51:40 2020 +0200
+++ b/sys/src/libdraw/mkfile	Tue May 12 08:37:35 2020 -0700
@@ -13,6 +13,7 @@
 	bytesperline.$O\
 	chan.$O\
 	cloadimage.$O\
+	plan9.$O\
 	computil.$O\
 	creadimage.$O\
 	debug.$O\
diff -r 965e0f59464d sys/src/libdraw/plan9.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/src/libdraw/plan9.c	Tue May 12 08:37:35 2020 -0700
@@ -0,0 +1,8 @@
+#include <u.h>
+#include <libc.h>
+
+static void
+_drawsleep(int ms)
+{
+	sleep(ms);
+}


===============================================================
Here's an alternative with ifdef:

diff -r 965e0f59464d sys/src/libdraw/event.c
--- a/sys/src/libdraw/event.c	Sun May 10 22:51:40 2020 +0200
+++ b/sys/src/libdraw/event.c	Tue May 12 08:29:57 2020 -0700
@@ -175,9 +175,23 @@
 	if(n <= 0)
 		n = 1000;
 	t[0] = t[1] = Stimer - MAXSLAVE;
-	do
+	do{
+		/*
+		 * This is gross. We need draw to compile under APE,
+		 * which defines sleep differently from plan 9.
+		 * Native sleep is in milliseconds, APE sleep is in
+		 * seconds. So we need to call different functions.
+		 */
+#ifdef _POSIX_SOURCE
+		struct timespec ts;
+		ts.tv_sec = n/1000;
+		ts.tv_nsec = (n%1000)*1000*1000;
+		while(ts.tv_sec != 0 && ts.tv_nsec != 0)
+			nanosleep(&ts, &ts);
+#else
 		sleep(n);
-	while(write(epipe[1], t, 2) == 2);
+#endif
+	}while(write(epipe[1], t, 2) == 2);
 	t[0] = MAXSLAVE;
 	write(epipe[1], t, 1);
 	_exits(0);



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] etimer() assumes seconds together with APE
  2020-05-12 15:39 ` ori
@ 2020-05-12 22:05   ` cinap_lenrek
  2020-05-12 22:12     ` ori
  0 siblings, 1 reply; 6+ messages in thread
From: cinap_lenrek @ 2020-05-12 22:05 UTC (permalink / raw)
  To: 9front

theres already a mechanism to compile plan9 code under ape:

add the following line to /sys/src/ape/lib/9/libc.h:

#define sleep _SLEEP

--
cinap


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] etimer() assumes seconds together with APE
  2020-05-12 22:05   ` cinap_lenrek
@ 2020-05-12 22:12     ` ori
  0 siblings, 0 replies; 6+ messages in thread
From: ori @ 2020-05-12 22:12 UTC (permalink / raw)
  To: cinap_lenrek, 9front

> theres already a mechanism to compile plan9 code under ape:
> 
> add the following line to /sys/src/ape/lib/9/libc.h:
> 
> #define sleep _SLEEP
> 
> --
> cinap

That's much better. Will commit as soon as I make sure everything
builds with it. Thanks, I knew what I had felt needlessly ugly.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [9front] etimer() assumes seconds together with APE
  2020-05-12 15:09 etimer() assumes seconds together with APE jamos
  2020-05-12 15:34 ` [9front] " Steve Simon
  2020-05-12 15:39 ` ori
@ 2020-05-12 22:16 ` cinap_lenrek
  2 siblings, 0 replies; 6+ messages in thread
From: cinap_lenrek @ 2020-05-12 22:16 UTC (permalink / raw)
  To: 9front

excellent bug report. yes, this is a bug.

we already have a special header to compile plan9 code (such
as libdraw, libsec and libmp) under ape.

the native plan9 system calls are still available, but under
a different name. the trick is to use the native ones when theres
a conflict.

--- a/sys/src/ape/lib/9/libc.h	Tue May 12 23:18:48 2020 +0200
+++ b/sys/src/ape/lib/9/libc.h	Wed May 13 00:12:27 2020 +0200
@@ -150,6 +150,7 @@
 #define mallocz _MALLOCZ
 #define nsec	_NSEC
 #define iounit	_IOUNIT
+#define sleep	_SLEEP

fix pushed.

--
cinap


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-05-12 22:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-12 15:09 etimer() assumes seconds together with APE jamos
2020-05-12 15:34 ` [9front] " Steve Simon
2020-05-12 15:39 ` ori
2020-05-12 22:05   ` cinap_lenrek
2020-05-12 22:12     ` ori
2020-05-12 22:16 ` cinap_lenrek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).