Drizzled Public API Documentation

ut0wqueue.cc

00001 /*****************************************************************************
00002 
00003 Copyright (C) 2006, 2009, Innobase Oy. All Rights Reserved.
00004 
00005 This program is free software; you can redistribute it and/or modify it under
00006 the terms of the GNU General Public License as published by the Free Software
00007 Foundation; version 2 of the License.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT
00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00011 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00012 
00013 You should have received a copy of the GNU General Public License along with
00014 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
00015 St, Fifth Floor, Boston, MA 02110-1301 USA
00016 
00017 *****************************************************************************/
00018 
00019 #include "ut0wqueue.h"
00020 
00021 /*******************************************************************/
00028 /****************************************************************/
00031 UNIV_INTERN
00032 ib_wqueue_t*
00033 ib_wqueue_create(void)
00034 /*===================*/
00035 {
00036   ib_wqueue_t*  wq = static_cast<ib_wqueue_t *>(mem_alloc(sizeof(ib_wqueue_t)));
00037 
00038   /* Function ib_wqueue_create() has not been used anywhere,
00039   not necessary to instrument this mutex */
00040   mutex_create(PFS_NOT_INSTRUMENTED, &wq->mutex, SYNC_WORK_QUEUE);
00041 
00042   wq->items = ib_list_create();
00043   wq->event = os_event_create(NULL);
00044 
00045   return(wq);
00046 }
00047 
00048 /****************************************************************/
00050 UNIV_INTERN
00051 void
00052 ib_wqueue_free(
00053 /*===========*/
00054   ib_wqueue_t*  wq) 
00055 {
00056   ut_a(!ib_list_get_first(wq->items));
00057 
00058   mutex_free(&wq->mutex);
00059   ib_list_free(wq->items);
00060   os_event_free(wq->event);
00061 
00062   mem_free(wq);
00063 }
00064 
00065 /****************************************************************/
00067 UNIV_INTERN
00068 void
00069 ib_wqueue_add(
00070 /*==========*/
00071   ib_wqueue_t*  wq, 
00072   void*   item, 
00073   mem_heap_t* heap) 
00075 {
00076   mutex_enter(&wq->mutex);
00077 
00078   ib_list_add_last(wq->items, item, heap);
00079   os_event_set(wq->event);
00080 
00081   mutex_exit(&wq->mutex);
00082 }
00083 
00084 /****************************************************************/
00087 UNIV_INTERN
00088 void*
00089 ib_wqueue_wait(
00090 /*===========*/
00091   ib_wqueue_t*  wq) 
00092 {
00093   ib_list_node_t* node;
00094 
00095   for (;;) {
00096     os_event_wait(wq->event);
00097 
00098     mutex_enter(&wq->mutex);
00099 
00100     node = ib_list_get_first(wq->items);
00101 
00102     if (node) {
00103       ib_list_remove(wq->items, node);
00104 
00105       if (!ib_list_get_first(wq->items)) {
00106         /* We must reset the event when the list
00107         gets emptied. */
00108         os_event_reset(wq->event);
00109       }
00110 
00111       break;
00112     }
00113 
00114     mutex_exit(&wq->mutex);
00115   }
00116 
00117   mutex_exit(&wq->mutex);
00118 
00119   return(node->data);
00120 }