2013-04-07 14:03:43 +02:00
|
|
|
#ifndef SQL_MY_APC_INCLUDED
|
|
|
|
#define SQL_MY_APC_INCLUDED
|
2011-08-23 17:28:32 +02:00
|
|
|
/*
|
2013-04-07 14:03:43 +02:00
|
|
|
Copyright (c) 2011, 2013 Monty Program Ab.
|
2012-06-28 11:58:37 +02:00
|
|
|
|
|
|
|
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
|
2017-02-10 12:26:55 +01:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
|
2011-08-23 17:28:32 +02:00
|
|
|
|
|
|
|
/*
|
2012-04-26 03:10:36 +02:00
|
|
|
Interface
|
|
|
|
~~~~~~~~~
|
|
|
|
(
|
|
|
|
- This is an APC request queue
|
|
|
|
- We assume there is a particular owner thread which periodically calls
|
|
|
|
process_apc_requests() to serve the call requests.
|
|
|
|
- Other threads can post call requests, and block until they are exectued.
|
|
|
|
)
|
|
|
|
|
|
|
|
Implementation
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
- The target has a mutex-guarded request queue.
|
2011-08-23 17:28:32 +02:00
|
|
|
|
|
|
|
- After the request has been put into queue, the requestor waits for request
|
|
|
|
to be satisfied. The worker satisifes the request and signals the
|
|
|
|
requestor.
|
|
|
|
*/
|
|
|
|
|
2012-07-05 20:04:13 +02:00
|
|
|
class THD;
|
|
|
|
|
2011-08-23 17:28:32 +02:00
|
|
|
/*
|
2012-06-30 04:05:06 +02:00
|
|
|
Target for asynchronous procedure calls (APCs).
|
|
|
|
- A target is running in some particular thread,
|
|
|
|
- One can make calls to it from other threads.
|
2011-08-23 17:28:32 +02:00
|
|
|
*/
|
|
|
|
class Apc_target
|
|
|
|
{
|
2012-06-07 10:19:06 +02:00
|
|
|
mysql_mutex_t *LOCK_thd_data_ptr;
|
2011-08-23 17:28:32 +02:00
|
|
|
public:
|
2012-06-25 16:39:26 +02:00
|
|
|
Apc_target() : enabled(0), apc_calls(NULL) {}
|
2011-08-23 17:28:32 +02:00
|
|
|
~Apc_target() { DBUG_ASSERT(!enabled && !apc_calls);}
|
|
|
|
|
2012-06-07 10:19:06 +02:00
|
|
|
void init(mysql_mutex_t *target_mutex);
|
2015-05-20 10:04:32 +02:00
|
|
|
|
|
|
|
/* Destroy the target. The target must be disabled when this call is made. */
|
|
|
|
void destroy() { DBUG_ASSERT(!enabled); }
|
|
|
|
|
|
|
|
/* Enter ther state where the target is available for serving APC requests */
|
|
|
|
void enable() { enabled++; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
Make the target unavailable for serving APC requests.
|
|
|
|
|
|
|
|
@note
|
|
|
|
This call will serve all requests that were already enqueued
|
|
|
|
*/
|
|
|
|
void disable()
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(enabled);
|
|
|
|
mysql_mutex_lock(LOCK_thd_data_ptr);
|
|
|
|
bool process= !--enabled && have_apc_requests();
|
|
|
|
mysql_mutex_unlock(LOCK_thd_data_ptr);
|
|
|
|
if (unlikely(process))
|
|
|
|
process_apc_requests();
|
|
|
|
}
|
|
|
|
|
2011-08-23 17:28:32 +02:00
|
|
|
void process_apc_requests();
|
2012-07-11 11:39:56 +02:00
|
|
|
/*
|
|
|
|
A lightweight function, intended to be used in frequent checks like this:
|
|
|
|
|
|
|
|
if (apc_target.have_requests()) apc_target.process_apc_requests()
|
|
|
|
*/
|
|
|
|
inline bool have_apc_requests()
|
|
|
|
{
|
2014-02-19 11:05:15 +01:00
|
|
|
return MY_TEST(apc_calls);
|
2012-07-11 11:39:56 +02:00
|
|
|
}
|
2013-02-12 05:20:14 +01:00
|
|
|
|
|
|
|
inline bool is_enabled() { return enabled; }
|
2012-06-28 14:46:24 +02:00
|
|
|
|
|
|
|
/* Functor class for calls you can schedule */
|
2012-06-28 11:58:37 +02:00
|
|
|
class Apc_call
|
|
|
|
{
|
|
|
|
public:
|
2012-06-28 14:46:24 +02:00
|
|
|
/* This function will be called in the target thread */
|
2012-06-28 11:58:37 +02:00
|
|
|
virtual void call_in_target_thread()= 0;
|
|
|
|
virtual ~Apc_call() {}
|
|
|
|
};
|
2012-06-28 14:46:24 +02:00
|
|
|
|
|
|
|
/* Make a call in the target thread (see function definition for details) */
|
2012-07-05 20:04:13 +02:00
|
|
|
bool make_apc_call(THD *caller_thd, Apc_call *call, int timeout_sec, bool *timed_out);
|
2011-08-23 17:28:32 +02:00
|
|
|
|
2011-09-24 19:56:42 +02:00
|
|
|
#ifndef DBUG_OFF
|
2012-06-08 00:19:36 +02:00
|
|
|
int n_calls_processed; /* Number of calls served by this target */
|
2011-09-24 19:56:42 +02:00
|
|
|
#endif
|
2011-08-23 17:28:32 +02:00
|
|
|
private:
|
|
|
|
class Call_request;
|
2012-04-26 03:10:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Non-zero value means we're enabled. It's an int, not bool, because one can
|
|
|
|
call enable() N times (and then needs to call disable() N times before the
|
|
|
|
target is really disabled)
|
|
|
|
*/
|
2011-08-23 17:28:32 +02:00
|
|
|
int enabled;
|
|
|
|
|
2012-04-26 03:10:36 +02:00
|
|
|
/*
|
|
|
|
Circular, double-linked list of all enqueued call requests.
|
|
|
|
We use this structure, because we
|
2012-06-30 06:29:29 +02:00
|
|
|
- process requests sequentially: requests are added at the end of the
|
|
|
|
list and removed from the front. With circular list, we can keep one
|
2012-07-17 19:52:08 +02:00
|
|
|
pointer, and access both front an back of the list with it.
|
2012-04-26 03:10:36 +02:00
|
|
|
- a thread that has posted a request may time out (or be KILLed) and
|
2012-06-07 10:19:06 +02:00
|
|
|
cancel the request, which means we need a fast request-removal
|
|
|
|
operation.
|
2012-04-26 03:10:36 +02:00
|
|
|
*/
|
2011-08-23 17:28:32 +02:00
|
|
|
Call_request *apc_calls;
|
2012-06-07 10:19:06 +02:00
|
|
|
|
2011-08-23 17:28:32 +02:00
|
|
|
class Call_request
|
|
|
|
{
|
|
|
|
public:
|
2012-06-28 11:58:37 +02:00
|
|
|
Apc_call *call; /* Functor to be called */
|
2011-08-23 17:28:32 +02:00
|
|
|
|
2012-06-19 16:10:32 +02:00
|
|
|
/* The caller will actually wait for "processed==TRUE" */
|
|
|
|
bool processed;
|
2012-06-07 10:19:06 +02:00
|
|
|
|
|
|
|
/* Condition that will be signalled when the request has been served */
|
|
|
|
mysql_cond_t COND_request;
|
2012-06-19 16:10:32 +02:00
|
|
|
|
|
|
|
/* Double linked-list linkage */
|
2011-08-23 17:28:32 +02:00
|
|
|
Call_request *next;
|
|
|
|
Call_request *prev;
|
|
|
|
|
2012-06-07 10:19:06 +02:00
|
|
|
const char *what; /* (debug) state of the request */
|
2011-08-23 17:28:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void enqueue_request(Call_request *qe);
|
|
|
|
void dequeue_request(Call_request *qe);
|
2012-04-26 03:10:36 +02:00
|
|
|
|
|
|
|
/* return the first call request in queue, or NULL if there are none enqueued */
|
2011-08-23 17:28:32 +02:00
|
|
|
Call_request *get_first_in_queue()
|
2012-04-26 03:10:36 +02:00
|
|
|
{
|
2011-08-23 17:28:32 +02:00
|
|
|
return apc_calls;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-07 06:47:41 +02:00
|
|
|
#ifdef HAVE_PSI_INTERFACE
|
|
|
|
void init_show_explain_psi_keys(void);
|
2013-11-14 15:00:00 +01:00
|
|
|
#else
|
|
|
|
#define init_show_explain_psi_keys() /* no-op */
|
2012-07-07 06:47:41 +02:00
|
|
|
#endif
|
|
|
|
|
2013-04-07 14:03:43 +02:00
|
|
|
#endif //SQL_MY_APC_INCLUDED
|
2012-07-17 19:52:08 +02:00
|
|
|
|