CRM-12466 -  Add locking to smart group creation

Please do not merge, totten can u review this version. I'll do some more testing