Skip to content

Commit

Permalink
Add experimental SUBR to call nanosleep() for experiments in reducing…
Browse files Browse the repository at this point in the history
… CPU load

This adds a SUBR, sb_YIELD, value (octal) 0322 which takes a single number
0..999999999 which is the number of nanoseconds to pass to nanosleep().

The return value is T if the call to nanosleep() was executed or NIL
if it was not (argument out-of-range, or other error in getting the
number from the argument).

To use this experimental SUBR in a sysout you should:

   (SETQ \INITSUBRS (CONS '(YIELD #o322) \INITSUBRS))

then you can define functions that use that SUBR:

   (DEFINEQ (BACKGROUND-YIELD (SUBRCALL YIELD 833333)))
   (COMPILE 'BACKGROUND-YIELD)
   (SETQ BACKGROUNDFNS (CONS 'BACKGROUND-YIELD BACKGROUNDFNS))
  • Loading branch information
nbriggs committed Feb 24, 2021
1 parent c6a74b2 commit 439d92a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/subrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,6 @@
#define user_subr_DUMMY 012
#define user_subr_SAMPLE_USER_SUBR 00

/* Experimental yield */
#define sb_YIELD 0322
#endif
15 changes: 15 additions & 0 deletions src/subr.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/***********************************************************/

#include <stdio.h>
#include <time.h>
#include "lispemul.h"
#include "address.h"
#include "adr68k.h"
Expand Down Expand Up @@ -779,6 +780,20 @@ void OP_subrcall(int subr_no, int argnum) {
break;
}
#endif /* LPSOLVE */
case sb_YIELD: {
struct timespec rqts = {0, 833333};
unsigned sleepnanos;
POP_SUBR_ARGS;
N_GETNUMBER(args[0], sleepnanos, ret_nil);
if (sleepnanos > 999999999) {
TopOfStack = NIL;
break;
}
rqts.tv_nsec = sleepnanos;
nanosleep(&rqts, NULL);
TopOfStack = ATOM_T;
break;
}
default: {
char errtext[200];
sprintf(errtext, "OP_subrcall: Invalid alpha byte 0%o", ((*(PC + 1)) & 0xff));
Expand Down

0 comments on commit 439d92a

Please sign in to comment.