Ejemplo n.º 1
0
void pthread_exit(void *retval) {
  /* Get all we need from the tdb before releasing it. */
  nc_thread_descriptor_t    *tdb = nc_get_tdb();
  nc_thread_memory_block_t  *stack_node = tdb->stack_node;
  int32_t                   *is_used = &stack_node->is_used;
  nc_basic_thread_data_t    *basic_data = tdb->basic_data;
  int                       joinable = tdb->joinable;

  /* Call cleanup handlers. */
  while (NULL != __nc_cleanup_handlers) {
    pthread_cleanup_pop(1);
  }

  /* Call the destruction functions for TSD. */
  __nc_tsd_exit();

  __newlib_thread_exit();

  __nc_futex_thread_exit();

  if (__nc_initial_thread_id != basic_data) {
    pthread_mutex_lock(&__nc_thread_management_lock);
    --__nc_running_threads_counter;
    pthread_mutex_unlock(&__nc_thread_management_lock);
  } else {
    /* This is the main thread - wait for other threads to complete. */
    wait_for_threads();
    exit(0);
  }

  pthread_mutex_lock(&__nc_thread_management_lock);

  basic_data->retval = retval;

  if (joinable) {
    /* If somebody is waiting for this thread, signal. */
    basic_data->status = THREAD_TERMINATED;
    pthread_cond_signal(&basic_data->join_condvar);
  }
  /*
   * We can release TLS+TDB - thread id and its return value are still
   * kept in basic_data.
   */
  nc_release_tls_node(tdb->tls_node, tdb);

  if (!joinable) {
    nc_release_basic_data_mu(basic_data);
  }

  /* Now add the stack to the list but keep it marked as used. */
  nc_free_memory_block_mu(THREAD_STACK_MEMORY, stack_node);

  if (1 == __nc_running_threads_counter) {
    pthread_cond_signal(&__nc_last_thread_cond);
  }

  pthread_mutex_unlock(&__nc_thread_management_lock);
  irt_thread.thread_exit(is_used);
  nc_abort();
}
Ejemplo n.º 2
0
static void do_child(void)
{
	int res, i, j, awake;
	pthread_t t[55];

	for (i = 0; i < (int)ARRAY_SIZE(t); i++) {
		res = pthread_create(&t[i], NULL, threaded, (void*)((long)i));
		if (res) {
			tst_brkm(TBROK, NULL, "pthread_create(): %s",
			         tst_strerrno(res));
		}
	}

	while (wait_for_threads(ARRAY_SIZE(t)))
		usleep(100);

	for (i = 1; i <= 10; i++) {
		clear_threads_awake();
		res = futex_wake(&futex, i, FUTEX_PRIVATE_FLAG);
		if (i != res) {
			tst_resm(TFAIL,
			         "futex_wake() woken up %i threads, expected %i",
			         res, i);
		}

		for (j = 0; j < 100000; j++) {
			awake = threads_awake();
			if (awake == i)
				break;

			usleep(100);
		}

		if (awake == i) {
			tst_resm(TPASS, "futex_wake() woken up %i threads", i);
		} else {
			tst_resm(TFAIL, "Woken up %i threads, expected %i",
			         awake, i);
		}
	}

	res = futex_wake(&futex, 1, FUTEX_PRIVATE_FLAG);

	if (res) {
		tst_resm(TFAIL, "futex_wake() woken up %i, none were waiting",
		         res);
	} else {
		tst_resm(TPASS, "futex_wake() woken up 0 threads");
	}

	for (i = 0; i < (int)ARRAY_SIZE(t); i++)
		pthread_join(t[i], NULL);

	tst_exit();
}
// MAIN routine
main(int argc, char **argv) {
    clock_t clkStart,clkStop; //Elapsed times using <times()>
    struct tms tStart,tStop;  //CPU times for the threads
    int k, i, j, temp;
    double max;

    vFlag = 0;
    GetParam(argc, argv);




    int row ;int col;
    pivoting(0);
    CurrentRow = Norm +1;
    Count = NumThreads -1;

    printf("Starting timing ... computing ...\n");
    clkStart = times(&tStart);

    create_threads();
    wait_for_threads();



    //printf("before back substitution:\n");
    //printAB();
    //PrintX();
    // Back substitution in sequential                         
    for (i=N-1; i>=0; i--) {
        X[idx[i]] = B[idx[i]];
        for (j=N-1; j>i; j--)
            //X[idx[i]]-=colA[j][idx[i]]*X[idx[j]];
            X[idx[i]]-=rowA[idx[i]][j]*X[idx[j]];
        X[idx[i]] /= rowA[idx[i]][i];
    }

    clkStop = times(&tStop);
    printf("Stopped timing.\n");

    if (N<MAXN4print) PrintX();
    if (vFlag) for (i=0;i<N;i++) if (fabs(X[idx[i]]-(double)(i+1))>0.01) 
        printf("Incorrect results, i=%d, X[i]=%f\n",i,X[idx[i]]);

    printf("Elapsed time = %g ms.\n", 
            (float)(clkStop-clkStart)
            /(float)sysconf(_SC_CLK_TCK)*1000);

    printf("The total CPU time comsumed = %g ms.\n", 
            (float)((tStop.tms_utime-tStart.tms_utime) 
                + (tStop.tms_stime-tStart.tms_stime))
            / (float)sysconf(_SC_CLK_TCK)*1000);
}
Ejemplo n.º 4
0
status_t MSNManager::LogOff(void) {
	status_t ret = B_ERROR;

	connectionlist::iterator it;
	
	std::list<thread_id> threads;
	
	for (it = fConnectionPool.begin(); it != fConnectionPool.end(); it++) {
		BMessenger((*it)).SendMessage(B_QUIT_REQUESTED);
	}
	fConnectionPool.clear();
	
	LOG(kProtocolName, liLow, "%i connection(s) to kill", fConnections.size());

	for (it = fConnections.begin(); it != fConnections.end(); it++) {
		MSNConnection *con = (*it);
		
		threads.push_back( con->Thread() );
		
		LOG(kProtocolName, liLow, "Killing switchboard connection %lX", con);
		
		LOG(kProtocolName, liDebug, "  B_QUIT_REQUESTED");
		BMessenger(con).SendMessage(B_QUIT_REQUESTED);
	};
	fConnections.clear();
	
	fConnectionState = otOffline;
	
	if (fNoticeCon) {
		LOG(kProtocolName, liDebug, "Closing fNoticeCon");
		threads.push_back( fNoticeCon->Thread() );
		Command *bye = new Command("OUT");
		bye->UseTrID(false);
		fNoticeCon->Send(bye, qsImmediate);
		
		BMessenger(fNoticeCon).SendMessage(B_QUIT_REQUESTED);
		
		fNoticeCon = NULL;
		LOG(kProtocolName, liDebug, "  Unset fNoticeCon");
	};
	
	if ( fPassport != "" )
		fHandler->StatusChanged(fPassport.String(), otOffline);
	
	wait_for_threads( threads );
	
	ret = B_OK;
	
	return ret;
};
Ejemplo n.º 5
0
int main()
{
    init_threads();
    wait_for_threads();   
    return 0;
}
Ejemplo n.º 6
0
void main(int argc, char **argv)
{
/*               Elapsed times using <gettimeofday()>.                */
 struct timeval etstart, etstop;
 struct timezone tzdummy;
 clock_t etstartt, etstoptt;

/*                 Elapsed times using <times()>.                     */
 unsigned long usecstart, usecstop;

/*                  CPU times for the threads.                        */
 struct tms cputstart, cputstop;

 int row, col;


 parameters(argc, argv);
 initialise_inputs();
 print_inputs();

 CurrentRow = Norm+1;
 Count = NumThreads-1;

 printf("Starting clock ...\n");
 gettimeofday(&etstart, &tzdummy);
 etstartt = times(&cputstart);

 create_threads();

 wait_for_threads();

/*
 * Diagonal elements are not normalised to 1.
 * This is treated in back substitution.
 */

/*                         Back substitution.                         */
 for (row = N-1; row >= 0; row--)
 {
  X[row] = B[row];
  for (col = N-1; col > row; col--)
  X[row] -= A[row][col]*X[col];
  X[row] /= A[row][row];
  }

 gettimeofday(&etstop, &tzdummy);
 etstoptt = times(&cputstop);
 printf("Stopped clock.\n");
 
 usecstart = (unsigned long)etstart.tv_sec*1000000+etstart.tv_usec;
 usecstop = (unsigned long)etstop.tv_sec*1000000+etstop.tv_usec;

 print_X();

 printf("Elapsed time = %g ms.\n", 
(float)(usecstop-usecstart)/(float)1000);

 printf("Elapsed time according to <times()> = %g ms.\n", 
(etstoptt-etstartt)/(float)CLK_TCK*1000);

 printf("CPU times are accurate to the nearest %g ms.\n", 
1.0/(float)CLK_TCK*1000.0);

 printf("The total CPU time for parent = %g ms.\n", 
(float)((cputstop.tms_utime+cputstop.tms_stime)-
(cputstart.tms_utime+cputstart.tms_stime))/(float)CLK_TCK*1000);

 printf("The system CPU time for parent = %g ms.\n", 
(float)(cputstop.tms_stime-cputstart.tms_stime)/(float)CLK_TCK*1000);
 }
Ejemplo n.º 7
0
static Uint32 download_files(update_info_t* infos, const Uint32 count,
	const char* server, const char* path, const Uint32 source_count,
	unzFile* sources, zipFile dest, progress_fnc update_progress_function,
	void* user_data)
{
	char file_name[256];
	download_files_thread_data_t thread_data;
	FILE *file;
	Uint64 len;
	Uint32 i, j, download, error, index;

#ifdef WINDOWS
	file = my_tmpfile();
#else
	file = tmpfile();
#endif

	if (file == 0)
	{
		return 1;
	}

	error = 0;

	init_threads(&thread_data, count, server, path, dest,
		update_progress_function, user_data);

	for (i = 0; i < count; i++)
	{
		CHECK_AND_LOCK_MUTEX(thread_data.mutex);

		index = thread_data.index;

		CHECK_AND_UNLOCK_MUTEX(thread_data.mutex);

		if (update_progress_function("Updating files", count, index,
			user_data) != 1)
		{
			error = 2;

			break;
		}

		download = 1;

		len = strlen(infos[i].file_name);

		safe_snprintf(file_name, sizeof(file_name), "%s",
			infos[i].file_name);

		if (has_suffix(file_name, len, ".xz", 3))
		{
			file_name[len - 3] = 0;
		}

		for (j = 0; j < source_count; j++)
		{
			if (check_md5_from_zip(sources[j], file_name,
				infos[i].digest) == 0)
			{
				CHECK_AND_LOCK_MUTEX(thread_data.mutex);

				copy_from_zip(sources[j], dest);
				thread_data.index++;

				CHECK_AND_UNLOCK_MUTEX(thread_data.mutex);

				download = 0;

				break;
			}
		}

		if (download == 1)
		{
			queue_push(thread_data.files, &infos[i]);
			SDL_CondSignal(thread_data.condition);
		}
	}

	wait_for_threads(&thread_data, &error);

	fclose(file);

	return error;
}
Ejemplo n.º 8
0
void MSNManager::MessageReceived(BMessage *msg) {
	switch (msg->what) {
		case msnMessageRecveived: {
			BString passport = msg->FindString("passport");
			BString message = msg->FindString("message");

			fHandler->MessageFromUser(passport.String(), message.String());
		} break;
		
		case msnmsgStatusChanged: {
			uint8 status = msg->FindInt8("status");
			BString passport = msg->FindString("passport");
			fHandler->StatusChanged(passport.String(), (online_types)status);
		} break;
		
		case msnmsgOurStatusChanged: {
			uint8 status = msg->FindInt8("status");
			fHandler->StatusChanged(fPassport.String(), (online_types)status);
			fConnectionState = status;
		} break;
		
		case msnmsgNewConnection: {
			int16 port = 0;
			char *host = NULL;
			const char *type = NULL;

			if (msg->FindString("host", (const char **)&host) != B_OK) {
				LOG(kProtocolName, liLow, "Got a malformed new connection message"
					" (Host)");
				return;
			};
			if (msg->FindInt16("port", &port) != B_OK) {
				LOG(kProtocolName, liLow, "Got a malformed new connection message"
					" (Port)");
				return;
			};
			
			msg->FindString("type", &type);
			
			LOG(kProtocolName, liDebug, "Got a new connection to \"%s\":%i of type \"%s\"", host,
				port, type);
			
			if (strcmp(type, "NS") == 0) {
				//MSNConnection *con = new MSNConnection(host, port, this);
				MSNConnection *con = *fConnectionPool.begin();
				fConnectionPool.pop_front();
				fConnectionPool.push_back( new MSNConnection() );
				con->SetTo( host, port, this );
				//con->Lock();
				
				Command *command = new Command("VER");
				command->AddParam(kProtocolsVers);
					
				con->Send(command);
				
				fNoticeCon = con;
					
				LOG(kProtocolName, liDebug, "  Set fNoticeCon");
				
				return;
			}
			
			if (strcmp(type, "RNG") == 0) {
				MSNSBConnection *con = new MSNSBConnection(host, port, this);
				//con->Run();
				
				const char *auth = msg->FindString("authString");
				const char *sessionID = msg->FindString("sessionID");
				//const char *inviter = msg->FindString("inviterPassport");
				
				Command *command = new Command("ANS");
				command->AddParam(fPassport.String());
				command->AddParam(auth);
				command->AddParam(sessionID);
				
				con->Send(command);
				
				fConnections.push_back( con );
				
				return;
			};
			
			if (strcmp(type, "SB") == 0) {
				MSNSBConnection *con = new MSNSBConnection(host, port, this);
				//con->Run();
				
				const char *authString = msg->FindString("authString");
				
				Command *command = new Command("USR");
				command->AddParam(Passport());
				command->AddParam(authString);
				
				con->Send(command);
				
				waitingmsgmap::iterator it = fWaitingSBs.begin();
				
				if (it != fWaitingSBs.end()) {
					BString passport = (*it).second.first;
					Command *message = (*it).second.second;
					
					Command *cal = new Command("CAL");
					cal->AddParam(passport.String());
					
					con->Send(cal, qsOnline);
					con->SendMessage(message);
					
					// assume it's a MSG here..
					fConnections.push_back( con );
					
					fWaitingSBs.erase( (*it).first );
				};
			};
		} break;

		case msnmsgCloseConnection: {
			MSNConnection *con = NULL;
			msg->FindPointer("connection", (void **)&con);
			
			if (con != NULL) {
				LOG(kProtocolName, liLow, "Connection (%lX) closed", con);
				
				std::list<thread_id> threads;
				
				if (con == fNoticeCon) {
					LOG(kProtocolName, liLow, "  Notice connection closed, go offline");
					connectionlist::iterator i;
					for (i = fConnections.begin(); i != fConnections.end(); i++) {
						threads.push_back( (*i)->Thread() );
						Command * bye = new Command("OUT");
						bye->UseTrID(false);
						(*i)->Send(bye, qsImmediate);
						BMessenger(*i).SendMessage(B_QUIT_REQUESTED);
					};
					
					fHandler->StatusChanged(Passport(), otOffline);
					
					fNoticeCon = NULL;
					fConnectionState = otOffline;
					LOG(kProtocolName, liDebug, "  Unset fNoticeCon");
				};
				
				threads.push_back( con->Thread() );
				BMessenger(con).SendMessage(B_QUIT_REQUESTED);
				
				connectionlist::iterator i = find(fConnections.begin(), fConnections.end(), con);
				
				if ( i != fConnections.end() )
					fConnections.erase( i );
				
				wait_for_threads( threads );
			};
		} break;
		
		case msnmsgRemoveConnection: {
			MSNConnection *con = NULL;
			msg->FindPointer("connection", (void **)&con);
			
			if (con != NULL) {
				connectionlist::iterator i = find(fConnections.begin(), fConnections.end(), con);
				
				LOG(kProtocolName, liLow, "Connection (%lX) removed", con);
				
				if ( i != fConnections.end() )
				{ // don't call anything in con, it's already deleted.
					fConnections.erase( i );
				}
				
				if ( con == fNoticeCon ) {
					std::list<thread_id> threads;
					
					LOG(kProtocolName, liLow, "  Notice connection closed, going offline");
					connectionlist::iterator i;
					for (i = fConnections.begin(); i != fConnections.end(); i++) {
						threads.push_back( (*i)->Thread() );
						Command * bye = new Command("OUT");
						bye->UseTrID(false);
						(*i)->Send(bye, qsImmediate);
						BMessenger(*i).SendMessage(B_QUIT_REQUESTED);
					}
					
					wait_for_threads( threads );
					
					fHandler->StatusChanged(Passport(), otOffline);
					
					fNoticeCon = NULL;
					fConnectionState = otOffline;
					
					LOG(kProtocolName, liDebug, "  Unset fNoticeCon");
				}
			};
		} break;
		
		case msnAuthRequest: {
			// check if that person is already in our AL before asking.
			BString display = msg->FindString("displayname");
			BString passport = msg->FindString("passport");
			list_types listType = (list_types)msg->FindInt8("list");
			
			Buddy *bud = new Buddy(passport.String());
			bud->Lists(listType);
			fWaitingAuth[passport] = bud;
			
			fHandler->AuthRequest(listType, passport.String(), display.String());
		} break;
		
		case msnContactInfo: {
			UpdateContactInfo( msg );
		} break;
		
		case msnmsgError: {
			fHandler->Error( msg->FindString("error") );
		} break;
		
		case msnmsgProgress: {
			float progress=0.0;
			if ( msg->FindFloat("progress", &progress) != B_OK )
			{
				LOG(kProtocolName, liHigh, "Malformed msnmsgProgress message received by MSNManager");
				break;
			}
			
			fHandler->Progress( msg->FindString("id"), msg->FindString("message"), progress );
		} break;
		
		default: {
			BLooper::MessageReceived(msg);
		};
	};
}