summaryrefslogtreecommitdiff
path: root/mysys/wqueue.c
blob: 15785048040dcdec09cfb8b25ec0bd6a3b2b43a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
   Copyright (c) 2007, 2008, Sun Microsystems, Inc,
   Copyright (c) 2011, 2012, Monty Program Ab

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */

#include <wqueue.h>

#define STRUCT_PTR(TYPE, MEMBER, a)                                           \
          (TYPE *) ((char *) (a) - offsetof(TYPE, MEMBER))
/*
  Link a thread into double-linked queue of waiting threads.

  SYNOPSIS
    wqueue_link_into_queue()
      wqueue              pointer to the queue structure
      thread              pointer to the thread to be added to the queue

  RETURN VALUE
    none

  NOTES.
    Queue is represented by a circular list of the thread structures
    The list is double-linked of the type (**prev,*next), accessed by
    a pointer to the last element.
*/

void wqueue_link_into_queue(WQUEUE *wqueue, struct st_my_thread_var *thread)
{
  struct st_my_thread_var *last;
  if (!(last= wqueue->last_thread))
  {
    /* Queue is empty */
    thread->next= thread;
    thread->prev= &thread->next;
  }
  else
  {
    thread->prev= last->next->prev;
    last->next->prev= &thread->next;
    thread->next= last->next;
    last->next= thread;
  }
  wqueue->last_thread= thread;
}


/*
  Add a thread to single-linked queue of waiting threads

  SYNOPSIS
    wqueue_add_to_queue()
      wqueue              pointer to the queue structure
      thread              pointer to the thread to be added to the queue

  RETURN VALUE
    none

  NOTES.
    Queue is represented by a circular list of the thread structures
    The list is single-linked of the type (*next), accessed by a pointer
    to the last element.
*/

void wqueue_add_to_queue(WQUEUE *wqueue, struct st_my_thread_var *thread)
{
  struct st_my_thread_var *last;
  if (!(last= wqueue->last_thread))
    thread->next= thread;
  else
  {
    thread->next= last->next;
    last->next= thread;
  }
#ifndef DBUG_OFF
  thread->prev= NULL; /* force segfault if used */
#endif
  wqueue->last_thread= thread;
}

/*
  Unlink a thread from double-linked queue of waiting threads

  SYNOPSIS
    wqueue_unlink_from_queue()
      wqueue              pointer to the queue structure
      thread              pointer to the thread to be removed from the queue

  RETURN VALUE
    none

  NOTES.
    See NOTES for link_into_queue
*/

void wqueue_unlink_from_queue(WQUEUE *wqueue, struct st_my_thread_var *thread)
{
  if (thread->next == thread)
    /* The queue contains only one member */
    wqueue->last_thread= NULL;
  else
  {
    thread->next->prev= thread->prev;
    *thread->prev= thread->next;
    if (wqueue->last_thread == thread)
      wqueue->last_thread= STRUCT_PTR(struct st_my_thread_var, next,
                                      thread->prev);
  }
  thread->next= NULL;
}


/*
  Remove all threads from queue signaling them to proceed

  SYNOPSIS
    wqueue_realease_queue()
      wqueue              pointer to the queue structure
      thread              pointer to the thread to be added to the queue

  RETURN VALUE
    none

  NOTES.
    See notes for add_to_queue
    When removed from the queue each thread is signaled via condition
    variable thread->suspend.
*/

void wqueue_release_queue(WQUEUE *wqueue)
{
  struct st_my_thread_var *last= wqueue->last_thread;
  struct st_my_thread_var *next= last->next;
  struct st_my_thread_var *thread;
  do
  {
    thread= next;
    mysql_cond_signal(&thread->suspend);
    next= thread->next;
    thread->next= NULL;
  }
  while (thread != last);
  wqueue->last_thread= NULL;
}


/**
  @brief Removes all threads waiting for read or first one waiting for write.

  @param wqueue          pointer to the queue structure
  @param thread          pointer to the thread to be added to the queue

  @note This function is applicable only to single linked lists.
*/

void wqueue_release_one_locktype_from_queue(WQUEUE *wqueue)
{
  struct st_my_thread_var *last= wqueue->last_thread;
  struct st_my_thread_var *next= last->next;
  struct st_my_thread_var *thread;
  struct st_my_thread_var *new_list= NULL;
  uint first_type= next->lock_type;
  if (first_type == MY_PTHREAD_LOCK_WRITE)
  {
    /* release first waiting for write lock */
    mysql_cond_signal(&next->suspend);
    if (next == last)
      wqueue->last_thread= NULL;
    else
      last->next= next->next;
    next->next= NULL;
    return;
  }
  do
  {
    thread= next;
    next= thread->next;
    if (thread->lock_type == MY_PTHREAD_LOCK_WRITE)
    {
      /* skip waiting for write lock */
      if (new_list)
      {
        thread->next= new_list->next;
        new_list= new_list->next= thread;
      }
      else
        new_list= thread->next= thread;
    }
    else
    {
      /* release waiting for read lock */
      mysql_cond_signal(&thread->suspend);
      thread->next= NULL;
    }
  } while (thread != last);
  wqueue->last_thread= new_list;
}


/*
  Add thread and wait

  SYNOPSIS
    wqueue_add_and_wait()
    wqueue               queue to add to
    thread               thread which is waiting
    lock                 mutex need for the operation
*/

void wqueue_add_and_wait(WQUEUE *wqueue,
                         struct st_my_thread_var *thread,
                         mysql_mutex_t *lock)
{
  DBUG_ENTER("wqueue_add_and_wait");
  DBUG_PRINT("enter",
             ("thread: %p cond: %p mutex: %p",
              thread, &thread->suspend, lock));
  wqueue_add_to_queue(wqueue, thread);
  do
  {
    DBUG_PRINT("info", ("wait... cond:  %p  mutex:  %p",
                        &thread->suspend, lock));
    mysql_cond_wait(&thread->suspend, lock);
    DBUG_PRINT("info", ("wait done cond: %p mutex: %p next: %p",
                        &thread->suspend, lock,
                        thread->next));
  }
  while (thread->next);
  DBUG_VOID_RETURN;
}