aboutsummaryrefslogtreecommitdiff
path: root/src/keeper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/keeper.c')
-rw-r--r--src/keeper.c39
1 files changed, 19 insertions, 20 deletions
diff --git a/src/keeper.c b/src/keeper.c
index f19beed..f89c638 100644
--- a/src/keeper.c
+++ b/src/keeper.c
@@ -53,12 +53,6 @@
53/*---=== Keeper states ===--- 53/*---=== Keeper states ===---
54*/ 54*/
55 55
56/* The selected number is not optimal; needs to be tested. Even using just
57* one keeper state may be good enough (depends on the number of Lindas used
58* in the applications).
59*/
60#define KEEPER_STATES_N 1 // 6
61
62/* 56/*
63* Pool of keeper states 57* Pool of keeper states
64* 58*
@@ -66,12 +60,8 @@
66* bigger the pool, the less chances of unnecessary waits. Lindas map to the 60* bigger the pool, the less chances of unnecessary waits. Lindas map to the
67* keepers randomly, by a hash. 61* keepers randomly, by a hash.
68*/ 62*/
69static struct s_Keeper GKeepers[KEEPER_STATES_N]; 63static struct s_Keeper *GKeepers = NULL;
70 64static int GNbKeepers = 0;
71/* We could use an empty table in 'keeper.lua' as the sentinel, but maybe
72* checking for a lightuserdata is faster.
73*/
74static bool_t nil_sentinel = 0;
75 65
76/* 66/*
77* Lua code for the keeper states (baked in) 67* Lua code for the keeper states (baked in)
@@ -88,10 +78,13 @@ static char const keeper_chunk[]=
88* unclosed, because it does not really matter. In production code, this 78* unclosed, because it does not really matter. In production code, this
89* function never fails. 79* function never fails.
90*/ 80*/
91const char *init_keepers(void) 81char const *init_keepers( int const _nbKeepers)
92{ 82{
93 unsigned int i; 83 int i;
94 for( i=0; i<KEEPER_STATES_N; i++ ) 84 assert( _nbKeepers >= 1);
85 GNbKeepers = _nbKeepers;
86 GKeepers = malloc( _nbKeepers * sizeof( struct s_Keeper));
87 for( i = 0; i < _nbKeepers; ++ i)
95 { 88 {
96 89
97 // Initialize Keeper states with bare minimum of libs (those required 90 // Initialize Keeper states with bare minimum of libs (those required
@@ -104,8 +97,11 @@ const char *init_keepers(void)
104 luaG_openlibs( L, "io,table,package" ); // 'io' for debugging messages, package because we need to require modules exporting idfuncs 97 luaG_openlibs( L, "io,table,package" ); // 'io' for debugging messages, package because we need to require modules exporting idfuncs
105 serialize_require( L); 98 serialize_require( L);
106 99
107 lua_pushlightuserdata( L, &nil_sentinel ); 100 /* We could use an empty table in 'keeper.lua' as the sentinel, but maybe
108 lua_setglobal( L, "nil_sentinel" ); 101 * checking for a lightuserdata is faster. (any unique value will do -> take the address of some global of ours)
102 */
103 lua_pushlightuserdata( L, &GNbKeepers);
104 lua_setglobal( L, "nil_sentinel");
109 105
110 // Read in the preloaded chunk (and run it) 106 // Read in the preloaded chunk (and run it)
111 // 107 //
@@ -131,12 +127,12 @@ const char *init_keepers(void)
131struct s_Keeper *keeper_acquire( const void *ptr) 127struct s_Keeper *keeper_acquire( const void *ptr)
132{ 128{
133 /* 129 /*
134 * Any hashing will do that maps pointers to 0..KEEPER_STATES_N-1 130 * Any hashing will do that maps pointers to 0..GNbKeepers-1
135 * consistently. 131 * consistently.
136 * 132 *
137 * Pointers are often aligned by 8 or so - ignore the low order bits 133 * Pointers are often aligned by 8 or so - ignore the low order bits
138 */ 134 */
139 unsigned int i= ((unsigned long)(ptr) >> 3) % KEEPER_STATES_N; 135 unsigned int i= ((unsigned long)(ptr) >> 3) % GNbKeepers;
140 struct s_Keeper *K= &GKeepers[i]; 136 struct s_Keeper *K= &GKeepers[i];
141 137
142 MUTEX_LOCK( &K->lock_); 138 MUTEX_LOCK( &K->lock_);
@@ -190,10 +186,13 @@ int keeper_call( lua_State *K, char const *func_name, lua_State *L, void *linda,
190void close_keepers(void) 186void close_keepers(void)
191{ 187{
192 int i; 188 int i;
193 for(i=0;i<KEEPER_STATES_N;i++) 189 for( i = 0; i < GNbKeepers; ++ i)
194 { 190 {
195 lua_close( GKeepers[i].L); 191 lua_close( GKeepers[i].L);
196 GKeepers[i].L = 0; 192 GKeepers[i].L = 0;
197 //assert( GKeepers[i].count == 0); 193 //assert( GKeepers[i].count == 0);
198 } 194 }
195 if( GKeepers) free( GKeepers);
196 GKeepers = NULL;
197 GNbKeepers = 0;
199} 198}