/* 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/ */ #ifndef PCMONITOR #define PCMONITOR #include /*********************************************** * Joseph Gleason * A set of functions to implement a thread-safe * Product-Consumer monitor, which uses a FIFO * queue. Allows any number of producers to pass * data to any number of consumers. * * Things put in * via produce are garanteed to go out in the same order * (FIFO). However, there is no garantee on which * consumer will get the entries. ***********************************************/ /* Internal PC monitor queue entry */ struct pc_monitor_entry { struct pc_monitor_entry *Next; void* Arg; }; typedef struct pc_monitor_entry pc_monitor_entry_t; /* pc_monitor data struct */ struct pc_monitor { pc_monitor_entry_t *HEAD; pc_monitor_entry_t *TAIL; pthread_mutex_t *MUT; pthread_cond_t *COND; int size; }; typedef struct pc_monitor pc_monitor_t; /* Initalizes given pc_monitor */ void pc_monitor_init(pc_monitor_t *P); /* Deallocates all entries, mutexes and conditions associated with pc_monitor. Does not deallocate the pc_monitor_t struct. */ void pc_monitor_destroy(pc_monitor_t *P); /* Block until there is an entry to consume. Entry is returned. */ void* pc_monitor_consume(pc_monitor_t *P); /* Non-blocking consume. Well, it blocks a little bit. It blocks on waiting for pc_monitor mutex (usually a short wait) but does not block waiting for an entry. Once it has the mutex, it checks for entry and returns it if there is one else returns null. Note: if you are in the habit of storing NULLs, there is no way to determine if the NULL returned by this was a NULL from the queue or a NULL meaning no entry was present.*/ void* pc_monitor_consume_nb(pc_monitor_t *P); /* Adds an entry to the queue. Blocks only waiting for mutex. */ void pc_monitor_produce(pc_monitor_t *P, void* Arg); /* Returns the number of entries in Q. Note: this should never be taken seriously unless you are certain that other thrads are not using the pc_monitor. Otherwise, after this returns the current size, the size could change based on activities of other threads */ int pc_monitor_getsize(pc_monitor_t *P); #endif