1 // Copyright 2021 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "pw_chrono/system_clock.h" 17 #include "pw_sync/thread_notification.h" 18 19 namespace pw::sync { 20 21 // The TimedThreadNotification is a synchronization primitive that can be used 22 // to permit a SINGLE thread to block and consume a latching, saturating 23 // notification from multiple notifiers. 24 // 25 // IMPORTANT: This is a single consumer/waiter, multiple producer/notifier API! 26 // The acquire APIs must only be invoked by a single consuming thread. As a 27 // result, having multiple threads receiving notifications via the acquire API 28 // is unsupported. 29 // 30 // This is effectively a subset of a binary semaphore API, except that only a 31 // single thread can be notified and block at a time. 32 // 33 // The single consumer aspect of the API permits the use of a smaller and/or 34 // faster native APIs such as direct thread signaling. 35 class TimedThreadNotification : public ThreadNotification { 36 public: 37 TimedThreadNotification() = default; 38 ~TimedThreadNotification() = default; 39 TimedThreadNotification(const TimedThreadNotification&) = delete; 40 TimedThreadNotification(TimedThreadNotification&&) = delete; 41 TimedThreadNotification& operator=(const TimedThreadNotification&) = delete; 42 TimedThreadNotification& operator=(TimedThreadNotification&&) = delete; 43 44 // Blocks until the specified timeout duration has elapsed or the thread 45 // has been notified (i.e. notification latch can be cleared because it was 46 // set), whichever comes first. 47 // 48 // Clears the notification latch. 49 // 50 // Returns true if the thread was notified, meaning the the internal latch was 51 // reset successfully. 52 // 53 // IMPORTANT: This should only be used by a single consumer thread. 54 bool try_acquire_for(chrono::SystemClock::duration timeout); 55 56 // Blocks until the specified deadline time has been reached the thread has 57 // been notified (i.e. notification latch can be cleared because it was set), 58 // whichever comes first. 59 // 60 // Clears the notification latch. 61 // 62 // Returns true if the thread was notified, meaning the the internal latch was 63 // reset successfully. 64 // 65 // IMPORTANT: This should only be used by a single consumer thread. 66 bool try_acquire_until(chrono::SystemClock::time_point deadline); 67 }; 68 69 } // namespace pw::sync 70 71 #include "pw_sync_backend/timed_thread_notification_inline.h" 72