int main()
{
  pthread_t  t1, t2, t3;

  my_main_start();

  my_obj_reg(&data);

  my_mutex_init(&A, NULL);
  my_mutex_init(&B, NULL);
  my_mutex_init(&C, NULL);

  my_thread_create(&t1, 0, thread_routine_1, 0);
  my_thread_create(&t2, 0, thread_routine_2, 0);
  my_thread_create(&t3, 0, thread_routine_3, 0);

  my_thread_join(t1, 0);
  my_thread_join(t2, 0);
  my_thread_join(t3, 0);

  my_mutex_destroy(&A);
  my_mutex_destroy(&B);
  my_mutex_destroy(&C);

  my_main_end();

  return 0;
}
/*
 * Initialise the small heap.
 * Parameters that are allocated inside of this functions are described in the header file.
 */
struct shMapType *SH_init(void *ptr, pthread_t thread) {
	struct shMapType * shMapElement = malloc(sizeof(struct shMapType));
	shMapElement->memArea = (unsigned int *) ptr;
	shMapElement->memAreaSize = heapSize / sizeof(unsigned int);
	shMapElement->memArea[0] = heapSize / sizeof(unsigned int) - 1;
	shMapElement->memArea[shMapElement->memAreaSize - 1] = heapSize / sizeof(unsigned int);
	shMapElement->thread_id = thread;
	shMapElement->available = 0;
	my_mutex_init(&shMapElement->mutex);
	PRINTFMC("Component created. Heap location: %p\n", shMapElement->memArea);
	return shMapElement;
}
Exemple #3
0
/**
  Initialize my_sys functions, resources and variables

  @return Initialization result
    @retval 0 Success
    @retval 1 Error. Couldn't initialize environment
*/
my_bool my_init(void)
{
  char *str;

  if (my_init_done)
    return 0;

  my_init_done= 1;

  mysys_usage_id++;
  my_umask= 0660;                       /* Default umask for new files */
  my_umask_dir= 0700;                   /* Default umask for new directories */
  my_global_flags= 0;

  /* Default creation of new files */
  if ((str= getenv("UMASK")) != 0)
    my_umask= (int) (atoi_octal(str) | 0600);
  /* Default creation of new dir's */
  if ((str= getenv("UMASK_DIR")) != 0)
    my_umask_dir= (int) (atoi_octal(str) | 0700);

  init_glob_errs();

  instrumented_stdin.m_file= stdin;
  instrumented_stdin.m_psi= NULL;       /* not yet instrumented */
  mysql_stdin= & instrumented_stdin;

  my_progname_short= "unknown";
  if (my_progname)
    my_progname_short= my_progname + dirname_length(my_progname);

  /* Initalize our mutex handling */
  my_mutex_init();

  if (my_thread_global_init())
    return 1;

  /* $HOME is needed early to parse configuration files located in ~/ */
  if ((home_dir= getenv("HOME")) != 0)
    home_dir= intern_filename(home_dir_buff, home_dir);

  {
    DBUG_ENTER("my_init");
    DBUG_PROCESS((char*) (my_progname ? my_progname : "unknown"));
    my_time_init();
    my_win_init();
    DBUG_PRINT("exit", ("home: '%s'", home_dir));
#ifdef __WIN__
    win32_init_tcp_ip();
#endif
    DBUG_RETURN(0);
  }
} /* my_init */
Exemple #4
0
/*
	RunTest

		This function runs a particular test for a number of times and reports the result.

	Parameters
		func	A function with the test
		str		A string with the test name

	Return Value
		None. This function prints the result

*/
void RunTest( void * ( * func)( void * ), char * str )
{
	// ------------------ Local Variables --------------------

		int testnum;
		int passedtests;
		double timesum;
		pthread_t *threads;
		ThreadParameters_t *threadparameters;

	// ------------------- Initialization -------------------

		passedtests = 0;
		timesum = 0;
		printf( "Now running [%s]\n", str );

	// ------------------- Run Tests ------------------------

		for ( testnum = 0; testnum < REPEATTESTS; testnum++ )
		{

			my_mutex_init( &gMutex );
			my_barrier_init( &gBarrier, gP );

			CreateThreads( &threads, gP, func, &threadparameters );
			JoinThreads( &threads, gP, &threadparameters );

			my_mutex_destroy( &gMutex );
			my_barrier_destroy( &gBarrier );

			timesum += gElapsedTime;
			if ( gCorrectTest )
			{
				passedtests++;
				printf( "+" ); fflush( stdout );
			}
			else
			{
				printf( "-" ); fflush( stdout );
			}
		}

		timesum /= REPEATTESTS;


	// ------------------- Print Outcome ---------------------
		printf( "\nTests passed: %d of %d. Average Time = %f (sec)\n", passedtests, REPEATTESTS, 
			 timesum );
}
Exemple #5
0
/*
 * Entry point of Multi-core Insense runtime.
 */
int main(int argc, char* argv[]) {
	PRINTFMC("Cache line size: %dB\n", cache_line_size());
	PRINTFMC("Main thread: %u\n", (unsigned) pthread_self());

	errval_t err;
	coreid_t mycore = disp_get_core_id();

	if (argc == 2) {
		num_to_span = atoi(argv[1]);
		if(num_to_span==0)
			all_spanned = true;		

		debug_printf("Spanning onto %d cores\n", num_to_span);
		for (int i = 1; i < num_to_span; i++) {
			err = domain_new_dispatcher(mycore + i, span_cb, NULL);
		    
			if (err_is_fail(err)) {
				DEBUG_ERR(err, "failed span %d", i);
			} 
		}
	} else {
		debug_printf("ERROR: Must specify number of cores to span\n");
		return EXIT_FAILURE;
	}

	posixcompat_pthread_set_placement_fn(rrPlacement);

	while (!all_spanned) {
		thread_yield();
	}

	my_mutex_init(&shared_heap_mutex);
#if HEAPS == HEAP_PRIVATE // Private heaps
	// Initialize mutex
	if (pthread_mutex_init(&thread_lock, NULL ) != 0) {
		PRINTF("Mutex initialization failed.\n");
		return -1;
	}
#endif

	mainThread = pthread_self(); // Note the ID of the main thread.

	// Create a list for storing references to p-threads
	threadList = listCreate();

	// Create map used to store memory locations of small heaps (using Thread safe list)
	SHList = listCreate();

	// Create map used to store memory locations what is allocated using malloc
	mallocList = listCreate();

// Start recording execution time
#if TIMING
	// CPU time
	uint64_t start, end;
	uint64_t tsc_per_ms = 0;
	sys_debug_get_tsc_per_ms(&tsc_per_ms);
	start = rdtsc();
#endif

	// Call primordial_main.
	primordial_main(NULL );

	// Join all p-threads
	if (threadList != NULL ) {
		listJoinThreads(threadList);
	}

// Stop recording execution time
#if TIMING
	end = rdtsc();
	
	uint64_t diff = (end - start) / tsc_per_ms;
	float elapsed = (diff / 1000) + ((diff % 1000) / 1000.0);

	printf("CPU:  %f seconds elapsed\n", elapsed);
#endif

	// Destroy lists and free memory
	listDestroy(threadList);
	listDestroy(SHList);
	listDestroy(mallocList);
#if HEAPS == HEAP_PRIVATE
	pthread_mutex_destroy(&thread_lock); 	// Destroy mutex lock used with pthreads
#endif
	return 0;
}