123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /**
- * Tencent is pleased to support the open source community by making Tars available.
- *
- * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
- *
- * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * https://opensource.org/licenses/BSD-3-Clause
- *
- * Unless required by applicable law or agreed to in writing, software distributed
- * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
- #ifndef __TC_SEM_MUTEX_H
- #define __TC_SEM_MUTEX_H
- #include "util/tc_platform.h"
- #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #endif
- #include "util/tc_lock.h"
- namespace tars
- {
- /////////////////////////////////////////////////
- /**
- * @file tc_sem_mutex.h
- * @brief 信号量锁类.
- *
- */
- /////////////////////////////////////////////////
- /**
- * @brief 信号量锁异常类
- */
- struct TC_SemMutex_Exception : public TC_Exception
- {
- TC_SemMutex_Exception(const string &buffer, int err) : TC_Exception(buffer, err){};
- ~TC_SemMutex_Exception() throw() {};
- };
- //锁名称统一用数字类型
- #if TARGET_PLATFORM_WINDOWS
- typedef int key_t;
- #endif
- /**
- * @brief 进程间锁, 提供两种锁机制:共享锁和排斥锁.
- *
- * 1 对于相同的key, 不同进程初始化时连接到相同的sem上
- * 2 采用IPC的信号量实现
- * 3 信号量采用了SEM_UNDO参数, 当进程结束时会自动调整信号量
- */
- class TC_SemMutex
- {
- public:
- /**
- * @brief 构造函数
- */
- TC_SemMutex();
- /**
- * @brief 析构函数
- */
- ~TC_SemMutex();
- /**
- * @brief 构造函数.
- *
- * @param iKey, key
- * @throws TC_SemMutex_Exception
- */
- TC_SemMutex(key_t iKey);
- /**
- * @brief 初始化.
- *
- * @param iKey, key
- * @throws TC_SemMutex_Exception
- * @return 无
- */
- void init(key_t iKey);
- /**
- * @brief 获取共享内存Key.
- *
- * @return key_t ,共享内存key
- */
- key_t getkey() const {return _semKey;}
- /**
- * @brief 获取共享内存ID.
- *
- * @return int ,共享内存Id
- */
- int getid() const {return _semID;}
- /**
- * @brief 加读锁.
- *
- *@return int
- */
- void rlock() const;
- /**
- * @brief 解读锁.
- *
- * @return int
- */
- void unrlock() const;
- /**
- * @brief 尝试读锁.
- *
- * @return bool : 加锁成功则返回false, 否则返回false
- */
- bool tryrlock() const;
- /**
- * @brief 加写锁.
- *
- * @return int
- */
- void wlock() const;
- /**
- * @brief 解写锁
- */
- void unwlock() const;
- /**
- * @brief 尝试写锁.
- *
- * @throws TC_SemMutex_Exception
- * @return bool : 加锁成功则返回false, 否则返回false
- */
- bool trywlock() const;
- /**
- * @brief 写锁.
- *
- * @return int, 0 正确
- */
- void lock() const {wlock();};
- /**
- * @brief 解写锁
- */
- void unlock() const {unwlock();};
- /**
- * @brief 尝试解锁.
- *
- * @throws TC_SemMutex_Exception
- * @return int, 0 正确
- */
- bool trylock() const {return trywlock();};
- #if TARGET_PLATFORM_WINDOWS
- protected:
- void addWriter() const;
- void removeWriter() const;
- void unlockImp() const;
- DWORD tryReadLockOnce() const ;
- protected:
-
- mutable HANDLE _mutex;
- mutable HANDLE _readEvent;
- mutable HANDLE _writeEvent;
- mutable unsigned _readers;
- mutable unsigned _writersWaiting;
- mutable unsigned _writers;
- #endif
- /**
- * 信号量ID
- */
- int _semID;
- /**
- * 信号量key
- */
- key_t _semKey;
- };
- }
- #endif
- //#endif
|