Changeset 14690


Ignore:
Timestamp:
04/22/14 11:24:50 (9 years ago)
Author:
Mark Evenson
Message:

THREADS:YIELD implements java.lang.Thread.yield().

Improved documenation strings in threads package.

Location:
trunk/abcl/src/org/armedbear/lisp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/src/org/armedbear/lisp/LispThread.java

    r14679 r14690  
    13661366    };
    13671367
     1368    public static final Primitive CURRENT_THREAD
     1369      = new pf_current_thread();
    13681370    @DocString(name="current-thread",
    1369     doc="Returns a reference to invoking thread.")
    1370     private static final Primitive CURRENT_THREAD =
    1371         new Primitive("current-thread", PACKAGE_THREADS, true)
    1372     {
    1373         @Override
    1374         public LispObject execute()
    1375         {
    1376             return currentThread();
    1377         }
    1378     };
    1379 
     1371               doc="Returns a reference to invoking thread.")
     1372    private static final class pf_current_thread extends Primitive {
     1373      pf_current_thread() {
     1374        super("current-thread", PACKAGE_THREADS, true);
     1375      }
     1376      @Override
     1377      public LispObject execute() {
     1378        return currentThread();
     1379      }
     1380    };
     1381
     1382    public static final Primitive BACKTRACE
     1383      = new pf_backtrace();
    13801384    @DocString(name="backtrace",
    1381                doc="Returns a backtrace of the invoking thread.")
    1382     private static final Primitive BACKTRACE =
    1383         new Primitive("backtrace", PACKAGE_SYS, true)
    1384     {
    1385         @Override
    1386         public LispObject execute(LispObject[] args)
    1387 
    1388         {
    1389             if (args.length > 1)
    1390                 return error(new WrongNumberOfArgumentsException(this, -1, 1));
    1391             int limit = args.length > 0 ? Fixnum.getValue(args[0]) : 0;
    1392             return currentThread().backtrace(limit);
    1393         }
    1394     };
    1395     @DocString(name="frame-to-string", args="frame")
    1396     private static final Primitive FRAME_TO_STRING =
    1397         new Primitive("frame-to-string", PACKAGE_SYS, true)
    1398     {
    1399         @Override
    1400         public LispObject execute(LispObject[] args)
    1401 
    1402         {
    1403             if (args.length != 1)
    1404                 return error(new WrongNumberOfArgumentsException(this, 1));
    1405            
    1406             return checkStackFrame(args[0]).toLispString();
    1407         }
    1408     };
    1409 
     1385               doc="Returns a Java backtrace of the invoking thread.")
     1386    private static final class pf_backtrace extends Primitive {
     1387      pf_backtrace() {
     1388        super("backtrace", PACKAGE_SYS, true);
     1389      }
     1390      @Override
     1391      public LispObject execute(LispObject[] args) {
     1392        if (args.length > 1)
     1393          return error(new WrongNumberOfArgumentsException(this, -1, 1));
     1394        int limit = args.length > 0 ? Fixnum.getValue(args[0]) : 0;
     1395        return currentThread().backtrace(limit);
     1396      }
     1397    };
     1398
     1399    public static final Primitive FRAME_TO_STRING
     1400      = new pf_frame_to_string();
     1401    @DocString(name="frame-to-string",
     1402               args="frame",
     1403               doc="Convert stack FRAME to a (potentially) readable string.")
     1404    private static final class pf_frame_to_string extends Primitive {
     1405      pf_frame_to_string() {
     1406        super("frame-to-string", PACKAGE_SYS, true);
     1407      }
     1408      @Override
     1409      public LispObject execute(LispObject[] args) {
     1410        if (args.length != 1)
     1411          return error(new WrongNumberOfArgumentsException(this, 1));
     1412        return checkStackFrame(args[0]).toLispString();
     1413      }
     1414    };
     1415
     1416    public static final Primitive FRAME_TO_LIST
     1417      = new pf_frame_to_list();
    14101418    @DocString(name="frame-to-list", args="frame")
    1411     private static final Primitive FRAME_TO_LIST =
    1412         new Primitive("frame-to-list", PACKAGE_SYS, true)
    1413     {
    1414         @Override
    1415         public LispObject execute(LispObject[] args)
    1416 
    1417         {
    1418             if (args.length != 1)
    1419                 return error(new WrongNumberOfArgumentsException(this, 1));
    1420 
    1421             return checkStackFrame(args[0]).toLispList();
    1422         }
    1423     };
    1424 
    1425 
     1419    private static final class pf_frame_to_list extends Primitive {
     1420      pf_frame_to_list() {
     1421        super("frame-to-list", PACKAGE_SYS, true);
     1422      }
     1423      @Override
     1424      public LispObject execute(LispObject[] args) {
     1425        if (args.length != 1)
     1426          return error(new WrongNumberOfArgumentsException(this, 1));
     1427
     1428        return checkStackFrame(args[0]).toLispList();
     1429      }
     1430    };
     1431
     1432
     1433    public static final SpecialOperator SYNCHRONIZED_ON
     1434      = new so_synchronized_on();
    14261435    @DocString(name="synchronized-on", args="form &body body")
    1427     private static final SpecialOperator SYNCHRONIZED_ON =
    1428         new SpecialOperator("synchronized-on", PACKAGE_THREADS, true,
    1429                             "form &body body")
    1430     {
    1431         @Override
    1432         public LispObject execute(LispObject args, Environment env)
    1433 
    1434         {
    1435           if (args == NIL)
    1436             return error(new WrongNumberOfArgumentsException(this, 1));
    1437 
    1438           LispThread thread = LispThread.currentThread();
    1439           synchronized (eval(args.car(), env, thread).lockableInstance()) {
    1440               return progn(args.cdr(), env, thread);
    1441           }
    1442         }
    1443     };
    1444 
     1436    private static final class so_synchronized_on extends SpecialOperator {
     1437      so_synchronized_on() {
     1438        super("synchronized-on", PACKAGE_THREADS, true, "form &body body");
     1439      }
     1440      @Override
     1441      public LispObject execute(LispObject args, Environment env) {
     1442        if (args == NIL)
     1443          return error(new WrongNumberOfArgumentsException(this, 1));
     1444       
     1445        LispThread thread = LispThread.currentThread();
     1446        synchronized (eval(args.car(), env, thread).lockableInstance()) {
     1447          return progn(args.cdr(), env, thread);
     1448        }
     1449      }
     1450    };
     1451
     1452 
     1453    public static final Primitive OBJECT_WAIT
     1454      = new pf_object_wait();
    14451455    @DocString(
    14461456    name="object-wait", args="object &optional timeout",
     
    14531463       + "information.\n"
    14541464    )
    1455     private static final Primitive OBJECT_WAIT =
    1456         new Primitive("object-wait", PACKAGE_THREADS, true)
    1457     {
    1458         @Override
    1459         public LispObject execute(LispObject object)
    1460 
    1461         {
    1462             try {
    1463                 object.lockableInstance().wait();
    1464             }
    1465             catch (InterruptedException e) {
    1466                 currentThread().processThreadInterrupts();
    1467             }
    1468             catch (IllegalMonitorStateException e) {
    1469                 return error(new IllegalMonitorState(e.getMessage()));
    1470             }
    1471             return NIL;
    1472         }
    1473 
    1474         @Override
    1475         public LispObject execute(LispObject object, LispObject timeout)
    1476 
    1477         {
    1478           long millis = sleepMillisPart(timeout);
    1479           int nanos = sleepNanosPart(timeout);
    1480           boolean zeroArgP = timeout.ZEROP() != NIL;
     1465    private static final class pf_object_wait extends Primitive {
     1466      pf_object_wait() {
     1467        super("object-wait", PACKAGE_THREADS, true);
     1468      }
     1469      @Override
     1470      public LispObject execute(LispObject object) {
     1471        try {
     1472          object.lockableInstance().wait();
     1473        } catch (InterruptedException e) {
     1474          currentThread().processThreadInterrupts();
     1475        } catch (IllegalMonitorStateException e) {
     1476          return error(new IllegalMonitorState(e.getMessage()));
     1477        }
     1478        return NIL;
     1479      }
     1480
     1481      @Override
     1482      public LispObject execute(LispObject object, LispObject timeout) {
     1483        long millis = sleepMillisPart(timeout);
     1484        int nanos = sleepNanosPart(timeout);
     1485        boolean zeroArgP = timeout.ZEROP() != NIL;
    14811486         
    1482             try {
    1483               if (millis == 0 && nanos == 0) {
    1484                 if (zeroArgP) {
    1485                   object.lockableInstance().wait(0, 0);
    1486                 } else {
    1487                   object.lockableInstance().wait(0, 1);
    1488                 }
    1489               } else {
    1490                 object.lockableInstance().wait(millis, nanos);
    1491               }
    1492             }
    1493             catch (InterruptedException e) {
    1494                 currentThread().processThreadInterrupts();
    1495             }
    1496             catch (IllegalMonitorStateException e) {
    1497                 return error(new IllegalMonitorState(e.getMessage()));
    1498             }
    1499             return NIL;
    1500         }
    1501     };
    1502 
    1503     @DocString(name="object-notify", args="object")
    1504     private static final Primitive OBJECT_NOTIFY =
    1505         new Primitive("object-notify", PACKAGE_THREADS, true,
    1506                       "object")
    1507     {
    1508         @Override
    1509         public LispObject execute(LispObject object)
    1510 
    1511         {
    1512             try {
    1513                 object.lockableInstance().notify();
    1514             }
    1515             catch (IllegalMonitorStateException e) {
    1516                 return error(new IllegalMonitorState(e.getMessage()));
    1517             }
    1518             return NIL;
    1519         }
    1520     };
    1521 
    1522     @DocString(name="object-notify-all", args="object")
    1523     private static final Primitive OBJECT_NOTIFY_ALL =
    1524         new Primitive("object-notify-all", PACKAGE_THREADS, true)
    1525     {
    1526         @Override
    1527         public LispObject execute(LispObject object)
    1528 
    1529         {
    1530             try {
    1531                 object.lockableInstance().notifyAll();
    1532             }
    1533             catch (IllegalMonitorStateException e) {
    1534                 return error(new IllegalMonitorState(e.getMessage()));
    1535             }
    1536             return NIL;
    1537         }
    1538     };
    1539 
    1540 
     1487        try {
     1488          if (millis == 0 && nanos == 0) {
     1489            if (zeroArgP) {
     1490              object.lockableInstance().wait(0, 0);
     1491            } else {
     1492              object.lockableInstance().wait(0, 1);
     1493            }
     1494          } else {
     1495            object.lockableInstance().wait(millis, nanos);
     1496          }
     1497        } catch (InterruptedException e) {
     1498          currentThread().processThreadInterrupts();
     1499        } catch (IllegalMonitorStateException e) {
     1500          return error(new IllegalMonitorState(e.getMessage()));
     1501        }
     1502        return NIL;
     1503      }
     1504    };
     1505
     1506    public static final Primitive OBJECT_NOTIFY
     1507      = new pf_object_notify();
     1508    @DocString(name="object-notify",
     1509               args="object",
     1510               doc="Wakes up a single thread that is waiting on OBJECT's monitor."
     1511+ "\nIf any threads are waiting on this object, one of them is chosen to be"
     1512+ " awakened. The choice is arbitrary and occurs at the discretion of the"
     1513+ " implementation. A thread waits on an object's monitor by calling one"
     1514+ " of the wait methods.")
     1515    private static final class pf_object_notify extends Primitive {
     1516      pf_object_notify() {
     1517        super("object-notify", PACKAGE_THREADS, true, "object");
     1518      }
     1519      @Override
     1520      public LispObject execute(LispObject object) {
     1521        try {
     1522          object.lockableInstance().notify();
     1523        } catch (IllegalMonitorStateException e) {
     1524          return error(new IllegalMonitorState(e.getMessage()));
     1525        }
     1526        return NIL;
     1527      }
     1528    };
     1529
     1530    public static final Primitive OBJECT_NOTIFY_ALL
     1531      = new pf_object_notify_all();
     1532    @DocString(name="object-notify-all",
     1533               args="object",
     1534               doc="Wakes up all threads that are waiting on this OBJECT's monitor."
     1535+ "\nA thread waits on an object's monitor by calling one of the wait methods.")
     1536    private static final class pf_object_notify_all extends Primitive {
     1537      pf_object_notify_all() {
     1538        super("object-notify-all", PACKAGE_THREADS, true);
     1539      }
     1540      @Override
     1541      public LispObject execute(LispObject object) {
     1542        try {
     1543          object.lockableInstance().notifyAll();
     1544        } catch (IllegalMonitorStateException e) {
     1545          return error(new IllegalMonitorState(e.getMessage()));
     1546        }
     1547        return NIL;
     1548      }
     1549    };
    15411550}
  • trunk/abcl/src/org/armedbear/lisp/threads.lisp

    r14106 r14690  
    3333(in-package #:threads)
    3434
    35 
    3635(export '(make-mailbox mailbox-send mailbox-empty-p
    3736          mailbox-read mailbox-peek
    3837          make-thread-lock with-thread-lock
     38          current-thread yield
    3939          make-mutex get-mutex release-mutex with-mutex))
    40 
    41 
    4240;;
    4341;; MAKE-THREAD helper to establish restarts
     
    148146          ,@body))))
    149147
     148(defun yield ()
     149  "A hint to the scheduler that the current thread is willing to yield its current use of a processor. The scheduler is free to ignore this hint.
     150
     151See java.lang.Thread.yield()."
     152  (java:jcall "yield" (JAVA:jstatic "currentThread" "java.lang.Thread")))
Note: See TracChangeset for help on using the changeset viewer.