Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
Hi all,
Starting from this post by @atondorf and my original believe (which once upon a time was true), I've been working on an extension to handle Mutex and Counter situations for now. Hope it helps anyone which faces this kind of problems which aren't easy to handle with ThingWorx out-of-the-box.
Here it's the public repository with the source code and the Extension ready to be used and fully documented:
https://github.com/carlescm/ThingWorxConcurrencyExtension
Here a copy of the Usage part on the README.md from GitHub:
We implemented a mutex ThingShape with Java ReentrantLook with fair execution enabled which allows to synchronize and block thread execution. Blocking it's done at Thing's level, it means that each Thing which implements the wupMutexTS ThingShape has it's own ReentrantLook.
Just add the wupMutexTS to the Thing or ThingTemplate to whom you want to add mutex blocking features.
In order to lock a piece of code in Javascript, and ensure that only one thread its entering on it at a time:
var meName = me.name;
me.Lock_wupMutexTS();
try {
// -- whatever code that needs to be mutex
} finally {
Things[meName].Unlock_wupMutexTS();
}
You can also tryLock a piece of code, in order to allow one thread and only one and discard the others. For instance it may be interesting if you have a timer which triggers once in a while and you don't want that two consecutive triggers are executed at the same time:
var meName = me.name;
if (me.TryLock_wupMutexTS()===true) {
try {
// -- whatever code that needs to be mutex
} finally {
Things[meName].Unlock_wupMutexTS();
}
} else {
// -- The lock was already got and previous code its skipped
}
You can create more than one mutex per thing, all wupMutexTS services has an optional "id" parameter, which allows to create a more quirurgic mutex. Each different passed "id" will create its own ReentrantLook. Sample with previous code but with a specific lock for one specific timer.
var meName = me.name;
if (me.TryLock_wupMutexTS({ id: "timer1" })===true) {
try {
// -- whatever code that needs to be mutex
} finally {
Things[meName].Unlock_wupMutexTS({ id: "timer1" });
}
} else {
// -- The lock was already got and previous code it's skipped
}
A thread safe "autoincrement" ThingShape. It provides a "counter" property and the corresponding services in order to increase (one by one) it's value.
To increase the counter value, no worries about having any amount of threads incrementing the property value, all will get it's own and unique value:
var newCounterValue = me.Increase_wupCounterTS();
You can reset or reset counter value with Set_wupCounterTS method:
me.Set_wupCounterTS({ value: 0 });
The counter it's stored and update on a persistent property named: counter_wupCounterTS
List of script helper functions related with this concurrency extension. This services should go on a subsystem like entity, but subsystems on ThingWorx can't be created through extensions
Returns the total active locks, in the whole ThingWorx running system.
Returns the total active threads which are waiting on a lock, in the whole ThingWorx running system.
Returns the total number of mutex created on Things (ReentranLocks), in the whole ThingWorx running system since last start.
Best Regards,
Carles Coll
Hi @CarlesColl.
Thank you for your contributions to the community! We are very appreciative of your level of participation.
Regards.
--Sharon