Example #1
0
  @JRubyMethod
  public synchronized IRubyObject unlock(ThreadContext context) {
    Ruby runtime = context.getRuntime();
    if (!lock.isLocked()) {
      throw runtime.newThreadError("Mutex is not locked");
    }
    if (!lock.isHeldByCurrentThread()) {
      throw runtime.newThreadError("Mutex is not owned by calling thread");
    }

    boolean hasQueued = lock.hasQueuedThreads();
    context.getThread().unlock(lock);
    return hasQueued ? context.nil : this;
  }
Example #2
0
  @JRubyMethod(rest = true)
  public IRubyObject initialize(ThreadContext context, IRubyObject[] _events, final Block block) {
    final Ruby runtime = context.runtime;

    if (!block.isGiven()) throw runtime.newThreadError("must be called with a block");

    RubyEvent[] events = new RubyEvent[_events.length];
    for (int i = 0; i < _events.length; i++) {
      IRubyObject _event = _events[i];
      if (_event instanceof RubySymbol || _event instanceof RubyString) {
        String eventName = _event.asJavaString();
        RubyEvent event = RubyEvent.valueOf(eventName);

        if (event == null) throw runtime.newArgumentError("unknown event: " + eventName);

        events[i] = event;
      }
    }

    EnumSet<RubyEvent> _eventSet;
    if (events.length > 0) {
      _eventSet = EnumSet.copyOf(Arrays.asList(events));
    } else {
      _eventSet = EnumSet.allOf(RubyEvent.class);
    }

    final EnumSet<RubyEvent> eventSet = _eventSet;
    hook =
        new EventHook() {
          @Override
          public synchronized void eventHandler(
              ThreadContext context,
              String eventName,
              String file,
              int line,
              String name,
              IRubyObject type) {
            if (!enabled || context.isWithinTrace()) return;

            inside = true;
            try {
              if (file == null) file = "(ruby)";
              if (type == null) type = context.runtime.getFalse();

              RubyBinding binding =
                  RubyBinding.newBinding(context.runtime, context.currentBinding());

              context.preTrace();

              // FIXME: get return value
              update(
                  eventName, file, line, name, type, context.getErrorInfo(), context.nil, binding);

              block.yieldSpecific(context, TracePoint.this);
            } finally {
              update(null, null, line, null, context.nil, context.nil, context.nil, context.nil);
              context.postTrace();
              inside = false;
            }
          }

          @Override
          public boolean isInterestedInEvent(RubyEvent event) {
            return eventSet.contains(event);
          }
        };

    return context.nil;
  }