Logo AND Algorithmique Numérique Distribuée

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