# HG changeset patch # User Jaroslav Tulach # Date 1243268822 -7200 # Node ID be82436a7a8bcb35d7296ab27325dcc7e567f371 # Parent 918e6f7f8136b188d7fc79d84aa63eab1d45aad7 More code snippets for the mutex example diff -r 918e6f7f8136 -r be82436a7a8b samples/privilegedcreator/build.xml --- a/samples/privilegedcreator/build.xml Wed Apr 29 11:56:47 2009 +0200 +++ b/samples/privilegedcreator/build.xml Mon May 25 18:27:02 2009 +0200 @@ -2,6 +2,11 @@ + + + + + Builds, tests, and runs the project privilegedcreator. diff -r 918e6f7f8136 -r be82436a7a8b samples/privilegedcreator/src/api/Executors.java --- a/samples/privilegedcreator/src/api/Executors.java Wed Apr 29 11:56:47 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ - -package api; - -import java.util.concurrent.Executor; - -/** - * - * @author Jaroslav Tulach - */ -public final class Executors { - /** let's prefer factory methods */ - private Executors() { - } - - - public static Executor create() { - return new Simple(); - } - - public static Executor create(boolean fair) { - Configuration conf = new Configuration(); - conf.setFair(fair); - return new Fair(conf); - } - - // BEGIN: design.less.privileged - public static Executor create(Configuration config) { - return new Fair(config); - } - - public static final class Configuration { - boolean fair; - int maxWaiters = -1; - - public void setFair(boolean fair) { - this.fair = fair; - } - public void setMaxWaiters(int max) { - this.maxWaiters = max; - } - } - // END: design.less.privileged - - private static final class Simple implements Executor { - public synchronized void execute(Runnable command) { - command.run(); - } - } - private static final class Fair implements Executor { - private final Configuration conf; - - public Fair(Configuration conf) { - this.conf = conf; - } - - public void execute(Runnable command) { - // TBD - } - } -} diff -r 918e6f7f8136 -r be82436a7a8b samples/privilegedcreator/src/api/Mutex.java --- a/samples/privilegedcreator/src/api/Mutex.java Wed Apr 29 11:56:47 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -/* - * To change this template, choose Tools | Templates - * and open the template in the editor. - */ - -package api; - -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -/** - * - * @author Jaroslav Tulach - */ -public class Mutex { - Lock lock = new ReentrantLock(); - - public Mutex() { - } - - - public void readAccess(Runnable r) { - try { - lock.lock(); - r.run(); - } finally { - lock.unlock(); - } - } - - - - public Mutex(Privileged privileged) { - if (privileged.mutex != null) { - throw new IllegalStateException(); - } - privileged.mutex = this; - } - - public static final class Privileged { - private Mutex mutex; - - public void enterReadAccess() { - mutex.lock.lock(); - } - - public void exitReadAccess() { - mutex.lock.unlock(); - } - } -} diff -r 918e6f7f8136 -r be82436a7a8b samples/privilegedcreator/src/org/apidesign/privileged/api/Executors.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/privilegedcreator/src/org/apidesign/privileged/api/Executors.java Mon May 25 18:27:02 2009 +0200 @@ -0,0 +1,60 @@ + +package org.apidesign.privileged.api; + +import java.util.concurrent.Executor; + +/** + * + * @author Jaroslav Tulach + */ +public final class Executors { + /** let's prefer factory methods */ + private Executors() { + } + + + public static Executor create() { + return new Simple(); + } + + public static Executor create(boolean fair) { + Configuration conf = new Configuration(); + conf.setFair(fair); + return new Fair(conf); + } + + // BEGIN: design.less.privileged + public static Executor create(Configuration config) { + return new Fair(config); + } + + public static final class Configuration { + boolean fair; + int maxWaiters = -1; + + public void setFair(boolean fair) { + this.fair = fair; + } + public void setMaxWaiters(int max) { + this.maxWaiters = max; + } + } + // END: design.less.privileged + + private static final class Simple implements Executor { + public synchronized void execute(Runnable command) { + command.run(); + } + } + private static final class Fair implements Executor { + private final Configuration conf; + + public Fair(Configuration conf) { + this.conf = conf; + } + + public void execute(Runnable command) { + // TBD + } + } +} diff -r 918e6f7f8136 -r be82436a7a8b samples/privilegedcreator/src/org/apidesign/privileged/api/Mutex.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/privilegedcreator/src/org/apidesign/privileged/api/Mutex.java Mon May 25 18:27:02 2009 +0200 @@ -0,0 +1,52 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.apidesign.privileged.api; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * + * @author Jaroslav Tulach + */ +// BEGIN: mutex.api +public class Mutex { + Lock lock = new ReentrantLock(); + + public Mutex() { + } + + public void readAccess(Runnable r) { + try { + lock.lock(); + r.run(); + } finally { + lock.unlock(); + } + } +// FINISH: mutex.api + +// BEGIN: mutex.privileged.api + public Mutex(Privileged privileged) { + if (privileged.mutex != null) { + throw new IllegalStateException(); + } + privileged.mutex = this; + } + + public static final class Privileged { + private Mutex mutex; + + public void enterReadAccess() { + mutex.lock.lock(); + } + + public void exitReadAccess() { + mutex.lock.unlock(); + } + } +// END: mutex.privileged.api +} diff -r 918e6f7f8136 -r be82436a7a8b samples/privilegedcreator/test/api/MutexTest.java --- a/samples/privilegedcreator/test/api/MutexTest.java Wed Apr 29 11:56:47 2009 +0200 +++ b/samples/privilegedcreator/test/api/MutexTest.java Mon May 25 18:27:02 2009 +0200 @@ -1,5 +1,6 @@ package api; +import org.apidesign.privileged.api.Mutex; import org.junit.After; import org.junit.AfterClass; import org.junit.Before;