/* Copyright (c) 2004 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ #include #include "pcmonitor.h" //Most comments are in header file void pc_monitor_init(pc_monitor_t *P) { P->HEAD=NULL; P->TAIL=NULL; P->COND=(pthread_cond_t*) malloc(sizeof(pthread_cond_t)); pthread_cond_init(P->COND,NULL); P->MUT=(pthread_mutex_t*) malloc(sizeof(pthread_cond_t)); pthread_mutex_init(P->MUT,NULL); P->size=0; } void pc_monitor_destroy(pc_monitor_t *P) { pc_monitor_entry_t* Old; pc_monitor_entry_t* E; E=P->HEAD; while(E!=NULL) { Old=E; E=E->Next; free(Old); } pthread_cond_destroy(P->COND); pthread_mutex_destroy(P->MUT); free(P->COND); free(P->MUT); } // Should never be called from outside. // Assumes mutex already locked void pc_monitor_remove_first(pc_monitor_t *P) { pc_monitor_entry_t *Head; Head=P->HEAD; P->HEAD=P->HEAD->Next; free(Head); if (P->HEAD==NULL) { P->TAIL=NULL; } P->size--; } void* pc_monitor_consume_nb(pc_monitor_t *P) { void* Arg; pthread_mutex_lock(P->MUT); if (P->HEAD==NULL) { Arg=NULL; } else { Arg=P->HEAD->Arg; pc_monitor_remove_first(P); } pthread_mutex_unlock(P->MUT); return Arg; } void* pc_monitor_consume(pc_monitor_t *P) { void* Arg; pthread_mutex_lock(P->MUT); while (P->HEAD==NULL) { pthread_cond_wait(P->COND,P->MUT); } Arg=P->HEAD->Arg; pc_monitor_remove_first(P); pthread_mutex_unlock(P->MUT); return Arg; } void pc_monitor_produce(pc_monitor_t *P, void* Arg) { pc_monitor_entry_t *T; pthread_mutex_lock(P->MUT); if (P->TAIL==NULL) { P->HEAD=(pc_monitor_entry_t*)malloc(sizeof(pc_monitor_entry_t)); P->HEAD->Next=NULL; P->HEAD->Arg=Arg; P->TAIL=P->HEAD; } else { T=P->TAIL; T->Next=(pc_monitor_entry_t*)malloc(sizeof(pc_monitor_entry_t)); T->Next->Next=NULL; T->Next->Arg=Arg; P->TAIL=T->Next; } P->size++; pthread_cond_signal(P->COND); pthread_mutex_unlock(P->MUT); } int pc_montor_getsize(pc_monitor_t *P) { int sizecopy; pthread_mutex_lock(P->MUT); sizecopy=P->size; pthread_mutex_unlock(P->MUT); return sizecopy; }