Example #1
0
  public static RubyClass createThreadClass(Ruby runtime) {
    // FIXME: In order for Thread to play well with the standard 'new' behavior,
    // it must provide an allocator that can create empty object instances which
    // initialize then fills with appropriate data.
    RubyClass threadClass =
        runtime.defineClass(
            "Thread", runtime.getObject(), ObjectAllocator.NOT_ALLOCATABLE_ALLOCATOR);
    runtime.setThread(threadClass);

    threadClass.index = ClassIndex.THREAD;
    threadClass.setReifiedClass(RubyThread.class);

    threadClass.defineAnnotatedMethods(RubyThread.class);

    RubyThread rubyThread = new RubyThread(runtime, threadClass);
    // TODO: need to isolate the "current" thread from class creation
    rubyThread.threadImpl = new NativeThread(rubyThread, Thread.currentThread());
    runtime.getThreadService().setMainThread(Thread.currentThread(), rubyThread);

    // set to default thread group
    runtime.getDefaultThreadGroup().addDirectly(rubyThread);

    threadClass.setMarshal(ObjectMarshal.NOT_MARSHALABLE_MARSHAL);

    return threadClass;
  }
Example #2
0
 /**
  * Try to acquire the given lock, adding it to a list of held locks for cleanup on thread
  * termination if it is acquired. Return immediately if the lock cannot be acquired.
  *
  * @param lock the lock to acquire, released on thread termination
  */
 public boolean tryLock(Lock lock) {
   assert Thread.currentThread() == getNativeThread();
   boolean locked = lock.tryLock();
   if (locked) {
     heldLocks.add(lock);
   }
   return locked;
 }
Example #3
0
 private static void debug(RubyThread thread, String message) {
   if (DEBUG) LOG.debug(Thread.currentThread() + "(" + thread.status + "): " + message);
 }
Example #4
0
 /** Release all locks held. */
 public void unlockAll() {
   assert Thread.currentThread() == getNativeThread();
   for (Lock lock : heldLocks) {
     lock.unlock();
   }
 }
Example #5
0
 /**
  * Release the given lock and remove it from the list of locks to be released on thread
  * termination.
  *
  * @param lock the lock to release and dereferences
  */
 public void unlock(Lock lock) {
   assert Thread.currentThread() == getNativeThread();
   lock.unlock();
   heldLocks.remove(lock);
 }
Example #6
0
 /**
  * Acquire the given lock interruptibly, holding a reference to it for cleanup on thread
  * termination.
  *
  * @param lock the lock to acquire, released on thread termination
  * @throws InterruptedException if the lock acquisition is interrupted
  */
 public void lockInterruptibly(Lock lock) throws InterruptedException {
   assert Thread.currentThread() == getNativeThread();
   lock.lockInterruptibly();
   heldLocks.add(lock);
 }
Example #7
0
 /**
  * Acquire the given lock, holding a reference to it for cleanup on thread termination.
  *
  * @param lock the lock to acquire, released on thread termination
  */
 public void lock(Lock lock) {
   assert Thread.currentThread() == getNativeThread();
   lock.lock();
   heldLocks.add(lock);
 }
 /**
  * Perform pre-execution tasks once the native thread is running, but we have not yet called the
  * Ruby code for the thread.
  */
 public void beforeStart() {
   // store initial priority, for restoring pooled threads to normal
   this.initialPriority = Thread.currentThread().getPriority();
 }