Class to help run an asynchronous fetch synchronously with a timeout. More...
#include "sync_fetcher_adapter_callback.h"
Public Member Functions | |
SyncFetcherAdapterCallback (ThreadSystem *thread_system, Writer *writer) | |
void | Release () |
bool | done () const |
bool | success () const |
bool | released () const |
bool | LockIfNotReleased () |
void | Unlock () |
Releases mutex acquired by a successful LockIfNotReleased() call. | |
Protected Member Functions | |
virtual void | HandleDone (bool success) |
virtual bool | HandleWrite (const StringPiece &content, MessageHandler *handler) |
virtual bool | HandleFlush (MessageHandler *handler) |
virtual void | HandleHeadersComplete () |
Class to help run an asynchronous fetch synchronously with a timeout.
bool net_instaweb::SyncFetcherAdapterCallback::LockIfNotReleased | ( | ) |
If this fetcher hasn't yet been released(), returns true with mutex_ held. Otherwise, returns false with the mutex_ released. These methods should be used to guard accesses to writer() and response_headers().
void net_instaweb::SyncFetcherAdapterCallback::Release | ( | ) |
When implementing a synchronous fetch with a timeout based on an underlying asynchronous mechanism, we need to ensure that we don't write to freed memory if the Done callback fires after the timeout.
So we need to make sure the Writer and Response Buffers are owned by this Callback class, which will forward the output and headers to the caller *if* it has not been released by the time the callback is called.
If this object may be accessed from multiple threads (e.g. due to async rewrites), you should use LockIfNotReleased() and Unlock() to guard access to these. When the 'owner' of this callback -- the code that calls 'new' -- is done with it, it can call release. This will only delete the callback if Done() has been called. Otherwise it will stay around waiting for Done() to be called, and only then will it be deleted.
When Release is called prior to Done(), the writer and response_headers will be NULLed out in this structure so they will not be updated when Done() is finally called.