2 * Copyright 2012 The SimGrid team. All right reserved.
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.
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.
16 public class Semaphore {
17 private long bind; // The C object -- don't touch it
19 * Semaphore capacity, defined when the semaphore is created. At most capacity
20 * process can acquire this semaphore at the same time.
22 protected final int capacity;
24 * Creates a new semaphore with the given capacity. At most capacity
25 * process can acquire this semaphore at the same time.
27 public Semaphore(int capacity) {
29 this.capacity = capacity;
31 /** The native implementation of semaphore initialization
33 private native void init(int capacity);
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.
40 public native void acquire(double timeout) throws TimeoutException;
41 /** Locks on the semaphore object with no timeout
43 public void acquire() {
46 } catch (TimeoutException e) {
47 // This should not happen.
51 /** Releases the semaphore object
53 public native void release();
54 /** returns a boolean indicating it this semaphore would block at this very specific time
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.
61 public native boolean wouldBlock();
63 /** Returns the semaphore capacity
65 public int getCapacity(){
70 /** Deletes this semaphore
72 protected void finalize() {
75 /** The native implementation for destroying a semaphore
77 private native void destroy();
79 * Class initializer, to initialize various JNI stuff
81 public static native void nativeInit();