1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/privilegedcreator/src/org/apidesign/privileged/api/Mutex.java Mon May 25 18:27:02 2009 +0200
1.3 @@ -0,0 +1,52 @@
1.4 +/*
1.5 + * To change this template, choose Tools | Templates
1.6 + * and open the template in the editor.
1.7 + */
1.8 +
1.9 +package org.apidesign.privileged.api;
1.10 +
1.11 +import java.util.concurrent.locks.Lock;
1.12 +import java.util.concurrent.locks.ReentrantLock;
1.13 +
1.14 +/**
1.15 + *
1.16 + * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
1.17 + */
1.18 +// BEGIN: mutex.api
1.19 +public class Mutex {
1.20 + Lock lock = new ReentrantLock();
1.21 +
1.22 + public Mutex() {
1.23 + }
1.24 +
1.25 + public void readAccess(Runnable r) {
1.26 + try {
1.27 + lock.lock();
1.28 + r.run();
1.29 + } finally {
1.30 + lock.unlock();
1.31 + }
1.32 + }
1.33 +// FINISH: mutex.api
1.34 +
1.35 +// BEGIN: mutex.privileged.api
1.36 + public Mutex(Privileged privileged) {
1.37 + if (privileged.mutex != null) {
1.38 + throw new IllegalStateException();
1.39 + }
1.40 + privileged.mutex = this;
1.41 + }
1.42 +
1.43 + public static final class Privileged {
1.44 + private Mutex mutex;
1.45 +
1.46 + public void enterReadAccess() {
1.47 + mutex.lock.lock();
1.48 + }
1.49 +
1.50 + public void exitReadAccess() {
1.51 + mutex.lock.unlock();
1.52 + }
1.53 + }
1.54 +// END: mutex.privileged.api
1.55 +}