public int sceKernelCreateLwMutex(
      int workAreaAddr, int name_addr, int attr, int count, int option_addr) {
    Memory mem = Processor.memory;

    String name = Utilities.readStringNZ(mem, name_addr, 32);

    if (log.isDebugEnabled()) {
      log.debug(
          "sceKernelCreateLwMutex (workAreaAddr='"
              + Integer.toHexString(workAreaAddr)
              + "', name='"
              + name
              + "', attr=0x"
              + Integer.toHexString(attr)
              + ", count=0x"
              + Integer.toHexString(count)
              + ", option_addr=0x"
              + Integer.toHexString(option_addr)
              + ")");
    }

    SceKernelLwMutexInfo info = new SceKernelLwMutexInfo(workAreaAddr, name, count, attr);
    lwMutexMap.put(info.uid, info);

    // If the initial count is 0, the lwmutex is not acquired.
    if (count > 0) {
      info.threadid = Modules.ThreadManForUserModule.getCurrentThreadID();
    }

    // Return 0 in case of no error, do not return the UID of the created mutex
    return 0;
  }
  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 boolean tryLockLwMutex(SceKernelLwMutexInfo info, int count, SceKernelThreadInfo thread) {
   if (info.lockedCount == 0) {
     // If the lwmutex is not locked, allow this thread to lock it.
     info.threadid = thread.uid;
     info.lockedCount += count;
     return true;
   } else if (info.threadid == thread.uid) {
     // If the lwmutex is already locked, but it's trying to be locked by the same thread
     // that acquired it initially, check if recursive locking is allowed.
     // If not, return an error.
     if (((info.attr & PSP_LWMUTEX_ATTR_ALLOW_RECURSIVE) == PSP_LWMUTEX_ATTR_ALLOW_RECURSIVE)) {
       info.lockedCount += count;
       return true;
     }
   }
   return false;
 }