~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/innobase/ut/ut0wqueue.c

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 2006, 2009, Innobase Oy. All Rights Reserved.
 
4
 
 
5
This program is free software; you can redistribute it and/or modify it under
 
6
the terms of the GNU General Public License as published by the Free Software
 
7
Foundation; version 2 of the License.
 
8
 
 
9
This program is distributed in the hope that it will be useful, but WITHOUT
 
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
12
 
 
13
You should have received a copy of the GNU General Public License along with
 
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 USA
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
#include "ut0wqueue.h"
 
20
 
 
21
/*******************************************************************//**
 
22
@file ut/ut0wqueue.c
 
23
A work queue
 
24
 
 
25
Created 4/26/2006 Osku Salerma
 
26
************************************************************************/
 
27
 
 
28
/****************************************************************//**
 
29
Create a new work queue.
 
30
@return work queue */
 
31
UNIV_INTERN
 
32
ib_wqueue_t*
 
33
ib_wqueue_create(void)
 
34
/*===================*/
 
35
{
 
36
        ib_wqueue_t*    wq = mem_alloc(sizeof(ib_wqueue_t));
 
37
 
 
38
        mutex_create(&wq->mutex, SYNC_WORK_QUEUE);
 
39
 
 
40
        wq->items = ib_list_create();
 
41
        wq->event = os_event_create(NULL);
 
42
 
 
43
        return(wq);
 
44
}
 
45
 
 
46
/****************************************************************//**
 
47
Free a work queue. */
 
48
UNIV_INTERN
 
49
void
 
50
ib_wqueue_free(
 
51
/*===========*/
 
52
        ib_wqueue_t*    wq)     /*!< in: work queue */
 
53
{
 
54
        ut_a(!ib_list_get_first(wq->items));
 
55
 
 
56
        mutex_free(&wq->mutex);
 
57
        ib_list_free(wq->items);
 
58
        os_event_free(wq->event);
 
59
 
 
60
        mem_free(wq);
 
61
}
 
62
 
 
63
/****************************************************************//**
 
64
Add a work item to the queue. */
 
65
UNIV_INTERN
 
66
void
 
67
ib_wqueue_add(
 
68
/*==========*/
 
69
        ib_wqueue_t*    wq,     /*!< in: work queue */
 
70
        void*           item,   /*!< in: work item */
 
71
        mem_heap_t*     heap)   /*!< in: memory heap to use for allocating the
 
72
                                list node */
 
73
{
 
74
        mutex_enter(&wq->mutex);
 
75
 
 
76
        ib_list_add_last(wq->items, item, heap);
 
77
        os_event_set(wq->event);
 
78
 
 
79
        mutex_exit(&wq->mutex);
 
80
}
 
81
 
 
82
/****************************************************************//**
 
83
Wait for a work item to appear in the queue.
 
84
@return work item */
 
85
UNIV_INTERN
 
86
void*
 
87
ib_wqueue_wait(
 
88
/*===========*/
 
89
        ib_wqueue_t*    wq)     /*!< in: work queue */
 
90
{
 
91
        ib_list_node_t* node;
 
92
 
 
93
        for (;;) {
 
94
                os_event_wait(wq->event);
 
95
 
 
96
                mutex_enter(&wq->mutex);
 
97
 
 
98
                node = ib_list_get_first(wq->items);
 
99
 
 
100
                if (node) {
 
101
                        ib_list_remove(wq->items, node);
 
102
 
 
103
                        if (!ib_list_get_first(wq->items)) {
 
104
                                /* We must reset the event when the list
 
105
                                gets emptied. */
 
106
                                os_event_reset(wq->event);
 
107
                        }
 
108
 
 
109
                        break;
 
110
                }
 
111
 
 
112
                mutex_exit(&wq->mutex);
 
113
        }
 
114
 
 
115
        mutex_exit(&wq->mutex);
 
116
 
 
117
        return(node->data);
 
118
}