#ifndef DEVICE_CTRL_BIZ_LOCK_H
#define DEVICE_CTRL_BIZ_LOCK_H
#include <mutex>
#include <string>
#include <unordered_set>

namespace zsy
{
    class BizLockHeader;

    class BizLock
    {
        std::unordered_set<std::string> locks;
        std::mutex locksMtx;
        std::atomic_bool destroyed;

    public:
        BizLock();

        ~BizLock();

        std::unique_ptr<BizLockHeader> lock(const std::string &bizId);

        void unlock(const std::string &bizId);
    };

    class BizLockHeader
    {
        const std::string bizId;
        BizLock *bizLock;

    public:
        friend class BizLock;

        BizLockHeader(const std::string &bizId, BizLock *bizLock);

        ~BizLockHeader();
    };
} // zsy

#define LOCK_BIZ(bizId) if (const auto bizLockHeader = bizLock->lock(bizId); bizLockHeader)
#define LOCK_BIZ_FAIL(bizId) const auto bizLockHeader = bizLock->lock(bizId); \
    if (!bizLockHeader)

#endif //DEVICE_CTRL_BIZ_LOCK_H