/* _,.---._ .-._ .--.-. ,--.--------. * _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\ * /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./ * |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \ * |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \ * |==|,| | -|==| , '=' |==| - _ | |==|- | * |==| '=' /\==\ - ,_ /|==| /\ , | |==|, | * |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/ * `-.`.____.' `--`--'' `--`./ `--` `--`--` * _ __ ,---. .-._ .=-.-. _,.----. * .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \ * /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-' * |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | . * |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \ * |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , | * |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. / * /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-' * `--`---' `--` `--`./ `--``--`-` * * @(#)Copyright (c) 2023, Nathan D. Fisher. * * This is free software. It comes with NO WARRANTY. * Permission to use, modify and distribute this source code * is granted subject to the following conditions. * 1/ that the above copyright notice and this notice * are preserved in all copies and that due credit be given * to the author. * 2/ that any changes to this code are clearly commented * as such so that the author does not get blamed for bugs * other than his own. */ #include "jobq.h" int haggis_jobq_push(haggis_jobq *queue, char *fname) { haggis_jobq_node *new = malloc(sizeof(haggis_jobq_node)); if (new == NULL) return 1; new->fname = fname; new->next = new->prev = NULL; pthread_mutex_lock(queue->mutex); if (queue->tail == NULL) { queue->tail = queue->head = new; } else { new->next = queue->tail; queue->tail->prev = new; queue->tail = new; } pthread_mutex_unlock(queue->mutex); return sem_post(queue->sem); } char* haggis_jobq_pop(haggis_jobq *queue) { haggis_jobq_node *j; char *fname; sem_wait(queue->sem); pthread_mutex_lock(queue->mutex); j = queue->head; fname = j->fname; if (queue->tail == queue->head) { queue->tail = queue->head = NULL; } else { queue->head = queue->head->prev; } free(j); pthread_mutex_unlock(queue->mutex); if (sem_post(queue->sem) != 0) return NULL; return fname; }