Logo AND Algorithmique Numérique Distribuée

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