14177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com/*
24177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com * Copyright 2012 Google Inc.
34177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com *
44177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com * Use of this source code is governed by a BSD-style license that can be
54177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com * found in the LICENSE file.
64177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com */
74177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
84177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com#ifndef SkCondVar_DEFINED
94177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com#define SkCondVar_DEFINED
104177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
11a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding/**
12a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding * Import any thread model setting from configuration files.
13a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding */
14a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding#include "SkTypes.h"
15a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding
164d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
174177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com#include <pthread.h>
184d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
190d9e3da8bb853c5ed96d13646a4264eb57b13a2abungeman@google.com#include <windows.h>
20a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding#else
21a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding/**
22a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding * Warn if the implementation of this class is empty, i.e. thread safety is not working.
23a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding */
24a9309f5e5b188bd2d323e115e6b343772ba231aahenrik.smiding#warning "Thread safety class SkCondVar has no implementation!"
254d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
264177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
274d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com/**
284d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com * Condition variable for blocking access to shared data from other threads and
294d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com * controlling which threads are awake.
304d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com *
314d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com * Currently only supported on platforms with posix threads and Windows Vista and
324d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com * above.
334d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com */
344177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comclass SkCondVar {
354177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.compublic:
364177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    SkCondVar();
374177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    ~SkCondVar();
384177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
394d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    /**
404d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com     * Lock a mutex. Must be done before calling the other functions on this object.
414d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com     */
424177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    void lock();
434d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com
444d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    /**
454d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com     * Unlock the mutex.
464d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com     */
474177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    void unlock();
484177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
494177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    /**
504d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com     * Pause the calling thread. Will be awoken when signal() or broadcast() is called.
514d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com     * Must be called while lock() is held (but gives it up while waiting). Once awoken,
524d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com     * the calling thread will hold the lock once again.
534177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com     */
544177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    void wait();
554177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
564177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    /**
574177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com     * Wake one thread waiting on this condition. Must be called while lock()
584177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com     * is held.
594177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com     */
604177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    void signal();
614177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
624177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    /**
634177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com     * Wake all threads waiting on this condition. Must be called while lock()
644177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com     * is held.
654177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com     */
664177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    void broadcast();
674177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
684177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.comprivate:
694d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#ifdef SK_USE_POSIX_THREADS
704177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_mutex_t  fMutex;
714177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com    pthread_cond_t   fCond;
724d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#elif defined(SK_BUILD_FOR_WIN32)
734d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    CRITICAL_SECTION   fCriticalSection;
744d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com    CONDITION_VARIABLE fCondition;
754d3c28158a65f9eac6b472dff29caa77b44f8134scroggo@google.com#endif
764177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com};
774177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com
784177ef4b229b5fb67f355569654981bb4bf8eb9cscroggo@google.com#endif
79