示例#1
0
文件: CurlInit.hpp 项目: dkj/libmaus2
			static bool init(std::ostream & errstr)
			{
				#if defined(LIBMAUS2_HAVE_LIBCURL)
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				if ( ! initcomplete )
				{
					CURLcode const initfailed = curl_global_init(CURL_GLOBAL_ALL);
					
					if ( ! initfailed )
					{			
						initcomplete += 1;
						return true;
					}
					else
					{
						errstr << "curl_global_init failed: " << curl_easy_strerror(initfailed) << std::endl;
						return false;
					}
				}
				else
				{
					return true;
				}
				#else
				errstr << "curl_global_init failed: curl support is missing" << std::endl;
				return false;
				#endif
			}
			package_type * getPackage()
			{
				libmaus::parallel::ScopePosixSpinLock llock(lock);
			
				if ( ! freelistFill )
				{
					uint64_t const newlistsize = packages.size() ? 2*packages.size() : 1;
					
					libmaus::autoarray::AutoArray<package_ptr_type> newpackages(newlistsize);
					libmaus::autoarray::AutoArray<package_type *> newfreelist(newlistsize);
					
					for ( uint64_t i = 0; i < packages.size(); ++i )
					{
						newpackages[i] = UNIQUE_PTR_MOVE(packages[i]);
					}
					for ( uint64_t i = packages.size(); i < newpackages.size(); ++i )
					{
						package_ptr_type tptr(new package_type);
						newpackages[i] = UNIQUE_PTR_MOVE(tptr);
						newfreelist[freelistFill++] = newpackages[i].get();
					}
					
					packages = newpackages;
					freelist = newfreelist;
				}
				
				return freelist[--freelistFill];
			}
示例#3
0
  T* get_instance()
  {
    while(1)
    {
      if( arr1.empty() )
      {
        if( arr2.empty() )
        {
          mo_semls.wait();
          continue;
        }
        else
        {
          boost::mutex::scoped_lock llock(lock);
          {/** 锁作用域 */
            arr1.swap(arr2);
          }/** 锁作用域 */
        }
      }

      T* ret = *arr1.rbegin();
      arr1.pop_back();
      --m_size;
      return ret;
    }
  }
示例#4
0
			value_type dequeBack()
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				value_type const v = Q.back();
				Q.pop_back();
				return v;
			}
示例#5
0
			static bool init(std::ostream & 
				#if !defined(LIBMAUS2_HAVE_OPENSSL)
				errstr
				#endif
			)
			{
				#if defined(LIBMAUS2_HAVE_OPENSSL)
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				if ( ! initcomplete )
				{
					SSL_load_error_strings();   
					SSL_library_init();
					ERR_load_BIO_strings();
					OpenSSL_add_all_algorithms();   
					return true;                                
				}
				else
				{
					return true;
				}
				#else
				errstr << "OpenSSL init failed: OpenSSL support is missing" << std::endl;
				return false;
				#endif
			}
示例#6
0
文件: CurlInit.hpp 项目: dkj/libmaus2
			static void shutdown()
			{
				#if defined(LIBMAUS2_HAVE_LIBCURL)
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				if ( initcomplete && (! --initcomplete) )
					curl_global_cleanup();
				#endif
			}
    middleware_looparray(
      const char* ainame,
      uint32_t apbuffersize,
      uint32_t aieverymaxsize,
      FUN_READ_CALLBACK aireadfun,
      bool apstartthread,
      bool apisclient
      ) :
      la(apbuffersize, aieverymaxsize, aireadfun, apstartthread),
      m_name(ainame),
      mpmc(nullptr)
    {
      
    std::map<std::string, std::pair<module_communicate*, module_communicate*> >::iterator itor;
      {/** 锁作用域 */
        boost::mutex::scoped_lock llock(m_lock);
        itor = m_module_communicate_tab.find(m_name);

        if (itor == m_module_communicate_tab.end())
        {
          /** 创建 */
          if (apisclient)
          {
            m_module_communicate_tab.insert(std::make_pair(ainame, std::make_pair(&la, nullptr)));
          }
          else
          {
            m_module_communicate_tab.insert(std::make_pair(ainame, std::make_pair(nullptr, &la)));
          }
          itor = m_module_communicate_tab.find(m_name);
        }
        else
        {
          if (apisclient)
          {
             itor->second.first = &la;
          }
          else
          {
             itor->second.second = &la;
          }

        }
      }/** 锁作用域 */

      while (1)
      {
        mpmc = (apisclient ? itor->second.second : itor->second.first);
        if (mpmc == nullptr)
        {
          boost::this_thread::sleep(boost::posix_time::milliseconds(20));
        }
        else
        {
          break;
        }
      }
    }
示例#8
0
 virtual value_type deque()
 {
         semaphore.wait();
         
 	libmaus2::parallel::ScopePosixSpinLock llock(lock);
         value_type const v = Q.front();
         Q.pop_front();
         return v;
 }
示例#9
0
			uint64_t tryDequeFront(std::vector<value_type> & V, uint64_t max)
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				while ( max-- && Q.size() )
				{
					V.push_back(Q.front());
					Q.pop_front();
				}
				return V.size();			
			}
示例#10
0
			static void shutdown()
			{
				#if defined(LIBMAUS2_HAVE_OPENSSL)
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				if ( initcomplete && (! --initcomplete) )
				{
					ERR_free_strings();
					EVP_cleanup();
				}
				#endif
			}
示例#11
0
                        void enque(value_type const q)
                        {
                        	{
	                        	libmaus2::parallel::ScopePosixSpinLock llock(lock);
        	                        Q.push_back(q);
				}

                                semaphore.post();
                                
                                if ( parent )
	                                parent->enque(q);
                        }
示例#12
0
 void write_finish(uint32_t ailen)
 {
   {/* 锁区间 */
     boost::mutex::scoped_lock llock(m_lock);
     m_write += ailen;
     if (m_write >= m_array_end_ptr)
     {
       ++m_wcount;
       m_write = m_array_beg_ptr;
     }
   }/* 锁区间 */
   m_read_sem->post();
 }
示例#13
0
			bool tryDequeFront(value_type & v)
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				if ( Q.size() )
				{
					v = Q.front();
					Q.pop_front();
					return true;
				}		
				else
				{
					return false;
				}	
			}
示例#14
0
                        virtual bool trydeque(value_type & v)
                        {
                        	bool const ok = semaphore.trywait();
                                
                                if ( ok )
                                {
	                        	libmaus2::parallel::ScopePosixSpinLock llock(lock);
	                        	v = Q.front();
                	                Q.pop_front();
                	                return true;
				}
				else
				{
					return false;
				}
                        }
示例#15
0
    char* write_start(uint32_t& ailen)
    {
      char* ltemp;
      uint32_t ltempcount;
      while (1)
      {
        {/* 锁区间 */
          boost::mutex::scoped_lock llock(m_lock);
          ltemp = m_read;
          ltempcount = m_rcount;

        }/* 锁区间 */
        if (m_write > ltemp)
        {
          ailen = m_array_end_ptr - m_write;
          break;
        }
        else if (m_write < ltemp)
        {
          ailen = ltemp - m_write;
          break;
        }
        else // ==
        {
          if (m_wcount > ltempcount)
          {
            m_write_sem->wait();
            continue;
          }
          else
          {
            ailen = m_array_end_ptr - m_write;
            break;
          }
        }
      }

      return m_write;
    }
示例#16
0
  static T* get_instance()
  {
    while(1)
    {
      while(1)
      {
        boost::mutex::scoped_lock llock(lock);
        {/** 锁作用域 */
          if( arr.empty() )
          {
            break;
          }

          T* ret = *arr.rbegin();
          arr.pop_back();
          return ret;
        }/** 锁作用域 */
      }
      mo_semls.wait();
    }
    
  }
示例#17
0
			uint64_t push_back_and_size(value_type const v)
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				Q.push_back(v);
				return Q.size();
			}
示例#18
0
			bool empty()
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				return Q.size() == 0;	
			}
示例#19
0
			uint64_t size()
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				return Q.size();
			}
示例#20
0
文件: pos-list.c 项目: kunulee/f-stm
int t_t_pos_list_remove( char * name , void *_key){ 
	if( flags_num & NORMAL_ROUTINE ){ 
		struct list_head * head ; 	
		struct list_node * node , ** prev_node ; 	
		// stm prev
		struct list_node * sprev_node ; 	
		struct list_node * next ;
		unsigned long * key ; 	
		
		key = (unsigned long *)_key ; 	
		head = (struct list_head *)pos_get_prime_object(name) ; 	
		
		if( flags_num & LOCK_ROUTINE ){ // if lock flags settings. 
			llock() ; //lock 
			prev_node = &head->head ; 	
			node = head->head ; 	
			while(node){ 
				if( key_cmp( node->key , key ) == 1 ){ 
					*prev_node = node->next ; 	
					PR_DEBUG("%p\n" , node->next) ;
					/* 
					pos_free(name , node->value) ; 	
					pos_free(name , node) ; 	
					*/
					pos_clflush_cache_range( prev_node , sizeof(struct list_node)) ; 	

					lunlock() ; //unlock 
					return 0 ; 	
				}
				prev_node = &node->next ; 	
				node = node->next ; 	
			}
			lunlock() ; //unlock
			return -1;	
		}else if( flags_num & STM_ROUTINE ){ //if stm flags settings. 
			/*PR_DEBUG("DA\n") ;
			TM_START(2,RW) ; 
			struct list_node * p , * n ; 	
			p = (struct list_node *)TM_LOAD(&head->head) ; 		
			PR_DEBUG("A\n") ;
			n = (struct list_node *)TM_LOAD(&p->next) ; 	
			PR_DEBUG("D\n") ; 	
			PR_DEBUG("p = %p , n = %p\n" , p, n ) ; 
			while(1){ 
				if(key_cmp(n->key , key ) == 1){
					PR_DEBUG("%d fount\n" , *(int*)p->key) ;	
					break; 	
				}
				p = n ; 	
				n = (struct list_node *)TM_LOAD(&p->next);
			}
			PR_DEBUG("DD\n") ; 
			node = (struct list_node *)TM_LOAD(&n->next);	
			PR_DEBUG("DD\n") ; 
			TM_STORE(&p->next , node) ; 	
			PR_DEBUG("DD\n") ; 
			//TM_POS_FREE// 
			TM_COMMIT ; */ 
			TM_START(2,RW) ; 	
			#if (KEONWOO_DEBUG == 1 )
			PR_DEBUG("[TM_START]\n") ; 	
			#endif 
			sprev_node = (struct list_node *)TM_LOAD(&head->head); 	
			next = (struct list_node*)TM_LOAD(&sprev_node->next); 	
//			while( next || ((next==NULL)&&(sprev_node!=NULL))){ 
			while(sprev_node){ 
				if( (next==NULL) && (sprev_node!=NULL) ){
					
					PR_DEBUG("LastNode\n");
					if( key_cmp( sprev_node->key , key ) == 1){
						PR_DEBUG("[%d] [%d]\n" , *(int*)sprev_node->key,*(int*)key);
						PR_DEBUG("IN HERE\n") ;
						node = (struct list_node*)TM_LOAD(&sprev_node->next);
						if( node == NULL ) node = 0 ;
						PR_DEBUG("node ; %p\n" , node) ;
						//sprev_node = node ; 
						//head->head = node ; 	
						TM_STORE(&head->head,node);
						PR_DEBUG("head->head: %p\n" , head->head ) ;	
						//pos_clflush_cache_range(&sprev_node->next,sizeof(struct list_node)) ; 		
						break ;
					}
				}
				if( key_cmp( next->key , key ) == 1 ){ 
					node = (struct list_node *)TM_LOAD(&next->next); 	
					TM_STORE(&sprev_node->next ,node ); 	
					pos_clflush_cache_range(&sprev_node->next ,sizeof(struct list_node)) ; 	
					break; // out to while loop.
				}
				sprev_node = next ; 	
				next = (struct list_node *)TM_LOAD(&next->next);
			}
			 
			TM_COMMIT ; 	
			#if (KEONWOO_DEBUG == 1) 
			PR_DEBUG("[TM_COMMIT]\n") ;
			#endif 
		}
	}
}
示例#21
0
			value_type back()
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				return Q.back();
			}
示例#22
0
文件: pos-list.c 项目: kunulee/f-stm
int t_t_pos_list_insert( char * name , void *_key, void * _val , unsigned long val_size, int thread_num){ 
	//normal: // not thread mechanism
	if( flags_num & NORMAL_ROUTINE ){ 
		#if (KEONWOO_DEBUG == 1)
		PR_DEBUG("[%s] [%d]:key [%d]:val\n", __func__, *(int*)_key , *(int*)_val ) ; 	 
		#endif 
		//Normal Mechanism start here.	
		struct list_head * head ; 	
		struct list_node * node ; 	
		unsigned long * key, * val ; 	
		int i ; 	
	
		key = (unsigned long *)_key ; 	
		val = (unsigned long *)_val ; 	
		head = (struct list_head *)pos_get_prime_object(name); 
		if( head == NULL ){ 
			PR_DEBUG("[head point] is NULL\n") ;	
			//Error exit return -1; 
			return -1; 	
		}	
		lock() ;
		node = (struct list_node *)pos_malloc(name, sizeof(struct list_node)) ; 	
		/* TM_POS_MALLOC(name , sizeof(struct list_node)); */ 
		unlock() ; 	
		if( node == NULL ){ 
			PR_DEBUG("[node address] is NULL\n") ; 	
			//Error exit return -1; 
			return -1; 	
		}
		for( i = 0 ; i < KEY_SIZE ; i++ ){ 
			node->key[i] = key[i] ; 	
		}
		pos_clflush_cache_range(node->key, KEY_SIZE*sizeof(unsigned long));

        	lock() ;
        	node->value = (unsigned long *)pos_malloc(name, val_size);
		/* TM_POS_MALLOC(name , val_size) */ 
       		unlock() ;
		if( node->value == NULL ){ 
			PR_DEBUG("[node->valude address] is NULL\n");	
			// Error exit return -1;
			return -1; 	
		}
   		pos_clflush_cache_range(&node->value, sizeof(node->value));
	       	memcpy(node->value, val, val_size);
    	   	pos_clflush_cache_range(node->value, val_size);
	
		if( flags_num & LOCK_ROUTINE ){ 
			#if (KEONWOO_DEBUG == 1)
			PR_DEBUG("[LOCK()] called\n") ; 	
			#endif 
			llock() ; 
			node->next = head->head;  	
			pos_clflush_cache_range(&node->next , sizeof(node->next)) ; 
		 	head->head = node ; 	 
			pos_clflush_cache_range((void*)&head->head , sizeof(unsigned long)) ; 
			lunlock() ;
			return 0;   	
		}else if( flags_num & STM_ROUTINE ){ 
			TM_START(1,RW ) ; 	
			#if (KEONWOO_DEBUG == 1) 
			PR_DEBUG("[TM_START]\n") ; 
			#endif 
			node->next = head->head ; 	
			pos_clflush_cache_range(&node->next , sizeof(node->next)) ; 	
			PR_DEBUG("head->head : %p\n" , head->head) ; 	
			TM_STORE(&head->head , node) ; 	
			PR_DEBUG("a head->head : %p\n", head->head) ; 
			pos_clflush_cache_range((void*)&head->head, sizeof(unsigned long)) ; 		
			TM_COMMIT ; 
			return 0 ; 
		}else{ // flags_num & STM_NORMAL 
			#if (KEONWOO_DEBUG == 1)
			PR_DEBUG("[Normal]\n") ;
			#endif 
			node->next = head->head ; 
			pos_clflush_cache_range(&node->next , sizeof(node->next)) ; 	
			head->head = node ; 	
			pos_clflush_cache_range(&head->head , sizeof(unsigned long)) ; 	
			return  0;
		}
		return -1; 
	}else{ 
		PR_DEBUG("[flags_num] not set\n") ; 	
		return -1;
	}
}  
示例#23
0
文件: pos-list.c 项目: kunulee/f-stm
int pos_list_insert(char *name, void *_key, void *_val, unsigned long val_size)
{
	PR_DEBUG("[pos_list_insert]\n") ; 	

	struct list_head * head;
	struct list_node *node;
	unsigned long *key, *val;
	int i;
 
	llock();
#if CONSISTENCY == 1
	#if UNDO_CONSISTENCY == 1
	pos_transaction_start(name, POS_TS_INSERT);	
	#endif
#endif
	
	key = (unsigned long *)_key;
	val = (unsigned long *)_val;
	PR_DEBUG("key : %d , val : %p\n" , *key , val) ; 	
	head = (struct list_head *)pos_get_prime_object(name);
	if( head == NULL){ 
		PR_DEBUG("head pointer is null\n") ; 	
		return -1; 	
	}
	PR_DEBUG("head : %p\n" , head) ; 	
//	PR_DEBUG("helo\n") ; 
	lock();
 	node = (struct list_node *)pos_malloc(name, sizeof(struct list_node));

	// if crash free overhead occured 
	unlock() ; 	

	if( node == NULL){ 
		return -1; 
	}


	PR_DEBUG( "node size : %d , node : %p , key :%lu\n" ,sizeof(struct list_node), node , *key ) ; 
	for (i=0; i<KEY_SIZE; i++) {
		node->key[i] = key[i]  ; 
			
	}
	
#if CONSISTENCY == 1
	pos_clflush_cache_range(node->key, KEY_SIZE*sizeof(unsigned long));
#endif
	lock() ; 	
	node->value = (unsigned long *)pos_malloc(name, val_size);
	unlock() ; 
   
//	node->value = (unsigned long *)malloc(val_size) ; 
	//node->value = (unsigned long *)pos_malloc(name, 16) ;
//	PR_DEBUG("node->value : %lu\n" , node->value) ; 
	/*if( node->value == NULL){ 
		PR_DEBUG("NULL \n") ;
		return -1;
	}*/ 
#if CONSISTENCY == 1
	pos_clflush_cache_range(&node->value, sizeof(node->value));
#endif
//	memcpy(node->value, val, val_size);
	node->value = val ;
	PR_DEBUG("node->value : %p\n" , node->value) ; 
#if CONSISTENCY == 1
	pos_clflush_cache_range(node->value, val_size);
#endif
	
	node->next = head->head;
//	PR_DEBUG("node->next : %lu\n" , node->next) ; 	
	
#if CONSISTENCY == 1
	pos_clflush_cache_range(&node->next, sizeof(node->next));
#endif

#if MODE == 1
#if CONSISTENCY == 1
	#if UNDO_CONSISTENCY == 1 

	pos_write_value(name, (unsigned long *)&head->head, (unsigned long)node);
	
	pos_transaction_end(name);
	#else 
	head->head = node ; 	
	// cacheline flush to save nvram.
	pos_clflush_cache_range((void *)&head->head , sizeof( unsigned long )) ; 	

	#endif
//	PR_DEBUG("head->head : %lu\n" , head->head ) ; 
#else
	head->head = node;
#endif
#elif MODE == 2
	head->head = node - OFFSET_BASE;
#endif
	//printf("[%s]new_node[%p]\n",__func__,node) ;

	PR_DEBUG("\n\n[%s] returned 0\n" , __func__) ; 	
	lunlock();
	return 0;
}
示例#24
0
文件: pos-list.c 项目: kunulee/f-stm
int pos_list_remove(char *name, void *_key)
{
	struct list_head * head;
	struct list_node *node, **prev_node;
	unsigned long *key;
	int i;
	llock() ; 	

//	#if remove_debug == 1 
	PR_DEBUG("[%s]\n" , __func__ ) ; 	
	PR_DEBUG("key = %d\n", *(int*)_key) ; 	
//	#endif	
	
#if CONSISTENCY == 1
	#if UNDO_CONSISTENCY == 1 	
	pos_transaction_start(name, POS_TS_REMOVE);	
	#endif
#endif

	key = (unsigned long *)_key;
	PR_DEBUG("key : %d\n" , *(unsigned long *)_key) ;
	head = (struct list_head *)pos_get_prime_object(name);

//	#if remove_debug == 1 	
	PR_DEBUG("head = %lu\n" , head) ; 	
//	#endif 

	prev_node = &head->head;
	node = head->head;

//	#if remove_debug == 1 	
	PR_DEBUG("prev_node = %lu\n", prev_node) ; 	
	PR_DEBUG("node = %lu\n" , node) ; 	
//	#endif 

	while (node) {

#if MODE == 2
		node += OFFSET_BASE;
#endif

		if (key_val_cmp(node->key, key) == 1) {
	//		#if remove_debug == 1 	
			PR_DEBUG("key find key=%d node->key=%d\n" , *key ,node->key) ; 	
	//		#endif
	//		PR_DEBUG("Find %d\n" , *key) ;
#if CONSISTENCY == 1
			pos_write_value(name, (unsigned long *)prev_node, (unsigned long)node->next);
#else
			*prev_node = node->next;
	
			// This code is change list , tm_store(pre_node ,node->next);
			#if remove_debug == 1 
			PR_DEBUG("*prev_node = %lu\n" , *prev_node) ; 	
			#endif 	
#endif
//			pos_free(name, node->value);
//			pos_free(name, node);

#if CONSISTENCY == 1
			pos_transaction_end(name);
#endif
			lunlock(); 
			return 0;
		}

		prev_node = &node->next;
		node = node->next;
		#if remove_debug == 1 	
		PR_DEBUG("w prev_node = %lu\n" , prev_node) ; 	
		PR_DEBUG("w node = %lu\n" , node) ; 	
		#endif 
	}

#if CONSISTENCY == 1
			pos_transaction_end(name);
#endif
	lunlock();
	return -1;
}
示例#25
0
sge::scenic::grid::object::object(
	sge::renderer::device::ffp &_renderer,
	sge::camera::base const &_camera,
	sge::scenic::grid::orientation const _orientation,
	sge::scenic::grid::rect const &_rect,
	sge::scenic::grid::spacing const &_spacing,
	sge::scenic::grid::distance_to_origin const &_distance_to_origin,
	sge::image::color::any::object const &_color)
:
	renderer_(
		_renderer),
	camera_(
		_camera),
	line_drawer_(
		_renderer)
{
	sge::line_drawer::scoped_lock llock(
		line_drawer_);

	for(
		sge::renderer::scalar y = _rect.pos()[1];
		y <= _rect.pos()[1] + _rect.size()[1];
		y += _spacing.get()[1])
	{
		llock.value().push_back(
			sge::line_drawer::line(
				permute_vector_according_to_orientation(
					_orientation,
					sge::renderer::vector3(
						_rect.pos()[0],
						y,
						_distance_to_origin.get())),
				permute_vector_according_to_orientation(
					_orientation,
					sge::renderer::vector3(
						_rect.pos()[0]+_rect.size()[0],
						y,
						_distance_to_origin.get())),
				_color,
				_color));
	}

	for(
		sge::renderer::scalar x = _rect.pos()[0];
		x <= _rect.pos()[0] + _rect.size()[0];
		x += _spacing.get()[0])
	{
		llock.value().push_back(
			sge::line_drawer::line(
				permute_vector_according_to_orientation(
					_orientation,
					sge::renderer::vector3(
						x,
						_rect.pos()[1],
						_distance_to_origin.get())),
				permute_vector_according_to_orientation(
					_orientation,
					sge::renderer::vector3(
						x,
						_rect.pos()[1]+_rect.size()[1],
						_distance_to_origin.get())),
				_color,
				_color));
	}
}
			void returnPackage(package_type * ptr)
			{
				libmaus::parallel::ScopePosixSpinLock llock(lock);
				freelist[freelistFill++] = ptr;
			}
示例#27
0
			void push_front(value_type const v)
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				Q.push_front(v);
			}
示例#28
0
template <class C> void asioConnection<C>::execute ( boost::shared_ptr<Query> query, const boost::system::error_code& err  )
{
  if (!query)
    return;
  if (err)
  {
    lughos::debugLog ( std::string ( "Unable to connect for sending. Error: " ) + err.message() );
    query->setError(std::string ( "Unable to connect for sending. Error: " ) + err.message() );
    return;
  }
  
  if ( query->getEOLPattern().empty() )
    query->setEOLPattern ( endOfLineRegExpr_ );
  boost::system::error_code ec;
  SharedLock lock(this->mutex);
  if ( !this->initialized())
  {
    lock.unlock();
    this->initialize();
    lock.lock();
    if (!this->initialized() || !socket)
    {
      lughos::debugLog ( std::string ( "Unable to initialize for sending." ) );
      query->setError(std::string ( "Unable to initialize for sending." ));
      return;
    }
  }
  if (!this->connected())
  {
    lock.unlock();
    this->connect(boost::bind(&asioConnection<C>::execute, this, query, boost::asio::placeholders::error));
    return;
  }
  lock.unlock();
  query->busy(this->busy);
  {
    ExclusiveLock llock(this->mutex);
    this->timeoutTimer->expires_from_now(boost::posix_time::seconds(1));
    this->timeoutTimer->async_wait(boost::bind ( &asioConnection<C>::handle_timeout, this, query,
                                 boost::asio::placeholders::error ));
  }
  lock.lock();
  boost::asio::async_write ( *socket, query->output(),
                             boost::bind ( &asioConnection<C>::handle_write_request, this, query,
                                 boost::asio::placeholders::error ) );

  lughos::debugLog ( std::string ( "\"" ) +query->getQuestion()+std::string ( "\" written to port. Query: " ) + query->idString);
  lock.unlock();
  try
    {
      this->io_service->poll();
    }
  catch ( ... )
    {
      lughos::debugLog ( std::string ( "I/O-Service exception while polling." ) );
      query->setError(std::string ( "I/O-Service exception while polling." ));
      this->abort();
      return;
    }


  if ( !socket || !socket->is_open() )
    {
      lughos::debugLog ( std::string ( "Socket is closed despite writing?!" ) );
      query->setError(std::string ( "Socket is closed despite writing?!" ));
      this->abort();
      return;
    }

  if ( this->io_service->stopped() )
    {
      lughos::debugLog ( std::string ( "I/O-Service was stopped after or during writing." ) );
      query->setError(std::string ( "I/O-Service was stopped after or during writing." ));
      this->abort();
      return;
    }


  return;
}
示例#29
0
			void pop_back()
			{
				libmaus2::parallel::ScopePosixSpinLock llock(lock);
				Q.pop_back();
			}
示例#30
0
    void read_run()
    {

      char* ltemp_write = nullptr;
      char* ltemp_read = m_read;
      uint32_t lbodylen = 0;                        /** 剩余body长度 */
      char lclen[sizeof(uint16_t)] = { 0 };         /** 出现数据不足sizeof( uint16_t )时 暂存数据 */
      bool lcbool = false;                          /** lclen中是否有数据 */
      size_t ailen;                                 /** 可用长度 */
      bool bodyrecvover = true;                     /** 是否需要获取头部的size*/
      uint32_t temp_size = 0;                       /** 记录前面长度有几字节  一般两字节  也会出现一字节 */

      uint32_t lbodylen_copy = 0;
      uint32_t ltempwcount = 0;
      uint32_t ltemprcount = m_rcount;


      bool is_same = true;                       /** 读副本与真正的读指针同步频率  */
      while (1)
      {
        if (m_close)  /** 是否被关闭 */
        {
          m_read_close = true;
          return;
        }

        {/* 锁区间 */
          boost::mutex::scoped_lock llock(m_lock);
          //重置m_read
          if (ltemp_read >= m_array_end_ptr)
          {
            ltemp_read = m_array_beg_ptr;
            ++ltemprcount;
          }

          if (temp_size == 0 && is_same)
          {
            is_same = false;
            m_read = ltemp_read;
            m_rcount = ltemprcount;
          }
          //拷贝临时 write ptr 和 写状态
          ltemp_write = m_write;
          ltempwcount = m_wcount;
        }/* 锁区间 */


        /* 获取此刻可用的数据长度 */
        if (ltemp_read > ltemp_write)
        {
          ailen = m_array_end_ptr - ltemp_read;
        }
        else if (ltemp_read < ltemp_write)
        {
          ailen = ltemp_write - ltemp_read;
        }
        else//==
        {
          if (ltemprcount < ltempwcount)
          {
            ailen = m_array_end_ptr - ltemp_read;
          }
          else
          {
            m_read_sem->wait();
            continue;
          }
        }

        if (bodyrecvover)
        {
          /** 获取body len uint16_t */
          if (lcbool)
          {
            lclen[1] = ltemp_read[0];
            lbodylen = *((uint16_t*)(lclen));
            lbodylen_copy = lbodylen;
            temp_size = 1;
            ltemp_read += 1;
          }
          else
          {
            if (ailen < sizeof(uint16_t)) //ailen == 1
            {
              if (ltemp_read + ailen == m_array_end_ptr)
              {
                lclen[0] = ltemp_read[0];
                lcbool = true;
                ltemp_read += ailen;
              }
              continue;
            }
            else
            {
              lbodylen = *((uint16_t*)(ltemp_read));
              lbodylen_copy = lbodylen;
              ltemp_read += 2;
              temp_size = 2;
            }
          }
        }

        /** 获取body */
        if (lbodylen + temp_size > ailen)
        {
          bodyrecvover = false;
          if (m_temp_len == 0)
          {
            temp_size = ailen - temp_size;
            memcpy(m_temp_arr, ltemp_read, temp_size);
            m_temp_len = temp_size;
            lbodylen -= temp_size;
            ltemp_read += temp_size;
          }
          else
          {
            memcpy(&(m_temp_arr[m_temp_len]), ltemp_read, ailen);
            m_temp_len += (uint16_t)ailen;
            lbodylen -= (uint32_t)ailen;
            ltemp_read += ailen;
          }
          temp_size = 0;
        }
        else
        {
          if (m_temp_len != 0)
          {
            memcpy(&(m_temp_arr[m_temp_len]), ltemp_read, lbodylen);
            /* ret do sth*/
            if (!m_read_fun(m_temp_arr, lbodylen_copy))
            {
              return;
            }
            ltemp_read += lbodylen;
          }
          else
          {
            //ret = ltemp_read;
            if (!m_read_fun(ltemp_read, lbodylen_copy))
            {
              return;
            }
            ltemp_read += lbodylen_copy;
          }
          is_same = true;
          bodyrecvover = true;
          lcbool = false;
          lbodylen = 0;
          m_temp_len = 0;
          temp_size = 0;
          m_write_sem->post();
        }
      }
    }