Squid Web Cache master
Loading...
Searching...
No Matches
Asynchronous Jobs
Collaboration diagram for Asynchronous Jobs:

Terminology

Typical life cycle

  1. Creator creates and initializes a job.
  2. If Initiator expects to communicate with the job after start, then it stores the job pointer
  3. Initiator starts the job by calling AsyncJob::Start.
  4. The job's start() method is called. The method usually schedules some I/O or registers to receive some other callbacks.
  5. The job runs and does what it is supposed to do. This usually involves scheduling I/O, setting timeouts, receiving Comm or Store callbacks, and then notifying Initiator of the final result.
  6. The job reaches its goal or encounters an error condition.
  7. The swanSong() method is called.
  8. The job object is destroyed.

If you want to do something before starting the job, do it in the constructor or some custom method that the job creator will call before calling AsyncJob::Start():

std::unique_ptr<MyJob> job(new MyJob(...)); // sync/blocking
job->prepare(...); // sync/blocking
job->prepareSomethingElse(...); // sync/blocking
AsyncStart(job.release()); // non-blocking

If you do not need complex preparations, it is better to do this instead:

AsyncJob::Start(new MyJob(...));

Keep in mind that you have no async debugging, cleanup, and protections until you call AsyncJob::Start with a job pointer.

Basic rules

swanSong() will be called automatically in all of these cases when the job is being terminated. It is a general cleanup method, like a destructor. The only difference is that a destructor must not throw.

Similarly, if your doneAll() implementation looks like "return isDone;", you are doing it wrong. Compute the condition directly rather than expecting various job methods to maintain some isDone variable correctly.