/* Cleanup */ void cleanup_this() { parsed_data_t* parsed = NULL; while((parsed = (parsed_data_t*)queue_pop_front(&g_q))) { parsed_data_destroy(parsed); free(parsed); } while((parsed = (parsed_data_t*)queue_pop_front(&g_p))) { parsed_data_destroy(parsed); free(parsed); } }
static void *udp_thread_work(void *param) { struct netinfo *netinfo = (struct netinfo*)param; struct sockaddr_in si_other; int sock; if ((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) { printf("errror opening %s %d\n", strerror(errno), errno); return NULL; } memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(netinfo->port); if (inet_aton(netinfo->address, &si_other.sin_addr)==0) { printf("errror inet %s %d\n", strerror(errno), errno); return NULL; } struct data_out_item *it; while(1) { pthread_mutex_lock(&data_queue.mutex); it = data_queue.head; pthread_mutex_unlock(&data_queue.mutex); while(it) { char *itr = it->data; char *end = it->data+it->size; uint16_t chunk; while(itr < end) { chunk = it->size > 65000 ? 65000 : it->size; if(sendto(sock, itr, chunk, 0, &si_other, sizeof(si_other)) != chunk) printf("errror sending %s %d\n", strerror(errno), errno); itr += chunk; it->size -= chunk; } pthread_mutex_lock(&data_queue.mutex); queue_pop_front(); it = data_queue.head; pthread_mutex_unlock(&data_queue.mutex); } usleep(1000); } close(sock); return NULL; }
/* * * pop_front * */ int buf_pop_front(){ if (!initialised) { //sprawdz, czy zainicjalizowane printf("First initialise!\n"); return -1; } sem_proberen(sem_empty); sem_proberen(sem_mutex); int retval = queue_pop_front(); sem_verhogen(sem_mutex); sem_verhogen(sem_full); return retval; }
int main(int argc, char *argv[]) { struct zc_published_service *service = malloc(sizeof(struct zc_published_service)); int ret = zc_rtp_publish(service); struct zc_queue *queue = create_queue_pointer(); ret = (zc_rtp_browse(queue) == 0) ? ret : 1; struct zc_queue_elem *cursor = NULL; struct zc_element *zc_elem = NULL; char *t= NULL; cursor = queue_pop_front(queue); while (cursor != NULL){ zc_elem = (struct zc_element *)cursor; t = avahi_string_list_to_string(zc_elem->txt_options); fprintf(stdout, "Elem : %s:%d %s %s %s %s \n", zc_elem->address, zc_elem->port, zc_elem->ifname, zc_elem->hostname, zc_elem->type, t); cursor = queue_pop_front(queue); } zc_rtp_free_service(service); queue_delete_queue_pointer(queue); return ret; }
/** * Safely dequeues an element from the normal queue and queues onto free queue * @param tq taskQ to operate on * @param idx index of queue to use * @param tid task id * @param qe queue element of element we operated on * @return nonzero if normal queue was empty */ static inline int tq_elem_into_free_seq ( taskQ_t* tq, int idx, int tid, queue_elem_t** qe) { queue_elem_t *queue_elem = NULL; int ret; ret = queue_pop_front (tq->queues[idx], &queue_elem); if (ret != 0) queue_push_back (tq->free_queues[idx], queue_elem); *qe = queue_elem; return ret; }
/** * Empties out a queue in the task queue by dequeuing and freeing * every task. */ static void tq_empty_queue(queue_t* q) { do { tq_entry_t *entry; queue_elem_t *queue_elem; if (queue_pop_front (q, &queue_elem) == 0) break; entry = queue_entry (queue_elem, tq_entry_t, queue_elem); assert (entry != NULL); phoenix_mem_free (entry); } while (1); }
int libtcas_double_cache_get(TCAS_pDoubleCache pDoubleCache, tcas_u32 n, tcas_byte **pBuf) { TCAS_QueuedFrame frame; WaitForSingleObject(pDoubleCache->semFrames, INFINITE); /* wait for available frames */ EnterCriticalSection(&pDoubleCache->lock); queue_pop_front(pDoubleCache->qFrames, &frame); if (frame.id == n) { /* the returned frame is just what we want, i.e., the playback moves in a smooth progress */ pDoubleCache->dcpArgs.n = frame.id + 1; /* require for next frames */ LeaveCriticalSection(&pDoubleCache->lock); *pBuf = frame.buf; ReleaseSemaphore(pDoubleCache->semQueue, 1, NULL); /* we consumed one frame, so there is an available room we left */ return 1; } else { /* we should drop all the frames cached, by calling this function in a loop */ pDoubleCache->dcpArgs.n = n; LeaveCriticalSection(&pDoubleCache->lock); free(frame.buf); *pBuf = NULL; ReleaseSemaphore(pDoubleCache->semQueue, 1, NULL); /* we consumed one frame, so there is an available room we left */ return 0; } }
static void queue_add(char *data, uint32_t size, uint32_t size_uncompressed) { uint32_t alloc_size = size + 14; struct data_out_item *it = malloc(sizeof(struct data_out_item)); it->next = NULL; it->data = malloc(sizeof(char)*alloc_size); it->size = alloc_size; strcpy(it->data, "FRAM\n"); it->data[6] = (size) >> 24; it->data[7] = (size) >> 16; it->data[8] = (size) >> 8; it->data[9] = (size); it->data[10] = (size_uncompressed) >> 24; it->data[11] = (size_uncompressed) >> 16; it->data[12] = (size_uncompressed) >> 8; it->data[13] = (size_uncompressed); memcpy(it->data+14, data, size); pthread_mutex_lock(&data_queue.mutex); if(data_queue.size >= 100) queue_pop_front(); it->prev = data_queue.tail; if(data_queue.tail) data_queue.tail->next = it; if(!data_queue.head) data_queue.head = it; data_queue.tail = it; ++data_queue.size; pthread_mutex_unlock(&data_queue.mutex); }
static void *tcp_thread_work(void *param) { struct netinfo *netinfo = (struct netinfo*)param; struct sockaddr_in addr; struct hostent *server; int sock; if ((sock=socket(AF_INET, SOCK_STREAM, 0))==-1) { printf("errror opening %s %d\n", strerror(errno), errno); return NULL; } server = gethostbyname(netinfo->address); if(!server) { printf("Could not find host %s", netinfo->address); return NULL; } memset((char *) &addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(netinfo->port); memcpy((char*)&addr.sin_addr.s_addr, server->h_addr, server->h_length); if(connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) { printf("errror connecting %s %d\n", strerror(errno), errno); return NULL; } struct data_out_item *it; while(1) { pthread_mutex_lock(&data_queue.mutex); it = data_queue.head; pthread_mutex_unlock(&data_queue.mutex); while(it) { char *itr = it->data; char *end = it->data+it->size; uint16_t chunk; while(itr < end) { chunk = it->size > 65000 ? 65000 : it->size; if(write(sock, itr, chunk) != chunk) printf("errror sending %s %d\n", strerror(errno), errno); itr += chunk; it->size -= chunk; } pthread_mutex_lock(&data_queue.mutex); queue_pop_front(); it = data_queue.head; pthread_mutex_unlock(&data_queue.mutex); } usleep(1000); } close(sock); return NULL; }
void* processWork(void* data) { int jobs = *(int*)data; int proc = 0; int i = 0; int b = 0; parsed_data_t* parsed = NULL; while(1) { /* Grab next available element from Queue */ pthread_mutex_lock(&accessQueue); parsed = (parsed_data_t*)queue_pop_front(&g_q); pthread_mutex_unlock(&accessQueue); /* Determine whether its dependencies have been met and it's non-NULL */ if(parsed != NULL) { pthread_mutex_lock(&accessProced); for(i = 0; i < parsed->numDeps; ++i) { /* If deps not met, send to back of queue and return to top of loop */ if(checkDep(parsed->deps[i])) { pthread_mutex_lock(&accessQueue); queue_push_back(&g_q, (void*)parsed); pthread_mutex_unlock(&accessQueue); b = 1; break; } } pthread_mutex_unlock(&accessProced); if(b) { b = 0; continue; } } else { /* If NULL, check processed against the number of threads */ /* If processed >= threads, then sleep this thread and wait on signal */ pthread_mutex_lock(&accessProced); proc = queue_size(&g_p); pthread_mutex_unlock(&accessProced); if(proc >= threads || threads > jobs) { pthread_mutex_lock(&waking); sleeping++; pthread_cond_signal(&wakeupMain); pthread_mutex_unlock(&waking); pthread_mutex_lock(&killing); pthread_cond_wait(&killThreads, &killing); pthread_mutex_unlock(&killing); break; } continue; } /* If deps met, then run its commands */ for(i = 0; i < parsed->numCmds; ++i) system(parsed->commands[i]); /* Add to the processed queue */ pthread_mutex_lock(&accessProced); queue_push_back(&g_p, (void*)parsed); proc = queue_size(&g_p); pthread_mutex_unlock(&accessProced); /* If the size of the processed queue == jobs, signal main */ if(proc >= jobs) { pthread_mutex_lock(&waking); sleeping++; pthread_cond_signal(&wakeupMain); pthread_mutex_unlock(&waking); pthread_mutex_lock(&killing); pthread_cond_wait(&killThreads, &killing); pthread_mutex_unlock(&killing); break; } } return 0; }
int old_main(int argc, char *argv[]) { struct zc_published_service service = { .client = NULL, .entry_group = NULL, .config = NULL }; char *zcp_argv[] = {"avahi-publish", "-s", "myservice", "_rtp._tcp", "12345", "Here it is"}; zc_publish_main(&service, 6, zcp_argv); struct zc_queue *queue = create_queue_pointer(); char *zcb_argv[] = {"avahi-browse", "-a", "-p", "-r", "-t", "-v"}; zc_browse_main(queue, 6, zcb_argv); struct zc_queue_elem *cursor = NULL; struct zc_element *zc_elem = NULL; char *t= NULL; cursor = queue_pop_front(queue); while (cursor != NULL){ zc_elem = (struct zc_element *)cursor; t = avahi_string_list_to_string(zc_elem->txt_options); fprintf(stdout, "Elem : %s:%d %s %s %s %s \n", zc_elem->address, zc_elem->port, zc_elem->ifname, zc_elem->hostname, zc_elem->type, t); cursor = queue_pop_front(queue); } if (!service.entry_group) { if (!(service.entry_group = avahi_entry_group_new(*(service.config->client), zcp_entry_group_callback, service.config))) { fprintf(stdout, ("Failed to create entry group: %s\n"), avahi_strerror(avahi_client_errno(*(service.config->client)))); return -1; }else{ fprintf(stdout, ("OK to create entry group: \n")); } } //assert(avahi_entry_group_is_empty(service.entry_group)); fprintf(stderr, ("Name %s: \n"), service.config->name); // service.config->txt = avahi_string_list_add(NULL, "plop=ok"); avahi_entry_group_update_service_txt_strlst(service.entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service.config->name, service.config->stype, service.config->domain, service.config->txt); //sleep(1); char *zcb_argv2[] = {"avahi-browse", "-a", "-p", "-r", "-t", "-v"}; zc_browse_main(queue, 6, zcb_argv2); cursor = queue_pop_front(queue); while (cursor != NULL){ zc_elem = (struct zc_element *)cursor; t = avahi_string_list_to_string(zc_elem->txt_options); fprintf(stdout, "Elem : %s:%d %s %s %s \n", zc_elem->address, zc_elem->port, zc_elem->hostname, zc_elem->type, t); cursor = queue_pop_front(queue); } // sleep(2); // char *zcp_argv3[] = {"avahi-publish", "-s", "myservice", "_http._tcp", "12345", "Here it is 2 le retour"}; // zc_publish_main(&client, &simple_poll, &service2, 6, zcp_argv3); // // char *zcb_argv2[] = {"avahi-browse", "-a", "-p", "-r", "-t", "-v"}; // sleep(5); // zc_browse_main(queue, 6, zcb_argv2); queue_delete_queue_pointer(queue); if (service.client){ fprintf(stderr, "Clean up client \n"); avahi_client_free(service.client); } if(service.config->simple_poll){ fprintf(stderr, "Clean up poll \n"); avahi_simple_poll_free(service.config->simple_poll); } if(service.config){ fprintf(stderr, "Clean up config \n"); free(service.config); } return 0; }