summaryrefslogtreecommitdiff
path: root/sql/threadpool_win.cc
blob: e036bd614fd8bcd3e8e05aa1edebeabf7257d4ab (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/* Copyright (C) 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif

#define _WIN32_WINNT 0x0601

#include <my_global.h>
#include <violite.h>
#include <sql_priv.h>
#include <sql_class.h>
#include <my_pthread.h>
#include <scheduler.h>
#include <sql_connect.h>
#include <mysqld.h>
#include <debug_sync.h>
#include <threadpool.h>
#include <windows.h>


/*
  Threadpool API is not available on XP. We still want to compile a single 
  version on Windows, but use the latest functionality if available.
  We cannot use threadpool functionality directly, since executable won't 
  start on XP and loader will complain about missing symbols.

  We solve using the usual way it is done on Windows, i.e with dynamic loading.
  We'll need to load a lot of function, and make this less painful with the
  WEAK_SYMBOL macro below
*/

/*
 WEAK_SYMBOL(return_type, function_name, argument_type1,..,argument_typeN)

 Declare and load function pointer from kernel32. The name of the static 
 variable that holds the function pointer is my_<original function name>
 This should be combined with 
 #define <original function name> my_<original function name>
 so that one could use Widows APIs transparently, without worrying whether
 they are present in a particular version or not.

 Of course, prior to use of any function there should be a check for correct
 Windows version, or check whether function pointer is not NULL.
*/
#define WEAK_SYMBOL(return_type, function, ...) \
  typedef return_type (WINAPI *pFN_##function)(__VA_ARGS__); \
  static pFN_##function my_##function = (pFN_##function) \
    (GetProcAddress(GetModuleHandle("kernel32"),#function))

WEAK_SYMBOL(VOID, CancelThreadpoolIo, PTP_IO);
#define CancelThreadpoolIo my_CancelThreadpoolIo

WEAK_SYMBOL(VOID, CloseThreadpool, PTP_POOL);
#define CloseThreadpool my_CloseThreadpool

WEAK_SYMBOL(VOID, CloseThreadpoolIo, PTP_IO);
#define CloseThreadpoolIo my_CloseThreadpoolIo

WEAK_SYMBOL(VOID, CloseThreadpoolTimer,PTP_TIMER);
#define CloseThreadpoolTimer my_CloseThreadpoolTimer

WEAK_SYMBOL(VOID, CloseThreadpoolWait,PTP_WAIT);
#define CloseThreadpoolWait my_CloseThreadpoolWait

WEAK_SYMBOL(PTP_POOL, CreateThreadpool,PVOID);
#define CreateThreadpool my_CreateThreadpool

WEAK_SYMBOL(PTP_IO, CreateThreadpoolIo, HANDLE, PTP_WIN32_IO_CALLBACK, PVOID ,
  PTP_CALLBACK_ENVIRON);
#define CreateThreadpoolIo my_CreateThreadpoolIo

WEAK_SYMBOL(PTP_TIMER, CreateThreadpoolTimer, PTP_TIMER_CALLBACK ,
 PVOID pv, PTP_CALLBACK_ENVIRON pcbe);
#define CreateThreadpoolTimer my_CreateThreadpoolTimer

WEAK_SYMBOL(PTP_WAIT, CreateThreadpoolWait, PTP_WAIT_CALLBACK, PVOID, 
  PTP_CALLBACK_ENVIRON);
#define CreateThreadpoolWait my_CreateThreadpoolWait

WEAK_SYMBOL(VOID, DisassociateCurrentThreadFromCallback, PTP_CALLBACK_INSTANCE);
#define DisassociateCurrentThreadFromCallback my_DisassociateCurrentThreadFromCallback

WEAK_SYMBOL(DWORD, FlsAlloc, PFLS_CALLBACK_FUNCTION);
#define FlsAlloc my_FlsAlloc

WEAK_SYMBOL(PVOID, FlsGetValue, DWORD);
#define FlsGetValue my_FlsGetValue

WEAK_SYMBOL(BOOL, FlsSetValue, DWORD, PVOID);
#define FlsSetValue my_FlsSetValue

WEAK_SYMBOL(VOID, SetThreadpoolThreadMaximum, PTP_POOL, DWORD);
#define SetThreadpoolThreadMaximum my_SetThreadpoolThreadMaximum

WEAK_SYMBOL(BOOL, SetThreadpoolThreadMinimum, PTP_POOL, DWORD);
#define SetThreadpoolThreadMinimum my_SetThreadpoolThreadMinimum

WEAK_SYMBOL(VOID, SetThreadpoolTimer, PTP_TIMER, PFILETIME,DWORD,DWORD);
#define SetThreadpoolTimer my_SetThreadpoolTimer

WEAK_SYMBOL(VOID, SetThreadpoolWait, PTP_WAIT,HANDLE,PFILETIME);
#define SetThreadpoolWait my_SetThreadpoolWait

WEAK_SYMBOL(VOID, StartThreadpoolIo, PTP_IO);
#define StartThreadpoolIo my_StartThreadpoolIo

WEAK_SYMBOL(VOID, WaitForThreadpoolIoCallbacks,PTP_IO, BOOL);
#define WaitForThreadpoolIoCallbacks my_WaitForThreadpoolIoCallbacks

WEAK_SYMBOL(VOID, WaitForThreadpoolTimerCallbacks, PTP_TIMER, BOOL);
#define WaitForThreadpoolTimerCallbacks my_WaitForThreadpoolTimerCallbacks

WEAK_SYMBOL(VOID, WaitForThreadpoolWaitCallbacks, PTP_WAIT, BOOL);
#define WaitForThreadpoolWaitCallbacks my_WaitForThreadpoolWaitCallbacks

WEAK_SYMBOL(BOOL, SetFileCompletionNotificationModes, HANDLE, UCHAR);
#define SetFileCompletionNotificationModes my_SetFileCompletionNotificationModes

WEAK_SYMBOL(BOOL, TrySubmitThreadpoolCallback, PTP_SIMPLE_CALLBACK pfns, 
  PVOID pv,PTP_CALLBACK_ENVIRON pcbe);
#define TrySubmitThreadpoolCallback my_TrySubmitThreadpoolCallback

WEAK_SYMBOL(PTP_WORK, CreateThreadpoolWork, PTP_WORK_CALLBACK pfnwk, PVOID pv,
  PTP_CALLBACK_ENVIRON pcbe);
#define CreateThreadpoolWork my_CreateThreadpoolWork

WEAK_SYMBOL(VOID, SubmitThreadpoolWork,PTP_WORK pwk);
#define SubmitThreadpoolWork my_SubmitThreadpoolWork

WEAK_SYMBOL(VOID, CloseThreadpoolWork, PTP_WORK pwk);
#define CloseThreadpoolWork my_CloseThreadpoolWork 

WEAK_SYMBOL(BOOL, CallbackMayRunLong, PTP_CALLBACK_INSTANCE pci);
#define CallbackMayRunLong my_CallbackMayRunLong

#if _MSC_VER >= 1600
/* Stack size manipulation available only on Win7+ /declarations in VS10 */
WEAK_SYMBOL(BOOL, SetThreadpoolStackInformation, PTP_POOL, 
  PTP_POOL_STACK_INFORMATION);
#define SetThreadpoolStackInformation my_SetThreadpoolStackInformation
#else /* _MSC_VER < 1600 */
#define SetThreadpoolCallbackPriority(env,prio)
typedef enum _TP_CALLBACK_PRIORITY {
    TP_CALLBACK_PRIORITY_HIGH,
    TP_CALLBACK_PRIORITY_NORMAL,
    TP_CALLBACK_PRIORITY_LOW,
    TP_CALLBACK_PRIORITY_INVALID
} TP_CALLBACK_PRIORITY;
#endif


/* Log a warning */
static void tp_log_warning(const char *msg, const char *fct)
{
  sql_print_warning("Threadpool: %s. %s failed (last error %d)",msg, fct,
    GetLastError());
}


PTP_POOL pool;
DWORD fls;

static bool skip_completion_port_on_success = false;

/*
  Threadpool callbacks.

  io_completion_callback  - handle client request
  timer_callback - handle wait timeout (kill connection)
  shm_read_callback, shm_close_callback - shared memory stuff
  login_callback - user login (submitted as threadpool work)

*/

static void CALLBACK timer_callback(PTP_CALLBACK_INSTANCE instance, 
  PVOID context, PTP_TIMER timer);

static void CALLBACK io_completion_callback(PTP_CALLBACK_INSTANCE instance, 
  PVOID context,  PVOID overlapped,  ULONG io_result, ULONG_PTR nbytes, PTP_IO io);

static void CALLBACK shm_read_callback(PTP_CALLBACK_INSTANCE instance,
  PVOID Context, PTP_WAIT wait,TP_WAIT_RESULT wait_result);

static void CALLBACK shm_close_callback(PTP_CALLBACK_INSTANCE instance,
  PVOID Context, PTP_WAIT wait,TP_WAIT_RESULT wait_result);

static void check_thread_init();

/* Get current time as Windows time */
static ulonglong now()
{
  ulonglong current_time;
  GetSystemTimeAsFileTime((PFILETIME)&current_time);
  return current_time;
}

/* 
  Connection structure, encapsulates THD + structures for asynchronous
  IO and pool.
*/

struct connection_t
{
  THD *thd;
  HANDLE handle;
  OVERLAPPED overlapped;
  /* absolute time for wait timeout (as Windows time) */
  volatile ulonglong timeout; 
  TP_CALLBACK_ENVIRON callback_environ;
  PTP_IO  io;
  PTP_TIMER timer;
  PTP_WAIT shm_read;
  /* Callback instance, used to inform treadpool about long callbacks */
  PTP_CALLBACK_INSTANCE callback_instance;
  bool logged_in;
};


void init_connection(connection_t *connection, THD *thd)
{
  connection->logged_in = false;
  connection->handle= 0;
  connection->io= 0;
  connection->shm_read= 0;
  connection->timer= 0;
  connection->logged_in = false;
  connection->timeout= ULONGLONG_MAX;
  connection->callback_instance= 0;
  memset(&connection->overlapped, 0, sizeof(OVERLAPPED));
  InitializeThreadpoolEnvironment(&connection->callback_environ);
  SetThreadpoolCallbackPool(&connection->callback_environ, pool);
  connection->thd = thd;
  thd->event_scheduler.data= connection;
}


int init_io(connection_t *connection, THD *thd)
{
  connection->thd= thd;
  Vio *vio = thd->net.vio;
  switch(vio->type)
  {
    case VIO_TYPE_SSL:
    case VIO_TYPE_TCPIP:
      connection->handle= (HANDLE)mysql_socket_getfd(connection->thd->net.vio->mysql_socket);
      break;
    case VIO_TYPE_NAMEDPIPE:
      connection->handle= (HANDLE)vio->hPipe;
      break;
    case VIO_TYPE_SHARED_MEMORY:
      connection->shm_read=  CreateThreadpoolWait(shm_read_callback, connection, 
        &connection->callback_environ);
      if (!connection->shm_read)
      {
        tp_log_warning("Allocation failed", "CreateThreadpoolWait");
        return -1;
      }
      break;
    default:
      abort();
  }

  if (connection->handle)
  {
    /* Performance tweaks (s. MSDN documentation)*/
    UCHAR flags= FILE_SKIP_SET_EVENT_ON_HANDLE;
    if (skip_completion_port_on_success)
    {
      flags |= FILE_SKIP_COMPLETION_PORT_ON_SUCCESS;
    }
    (void)SetFileCompletionNotificationModes(connection->handle, flags);

    /* Assign io completion callback */
    connection->io= CreateThreadpoolIo(connection->handle, 
      io_completion_callback, connection, &connection->callback_environ);
    if(!connection->io)
    {
      tp_log_warning("Allocation failed", "CreateThreadpoolWait");
      return -1;
    }
  }
  connection->timer= CreateThreadpoolTimer(timer_callback, connection, 
    &connection->callback_environ);
  if (!connection->timer)
  {
    tp_log_warning("Allocation failed", "CreateThreadpoolWait");
    return -1;
  }

  return 0;
}


/*
  Start asynchronous read
*/
int start_io(connection_t *connection, PTP_CALLBACK_INSTANCE instance)
{
  /* Start async read */
  DWORD num_bytes = 0;
  static char c;
  WSABUF buf;
  buf.buf= &c;
  buf.len= 0;
  DWORD flags=0;
  DWORD last_error= 0;

  int retval;
  Vio *vio= connection->thd->net.vio;

  if (vio->type == VIO_TYPE_SHARED_MEMORY)
  {
      SetThreadpoolWait(connection->shm_read, vio->event_server_wrote, NULL);
      return 0;
  }
  if (vio->type == VIO_CLOSED)
  {
    return -1;
  }

  DBUG_ASSERT(vio->type == VIO_TYPE_TCPIP || 
    vio->type == VIO_TYPE_SSL ||
    vio->type == VIO_TYPE_NAMEDPIPE);

  OVERLAPPED *overlapped= &connection->overlapped;
  PTP_IO io= connection->io;
  StartThreadpoolIo(io);

  if (vio->type == VIO_TYPE_TCPIP || vio->type == VIO_TYPE_SSL)
  {
    /* Start async io (sockets). */
    if (WSARecv(mysql_socket_getfd(vio->mysql_socket) , &buf, 1, &num_bytes, &flags,
          overlapped,  NULL) == 0)
    {
        retval= last_error= 0;
    }
    else
    {
      retval= -1;
      last_error=  WSAGetLastError();
    }
  }
  else
  {
    /* Start async io (named pipe) */
    if (ReadFile(vio->hPipe, &c, 0, &num_bytes ,overlapped))
    {
      retval= last_error= 0;
    }
    else
    {
      retval= -1;
      last_error= GetLastError();
    }
  }

  if (retval == 0 || last_error == ERROR_MORE_DATA)
  {
    /*
      IO successfully finished (synchronously). 
      If skip_completion_port_on_success is set, we need to handle it right 
      here, because completion callback would not be executed by the pool.
    */
    if(skip_completion_port_on_success)
    {
      CancelThreadpoolIo(io);
      io_completion_callback(instance, connection, overlapped, last_error, 
        num_bytes, io);
    }
    return 0;
  }

  if(last_error == ERROR_IO_PENDING)
  {
    return 0;
  }

  /* Some error occured */
  CancelThreadpoolIo(io);
  return -1;
}


int login(connection_t *connection, PTP_CALLBACK_INSTANCE instance)
{
  if (threadpool_add_connection(connection->thd) == 0
      && init_io(connection, connection->thd) == 0 
      && start_io(connection, instance) == 0)
  {
    return 0;
  }
  return -1;
}

/*
  Recalculate wait timeout, maybe reset timer. 
*/
void set_wait_timeout(connection_t *connection, ulonglong old_timeout)
{
  ulonglong new_timeout = now() + 
    10000000LL*connection->thd->variables.net_wait_timeout;

  if (new_timeout < old_timeout)
  {
    SetThreadpoolTimer(connection->timer, (PFILETIME) &new_timeout, 0, 1000);
  }
  connection->timeout = new_timeout;
}


/* Connection destructor */
void destroy_connection(connection_t *connection, PTP_CALLBACK_INSTANCE instance)
{
  if (instance)
    DisassociateCurrentThreadFromCallback(instance);
  if (connection->io)
  {
     WaitForThreadpoolIoCallbacks(connection->io, TRUE); 
     CloseThreadpoolIo(connection->io);
  }

  if(connection->shm_read)
  {
    WaitForThreadpoolWaitCallbacks(connection->shm_read, TRUE);
    CloseThreadpoolWait(connection->shm_read);
  }

  if(connection->timer)
  {
    SetThreadpoolTimer(connection->timer, 0, 0, 0);
    WaitForThreadpoolTimerCallbacks(connection->timer, TRUE);
    CloseThreadpoolTimer(connection->timer);
  }
  
  if (connection->thd)
  {
    threadpool_remove_connection(connection->thd);
  }

  DestroyThreadpoolEnvironment(&connection->callback_environ);
}



/* 
  This function should be called first whenever a callback is invoked in the 
  threadpool, does my_thread_init() if not yet done
*/
extern ulong thread_created;
static void check_thread_init()
{
  if (FlsGetValue(fls) == NULL)
  {
    FlsSetValue(fls, (void *)1);
    statistic_increment(thread_created, &LOCK_status);
    InterlockedIncrement((volatile long *)&tp_stats.num_worker_threads);
  }
}


/*
  Decrement number of threads when a thread exits . 
  On Windows, FlsAlloc() provides the thread destruction callbacks.
*/
static VOID WINAPI thread_destructor(void *data)
{
  if(data)
  {
    InterlockedDecrement((volatile long *)&tp_stats.num_worker_threads);
  }
}


/* Scheduler callback : init */
bool tp_init(void)
{
  fls= FlsAlloc(thread_destructor);
  pool= CreateThreadpool(NULL);
  if(!pool)
  {
    sql_print_error("Can't create threadpool. "
      "CreateThreadpool() failed with %d. Likely cause is memory pressure", 
      GetLastError());
    exit(1);
  }

  if (threadpool_max_threads)
  {
    SetThreadpoolThreadMaximum(pool,threadpool_max_threads);
  }

  if (threadpool_min_threads)
  {
    if (!SetThreadpoolThreadMinimum(pool, threadpool_min_threads))
    {
      tp_log_warning( "Can't set threadpool minimum threads", 
        "SetThreadpoolThreadMinimum");
    }
  }

  /*
    Control stack size (OS must be Win7 or later, plus corresponding SDK)
  */
#if _MSC_VER >=1600
  if (SetThreadpoolStackInformation)
  {
    TP_POOL_STACK_INFORMATION stackinfo;
    stackinfo.StackCommit = 0;
    stackinfo.StackReserve = (SIZE_T)my_thread_stack_size;
    if (!SetThreadpoolStackInformation(pool, &stackinfo))
    {
      tp_log_warning("Can't set threadpool stack size", 
        "SetThreadpoolStackInformation");
    }
  }
#endif

  return 0;
}


/* Dummy functions, do nothing */

bool tp_init_new_connection_thread()
{
  return 0;
}

bool tp_end_thread(THD *thd, bool cache_thread)
{}

/**
  Scheduler callback : Destroy the scheduler.
*/
void tp_end(void)
{
  if(pool)
  {
    SetThreadpoolThreadMaximum(pool, 0);
    CloseThreadpool(pool);
  }
}

/*
  Handle read completion/notification.
*/
static VOID CALLBACK io_completion_callback(PTP_CALLBACK_INSTANCE instance, 
  PVOID context,  PVOID overlapped,  ULONG io_result, ULONG_PTR nbytes, PTP_IO io)
{
  if(instance)
  {
    check_thread_init();
  }

  connection_t *connection = (connection_t*)context;

  if (io_result != ERROR_SUCCESS)
    goto error;

  THD *thd= connection->thd;
  ulonglong old_timeout = connection->timeout;
  connection->timeout = ULONGLONG_MAX;
  connection->callback_instance= instance;
  if (threadpool_process_request(connection->thd))
    goto error;

  set_wait_timeout(connection, old_timeout);
  if(start_io(connection, instance))
    goto error;

  return;

error:
  /* Some error has occured. */

  destroy_connection(connection, instance);
  free(connection);
}


/* Simple callback for login */
static void CALLBACK login_callback(PTP_CALLBACK_INSTANCE instance, 
  PVOID context, PTP_WORK work)
{
  if(instance)
  {
    check_thread_init();
  }

  connection_t *connection =(connection_t *)context;
  if (login(connection, instance) != 0)
  {
    destroy_connection(connection, instance);
    free(connection);
  }
}

/*
  Timer callback.
  Invoked when connection times out (wait_timeout)
*/
static VOID CALLBACK timer_callback(PTP_CALLBACK_INSTANCE instance, 
  PVOID parameter, PTP_TIMER timer)
{
  check_thread_init();

  connection_t *con= (connection_t*)parameter;
  ulonglong timeout= con->timeout;

  if (timeout <= now())
  {
    con->thd->killed = KILL_CONNECTION;
    if(con->thd->net.vio)
      vio_shutdown(con->thd->net.vio, SD_BOTH);
  }
  else if(timeout != ULONGLONG_MAX)
  {
    /* 
      Reset timer. 
      There is a tiny possibility of a race condition, since the value of timeout 
      could have changed to smaller value in the thread doing io callback. 

      Given the relative unimportance of the wait timeout, we accept race 
      condition.
    */
    SetThreadpoolTimer(timer, (PFILETIME)&timeout, 0, 1000);
  }
}


/*
  Shared memory read callback.
  Invoked when read event is set on connection.
*/
static void CALLBACK shm_read_callback(PTP_CALLBACK_INSTANCE instance,
  PVOID context, PTP_WAIT wait,TP_WAIT_RESULT wait_result)
{
  connection_t *con= (connection_t *)context;
  /* Disarm wait. */
  SetThreadpoolWait(wait, NULL, NULL);

  /* 
    This is an autoreset event, and one wakeup is eaten already by threadpool,
    and the current state is "not set". Thus we need to reset the event again, 
    or vio_read will hang.
  */
  HANDLE h = con->thd->net.vio->event_server_wrote;
  SetEvent(h);
  io_completion_callback(instance, context, NULL, 0, 0 , 0);
}


/*
  Notify the thread pool about a new connection.
*/

void tp_add_connection(CONNECT *connect)
{
  THD *thd;
  connection_t *con;

  if (!(con = (connection_t *) malloc(sizeof(connection_t))) ||
      !(thd= connect->create_thd()))
  {
    tp_log_warning("Allocation failed", "tp_add_connection");
    free(con)
    connect->close_and_delete();
    return;
  }
  delete connect;

  add_to_active_threads(thd);

  init_connection(con, thd);

  /* Try to login asynchronously, using threads in the pool */
  PTP_WORK wrk =  CreateThreadpoolWork(login_callback,con, &con->callback_environ);
  if (wrk)
  {
    SubmitThreadpoolWork(wrk);
    CloseThreadpoolWork(wrk);
  }
  else
  {
    /* Likely memory pressure */
    threadpool_cleanup_connection(thd);
  }
}


/**
  Sets the number of idle threads the thread pool maintains in anticipation of new
  requests.
*/
void tp_set_min_threads(uint val)
{
  if (pool)
    SetThreadpoolThreadMinimum(pool, val);
}

void tp_set_max_threads(uint val)
{
  if (pool)
    SetThreadpoolThreadMaximum(pool, val);
}

void tp_wait_begin(THD *thd, int type)
{
  DBUG_ASSERT(thd);

  /*
    Signal to the threadpool whenever callback can run long. Currently, binlog
    waits are a good candidate, its waits are really long
  */
  if (type == THD_WAIT_BINLOG)
  {
    connection_t *connection= (connection_t *)thd->event_scheduler.data;
    if(connection && connection->callback_instance)
    {
      CallbackMayRunLong(connection->callback_instance);
      /* 
        Reset instance, to avoid calling CallbackMayRunLong  twice within 
        the same callback (it is an error according to docs).
      */
      connection->callback_instance= 0;
    }
  }
}

void tp_wait_end(THD *thd) 
{
  /* Do we need to do anything ? */
}


/**
 Number of idle threads in pool.
 This info is not available in Windows implementation,
 thus function always returns 0.
*/
int tp_get_idle_thread_count()
{
  return 0;
}