Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
59b06b14
Commit
59b06b14
authored
Apr 28, 2017
by
antirez
Browse files
Modules TSC: GIL and cooperative multi tasking setup.
parent
c180bc7d
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/module.c
View file @
59b06b14
...
...
@@ -105,7 +105,7 @@ struct RedisModuleCtx {
int
flags
;
/* REDISMODULE_CTX_... flags. */
void
**
postponed_arrays
;
/* To set with RM_ReplySetArrayLength(). */
int
postponed_arrays_count
;
/* Number of entries in postponed_arrays. */
void
*
blocked_privdata
;
/* Privdata set when unblocking a cli
n
et. */
void
*
blocked_privdata
;
/* Privdata set when unblocking a clie
n
t. */
/* Used if there is the REDISMODULE_CTX_KEYS_POS_REQUEST flag set. */
int
*
keys_pos
;
...
...
@@ -203,6 +203,10 @@ typedef struct RedisModuleBlockedClient {
static
pthread_mutex_t
moduleUnblockedClientsMutex
=
PTHREAD_MUTEX_INITIALIZER
;
static
list
*
moduleUnblockedClients
;
/* We need a mutex that is unlocked / relocked in beforeSleep() in order to
* allow thread safe contexts to execute commands at a safe moment. */
static
pthread_mutex_t
moduleGIL
=
PTHREAD_MUTEX_INITIALIZER
;
/* --------------------------------------------------------------------------
* Prototypes
* -------------------------------------------------------------------------- */
...
...
@@ -3278,6 +3282,24 @@ void *RM_GetBlockedClientPrivateData(RedisModuleCtx *ctx) {
return
ctx
->
blocked_privdata
;
}
/* --------------------------------------------------------------------------
* Thread Safe Contexts
* -------------------------------------------------------------------------- */
/* Operations executed in thread safe contexts use a global lock in order to
* be ran at a safe time. This function unlocks and re-acquire the locks:
* hopefully with *any* sane implementation of pthreads, this will allow the
* modules to make progresses.
*
* This function is called in beforeSleep(). */
void
moduleCooperativeMultiTaskingCycle
(
void
)
{
if
(
dictSize
(
modules
)
==
0
)
return
;
/* No modules, no async ops. */
pthread_mutex_unlock
(
&
moduleGIL
);
/* Here hopefully thread modules waiting to be executed at a safe time
* should be able to acquire the lock. */
pthread_mutex_lock
(
&
moduleGIL
);
}
/* --------------------------------------------------------------------------
* Modules API internals
* -------------------------------------------------------------------------- */
...
...
@@ -3329,6 +3351,10 @@ void moduleInitModulesSystem(void) {
* and we do not want to block not in the read nor in the write half. */
anetNonBlock
(
NULL
,
server
.
module_blocked_pipe
[
0
]);
anetNonBlock
(
NULL
,
server
.
module_blocked_pipe
[
1
]);
/* Our thread-safe contexts GIL must start with already locked:
* it is just unlocked when it's safe. */
pthread_mutex_lock
(
&
moduleGIL
);
}
/* Load all the modules in the server.loadmodule_queue list, which is
...
...
src/server.c
View file @
59b06b14
...
...
@@ -1172,6 +1172,9 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
void
beforeSleep
(
struct
aeEventLoop
*
eventLoop
)
{
UNUSED
(
eventLoop
);
/* Give some run time to modules threads using thread safe contexts. */
moduleCooperativeMultiTaskingCycle
();
/* Call the Redis Cluster before sleep function. Note that this function
* may change the state of Redis Cluster (from ok to fail or vice versa),
* so it's a good idea to call it before serving the unblocked clients
...
...
src/server.h
View file @
59b06b14
...
...
@@ -1294,6 +1294,7 @@ void unblockClientFromModule(client *c);
void
moduleHandleBlockedClients
(
void
);
void
moduleBlockedClientTimedOut
(
client
*
c
);
void
moduleBlockedClientPipeReadable
(
aeEventLoop
*
el
,
int
fd
,
void
*
privdata
,
int
mask
);
void
moduleCooperativeMultiTaskingCycle
(
void
);
/* Utils */
long
long
ustime
(
void
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment