2 * Copyright (c) 2012-2013. The SimGrid Team.
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.
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.
17 public class Semaphore {
18 private long bind; // The C object -- don't touch it
20 * Semaphore capacity, defined when the semaphore is created. At most capacity
21 * process can acquire this semaphore at the same time.
23 protected final int capacity;
25 * Creates a new semaphore with the given capacity. At most capacity
26 * process can acquire this semaphore at the same time.
28 public Semaphore(int capacity) {
30 this.capacity = capacity;
32 /** The native implementation of semaphore initialization
34 private native void init(int capacity);
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.
41 public native void acquire(double timeout) throws TimeoutException;
42 /** Locks on the semaphore object with no timeout
44 public void acquire() {
47 } catch (TimeoutException e) {
48 // This should not happen.
52 /** Releases the semaphore object
54 public native void release();
55 /** returns a boolean indicating it this semaphore would block at this very specific time
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.
62 public native boolean wouldBlock();
64 /** Returns the semaphore capacity
66 public int getCapacity(){
71 /** Deletes this semaphore
73 protected void finalize() {
76 /** The native implementation for destroying a semaphore
78 private native void destroy();
80 * Class initializer, to initialize various JNI stuff
82 public static native void nativeInit();