Example #1
0
    void barber_action()
    {
        //funkcja bedaca procesem fryzjera
    
              
	    while(1)
	    {
		 //this_thread::sleep_for(30ms);

		  // cout<<"t\n";
		

         	int cust;


	        customer_ready.wait();
 

		queue_lock.wait();		    
                cust=waiting.front();
                waiting.pop();
		queue_lock.signal();
		barber_ready.signal();



		//cout<<"lol"<<endl;

                do_the_service(cust);
 	    }
            
    }
Example #2
0
    void notifier()
    {
        //time_t begin=time(NULL);
 
 
        while(1)
        {

	  //  cout<<"aa"<<endl;
 
            notify_lock.wait();
 
            while(!notifies.empty())
            {
 
       		pair<int,int> i=notifies.front();     
 
                if(i.first==QUEUE_FULL)
                    cout<<"Kolejka pelna. Nie umieszczono klienta nr "<<i.second<<endl;
                else if(i.first==CUSTOMER_PUT_IN_QUEUE)
                    cout<<"Umieszczono w kolejce klienta nr "<<i.second<<endl;
                else // CUSTOMER_SERVICED
                    cout<<"Obsluzono klienta nr "<<i.second<<endl;
 
       		notifies.pop();
            }
 
            //clear(notifies);
 
            notify_lock.signal();

	     this_thread::sleep_for(30ms);
        }
    }
    void task1()   // producer task
     {
		unsigned int data_token = 0;
		
		while(true) {
		  cout << "\ttask 1 " << " starts some computation at t=" << sc_time_stamp() << endl;
		  consume(t_prod);
          cout << "\ttask 1 " << " dumps data on the circular buffer at t=" << sc_time_stamp() << endl;		  
		  
#ifdef _USING_SEMAPHORE_FOR_PROTECTING_THE_ACCESS
		  write_sem.wait();
#endif		
		  unprotected_cbuff.push(data_token);

#ifdef _USING_SEMAPHORE_FOR_PROTECTING_THE_ACCESS
		  read_sem.post();
#endif				  
		  data_token++;
		  
#ifdef _USING_FLAG_FOR_PROTECTING_THE_ACCESS
		  flag1.set();
#endif		  
  
		}
     }
  /**
   * Consume a book by filter.
   *
   * Return number of items consumed.
   */
  unsigned int consume(filter &f) {
    can_read.acquire();
    unsigned int consumed = 0;
    #pragma omp critical
    {
      book *b = find_by_year(f.year);
      if (b != NULL) {
        unsigned int wants_to_consume = f.get_wants_to_consume();
        if (b->count < wants_to_consume)
          consumed = b->count;
        else
          consumed = wants_to_consume;

        b->count -= consumed;
        if (b->empty()) {
          debug << "Deleting " << b->title << endl;
          remove(b);
        }

        f.consume(consumed);
      }
      else {
        can_read.release();
      }
    }
    return consumed;
  }
Example #5
0
void function(void){
	for(int i = 0; i < 5; i++){
		counter_mutex.wait();
		for(int j = 0; j < 1000; j++){
			result = result + sin(counter) * tan(counter);
		}
		counter++;
		counter_mutex.signal();
	}
}
void Make(int add) {
	int used;
	while(!doneUsing){
		S.acquire();
		if((howMany % 2 == 0 && howMany != used) || (howMany % 3 == 0 && howMany != used)){
			common += add;
			used = howMany;
		}
		S.release();
	}
	
}
Example #7
0
unsigned long WINAPI barberFunc(void * data) {
	while (shopOpen) {
		customersWaiting.wait();
		seatsMutex.wait();
		numberOfFreeSeats++;
		barberReady.signal();
		seatsMutex.signal();

		std::cout << "Barber\tI am cutting someones hair" << std::endl;
		Sleep(20);
	}
	return 0;
}
Example #8
0
    void customer_action()
    {
        //funkcja bedaca procesem klienta
    time_t begin=time(NULL);
 
 
        while(1)
        { 
            queue_lock.wait();

	   // cout<<waiting.size()<<endl;

	    if( customer_is_to_be_produced())
                if(waiting.size()<=MAX_QUEUE)
                {

		   // cout<<"ff"<<endl;		
			
	            int new_cust=produce_customer();		    

                    waiting.push(new_cust);
 
              	    notify_lock.wait();
                    notifies.push(make_pair(CUSTOMER_PUT_IN_QUEUE, new_cust));
                    notify_lock.signal();
   
                    customer_ready.signal();
                    queue_lock.signal();
                    barber_ready.wait();
 		    
	         //   cout<<"ff"<<endl;
			

                }
 
                else
                {
              	    notify_lock.wait();
                    notifies.push(make_pair(QUEUE_FULL, produce_customer()));
                    notify_lock.signal();
 
                    queue_lock.signal();
                }
	else queue_lock.signal();
 
       // this_thread::sleep_for(30ms);
 
        }
 
    }
Example #9
0
    void do_the_service(int customer)
    {
        //funkcja obslugujaca klienta
        //tutaj czekamy losowy kwant czasu
 
        srand(time(NULL));
        int timer=(rand()%3)+1;
        time_t begin_time=time(NULL);
        while(time(NULL)-begin_time<timer);

	notify_lock.wait();
	notifies.push(make_pair(CUSTOMER_SERVICED,customer));
	notify_lock.signal();

    }
void Use(int procNr) {
	int val;
	while(!doneUsing){
		S.acquire();
		if(howMany < 10 && val != common){
			cout << procNr << ") reiksme: " << common << endl;
			val = common;
			howMany++;
		} else if (howMany == 10) {
			doneUsing = true;
		}
		S.release();
	}
	
}
void ste_presentation_surface::present(std::uint32_t image_index,
									   const vk::vk_queue<> &presentation_queue,
									   const semaphore &wait_semaphore) {
	VkSwapchainKHR swapchain = *swap_chain;
	VkSemaphore semaphore_handle = wait_semaphore;

	VkPresentInfoKHR info = {};
	info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
	info.pNext = nullptr;
	info.waitSemaphoreCount = 1;
	info.pWaitSemaphores = &semaphore_handle;
	info.swapchainCount = 1;
	info.pSwapchains = &swapchain;
	info.pImageIndices = &image_index;
	info.pResults = nullptr;

	// Host wait for semaphore
	wait_semaphore.wait_host();

	vk::vk_result res;
	{
		//		std::unique_lock<std::mutex> l(shared_data.swap_chain_guard);
		res = vkQueuePresentKHR(presentation_queue, &info);
	}

	// Raise flag to recreate swap-chain
	if (res != VK_SUCCESS)
		shared_data.swap_chain_optimal_flag.clear(std::memory_order_release);
	if (res != VK_SUCCESS && res != VK_SUBOPTIMAL_KHR && res != VK_ERROR_OUT_OF_DATE_KHR) {
		throw vk::vk_exception(res);
	}
}
ste_presentation_surface::acquire_next_image_return_t  ste_presentation_surface::acquire_swapchain_image_impl(
	std::uint64_t timeout_ns,
	semaphore &presentation_image_ready_semaphore,
	const vk::vk_fence<> *presentation_image_ready_fence) const
{
	acquire_next_image_return_t ret;
	vk::vk_result res = vkAcquireNextImageKHR(*presentation_device,
											  *swap_chain,
											  timeout_ns,
											  presentation_image_ready_semaphore ? static_cast<VkSemaphore>(*presentation_image_ready_semaphore) : vk::vk_null_handle,
											  presentation_image_ready_fence ? static_cast<VkFence>(*presentation_image_ready_fence) : vk::vk_null_handle,
											  &ret.image_index);

	// Host signal semaphore
	presentation_image_ready_semaphore.signal_host();

	switch (res.get()) {
	case VK_SUBOPTIMAL_KHR:
		ret.sub_optimal = true;
	case VK_SUCCESS:
		ret.image = &swap_chain_images[ret.image_index];
		break;
	case VK_ERROR_OUT_OF_DATE_KHR:
		ret.sub_optimal = true;
		break;
	default:
		throw vk::vk_exception(res);
	}

	// Furthermore raise flag to recreate swap-chain
	if (res != VK_SUCCESS)
		shared_data.swap_chain_optimal_flag.clear(std::memory_order_release);

	return ret;
}
 /**
  * Reduce working producer count by 1.
  */
 void producer_finished() {
   #pragma omp critical
   {
     producer_count -= 1;
     can_read.release();
   }
 }
Example #14
0
unsigned long WINAPI customerFunc(void * data) {
	seatsMutex.wait();
	if (numberOfFreeSeats > 0) {
		numberOfFreeSeats--;
		std::cout << ((int) data) << "\tI have taken a seat." << std::endl;
		customersWaiting.signal();
		seatsMutex.signal();
		barberReady.wait();
		std::cout << ((int)data) << "\tI am having my hair cut. Yay." << std::endl;
	}
	else {
		seatsMutex.signal();
		std::cout << ((int)data) << "\tShop full. Not getting hair cut." << std::endl;
	}

	return 0;
}
Example #15
0
semaphore::semaphore(const semaphore& other)
{
	i32 value = other.value();
#if defined(CRAP_PLATFORM_WIN)
	_semaphore = CreateSemaphore( 0, value, LMAXIMUMCOUNT, 0 );
#else
	sem_init( &_semaphore, 0, value );
#endif
}
Example #16
0
    pair<int,int> notify_pop()
    {
        //funkcja pobierajaca komunikat ze stosu komunikatow
        //jak nie ma komunikatu, zwracana jest wartosc specjalna
 
 
       	pair <int,int> element;
	notify_lock.wait();
        if(notifies.empty()) element=make_pair(NO_NOTIFIES,0);
        else
	{
 	    element=notifies.front();
	    notifies.pop();
	}
        notify_lock.signal();
 
        return element;
    }
Example #17
0
File: main.cpp Project: CCJY/coliru
 void drive()
 {
     // do wakeups
     for (unsigned i = 0; i<threads.size(); ++i)
     {
         this_thread::sleep_for(chrono::milliseconds(rand()%100));
         sem.notify();
     }
 }
     void task2()  // consumer task
     {
		while(true) {
			cout << "Task 2 waits for data at time " << sc_time_stamp() << endl;
#ifdef _USING_FLAG_FOR_PROTECTING_THE_ACCESS			
			wait(flag1);
#endif

#ifdef _USING_SEMAPHORE_FOR_PROTECTING_THE_ACCESS
			read_sem.wait();
#endif
			// KisTA model
		    cout << "task 2 " << "READ " << unprotected_cbuff.pop() << " from circular buffer at t=" << sc_time_stamp() << endl;
			consume(t_cons);
			cout << "task 2 " << "ends some processing at t=" << sc_time_stamp() << endl;
#ifdef _USING_SEMAPHORE_FOR_PROTECTING_THE_ACCESS
			write_sem.post();
#endif		    
		}
     }
  /**
   * Store book into storage.
   */
  void store(book &b) {
    #pragma omp critical
    {
      bool saved = false;
      for (vector<book>::iterator it = books.begin(); it < books.end(); it++) {
        if (it->year == b.year) {
          debug << "found existing book " << it->title << ", incrementing from " 
            << it->count << " to " << (it->count + b.count) << " by " << b.count << "\n";
          it->count += b.count;
          saved = true;
        }
      }

      if (! saved) {
        debug << "adding new book " << b.title << "\n";
        add_book(b);
      }
      can_read.release();
    }
  }
Example #20
0
void semaphore::operator=( const semaphore& other )
{
	reset( other.value() );
}
 void run(semaphore & sync_sem)
 {
     running.store(true, boost::memory_order_relaxed);
     sync_sem.post();
     perform();
 }