private void onLwMutexModified(SceKernelLwMutexInfo info) {
    ThreadManForUser threadMan = Modules.ThreadManForUserModule;
    boolean reschedule = false;

    if ((info.attr & PSP_LWMUTEX_ATTR_PRIORITY) == PSP_LWMUTEX_ATTR_FIFO) {
      for (Iterator<SceKernelThreadInfo> it = Modules.ThreadManForUserModule.iterator();
          it.hasNext(); ) {
        SceKernelThreadInfo thread = it.next();
        if (thread.isWaitingForType(PSP_WAIT_LWMUTEX)
            && thread.wait.LwMutex_id == info.uid
            && tryLockLwMutex(info, thread.wait.LwMutex_count, thread)) {
          // New thread is taking control of LwMutex.
          info.threadid = thread.uid;
          // Update numWaitThreads
          info.numWaitThreads--;
          // Return success or failure
          thread.cpuContext.gpr[2] = 0;
          // Wakeup
          threadMan.hleChangeThreadState(thread, PSP_THREAD_READY);
          reschedule = true;
        }
      }
    } else if ((info.attr & PSP_LWMUTEX_ATTR_PRIORITY) == PSP_LWMUTEX_ATTR_PRIORITY) {
      for (Iterator<SceKernelThreadInfo> it = Modules.ThreadManForUserModule.iteratorByPriority();
          it.hasNext(); ) {
        SceKernelThreadInfo thread = it.next();
        if (thread.isWaitingForType(PSP_WAIT_LWMUTEX)
            && thread.wait.LwMutex_id == info.uid
            && tryLockLwMutex(info, thread.wait.LwMutex_count, thread)) {
          // New thread is taking control of LwMutex.
          info.threadid = thread.uid;
          // Update numWaitThreads
          info.numWaitThreads--;
          // Return success or failure
          thread.cpuContext.gpr[2] = 0;
          // Wakeup
          threadMan.hleChangeThreadState(thread, PSP_THREAD_READY);
          reschedule = true;
        }
      }
    }
    // Reschedule only if threads waked up.
    if (reschedule) {
      Modules.ThreadManForUserModule.hleRescheduleCurrentThread();
    }
  }
  private int hleKernelLockLwMutex(
      int uid, int count, int timeout_addr, boolean wait, boolean doCallbacks) {
    SceKernelLwMutexInfo info = lwMutexMap.get(uid);
    if (info == null) {
      log.warn(
          String.format(
              "hleKernelLockLwMutex uid=%d, count=%d, timeout_addr=0x%08X, wait=%b, doCallbacks=%b -  - unknown UID",
              uid, count, timeout_addr, wait, doCallbacks));
      return ERROR_KERNEL_LWMUTEX_NOT_FOUND;
    }

    ThreadManForUser threadMan = Modules.ThreadManForUserModule;
    SceKernelThreadInfo currentThread = threadMan.getCurrentThread();
    if (!tryLockLwMutex(info, count, currentThread)) {
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "hleKernelLockLwMutex %s, count=%d, timeout_addr=0x%08X, wait=%b, doCallbacks=%b - fast check failed",
                info.toString(), count, timeout_addr, wait, doCallbacks));
      }
      if (wait && info.threadid != currentThread.uid) {
        // Failed, but it's ok, just wait a little
        info.numWaitThreads++;
        // Wait on a specific lwmutex
        currentThread.wait.LwMutex_id = uid;
        currentThread.wait.LwMutex_count = count;
        threadMan.hleKernelThreadEnterWaitState(
            PSP_WAIT_LWMUTEX, uid, lwMutexWaitStateChecker, timeout_addr, doCallbacks);
      } else {
        if ((info.attr & PSP_LWMUTEX_ATTR_ALLOW_RECURSIVE) != PSP_LWMUTEX_ATTR_ALLOW_RECURSIVE) {
          return ERROR_KERNEL_LWMUTEX_RECURSIVE_NOT_ALLOWED;
        }
        return ERROR_KERNEL_LWMUTEX_LOCKED;
      }
    } else {
      // Success, do not reschedule the current thread.
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "hleKernelLockLwMutex %s, count=%d, timeout_addr=0x%08X, wait=%b, doCallbacks=%b - fast check succeeded",
                info.toString(), count, timeout_addr, wait, doCallbacks));
      }
    }

    return 0;
  }
  private void onLwMutexDeleted(int lwmid) {
    ThreadManForUser threadMan = Modules.ThreadManForUserModule;
    boolean reschedule = false;

    for (Iterator<SceKernelThreadInfo> it = threadMan.iterator(); it.hasNext(); ) {
      SceKernelThreadInfo thread = it.next();
      if (thread.isWaitingForType(PSP_WAIT_LWMUTEX) && thread.wait.LwMutex_id == lwmid) {
        thread.cpuContext.gpr[2] = ERROR_KERNEL_WAIT_DELETE;
        threadMan.hleChangeThreadState(thread, PSP_THREAD_READY);
        reschedule = true;
      }
    }
    // Reschedule only if threads waked up.
    if (reschedule) {
      threadMan.hleRescheduleCurrentThread();
    }
  }