2013-04-16 23:59:00 -04:00
# ifndef _TOKU_WORKSET_H
# define _TOKU_WORKSET_H
2013-04-16 23:59:11 -04:00
# ident "$Id$"
# ident "Copyright (c) 2007-2010 Tokutek Inc. All rights reserved."
# ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11 / 760379 and to the patents and / or patent applications resulting from it."
2013-04-16 23:59:00 -04:00
# include <toku_list.h>
# include <toku_pthread.h>
2013-04-16 23:59:09 -04:00
# if defined(__cplusplus) || defined(__cilkplusplus)
extern " C " {
# endif
2013-04-16 23:59:00 -04:00
// the work struct is the base class for work to be done by some threads
struct work {
struct toku_list next ;
} ;
// the workset struct contains the set of work to be done by some threads
// the lock protects the work list
struct workset {
2013-04-16 23:59:01 -04:00
toku_pthread_mutex_t lock ;
2013-04-16 23:59:00 -04:00
struct toku_list worklist ;
} ;
static inline void workset_init ( struct workset * ws ) {
int r = toku_pthread_mutex_init ( & ws - > lock , NULL ) ; assert ( r = = 0 ) ;
toku_list_init ( & ws - > worklist ) ;
2013-04-16 23:59:01 -04:00
}
2013-04-16 23:59:00 -04:00
static inline void workset_destroy ( struct workset * ws ) {
assert ( toku_list_empty ( & ws - > worklist ) ) ;
int r = toku_pthread_mutex_destroy ( & ws - > lock ) ; assert ( r = = 0 ) ;
}
static inline void workset_lock ( struct workset * ws ) {
int r = toku_pthread_mutex_lock ( & ws - > lock ) ; assert ( r = = 0 ) ;
}
static inline void workset_unlock ( struct workset * ws ) {
int r = toku_pthread_mutex_unlock ( & ws - > lock ) ; assert ( r = = 0 ) ;
}
// put work in the workset
static inline void workset_put ( struct workset * ws , struct work * w ) {
workset_lock ( ws ) ;
toku_list_push ( & ws - > worklist , & w - > next ) ;
workset_unlock ( ws ) ;
}
// put work in the workset. assume already locked.
static inline void workset_put_locked ( struct workset * ws , struct work * w ) {
toku_list_push ( & ws - > worklist , & w - > next ) ;
}
// get work from the workset
static inline struct work * workset_get ( struct workset * ws ) {
workset_lock ( ws ) ;
struct work * w = NULL ;
if ( ! toku_list_empty ( & ws - > worklist ) ) {
struct toku_list * l = toku_list_pop_head ( & ws - > worklist ) ;
w = toku_list_struct ( l , struct work , next ) ;
}
workset_unlock ( ws ) ;
return w ;
}
// create a set of threads to run a given function
// tids will contain the thread id's of the created threads
// *ntids on input contains the number of threads requested, on output contains the number of threads created
static inline void threadset_create ( toku_pthread_t tids [ ] , int * ntids , void * ( * f ) ( void * arg ) , void * arg ) {
int n = * ntids ;
int i ;
for ( i = 0 ; i < n ; i + + ) {
int r = toku_pthread_create ( & tids [ i ] , NULL , f , arg ) ;
if ( r ! = 0 )
break ;
}
* ntids = i ;
}
// join with a set of threads
static inline void threadset_join ( toku_pthread_t tids [ ] , int ntids ) {
for ( int i = 0 ; i < ntids ; i + + ) {
void * ret ;
int r = toku_pthread_join ( tids [ i ] , & ret ) ; assert ( r = = 0 ) ;
}
}
2013-04-16 23:59:09 -04:00
# if defined(__cplusplus) || defined(__cilkplusplus)
} ;
# endif
2013-04-16 23:59:00 -04:00
# endif