# include "pswarp.h"

// each thread runs this function, starting a new job when it finished with an old one
// it is called with a (void *) pointer to its own thread pointer
void *pswarpThreadLauncher (void *data) {

    psThread *self = data;
    psThreadJob *job = NULL;

    while (1) {

	// if we get an error, just wait until we are cleared or killed
	while (self->fault) {
	    usleep (10000);
	}

	// request a new job, if there are none available, sleep a bit
	// we have to lock here so the job queue cannot be empty yet no threads busy
	psThreadLock();
	while ((job = psThreadJobGetPending ()) == NULL) {
	    psThreadUnlock();
	    usleep (10000);
	}
	self->busy = true;
	psThreadUnlock();

	// list all allowed job types here
	if (!strcmp (job->type, "PSWARP_TRANSFORM_TILE")) {
	    pswarpTransformTileArgs *args = job->args->data[0];
	    bool status = pswarpTransformTile (args);
	    if (!status) {
		self->fault = true;
	    }
	    // we do not have to lock here because this transition is not tied to the job queue
	    self->busy = false;  
	    continue;
	}
    }  
}
