예제 #1
0
objectmap_t* objectmap_allocate( unsigned int size )
{
	uint64_t bits;
	unsigned int ip;
	uintptr_t next_indexshift;
	objectmap_t* map;
	void** slot;

	FOUNDATION_ASSERT_MSG( size > 2, "Invalid objectmap size" );
	if( size <= 2 )
		size = 2;
	bits = math_round( math_log2( (real)size ) ); //Number of bits needed
	FOUNDATION_ASSERT_MSGFORMAT( bits < 50, "Invalid objectmap size %d", size );

	//Top two bits unused for Lua compatibility
	map = memory_allocate_zero( sizeof( objectmap_t ) + ( sizeof( void* ) * size ), 16, MEMORY_PERSISTENT );
	map->size_bits   = bits;
	map->id_max      = ((1ULL<<(62ULL-bits))-1);
	map->size        = size;
	map->mask_index  = ((1ULL<<bits)-1ULL);
	map->mask_id     = ( 0x3FFFFFFFFFFFFFFFULL & ~map->mask_index );
	atomic_store64( &map->free, 0 );
	atomic_store64( &map->id, 1 );

	slot = map->map;
	for( ip = 0, next_indexshift = 3; ip < ( size - 1 ); ++ip, next_indexshift += 2, ++slot )
		*slot = (void*)next_indexshift;
	*slot = (void*)((uintptr_t)-1);
	
	return map;
}
예제 #2
0
void
lua_release_execution_right(lua_t* env) {
	FOUNDATION_ASSERT(atomic_load64(&env->executing_thread) == thread_id());
	FOUNDATION_ASSERT(env->executing_count > 0);
	if (!--env->executing_count) {
		atomic_store64(&env->executing_thread, 0);
		semaphore_post(&env->execution_right);
	}
}
예제 #3
0
bool
lua_acquire_execution_right(lua_t* env, bool force) {
	uint64_t self = thread_id();
	if (atomic_load64(&env->executing_thread) == self) {
		++env->executing_count;
		return true;
	}
	if (force) {
		semaphore_wait(&env->execution_right);
		atomic_store64(&env->executing_thread, self);
		FOUNDATION_ASSERT(env->executing_count == 0);
		++env->executing_count;
		return true;
	}
	if (semaphore_try_wait(&env->execution_right, 0)) {
		atomic_store64(&env->executing_thread, self);
		FOUNDATION_ASSERT(env->executing_count == 0);
		++env->executing_count;
		return true;
	}
	return false;
}