diff --git a/base/include/semaphore_ex.h b/base/include/semaphore_ex.h index 5c4ca67cb0156c526dcb1bcdfaefa19dfca6fa1f..ccb1fd11abb901f3b3502acd82799f1b11999ac5 100644 --- a/base/include/semaphore_ex.h +++ b/base/include/semaphore_ex.h @@ -39,172 +39,6 @@ #include // timespec since c11 namespace OHOS { - -const int INVALID_SEMA_VALUE = -1; - -/** - * @brief NamedSemaphore. - * - * The only difference between the NamedSemaphore and the Semaphore - * lies in the form of creation and destruction. - * Generally, the name of the specified semaphore is explicitly specified - * at the time of creation, and can be accessed directly by name, - * so it can be used in any process/thread that knows its name. - * The characteristic of the NamedSemaphore is that they are saved in a file, - * so it is necessary to close the semaphore at the end like closing the file. - */ -class NamedSemaphore : public NoCopyable { -public: - -/** - * @brief Construct a NamedSemaphore object with a default name, - * and initial value of the semaphore. - * - * The constructor only creates a NamedSemaphore object and does not - * call sem_open(), so you need to call Create() to create semaphores - * after construction. - * - * @param size the initial value of semaphore. - */ - explicit NamedSemaphore(size_t); - -/** - * @brief Construct a NamedSemaphore object, - * specify the name and initial value of the semaphore. - * - * The constructor only creates a NamedSemaphore object and does not - * call sem_open(), so you need to call Create() to create semaphores - * after construction. - * - * @param name the name of NamedSemaphore; NamedSemaphore objects constructed - * by constructors that do not specify a name are assigned - * a unique name by default. - * @param size the initial value of semaphore. - */ - NamedSemaphore(const std::string&, size_t); - -/** - * @brief Destroy a NamedSemaphore object. - */ - ~NamedSemaphore() override; - -/** - * @brief Create and initialize a named semaphore. - * - * When a Namedsemaphore is created, a sem.xxx file is generated under - * /dev/shm, and all processes/threads that open the semaphore - * (including the processes/threads that created it) increment the reference - * counter for that file managed by the kernel. - * - * @return Success returns ture, failure returns false. - */ - bool Create(); - -/** - * @brief Remove the semaphore from the system. - * - * When Unlink() is called, the sem.xxx file under /dev/shm rather than - * the semaphore itself will be deleted immediately, and all processes that - * have opened the semaphore can still use it normally (because the semaphore - * file that has been opened is in memory), even if Unlink() is called - * immediately after opening the file, Wait(), Post() can also operate - * normally on the semaphore. - * Each semaphore has a reference counter that records the number of times - * it has been opened, and when the reference counter is 0, - * the named semaphore can be deleted from the file system by Unlink(). - * That is, the kernel does not actually delete the semaphore until - * all processes that open the semaphore have closed the semaphore - * (the last sem_close call completes). - * - * @return Success returns ture, failure returns false. - */ - bool Unlink(); - -/** - * @brief Open a Namedsemaphore file that has already been created. - * - * @return Success returns ture, failure returns false. - */ - bool Open(); - -/** - * @brief Turn off the NamedSemaphore. - * - * Turn off the semaphore and do not remove it from the system. - * - * @return Success returns ture, failure returns false. - */ - bool Close(); - -/** - * @brief Interface for acquiring semaphore (the count of semaphore -1). - * - * If the current count value is 0, the call blocks util another call of Post() - * makes the count greater than 0. - * - * @return Success returns 0; Failure returns -1, the semaphore will not be - * changed by the interface, and errno is set to indicate a specific error. - */ - bool Wait(); - -/** - * @brief Interface for acquiring semaphore (the count of semaphore -1); - * Non-blocking version. - * - * The non-blocking version of Wait() returns an error directly - * when the current semaphore = 0, not blocking. - * - * @return Success returns 0; Failure returns -1, the signal will - * not be changed by the interface, - * and errno is set to indicate a specific error. - */ - bool TryWait(); - -/** - * @brief Interface for acquiring semaphore (the count of semaphore -1); - * Specifies the blocking time version. - * - * The version of Wait() that specifies the blocking time, - * if the value of the semaphore is great than 0, - * the -1 operation is performed, and the function returns immediately; - * When the -1 operation cannot be executed, the call will block - * (specify the blocking time). - * - * @param ts Specifies the blocking time of the call. - * @return Success returns 0; Failure returns -1, the semaphore will - * not be changed by the interface, - * and errno is set to indicate a specific error. - */ - bool TimedWait(const struct timespec& ts); - -/** - * @brief Interface for releasing semaphore (the count of semaphore +1). - * - * If the value of the semaphore thus becomes greater than 0, - * another process/thread that is blocked due to the sem_wait of the call - * will be woken up and will lock the semaphore. - * - * @return Success returns 0; Failure returns -1, the semaphore will not be - * changed by the interface, and errno is set to indicate a specific error. - */ - bool Post(); - -/** - * @brief Get the value of the semaphore. - * - * @return The value of the semaphore is successfully returned; - * Failure returns INVALID_SEMA_VALUE, which is -1. - */ - int GetValue() const; - -private: - std::string name_; // The name of the NamedSemaphore object - int maxCount_; // The initial value of the NamedSemaphore object - void* sema_; // The pointer to the sem_t structure, which is essentially a long integer. - bool named_; // The flag of a Namedsemaphore that is false when no named semaphore name is specified. - -}; - /** * @brief Semaphore. This class is a counter to implement functions such as * mutual exclusion between processes/threads, synchronization, etc. diff --git a/base/src/event_demultiplexer.cpp b/base/src/event_demultiplexer.cpp index fef76d7bfee08014ad1fcd521cfeb5c85b313e43..87df8a71f0527960f537ad45991a12f199ccca61 100644 --- a/base/src/event_demultiplexer.cpp +++ b/base/src/event_demultiplexer.cpp @@ -85,26 +85,6 @@ uint32_t EventDemultiplexer::UpdateEventHandler(EventHandler* handler) return Update(EPOLL_CTL_MOD, handler); } -uint32_t EventDemultiplexer::RemoveEventHandler(EventHandler* handler) -{ - if (handler == nullptr) { - return TIMER_ERR_INVALID_VALUE; - } - - std::lock_guard lock(mutex_); - auto itor = eventHandlers_.find(handler->GetHandle()); - if (itor == eventHandlers_.end()) { - return TIMER_ERR_OK; - } - - eventHandlers_.erase(itor); - if (static_cast(eventHandlers_.size()) < maxEvents_) { - maxEvents_ = eventHandlers_.size() / HALF_OF_MAX_EVENT; - } - - return Update(EPOLL_CTL_DEL, handler); -} - uint32_t EventDemultiplexer::Update(int operation, EventHandler* handler) { struct epoll_event event; @@ -148,22 +128,10 @@ void EventDemultiplexer::Polling(int timeout /* ms */) uint32_t EventDemultiplexer::Epoll2Reactor(uint32_t epollEvents) { - if ((epollEvents & EPOLLHUP) && !(epollEvents & EPOLLIN)) { - return EventReactor::CLOSE_EVENT; - } - - if (epollEvents & EPOLLERR) { - return EventReactor::ERROR_EVENT; - } - if (epollEvents & (EPOLLIN | EPOLLPRI | EPOLLRDHUP)) { return EventReactor::READ_EVENT; } - if (epollEvents & EPOLLOUT) { - return EventReactor::WRITE_EVENT; - } - return EventReactor::NONE_EVENT; } @@ -174,10 +142,6 @@ uint32_t EventDemultiplexer::Reactor2Epoll(uint32_t reactorEvent) return TIMER_ERR_OK; case EventReactor::READ_EVENT: return EPOLLIN | EPOLLPRI; - case EventReactor::WRITE_EVENT: - return EPOLLOUT; - case EventReactor::READ_EVENT | EventReactor::WRITE_EVENT: - return EPOLLIN | EPOLLPRI | EPOLLOUT; default: UTILS_LOGD("invalid event %{public}u.", reactorEvent); return TIMER_ERR_DEAL_FAILED; diff --git a/base/src/event_demultiplexer.h b/base/src/event_demultiplexer.h index 3440abd8a5078b3f2c1fa82627a1d910b6e4d48a..5c25e5663c090835009b9066f20ea6c67d439edc 100644 --- a/base/src/event_demultiplexer.h +++ b/base/src/event_demultiplexer.h @@ -38,7 +38,6 @@ public: void Polling(int timeout); uint32_t UpdateEventHandler(EventHandler* handler); - uint32_t RemoveEventHandler(EventHandler* handler); private: uint32_t Update(int operation, EventHandler* handler); diff --git a/base/src/event_handler.cpp b/base/src/event_handler.cpp index f318cd0477aad27e1e8ceec45fbe75329382d221..fee1cabe71374f527094583dbd0d510cc315522b 100644 --- a/base/src/event_handler.cpp +++ b/base/src/event_handler.cpp @@ -31,17 +31,6 @@ void EventHandler::EnableRead() Update(); } -void EventHandler::EnableWrite() -{ - events_ |= EventReactor::WRITE_EVENT; - Update(); -} - -void EventHandler::DisableWrite() -{ - events_ &= ~EventReactor::WRITE_EVENT; - Update(); -} void EventHandler::DisableAll() { events_ = EventReactor::NONE_EVENT; @@ -50,29 +39,11 @@ void EventHandler::DisableAll() void EventHandler::HandleEvents(uint32_t events) { - if (events & (EventReactor::CLOSE_EVENT)) { - if (closeCallback_) { - closeCallback_(); - } - } - - if (events & (EventReactor::ERROR_EVENT)) { - if (errorCallback_) { - errorCallback_(); - } - } - if (events & (EventReactor::READ_EVENT)) { if (readCallback_) { readCallback_(); } } - - if (events & (EventReactor::WRITE_EVENT)) { - if (writeCallback_) { - writeCallback_(); - } - } } void EventHandler::Update() diff --git a/base/src/event_handler.h b/base/src/event_handler.h index 5a17082454b27aaeb709b3a9877736b55a70000f..af75cbea09bfb1a381843b1cf5a9c7e43465c8da 100644 --- a/base/src/event_handler.h +++ b/base/src/event_handler.h @@ -40,16 +40,11 @@ public: uint32_t Events() const { return (events_); } void EnableRead(); - void EnableWrite(); - void DisableWrite(); void DisableAll(); const EventReactor* GetEventReactor() const { return reactor_; } - void SetCloseCallback(const Callback& closeCallback) { closeCallback_ = closeCallback; } - void SetErrorCallback(const Callback& errorCallback) { errorCallback_ = errorCallback; } void SetReadCallback(const Callback& readCallback) { readCallback_ = readCallback; } - void SetWriteCallback(const Callback& writeCallback) { writeCallback_ = writeCallback; } void HandleEvents(uint32_t events); @@ -62,9 +57,6 @@ private: EventReactor* reactor_; Callback readCallback_; - Callback writeCallback_; - Callback closeCallback_; - Callback errorCallback_; }; } diff --git a/base/src/event_reactor.cpp b/base/src/event_reactor.cpp index bb57ca644107ee76ec47f089e5b60b88dc729c57..91c6ebdada19d307a9b2fa5a8240f244fc98ebea 100644 --- a/base/src/event_reactor.cpp +++ b/base/src/event_reactor.cpp @@ -35,13 +35,6 @@ EventReactor::~EventReactor() { } -void EventReactor::RemoveEventHandler(EventHandler* handler) -{ - if ((handler != nullptr) && (handler->GetEventReactor() == this) && (demultiplexer_ != nullptr)) { - demultiplexer_->RemoveEventHandler(handler); - } -} - void EventReactor::UpdateEventHandler(EventHandler* handler) { if ((handler != nullptr) && (handler->GetEventReactor() == this) && (demultiplexer_ != nullptr)) { diff --git a/base/src/event_reactor.h b/base/src/event_reactor.h index 794e44a50ef93a470ae7c315f1dd70cee1679e3d..83139bdc6277a4153a068696ddcba0d504cc7d26 100644 --- a/base/src/event_reactor.h +++ b/base/src/event_reactor.h @@ -63,7 +63,6 @@ public: } void UpdateEventHandler(EventHandler* handler); - void RemoveEventHandler(EventHandler* handler); uint32_t ScheduleTimer(const TimerCallback& cb, uint32_t interval /* ms */, int& timerFd, bool once); void CancelTimer(int timerFd); diff --git a/base/src/semaphore_ex.cpp b/base/src/semaphore_ex.cpp index 205bf09cc36159ab6f7419a0496120a5811b3fbd..5023a75884a9b595aaf793e17d3f86cc042c595d 100644 --- a/base/src/semaphore_ex.cpp +++ b/base/src/semaphore_ex.cpp @@ -26,122 +26,6 @@ using namespace std; namespace OHOS { - -std::string GenUniqueName() -{ - static const uint64_t magic = 0xbcdecdeffedcedcbULL; - static uint64_t count = 0; - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; - - pthread_mutex_lock(&mutex); - count++; - std::ostringstream fmt; - static const int outWidth = 20; - fmt << "_unnamed_sema_" << getpid() << "_" << magic << "_" - << std::setw(outWidth) << std::setfill('0') << count++; - pthread_mutex_unlock(&mutex); - return fmt.str(); -} - -NamedSemaphore::NamedSemaphore(size_t size) - :name_(GenUniqueName()), maxCount_(size), sema_(nullptr), named_(false) -{ -} - -NamedSemaphore::NamedSemaphore(const std::string& name, size_t size) - :name_(name), maxCount_(size), sema_(nullptr), named_(true) -{ -} - -NamedSemaphore::~NamedSemaphore() -{ - if (!named_) { - Unlink(); - } -} - -bool NamedSemaphore::Create() -{ - Unlink(); - sema_ = sem_open(name_.c_str(), O_CREAT, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH, maxCount_); // mode: 0666 - return (sema_ != SEM_FAILED); -} - -bool NamedSemaphore::Unlink() -{ - return sem_unlink(name_.c_str()) == 0; -} - -bool NamedSemaphore::Open() -{ - sema_ = sem_open(name_.c_str(), 0); - return (sema_ != SEM_FAILED); -} - -bool NamedSemaphore::Close() -{ - if (sema_ == nullptr) { - return false; - } - - if (sem_close(reinterpret_cast(sema_)) == 0) { - sema_ = nullptr; - return true; - } - return false; -} - -bool NamedSemaphore::Wait() -{ - if (sema_ == nullptr) { - return false; - } - - return (sem_wait(reinterpret_cast(sema_)) == 0); -} - -bool NamedSemaphore::TimedWait(const struct timespec& ts) -{ - if (sema_ == nullptr) { - return false; - } - - return (sem_timedwait(reinterpret_cast(sema_), &ts) == 0); -} - -bool NamedSemaphore::TryWait() -{ - if (sema_ == nullptr) { - return false; - } - - return (sem_trywait(reinterpret_cast(sema_)) == 0); - -} - -bool NamedSemaphore::Post() -{ - if ((sema_ == nullptr) || (GetValue() >= maxCount_)) { - return false; - } - - return (sem_post(reinterpret_cast(sema_)) == 0); -} - -int NamedSemaphore::GetValue() const -{ - if (sema_ == nullptr) { - return INVALID_SEMA_VALUE; - } - - int val = -1; - if (sem_getvalue(reinterpret_cast(sema_), &val) == 0) { - return val; - } - return INVALID_SEMA_VALUE; -} - void Semaphore::Wait() { std::unique_lock lck(mutex_); diff --git a/base/test/unittest/common/utils_timer_test.cpp b/base/test/unittest/common/utils_timer_test.cpp index acaed1b64dbcf4af3ee8bb1650207d4377d6f122..5e15470a3fb63447bb531a42eb1a73b32459ac9a 100644 --- a/base/test/unittest/common/utils_timer_test.cpp +++ b/base/test/unittest/common/utils_timer_test.cpp @@ -337,5 +337,41 @@ HWTEST_F(UtilsTimerTest, testTimer011, TestSize.Level0) timer.Shutdown(); EXPECT_GE(g_data1, 8); /* 12 for max */ } + +/* + * @tc.name: testTimer012 + * @tc.desc: Test double setup. + */ +HWTEST_F(UtilsTimerTest, testTimer012, TestSize.Level0) +{ + g_data1 = 0; + Utils::Timer timer("test_timer"); + uint32_t ret = timer.Setup(); + EXPECT_EQ(Utils::TIMER_ERR_OK, ret); + ret = timer.Setup(); + EXPECT_EQ(Utils::TIMER_ERR_INVALID_VALUE, ret); + + timer.Shutdown(); +} + +/* + * @tc.name: testTimer013 + * @tc.desc: Test uncommon operations. + */ +HWTEST_F(UtilsTimerTest, testTimer013, TestSize.Level0) +{ + g_data1 = 0; + Utils::Timer timer("test_timer", -1); + uint32_t ret = timer.Setup(); + EXPECT_EQ(Utils::TIMER_ERR_OK, ret); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + timer.Shutdown(); + + Utils::Timer timer1("test_timer_1"); + ret = timer1.Setup(); + EXPECT_EQ(Utils::TIMER_ERR_OK, ret); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + timer1.Shutdown(false); +} } // namespace } // namespace OHOS \ No newline at end of file