Addition of an arbitary bitmask handling set of functions

New memory deallocation routines for the DCBS
This commit is contained in:
Mark Riddoch
2013-06-28 17:39:43 +02:00
parent 3e8b19733e
commit 8257eadf12
12 changed files with 456 additions and 72 deletions

View File

@ -19,6 +19,7 @@
*/
#include <spinlock.h>
#include <buffer.h>
#include <gwbitmask.h>
struct session;
struct server;
@ -87,6 +88,29 @@ typedef struct dcbstats {
int n_buffered; /**< Number of buffered writes */
} DCBSTATS;
/**
* The data structure that is embedded witin a DCB and manages the complex memory
* management issues of a DCB.
*
* The DCB structures are used as the user data within the polling loop. This means that
* polling threads may aschronously wake up and access these structures. It is not possible
* to simple remove the DCB from the epoll system and then free the data, as every thread
* that is currently running an epoll call must wake up and re-issue the epoll_wait system
* call, the is the only way we can be sure that no polling thread is pending a wakeup or
* processing an event that will access the DCB.
*
* We solve this issue by making the dcb_free routine merely mark a DCB as a zombie and
* place it on a special zombie list. Before placing the DCB on the zombie list we create
* a bitmask with a bit set in it for each active polling thread. Each thread will call
* a routine to process the zombie list at the end of the polling loop. This routine
* will clear the bit value that corresponds to the calling thread. Once the bitmask
* is completely cleared the DCB can finally be freed and removed from the zombie list.
*/
typedef struct {
GWBITMASK bitmask; /**< The bitmask of threads */
struct dcb *next; /**< Next pointer for the zombie list */
} DCBMM;
/**
* Descriptor Control Block
*
@ -114,6 +138,7 @@ typedef struct dcb {
struct dcb *next; /**< Next DCB in the chain of allocated DCB's */
struct service *service; /**< The related service */
void *data; /**< Specific client data */
DCBMM memdata; /**< The data related to DCB memory management */
} DCB;
/* DCB states */
@ -124,10 +149,12 @@ typedef struct dcb {
#define DCB_STATE_LISTENING 5 /**< The DCB is for a listening socket */
#define DCB_STATE_DISCONNECTED 6 /**< The socket is now closed */
#define DCB_STATE_FREED 7 /**< Memory freed */
#define DCB_STATE_ZOMBIE 8 /**< DCB is no longer active, waiting to free it */
/* A few useful macros */
#define DCB_SESSION(x) (x)->session
#define DCB_PROTOCOL(x, type) (type *)((x)->protocol)
#define DCB_ISZOMBIE(x) ((x)->state == DCB_STATE_ZOMBIE)
extern DCB *dcb_alloc(); /* Allocate a DCB */
extern void dcb_free(DCB *); /* Free a DCB */
@ -136,6 +163,7 @@ extern int dcb_read(DCB *, GWBUF **); /* Generic read routine */
extern int dcb_write(DCB *, GWBUF *); /* Generic write routine */
extern int dcb_drain_writeq(DCB *); /* Generic write routine */
extern void dcb_close(DCB *); /* Generic close functionality */
extern void dcb_process_zombies(int); /* Process Zombies */
extern void printAllDCBs(); /* Debug to print all DCB in the system */
extern void printDCB(DCB *); /* Debug print routine */
extern void dprintAllDCBs(DCB *); /* Debug to print all DCB in the system */

52
include/gwbitmask.h Normal file
View File

@ -0,0 +1,52 @@
#ifndef _GWBITMASK_H
#define _GWBITMASK_H
/*
* This file is distributed as part of the SkySQL Gateway. It 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.
*
* 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-1301 USA.
*
* Copyright SkySQL Ab 2013
*/
#include <spinlock.h>
/**
* @file gwbitmask.h An implementation of an arbitarly long bitmask
*
* @verbatim
* Revision History
*
* Date Who Description
* 28/06/13 Mark Riddoch Initial implementation
*
* @endverbatim
*/
#define BIT_LENGTH_INITIAL 32 /**< Initial number of bits in the bitmask */
#define BIT_LENGTH_INC 32 /**< Number of bits to add on each increment */
/**
* The bitmask structure used to store an arbitary large bitmask
*/
typedef struct {
SPINLOCK lock; /**< Lock to protect the bitmask */
unsigned char *bits; /**< Pointer to the bits themselves */
unsigned int length; /**< The number of bits in the bitmask */
} GWBITMASK;
extern void bitmask_init(GWBITMASK *);
extern void bitmask_set(GWBITMASK *, int);
extern void bitmask_clear(GWBITMASK *, int);
extern int bitmask_isset(GWBITMASK *, int);
extern int bitmask_isallclear(GWBITMASK *);
extern void bitmask_copy(GWBITMASK *, GWBITMASK *);
#endif

View File

@ -18,6 +18,7 @@
* Copyright SkySQL Ab 2013
*/
#include <dcb.h>
#include <gwbitmask.h>
/**
* @file poll.h The poll related functionality
@ -33,10 +34,11 @@
#define MAX_EVENTS 1000
#define EPOLL_TIMEOUT 1000 /**< The epoll timeout we use (milliseconds) */
extern void poll_init();
extern int poll_add_dcb(DCB *);
extern int poll_remove_dcb(DCB *);
extern void poll_waitevents();
extern void poll_shutdown();
extern void dprintPollStats(DCB *);
extern void poll_init();
extern int poll_add_dcb(DCB *);
extern int poll_remove_dcb(DCB *);
extern void poll_waitevents(void *);
extern void poll_shutdown();
extern GWBITMASK *poll_bitmask();
extern void dprintPollStats(DCB *);
#endif

View File

@ -19,10 +19,19 @@
*/
#include <pthread.h>
/**
* @file thread.h The gateway threading interface
*
* An encapsulation of the threading used by the gateway. This is designed to
* isolate the majority of the gateway code from th epthread library, enabling
* the gateway to be ported to a different threading package with the minimum
* of changes.
*/
#define THREAD pthread_t
#define THREAD_SHELF pthread_self
extern void *thread_start(void (*entry)());
extern void *thread_start(void (*entry)(void *), void *arg);
extern void thread_wait(void *thd);
#endif