Пример #1
0
// 使用atomic_floag实现spin-lock和临界区
void thread_safe_using_atomic_flag()
{
    for(int i=0;i<loop;i++){
        while(ff.test_and_set()){} // 如果test_and_set()返回0,则set为1,并进入临界区。其它线程进行test_and_set返回1,则一直循环等待(busy-waiting)
        // ff -> 1
        count++;
        ff.clear(); // ff -> 0 其它线程可获取
    }
}
Пример #2
0
int calcPowersOf2()
{
	//load
	cout << "Getting data..." << endl;
	deque<string> data;
    unsigned long long x = getCount("Files/PowersOf2/");
	string s = getLine("Files/PowersOf2/",x-1);
	if(s=="")
	{
		cout << "Error! Calculation was not executed." << endl;
		return -2;
	}
	data.push_back(s);

	//calculate
	cout << "Calculating..." << endl;
	bool rerun = false;
	Integer a(s);
	unsigned long count = 0;
    while(stop.test_and_set(memory_order_acquire))
    {
        a = a+a;
		string a2 = a.value();
        data.push_back(a2);
		count++;
		if(count>=MAXSIZE)
		{
			rerun = true;
			break;
		}
    }
    
	//save
	cout << "Saving data..." << endl;
	int o = printLines("Files/PowersOf2/",data,1,data.size()-1);
	if(o==-3)
	{
		cout << "Big error!! You should reset all files to initial setup before continuing." << endl;
		return -5;
	}
	else if(o!=0)
	{
		cout << "Error! Calculated data might not have been saved." << endl;
		return -3;
	}
	if(rerun)
	{
		return calcPowersOf2();
	}

	cout << "Powers of 2 process was productive. I found " << (data.size()-1) << " powers of 2("
		<< (x+1) << "-" << (x+data.size()-1) << ")." << endl;
	stop.clear();
	return 0;
}
Пример #3
0
int calcFibonacci()
{
	//load
	cout << "Getting data..." << endl;
	deque<string> data;
    unsigned long long x = getCount("Files/Fibonacci/");
	string s = getLine("Files/Fibonacci/",x-2);
	if(s=="")
	{
		cout << "Error! Calculation was not executed." << endl;
		return -2;
	}
	data.push_back(s);
	s = getLine("Files/Fibonacci/",x-1);
	if(s=="")
	{
		cout << "Error! Calculation was not executed." << endl;
		return -2;
	}
	data.push_back(s);
	//calculate
	unsigned long siz = 2;
	cout << "Calculating..." << endl;
    while(stop.test_and_set(memory_order_acquire))
    {
		Integer a(data.at(siz-2));
		Integer b(data.at(siz-1));
        Integer z = a+b;
		string z2 = z.value();
        data.push_back(z2);
		siz++;
    }
    
	//save
	cout << "Saving data..." << endl;
	int o = printLines("Files/Fibonacci/",data,2,data.size()-2);
	if(o==-3)
	{
		cout << "Big error!! You should reset all files to initial setup before continuing." << endl;
		return -5;
	}
	else if(o!=0)
	{
		cout << "Error! Calculated data might not have been saved." << endl;
		return -3;
	}
	cout << "Fibonacci process was successful. I found " << (data.size()-2) << " fibonacci numbers("
		<< (x+1) << "-" << (x+data.size()-2) << ")." << endl;
	stop.clear();
	return 0;
}
Пример #4
0
		static T * Instance(Args && ...args)
		{
			static SingletonCleaner singleton_cleaner;
			if(instance_ != nullptr)
				return instance_;
			else
			{
				while(lock_.test_and_set());//spinlock
				if(instance_ == nullptr)
					instance_ = new T(std::forward<Args>(args)...);
				lock_.clear();
			}
			return instance_;
		}
Пример #5
0
static void default_emit_rtprio_warning(void) {
    if (!rtprio_seen.test_and_set()) {
        fprintf(stderr, "warning: unable to set high priority thread: Operation not permitted\n");
        fprintf(stderr, "See "
            "https://github.com/andrewrk/genesis/wiki/warning:-unable-to-set-high-priority-thread:-Operation-not-permitted\n");
    }
}
Пример #6
0
int calcFactorial()
{
	//load
	cout << "Getting data..." << endl;
	deque<string> data;
    unsigned long long x = getCount("Files/Factorial/");
	string s = getLine("Files/Factorial/",x-1);
	if(s=="")
	{
		cout << "Error! Calculation was not executed." << endl;
		return -2;
	}
	data.push_back(s);

	//calculate
	Integer k(Int2Str(x));
	Integer one("1");
	cout << "Calculating..." << endl;
    while(stop.test_and_set(memory_order_acquire))
    {
		Integer a(data.at(data.size()-1));
        Integer z = a*k;
		string z2 = z.value();
        data.push_back(z2);
		k = k + one;
    }
    
	//save
	cout << "Saving data..." << endl;
	int o = printLines("Files/Factorial/",data,1,data.size()-1);
	if(o==-3)
	{
		cout << "Big error!! You should reset all files to initial setup before continuing." << endl;
		return -5;
	}
	else if(o!=0)
	{
		cout << "Error! Calculated data might not have been saved." << endl;
		return -3;
	}

	cout << "Factorial process was productive. I found the factorials of " << (data.size()-1) << " numbers("
		<< (x+1) << "-" << (x+data.size()-1) << ")." << endl;
	stop.clear();
	return 0;
}
	void unlock()
	{
		isSet.clear();
	}
	void lock()
	{
		while(isSet.test_and_set());
	}
Пример #9
0
int soundio_jack_init(struct SoundIoPrivate *si) {
    SoundIoJack *sij = &si->backend_data.jack;
    SoundIo *soundio = &si->pub;

    if (!global_msg_callback_flag.test_and_set()) {
        if (soundio->jack_error_callback)
            jack_set_error_function(soundio->jack_error_callback);
        if (soundio->jack_info_callback)
            jack_set_info_function(soundio->jack_info_callback);
        global_msg_callback_flag.clear();
    }

    sij->mutex = soundio_os_mutex_create();
    if (!sij->mutex) {
        destroy_jack(si);
        return SoundIoErrorNoMem;
    }

    sij->cond = soundio_os_cond_create();
    if (!sij->cond) {
        destroy_jack(si);
        return SoundIoErrorNoMem;
    }

    // We pass JackNoStartServer due to
    // https://github.com/jackaudio/jack2/issues/138
    jack_status_t status;
    sij->client = jack_client_open(soundio->app_name, JackNoStartServer, &status);
    if (!sij->client) {
        destroy_jack(si);
        assert(!(status & JackInvalidOption));
        if (status & JackShmFailure)
            return SoundIoErrorSystemResources;
        if (status & JackNoSuchClient)
            return SoundIoErrorNoSuchClient;

        return SoundIoErrorInitAudioBackend;
    }

    int err;
    if ((err = jack_set_buffer_size_callback(sij->client, buffer_size_callback, si))) {
        destroy_jack(si);
        return SoundIoErrorInitAudioBackend;
    }
    if ((err = jack_set_sample_rate_callback(sij->client, sample_rate_callback, si))) {
        destroy_jack(si);
        return SoundIoErrorInitAudioBackend;
    }
    if ((err = jack_set_port_registration_callback(sij->client, port_registration_callback, si))) {
        destroy_jack(si);
        return SoundIoErrorInitAudioBackend;
    }
    if ((err = jack_set_port_rename_callback(sij->client, port_rename_calllback, si))) {
        destroy_jack(si);
        return SoundIoErrorInitAudioBackend;
    }
    jack_on_shutdown(sij->client, shutdown_callback, si);

    sij->refresh_devices_flag.clear();
    sij->period_size = jack_get_buffer_size(sij->client);
    sij->sample_rate = jack_get_sample_rate(sij->client);

    if ((err = jack_activate(sij->client))) {
        destroy_jack(si);
        return SoundIoErrorInitAudioBackend;
    }

    if ((err = refresh_devices(si))) {
        destroy_jack(si);
        return err;
    }

    si->destroy = destroy_jack;
    si->flush_events = flush_events_jack;
    si->wait_events = wait_events_jack;
    si->wakeup = wakeup_jack;
    si->force_device_scan = force_device_scan_jack;

    si->outstream_open = outstream_open_jack;
    si->outstream_destroy = outstream_destroy_jack;
    si->outstream_start = outstream_start_jack;
    si->outstream_begin_write = outstream_begin_write_jack;
    si->outstream_end_write = outstream_end_write_jack;
    si->outstream_clear_buffer = outstream_clear_buffer_jack;
    si->outstream_pause = outstream_pause_jack;
    si->outstream_get_latency = outstream_get_latency_jack;

    si->instream_open = instream_open_jack;
    si->instream_destroy = instream_destroy_jack;
    si->instream_start = instream_start_jack;
    si->instream_begin_read = instream_begin_read_jack;
    si->instream_end_read = instream_end_read_jack;
    si->instream_pause = instream_pause_jack;
    si->instream_get_latency = instream_get_latency_jack;

    return 0;
}
Пример #10
0
 void lock()
 {
   while(flag.test_and_set(memory_order_acquire));
 }
Пример #11
0
 void unlock()
 {
   flag.clear(memory_order_release);
 }