Home | History | Annotate | Download | only in media
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 //
      5 // This file contains helper classes for video accelerator unittests.
      6 
      7 #ifndef CONTENT_COMMON_GPU_MEDIA_VIDEO_ACCELERATOR_UNITTEST_HELPERS_H_
      8 #define CONTENT_COMMON_GPU_MEDIA_VIDEO_ACCELERATOR_UNITTEST_HELPERS_H_
      9 
     10 #include <queue>
     11 
     12 #include "base/synchronization/condition_variable.h"
     13 #include "base/synchronization/lock.h"
     14 
     15 namespace content {
     16 
     17 // Helper class allowing one thread to wait on a notification from another.
     18 // If notifications come in faster than they are Wait()'d for, they are
     19 // accumulated (so exactly as many Wait() calls will unblock as Notify() calls
     20 // were made, regardless of order).
     21 template <typename StateEnum>
     22 class ClientStateNotification {
     23  public:
     24   ClientStateNotification();
     25   ~ClientStateNotification();
     26 
     27   // Used to notify a single waiter of a ClientState.
     28   void Notify(StateEnum state);
     29   // Used by waiters to wait for the next ClientState Notification.
     30   StateEnum Wait();
     31 
     32  private:
     33   base::Lock lock_;
     34   base::ConditionVariable cv_;
     35   std::queue<StateEnum> pending_states_for_notification_;
     36 };
     37 
     38 template <typename StateEnum>
     39 ClientStateNotification<StateEnum>::ClientStateNotification() : cv_(&lock_) {}
     40 
     41 template <typename StateEnum>
     42 ClientStateNotification<StateEnum>::~ClientStateNotification() {}
     43 
     44 template <typename StateEnum>
     45 void ClientStateNotification<StateEnum>::Notify(StateEnum state) {
     46   base::AutoLock auto_lock(lock_);
     47   pending_states_for_notification_.push(state);
     48   cv_.Signal();
     49 }
     50 
     51 template <typename StateEnum>
     52 StateEnum ClientStateNotification<StateEnum>::Wait() {
     53   base::AutoLock auto_lock(lock_);
     54   while (pending_states_for_notification_.empty())
     55     cv_.Wait();
     56   StateEnum ret = pending_states_for_notification_.front();
     57   pending_states_for_notification_.pop();
     58   return ret;
     59 }
     60 
     61 }  // namespace content
     62 
     63 #endif  // CONTENT_COMMON_GPU_MEDIA_VIDEO_ACCELERATOR_UNITTEST_HELPERS_H_
     64