Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Added Semaphore visibility to the Java bindings
[simgrid.git] / src / bindings / java / org / simgrid / msg / Semaphore.java
1 /* 
2  * Copyright 2012 The SimGrid team. All right reserved. 
3  *
4  * This program is free software; you can redistribute 
5  * it and/or modify it under the terms of the license 
6  * (GNU LGPL) which comes with this package.
7  *
8  */
9 package org.simgrid.msg;
10 /** A semaphore implemented on top of SimGrid synchronization mechanisms. 
11  * You can use it exactly the same way that you use classical semaphores
12  * but to handle the interactions between the processes within the simulation.   
13  *
14  */
15
16 public class Semaphore {
17     private long bind; // The C object -- don't touch it
18     /**
19      * Semaphore capacity, defined when the semaphore is created. At most capacity 
20      * process can acquire this semaphore at the same time.
21      */
22     protected final int capacity;
23     /**
24      * Creates a new semaphore with the given capacity. At most capacity 
25      * process can acquire this semaphore at the same time.
26      */
27     public Semaphore(int capacity) {
28         init(capacity);
29         this.capacity = capacity;
30     }
31     /** The native implementation of semaphore initialization
32      */
33     private native void init(int capacity);
34
35
36     /** Locks on the semaphore object until the provided timeout expires
37      * @exception TimeoutException if the timeout expired before 
38      *            the semaphore could be acquired.
39      */
40     public native void acquire(double timeout) throws TimeoutException;
41     /** Locks on the semaphore object with no timeout
42      */
43     public void acquire() {
44         try {
45             acquire(-1);
46         } catch (TimeoutException e) {
47             // This should not happen.
48             assert(false);
49         }
50     }
51     /** Releases the semaphore object
52      */
53     public native void release();
54     /** returns a boolean indicating it this semaphore would block at this very specific time
55      *
56      * Note that the returned value may be wrong right after the
57      * function call, when you try to use it...  But that's a
58      * classical semaphore issue, and SimGrid's semaphores are not
59      * different to usual ones here.
60      */
61     public native boolean wouldBlock();
62
63     /** Returns the semaphore capacity
64      */
65     public int getCapacity(){
66         return this.capacity;
67     }
68
69
70     /** Deletes this semaphore 
71      */
72     protected void finalize() {
73         destroy();
74     }
75     /** The native implementation for destroying a semaphore
76      */
77     private native void destroy();
78     /**
79      * Class initializer, to initialize various JNI stuff
80      */
81     public static native void nativeInit();
82     static {
83         Msg.nativeInit();
84         nativeInit();
85     }
86 }