Crafting critical section

This is not a Support area! Discuss about the Server here. Non-Server related discussion goes in Off-Topic Discussion.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
Vith
Posts: 11
Joined: Mon Jun 28, 2010 2:17 pm
Location: Poland
Contact:

Crafting critical section

Post by Vith »

I was just glancing through the crafting code, and I have some doubts about ingredients availability check.

It goes like this:
  1. The check is done with RecipeController$RecipeItemMaker.listItems(boolean remove) method, with the "remove" parameter indicating if ingredients shall be taken out of the inventory or not.
  2. First time, it's run in RecipeItemMaker constructor ("remove" parameter = false), as one of verification steps.
  3. Second time, it's run just before the success/fail check, in RecipeItemMaker.finishCrafting() method, this time with "remove" parameter = true.
Now, listItem() method looks like this:

Code: Select all

foreach (ingredient in ingredients)    check if there is enough items in player's inventoryif (remove)    remove items from player's inventory
Now, for me it looks like a classical critical section, that is not thread-safe. It is theoretically possible for a player (i.e. another server thread) to remove the ingredients just after the "foreach" part, but before the "remove" part. Of course that it *is* hard, but it is *not* impossible.

I believe introducing synchronization to the critical section mentioned is required. Please correct me if I'm wrong, I'm definitely not an L2j guru.
me = new Geek();
while (me.getSpareTime() > 0)
{
me.playLineage();
}
printf("Another nonconstructive day (-;\n");
User avatar
JIV
L2j Veteran
L2j Veteran
Posts: 1882
Joined: Sun Jan 06, 2008 8:17 pm
Location: Slovakia
Contact:

Re: Crafting critical section

Post by JIV »

in theory yes but we dont make bank soft so in some places are synces ignored for performance benefit. This code is quite old maybe there would be a better way to handle it now (it dont check even destroyitem return value..). Also this will not be able with single packet executor.
User avatar
Vith
Posts: 11
Joined: Mon Jun 28, 2010 2:17 pm
Location: Poland
Contact:

Re: Crafting critical section

Post by Vith »

By no means I consider this thing a critical issue or something. It's more just a tip to point your attention towards "proper" multi-threaded coding. I mean, from what I understand, L2j was created as an educational project, so this is like educational stuff. We all want to be *good* software devs (-;
me = new Geek();
while (me.getSpareTime() > 0)
{
me.playLineage();
}
printf("Another nonconstructive day (-;\n");
Post Reply